cmCTestSubmit.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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
  242. = url + ((url.find("?",0) == cmStdString::npos) ? "?" : "&")
  243. + "FileName=" + ofile;
  244. struct stat st;
  245. if ( ::stat(local_file.c_str(), &st) )
  246. {
  247. return false;
  248. }
  249. ftpfile = ::fopen(local_file.c_str(), "rb");
  250. if ( m_Verbose )
  251. {
  252. std::cout << " Upload file: " << local_file.c_str() << " to "
  253. << upload_as.c_str() << " Size: " << st.st_size << std::endl;
  254. }
  255. // specify target
  256. ::curl_easy_setopt(curl,CURLOPT_URL, upload_as.c_str());
  257. // now specify which file to upload
  258. ::curl_easy_setopt(curl, CURLOPT_INFILE, ftpfile);
  259. // and give the size of the upload (optional)
  260. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
  261. // and give curl the buffer for errors
  262. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  263. // Now run off and do what you've been told!
  264. res = ::curl_easy_perform(curl);
  265. fclose(ftpfile);
  266. if ( res )
  267. {
  268. std::cout << " Error when uploading file: " << local_file.c_str() << std::endl;
  269. *m_LogFile << " Error when uploading file: " << local_file.c_str() << std::endl
  270. << " Error message was: " << error_buffer << std::endl;
  271. ::curl_easy_cleanup(curl);
  272. ::curl_global_cleanup();
  273. return false;
  274. }
  275. // always cleanup
  276. ::curl_easy_cleanup(curl);
  277. std::cout << " Uploaded: " + local_file << std::endl;
  278. }
  279. }
  280. ::curl_global_cleanup();
  281. return true;
  282. }
  283. bool cmCTestSubmit::TriggerUsingHTTP(const std::vector<cmStdString>& files,
  284. const cmStdString& remoteprefix,
  285. const cmStdString& url)
  286. {
  287. CURL *curl;
  288. char error_buffer[1024];
  289. /* In windows, this will init the winsock stuff */
  290. ::curl_global_init(CURL_GLOBAL_ALL);
  291. cmStdString::size_type cc, kk;
  292. for ( cc = 0; cc < files.size(); cc ++ )
  293. {
  294. /* get a curl handle */
  295. curl = curl_easy_init();
  296. if(curl)
  297. {
  298. // Using proxy
  299. if ( m_HTTPProxyType > 0 )
  300. {
  301. curl_easy_setopt(curl, CURLOPT_PROXY, m_HTTPProxy.c_str());
  302. switch (m_HTTPProxyType)
  303. {
  304. case 2:
  305. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
  306. break;
  307. case 3:
  308. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  309. break;
  310. default:
  311. curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  312. }
  313. }
  314. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
  315. if ( m_Verbose )
  316. {
  317. ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  318. }
  319. // and give curl the buffer for errors
  320. ::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error_buffer);
  321. cmStdString file = remoteprefix + files[cc];
  322. cmStdString ofile = "";
  323. for ( kk = 0; kk < file.size(); kk ++ )
  324. {
  325. char c = file[kk];
  326. char hex[4] = { 0, 0, 0, 0 };
  327. hex[0] = c;
  328. switch ( c )
  329. {
  330. case '+':
  331. case '?':
  332. case '/':
  333. case '\\':
  334. case '&':
  335. case ' ':
  336. case '=':
  337. case '%':
  338. sprintf(hex, "%%%02X", (int)c);
  339. ofile.append(hex);
  340. break;
  341. default:
  342. ofile.append(hex);
  343. }
  344. }
  345. cmStdString turl
  346. = url + ((url.find("?",0) == cmStdString::npos) ? "?" : "&")
  347. + "xmlfile=" + ofile;
  348. *m_LogFile << "Trigger url: " << turl.c_str() << std::endl;
  349. if ( m_Verbose )
  350. {
  351. std::cout << " Trigger url: " << turl.c_str() << std::endl;
  352. }
  353. curl_easy_setopt(curl, CURLOPT_URL, turl.c_str());
  354. if ( curl_easy_perform(curl) )
  355. {
  356. std::cout << " Error when triggering: " << turl.c_str() << std::endl;
  357. *m_LogFile << "\tTrigerring failed with error: " << error_buffer << std::endl;
  358. ::curl_easy_cleanup(curl);
  359. ::curl_global_cleanup();
  360. return false;
  361. }
  362. // always cleanup
  363. ::curl_easy_cleanup(curl);
  364. std::cout << std::endl;
  365. }
  366. }
  367. ::curl_global_cleanup();
  368. std::cout << " Dart server triggered..." << std::endl;
  369. return true;
  370. }
  371. bool cmCTestSubmit::SubmitUsingSCP(
  372. const cmStdString& scp_command,
  373. const cmStdString& localprefix,
  374. const std::vector<cmStdString>& files,
  375. const cmStdString& remoteprefix,
  376. const cmStdString& url)
  377. {
  378. if ( !scp_command.size() || !localprefix.size() ||
  379. !files.size() || !remoteprefix.size() || !url.size() )
  380. {
  381. return 0;
  382. }
  383. std::vector<const char*> argv;
  384. argv.push_back(scp_command.c_str()); // Scp command
  385. argv.push_back(scp_command.c_str()); // Dummy string for file
  386. argv.push_back(scp_command.c_str()); // Dummy string for remote url
  387. argv.push_back(0);
  388. cmsysProcess* cp = cmsysProcess_New();
  389. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  390. //cmsysProcess_SetTimeout(cp, timeout);
  391. int problems = 0;
  392. std::vector<cmStdString>::const_iterator it;
  393. for ( it = files.begin();
  394. it != files.end();
  395. it ++ )
  396. {
  397. int retVal;
  398. std::string lfname = localprefix;
  399. cmSystemTools::ConvertToUnixSlashes(lfname);
  400. lfname += "/" + *it;
  401. lfname = cmSystemTools::ConvertToOutputPath(lfname.c_str());
  402. argv[1] = lfname.c_str();
  403. std::string rfname = url + "/" + remoteprefix + *it;
  404. argv[2] = rfname.c_str();
  405. if ( m_Verbose )
  406. {
  407. std::cout << "Execute \"" << argv[0] << "\" \"" << argv[1] << "\" \""
  408. << argv[2] << "\"" << std::endl;
  409. }
  410. *m_LogFile << "Execute \"" << argv[0] << "\" \"" << argv[1] << "\" \""
  411. << argv[2] << "\"" << std::endl;
  412. cmsysProcess_SetCommand(cp, &*argv.begin());
  413. cmsysProcess_Execute(cp);
  414. char* data;
  415. int length;
  416. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  417. {
  418. std::cout.write(data, length);
  419. }
  420. cmsysProcess_WaitForExit(cp, 0);
  421. int result = cmsysProcess_GetState(cp);
  422. if(result == cmsysProcess_State_Exited)
  423. {
  424. retVal = cmsysProcess_GetExitValue(cp);
  425. if ( retVal != 0 )
  426. {
  427. if ( m_Verbose )
  428. {
  429. std::cout << "\tSCP returned: " << retVal << std::endl;
  430. }
  431. *m_LogFile << "\tSCP returned: " << retVal << std::endl;
  432. problems ++;
  433. }
  434. }
  435. else if(result == cmsysProcess_State_Exception)
  436. {
  437. retVal = cmsysProcess_GetExitException(cp);
  438. if ( m_Verbose )
  439. {
  440. std::cout << "\tThere was an exception: " << retVal << std::endl;
  441. }
  442. *m_LogFile << "\tThere was an exception: " << retVal << std::endl;
  443. problems ++;
  444. }
  445. else if(result == cmsysProcess_State_Expired)
  446. {
  447. if ( m_Verbose )
  448. {
  449. std::cout << "\tThere was a timeout" << std::endl;
  450. }
  451. *m_LogFile << "\tThere was a timeout" << std::endl;
  452. problems ++;
  453. }
  454. else if(result == cmsysProcess_State_Error)
  455. {
  456. if ( m_Verbose )
  457. {
  458. std::cout << "\tError executing SCP: "
  459. << cmsysProcess_GetErrorString(cp) << std::endl;
  460. }
  461. *m_LogFile << "\tError executing SCP: "
  462. << cmsysProcess_GetErrorString(cp) << std::endl;
  463. problems ++;
  464. }
  465. }
  466. cmsysProcess_Delete(cp);
  467. if ( problems )
  468. {
  469. return false;
  470. }
  471. return true;
  472. }