cmCTestSubmit.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestSubmit.h"
  14. #include "cmSystemTools.h"
  15. #include <cmsys/Process.h>
  16. #include "curl/curl.h"
  17. #include <sys/stat.h>
  18. cmCTestSubmit::cmCTestSubmit() : m_HTTPProxy(), m_FTPProxy()
  19. {
  20. m_Verbose = false;
  21. m_HTTPProxy = "";
  22. m_HTTPProxyType = 0;
  23. if ( getenv("HTTP_PROXY") )
  24. {
  25. m_HTTPProxyType = 1;
  26. m_HTTPProxy = getenv("HTTP_PROXY");
  27. if ( getenv("HTTP_PROXY_PORT") )
  28. {
  29. m_HTTPProxy += ":";
  30. m_HTTPProxy += getenv("HTTP_PROXY_PORT");
  31. }
  32. if ( getenv("HTTP_PROXY_TYPE") )
  33. {
  34. cmStdString type = getenv("HTTP_PROXY_TYPE");
  35. // HTTP/SOCKS4/SOCKS5
  36. if ( type == "HTTP" )
  37. {
  38. m_HTTPProxyType = 1;
  39. }
  40. else if ( type == "SOCKS4" )
  41. {
  42. m_HTTPProxyType = 2;
  43. }
  44. else if ( type == "SOCKS5" )
  45. {
  46. m_HTTPProxyType = 3;
  47. }
  48. }
  49. }
  50. m_FTPProxy = "";
  51. m_FTPProxyType = 0;
  52. if ( getenv("FTP_PROXY") )
  53. {
  54. m_FTPProxyType = 1;
  55. m_FTPProxy = getenv("FTP_PROXY");
  56. if ( getenv("FTP_PROXY_PORT") )
  57. {
  58. m_FTPProxy += ":";
  59. m_FTPProxy += getenv("FTP_PROXY_PORT");
  60. }
  61. if ( getenv("FTP_PROXY_TYPE") )
  62. {
  63. cmStdString type = getenv("FTP_PROXY_TYPE");
  64. // HTTP/SOCKS4/SOCKS5
  65. if ( type == "HTTP" )
  66. {
  67. m_FTPProxyType = 1;
  68. }
  69. else if ( type == "SOCKS4" )
  70. {
  71. m_FTPProxyType = 2;
  72. }
  73. else if ( type == "SOCKS5" )
  74. {
  75. m_FTPProxyType = 3;
  76. }
  77. }
  78. }
  79. if ( m_HTTPProxy.size() > 0 )
  80. {
  81. std::cout << " Use HTTP Proxy: " << m_HTTPProxy << std::endl;
  82. }
  83. if ( m_FTPProxy.size() > 0 )
  84. {
  85. std::cout << " Use FTP Proxy: " << m_FTPProxy << std::endl;
  86. }
  87. }
  88. bool cmCTestSubmit::SubmitUsingFTP(const cmStdString& localprefix,
  89. const std::vector<cmStdString>& files,
  90. const cmStdString& remoteprefix,
  91. const cmStdString& url)
  92. {
  93. CURL *curl;
  94. CURLcode res;
  95. FILE* ftpfile;
  96. char error_buffer[1024];
  97. /* In windows, this will init the winsock stuff */
  98. ::curl_global_init(CURL_GLOBAL_ALL);
  99. cmStdString::size_type cc;
  100. for ( cc = 0; cc < files.size(); cc ++ )
  101. {
  102. /* get a curl handle */
  103. curl = curl_easy_init();
  104. if(curl)
  105. {
  106. // Using proxy
  107. if ( m_FTPProxyType > 0 )
  108. {
  109. curl_easy_setopt(curl, CURLOPT_PROXY, m_FTPProxy.c_str());
  110. switch (m_FTPProxyType)
  111. {
  112. case 2:
  113. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  114. break;
  115. case 3:
  116. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  117. break;
  118. default:
  119. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  120. }
  121. }
  122. // enable uploading
  123. ::curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  124. cmStdString local_file = localprefix + "/" + files[cc];
  125. cmStdString upload_as = url + "/" + remoteprefix + files[cc];
  126. struct stat st;
  127. if ( ::stat(local_file.c_str(), &st) )
  128. {
  129. return false;
  130. }
  131. ftpfile = ::fopen(local_file.c_str(), "rb");
  132. *m_LogFile << "\tUpload file: " << local_file.c_str() << " to "
  133. << upload_as.c_str() << std::endl;
  134. if ( m_Verbose )
  135. {
  136. std::cout << " Upload file: " << local_file.c_str() << " to "
  137. << upload_as.c_str() << std::endl;
  138. }
  139. if ( m_Verbose )
  140. {
  141. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  142. }
  143. // specify target
  144. ::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
  145. // now specify which file to upload
  146. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  147. // and give the size of the upload (optional)
  148. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
  149. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  150. // Now run off and do what you've been told!
  151. res = ::curl_easy_perform(curl);
  152. fclose(ftpfile);
  153. if ( res )
  154. {
  155. std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
  156. std::cout << " Error message was: " << error_buffer << std::endl;
  157. *m_LogFile << " Error when uploading file: " << local_file.c_str() << std::endl
  158. << " Error message was: " << error_buffer << std::endl;
  159. ::curl_easy_cleanup(curl);
  160. ::curl_global_cleanup();
  161. return false;
  162. }
  163. // always cleanup
  164. ::curl_easy_cleanup(curl);
  165. std::cout << " Uploaded: " + local_file << std::endl;
  166. }
  167. }
  168. ::curl_global_cleanup();
  169. return true;
  170. }
  171. // Uploading files is simpler
  172. bool cmCTestSubmit::SubmitUsingHTTP(const cmStdString& localprefix,
  173. const std::vector<cmStdString>& files,
  174. const cmStdString& remoteprefix,
  175. const cmStdString& url)
  176. {
  177. CURL *curl;
  178. CURLcode res;
  179. FILE* ftpfile;
  180. char error_buffer[1024];
  181. /* In windows, this will init the winsock stuff */
  182. ::curl_global_init(CURL_GLOBAL_ALL);
  183. cmStdString::size_type cc, kk;
  184. for ( cc = 0; cc < files.size(); cc ++ )
  185. {
  186. /* get a curl handle */
  187. curl = curl_easy_init();
  188. if(curl)
  189. {
  190. // Using proxy
  191. if ( m_HTTPProxyType > 0 )
  192. {
  193. curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
  194. switch (m_HTTPProxyType)
  195. {
  196. case 2:
  197. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  198. break;
  199. case 3:
  200. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  201. break;
  202. default:
  203. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  204. }
  205. }
  206. /* enable uploading */
  207. curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  208. /* HTTP PUT please */
  209. curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
  210. if ( m_Verbose )
  211. {
  212. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  213. }
  214. cmStdString local_file = localprefix + "/" + files[cc];
  215. cmStdString remote_file = remoteprefix + files[cc];
  216. *m_LogFile << "\tUpload file: " << local_file.c_str() << " to "
  217. << remote_file.c_str() << std::endl;
  218. cmStdString ofile = "";
  219. for ( kk = 0; kk < remote_file.size(); kk ++ )
  220. {
  221. char c = remote_file[kk];
  222. char hex[4] = { 0, 0, 0, 0 };
  223. hex[0] = c;
  224. switch ( c )
  225. {
  226. case '+':
  227. case '?':
  228. case '/':
  229. case '\\':
  230. case '&':
  231. case ' ':
  232. case '=':
  233. case '%':
  234. sprintf(hex, "%%%02X", (int)c);
  235. ofile.append(hex);
  236. break;
  237. default:
  238. ofile.append(hex);
  239. }
  240. }
  241. cmStdString upload_as = url + "?FileName=" + ofile;
  242. struct stat st;
  243. if ( ::stat(local_file.c_str(), &st) )
  244. {
  245. return false;
  246. }
  247. ftpfile = ::fopen(local_file.c_str(), "rb");
  248. if ( m_Verbose )
  249. {
  250. std::cout << " Upload file: " << local_file.c_str() << " to "
  251. << upload_as.c_str() << " Size: " << st.st_size << std::endl;
  252. }
  253. // specify target
  254. ::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
  255. // now specify which file to upload
  256. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  257. // and give the size of the upload (optional)
  258. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
  259. // and give curl the buffer for errors
  260. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  261. // Now run off and do what you've been told!
  262. res = ::curl_easy_perform(curl);
  263. fclose(ftpfile);
  264. if ( res )
  265. {
  266. std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
  267. *m_LogFile << " Error when uploading file: " << local_file.c_str() << std::endl
  268. << " Error message was: " << error_buffer << std::endl;
  269. ::curl_easy_cleanup(curl);
  270. ::curl_global_cleanup();
  271. return false;
  272. }
  273. // always cleanup
  274. ::curl_easy_cleanup(curl);
  275. std::cout << " Uploaded: " + local_file << std::endl;
  276. }
  277. }
  278. ::curl_global_cleanup();
  279. return true;
  280. }
  281. bool cmCTestSubmit::TriggerUsingHTTP(const std::vector<cmStdString>& files,
  282. const cmStdString& remoteprefix,
  283. const cmStdString& url)
  284. {
  285. CURL *curl;
  286. char error_buffer[1024];
  287. /* In windows, this will init the winsock stuff */
  288. ::curl_global_init(CURL_GLOBAL_ALL);
  289. cmStdString::size_type cc, kk;
  290. for ( cc = 0; cc < files.size(); cc ++ )
  291. {
  292. /* get a curl handle */
  293. curl = curl_easy_init();
  294. if(curl)
  295. {
  296. // Using proxy
  297. if ( m_HTTPProxyType > 0 )
  298. {
  299. curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
  300. switch (m_HTTPProxyType)
  301. {
  302. case 2:
  303. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  304. break;
  305. case 3:
  306. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  307. break;
  308. default:
  309. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  310. }
  311. }
  312. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
  313. if ( m_Verbose )
  314. {
  315. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  316. }
  317. // and give curl the buffer for errors
  318. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  319. cmStdString file = remoteprefix + files[cc];
  320. cmStdString ofile = "";
  321. for ( kk = 0; kk < file.size(); kk ++ )
  322. {
  323. char c = file[kk];
  324. char hex[4] = { 0, 0, 0, 0 };
  325. hex[0] = c;
  326. switch ( c )
  327. {
  328. case '+':
  329. case '?':
  330. case '/':
  331. case '\\':
  332. case '&':
  333. case ' ':
  334. case '=':
  335. case '%':
  336. sprintf(hex, "%%%02X", (int)c);
  337. ofile.append(hex);
  338. break;
  339. default:
  340. ofile.append(hex);
  341. }
  342. }
  343. cmStdString turl = url + "?xmlfile=" + ofile;
  344. *m_LogFile << "Trigger url: " << turl.c_str() << std::endl;
  345. if ( m_Verbose )
  346. {
  347. std::cout << " Trigger url: " << turl.c_str() << std::endl;
  348. }
  349. curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
  350. if ( curl_easy_perform(curl) )
  351. {
  352. std::cout << " Error when triggering: " << turl.c_str() << std::endl;
  353. *m_LogFile << "\tTrigerring failed with error: " << error_buffer << std::endl;
  354. ::curl_easy_cleanup(curl);
  355. ::curl_global_cleanup();
  356. return false;
  357. }
  358. // always cleanup
  359. ::curl_easy_cleanup(curl);
  360. std::cout << std::endl;
  361. }
  362. }
  363. ::curl_global_cleanup();
  364. std::cout << " Dart server triggered..." << std::endl;
  365. return true;
  366. }
  367. bool cmCTestSubmit::SubmitUsingSCP(
  368. const cmStdString& scp_command,
  369. const cmStdString& localprefix,
  370. const std::vector<cmStdString>& files,
  371. const cmStdString& remoteprefix,
  372. const cmStdString& url)
  373. {
  374. if ( !scp_command.size() || !localprefix.size() ||
  375. !files.size() || !remoteprefix.size() || !url.size() )
  376. {
  377. return 0;
  378. }
  379. std::vector<const char*> argv;
  380. argv.push_back(scp_command.c_str()); // Scp command
  381. argv.push_back(scp_command.c_str()); // Dummy string for file
  382. argv.push_back(scp_command.c_str()); // Dummy string for remote url
  383. argv.push_back(0);
  384. cmsysProcess* cp = cmsysProcess_New();
  385. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  386. //cmsysProcess_SetTimeout(cp, timeout);
  387. int problems = 0;
  388. std::vector<cmStdString>::const_iterator it;
  389. for ( it = files.begin();
  390. it != files.end();
  391. it ++ )
  392. {
  393. int retVal;
  394. std::string lfname = localprefix;
  395. cmSystemTools::ConvertToUnixSlashes(lfname);
  396. lfname += "/" + *it;
  397. lfname = cmSystemTools::ConvertToOutputPath(lfname.c_str());
  398. argv[1] = lfname.c_str();
  399. std::string rfname = url + "/" + remoteprefix + *it;
  400. argv[2] = rfname.c_str();
  401. if ( m_Verbose )
  402. {
  403. std::cout << "Execute \"" << argv[0] << "\" \"" << argv[1] << "\" \""
  404. << argv[2] << "\"" << std::endl;
  405. }
  406. *m_LogFile << "Execute \"" << argv[0] << "\" \"" << argv[1] << "\" \""
  407. << argv[2] << "\"" << std::endl;
  408. cmsysProcess_SetCommand(cp, &*argv.begin());
  409. cmsysProcess_Execute(cp);
  410. char* data;
  411. int length;
  412. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  413. {
  414. std::cout.write(data, length);
  415. }
  416. cmsysProcess_WaitForExit(cp, 0);
  417. int result = cmsysProcess_GetState(cp);
  418. if(result == cmsysProcess_State_Exited)
  419. {
  420. retVal = cmsysProcess_GetExitValue(cp);
  421. if ( retVal != 0 )
  422. {
  423. if ( m_Verbose )
  424. {
  425. std::cout << "\tSCP returned: " << retVal << std::endl;
  426. }
  427. *m_LogFile << "\tSCP returned: " << retVal << std::endl;
  428. problems ++;
  429. }
  430. }
  431. else if(result == cmsysProcess_State_Exception)
  432. {
  433. retVal = cmsysProcess_GetExitException(cp);
  434. if ( m_Verbose )
  435. {
  436. std::cout << "\tThere was an exception: " << retVal << std::endl;
  437. }
  438. *m_LogFile << "\tThere was an exception: " << retVal << std::endl;
  439. problems ++;
  440. }
  441. else if(result == cmsysProcess_State_Expired)
  442. {
  443. if ( m_Verbose )
  444. {
  445. std::cout << "\tThere was a timeout" << std::endl;
  446. }
  447. *m_LogFile << "\tThere was a timeout" << std::endl;
  448. problems ++;
  449. }
  450. else if(result == cmsysProcess_State_Error)
  451. {
  452. if ( m_Verbose )
  453. {
  454. std::cout << "\tError executing SCP: "
  455. << cmsysProcess_GetErrorString(cp) << std::endl;
  456. }
  457. *m_LogFile << "\tError executing SCP: "
  458. << cmsysProcess_GetErrorString(cp) << std::endl;
  459. problems ++;
  460. }
  461. }
  462. cmsysProcess_Delete(cp);
  463. if ( problems )
  464. {
  465. return false;
  466. }
  467. return true;
  468. }