cmCTestUpdateHandler.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestUpdateHandler.h"
  11. #include "cmCTest.h"
  12. #include "cmake.h"
  13. #include "cmMakefile.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmVersion.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmXMLParser.h"
  19. #include "cmXMLSafe.h"
  20. #include "cmCTestVC.h"
  21. #include "cmCTestCVS.h"
  22. #include "cmCTestSVN.h"
  23. #include "cmCTestBZR.h"
  24. #include "cmCTestGIT.h"
  25. #include "cmCTestHG.h"
  26. #include "cmCTestP4.h"
  27. #include <cmsys/auto_ptr.hxx>
  28. //#include <cmsys/RegularExpression.hxx>
  29. #include <cmsys/Process.h>
  30. // used for sleep
  31. #ifdef _WIN32
  32. #include "windows.h"
  33. #endif
  34. #include <stdlib.h>
  35. #include <math.h>
  36. #include <float.h>
  37. //----------------------------------------------------------------------
  38. static const char* cmCTestUpdateHandlerUpdateStrings[] =
  39. {
  40. "Unknown",
  41. "CVS",
  42. "SVN",
  43. "BZR",
  44. "GIT",
  45. "HG",
  46. "P4"
  47. };
  48. static const char* cmCTestUpdateHandlerUpdateToString(int type)
  49. {
  50. if ( type < cmCTestUpdateHandler::e_UNKNOWN ||
  51. type >= cmCTestUpdateHandler::e_LAST )
  52. {
  53. return cmCTestUpdateHandlerUpdateStrings[cmCTestUpdateHandler::e_UNKNOWN];
  54. }
  55. return cmCTestUpdateHandlerUpdateStrings[type];
  56. }
  57. class cmCTestUpdateHandlerLocale
  58. {
  59. public:
  60. cmCTestUpdateHandlerLocale();
  61. ~cmCTestUpdateHandlerLocale();
  62. private:
  63. std::string saveLCMessages;
  64. };
  65. cmCTestUpdateHandlerLocale::cmCTestUpdateHandlerLocale()
  66. {
  67. const char* lcmess = cmSystemTools::GetEnv("LC_MESSAGES");
  68. if(lcmess)
  69. {
  70. saveLCMessages = lcmess;
  71. }
  72. // if LC_MESSAGES is not set to C, then
  73. // set it, so that svn/cvs info will be in english ascii
  74. if(! (lcmess && strcmp(lcmess, "C") == 0))
  75. {
  76. cmSystemTools::PutEnv("LC_MESSAGES=C");
  77. }
  78. }
  79. cmCTestUpdateHandlerLocale::~cmCTestUpdateHandlerLocale()
  80. {
  81. // restore the value of LC_MESSAGES after running the version control
  82. // commands
  83. if(saveLCMessages.size())
  84. {
  85. std::string put = "LC_MESSAGES=";
  86. put += saveLCMessages;
  87. cmSystemTools::PutEnv(put);
  88. }
  89. else
  90. {
  91. cmSystemTools::UnsetEnv("LC_MESSAGES");
  92. }
  93. }
  94. //----------------------------------------------------------------------
  95. cmCTestUpdateHandler::cmCTestUpdateHandler()
  96. {
  97. }
  98. //----------------------------------------------------------------------
  99. void cmCTestUpdateHandler::Initialize()
  100. {
  101. this->Superclass::Initialize();
  102. this->UpdateCommand = "";
  103. this->UpdateType = e_CVS;
  104. }
  105. //----------------------------------------------------------------------
  106. int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
  107. {
  108. cmCTestLog(this->CTest, DEBUG, "Determine update type from command: " << cmd
  109. << " and type: " << type << std::endl);
  110. if ( type && *type )
  111. {
  112. cmCTestLog(this->CTest, DEBUG, "Type specified: " << type << std::endl);
  113. std::string stype = cmSystemTools::LowerCase(type);
  114. if ( stype.find("cvs") != std::string::npos )
  115. {
  116. return cmCTestUpdateHandler::e_CVS;
  117. }
  118. if ( stype.find("svn") != std::string::npos )
  119. {
  120. return cmCTestUpdateHandler::e_SVN;
  121. }
  122. if ( stype.find("bzr") != std::string::npos )
  123. {
  124. return cmCTestUpdateHandler::e_BZR;
  125. }
  126. if ( stype.find("git") != std::string::npos )
  127. {
  128. return cmCTestUpdateHandler::e_GIT;
  129. }
  130. if ( stype.find("hg") != std::string::npos )
  131. {
  132. return cmCTestUpdateHandler::e_HG;
  133. }
  134. if ( stype.find("p4") != std::string::npos )
  135. {
  136. return cmCTestUpdateHandler::e_P4;
  137. }
  138. }
  139. else
  140. {
  141. cmCTestLog(this->CTest, DEBUG, "Type not specified, check command: "
  142. << cmd << std::endl);
  143. std::string stype = cmSystemTools::LowerCase(cmd);
  144. if ( stype.find("cvs") != std::string::npos )
  145. {
  146. return cmCTestUpdateHandler::e_CVS;
  147. }
  148. if ( stype.find("svn") != std::string::npos )
  149. {
  150. return cmCTestUpdateHandler::e_SVN;
  151. }
  152. if ( stype.find("bzr") != std::string::npos )
  153. {
  154. return cmCTestUpdateHandler::e_BZR;
  155. }
  156. if ( stype.find("git") != std::string::npos )
  157. {
  158. return cmCTestUpdateHandler::e_GIT;
  159. }
  160. if ( stype.find("hg") != std::string::npos )
  161. {
  162. return cmCTestUpdateHandler::e_HG;
  163. }
  164. if ( stype.find("p4") != std::string::npos )
  165. {
  166. return cmCTestUpdateHandler::e_P4;
  167. }
  168. }
  169. return cmCTestUpdateHandler::e_UNKNOWN;
  170. }
  171. //----------------------------------------------------------------------
  172. //clearly it would be nice if this were broken up into a few smaller
  173. //functions and commented...
  174. int cmCTestUpdateHandler::ProcessHandler()
  175. {
  176. // Make sure VCS tool messages are in English so we can parse them.
  177. cmCTestUpdateHandlerLocale fixLocale;
  178. static_cast<void>(fixLocale);
  179. // Get source dir
  180. const char* sourceDirectory = this->GetOption("SourceDirectory");
  181. if ( !sourceDirectory )
  182. {
  183. cmCTestLog(this->CTest, ERROR_MESSAGE,
  184. "Cannot find SourceDirectory key in the DartConfiguration.tcl"
  185. << std::endl);
  186. return -1;
  187. }
  188. cmGeneratedFileStream ofs;
  189. if ( !this->CTest->GetShowOnly() )
  190. {
  191. this->StartLogFile("Update", ofs);
  192. }
  193. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Updating the repository: "
  194. << sourceDirectory << std::endl);
  195. if(!this->SelectVCS())
  196. {
  197. return -1;
  198. }
  199. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Use "
  200. << cmCTestUpdateHandlerUpdateToString(this->UpdateType)
  201. << " repository type"
  202. << std::endl;);
  203. // Create an object to interact with the VCS tool.
  204. cmsys::auto_ptr<cmCTestVC> vc;
  205. switch (this->UpdateType)
  206. {
  207. case e_CVS: vc.reset(new cmCTestCVS(this->CTest, ofs)); break;
  208. case e_SVN: vc.reset(new cmCTestSVN(this->CTest, ofs)); break;
  209. case e_BZR: vc.reset(new cmCTestBZR(this->CTest, ofs)); break;
  210. case e_GIT: vc.reset(new cmCTestGIT(this->CTest, ofs)); break;
  211. case e_HG: vc.reset(new cmCTestHG(this->CTest, ofs)); break;
  212. case e_P4: vc.reset(new cmCTestP4(this->CTest, ofs)); break;
  213. default: vc.reset(new cmCTestVC(this->CTest, ofs)); break;
  214. }
  215. vc->SetCommandLineTool(this->UpdateCommand);
  216. vc->SetSourceDirectory(sourceDirectory);
  217. // Cleanup the working tree.
  218. vc->Cleanup();
  219. //
  220. // Now update repository and remember what files were updated
  221. //
  222. cmGeneratedFileStream os;
  223. if(!this->StartResultingXML(cmCTest::PartUpdate, "Update", os))
  224. {
  225. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file"
  226. << std::endl);
  227. return -1;
  228. }
  229. std::string start_time = this->CTest->CurrentTime();
  230. unsigned int start_time_time =
  231. static_cast<unsigned int>(cmSystemTools::GetTime());
  232. double elapsed_time_start = cmSystemTools::GetTime();
  233. bool updated = vc->Update();
  234. std::string buildname = cmCTest::SafeBuildIdField(
  235. this->CTest->GetCTestConfiguration("BuildName"));
  236. os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  237. << "<Update mode=\"Client\" Generator=\"ctest-"
  238. << cmVersion::GetCMakeVersion() << "\">\n"
  239. << "\t<Site>" << this->CTest->GetCTestConfiguration("Site") << "</Site>\n"
  240. << "\t<BuildName>" << buildname
  241. << "</BuildName>\n"
  242. << "\t<BuildStamp>" << this->CTest->GetCurrentTag() << "-"
  243. << this->CTest->GetTestModelString() << "</BuildStamp>" << std::endl;
  244. os << "\t<StartDateTime>" << start_time << "</StartDateTime>\n"
  245. << "\t<StartTime>" << start_time_time << "</StartTime>\n"
  246. << "\t<UpdateCommand>"
  247. << cmXMLSafe(vc->GetUpdateCommandLine()).Quotes(false)
  248. << "</UpdateCommand>\n"
  249. << "\t<UpdateType>" << cmXMLSafe(
  250. cmCTestUpdateHandlerUpdateToString(this->UpdateType))
  251. << "</UpdateType>\n";
  252. vc->WriteXML(os);
  253. int localModifications = 0;
  254. int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  255. if(numUpdated)
  256. {
  257. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  258. " Found " << numUpdated << " updated files\n");
  259. }
  260. if(int numModified = vc->GetPathCount(cmCTestVC::PathModified))
  261. {
  262. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  263. " Found " << numModified << " locally modified files\n");
  264. localModifications += numModified;
  265. }
  266. if(int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting))
  267. {
  268. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  269. " Found " << numConflicting << " conflicting files\n");
  270. localModifications += numConflicting;
  271. }
  272. cmCTestLog(this->CTest, DEBUG, "End" << std::endl);
  273. std::string end_time = this->CTest->CurrentTime();
  274. os << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  275. << "\t<EndTime>" << static_cast<unsigned int>(cmSystemTools::GetTime())
  276. << "</EndTime>\n"
  277. << "<ElapsedMinutes>" <<
  278. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  279. << "</ElapsedMinutes>\n"
  280. << "\t<UpdateReturnStatus>";
  281. if(localModifications)
  282. {
  283. os << "Update error: There are modified or conflicting files in the "
  284. "repository";
  285. cmCTestLog(this->CTest, ERROR_MESSAGE,
  286. " There are modified or conflicting files in the repository"
  287. << std::endl);
  288. }
  289. if(!updated)
  290. {
  291. os << "Update command failed:\n" << vc->GetUpdateCommandLine();
  292. cmCTestLog(this->CTest, ERROR_MESSAGE, " Update command failed: "
  293. << vc->GetUpdateCommandLine() << "\n");
  294. }
  295. os << "</UpdateReturnStatus>" << std::endl;
  296. os << "</Update>" << std::endl;
  297. return numUpdated;
  298. }
  299. //----------------------------------------------------------------------
  300. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  301. {
  302. std::string sourceDirectory = dir;
  303. cmCTestLog(this->CTest, DEBUG, "Check directory: "
  304. << sourceDirectory << std::endl);
  305. sourceDirectory += "/.svn";
  306. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  307. {
  308. return cmCTestUpdateHandler::e_SVN;
  309. }
  310. sourceDirectory = dir;
  311. sourceDirectory += "/CVS";
  312. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  313. {
  314. return cmCTestUpdateHandler::e_CVS;
  315. }
  316. sourceDirectory = dir;
  317. sourceDirectory += "/.bzr";
  318. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  319. {
  320. return cmCTestUpdateHandler::e_BZR;
  321. }
  322. sourceDirectory = dir;
  323. sourceDirectory += "/.git";
  324. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  325. {
  326. return cmCTestUpdateHandler::e_GIT;
  327. }
  328. sourceDirectory = dir;
  329. sourceDirectory += "/.hg";
  330. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  331. {
  332. return cmCTestUpdateHandler::e_HG;
  333. }
  334. sourceDirectory = dir;
  335. sourceDirectory += "/.p4";
  336. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  337. {
  338. return cmCTestUpdateHandler::e_P4;
  339. }
  340. sourceDirectory = dir;
  341. sourceDirectory += "/.p4config";
  342. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  343. {
  344. return cmCTestUpdateHandler::e_P4;
  345. }
  346. return cmCTestUpdateHandler::e_UNKNOWN;
  347. }
  348. //----------------------------------------------------------------------
  349. bool cmCTestUpdateHandler::SelectVCS()
  350. {
  351. // Get update command
  352. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  353. // Detect the VCS managing the source tree.
  354. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  355. if (this->UpdateType == e_UNKNOWN)
  356. {
  357. // The source tree does not have a recognized VCS. Check the
  358. // configuration value or command name.
  359. this->UpdateType = this->DetermineType(this->UpdateCommand.c_str(),
  360. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  361. }
  362. // If no update command was specified, lookup one for this VCS tool.
  363. if (this->UpdateCommand.empty())
  364. {
  365. const char* key = 0;
  366. switch (this->UpdateType)
  367. {
  368. case e_CVS: key = "CVSCommand"; break;
  369. case e_SVN: key = "SVNCommand"; break;
  370. case e_BZR: key = "BZRCommand"; break;
  371. case e_GIT: key = "GITCommand"; break;
  372. case e_HG: key = "HGCommand"; break;
  373. case e_P4: key = "P4Command"; break;
  374. default: break;
  375. }
  376. if (key)
  377. {
  378. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  379. }
  380. if (this->UpdateCommand.empty())
  381. {
  382. cmOStringStream e;
  383. e << "Cannot find UpdateCommand ";
  384. if (key)
  385. {
  386. e << "or " << key;
  387. }
  388. e << " configuration key.";
  389. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  390. return false;
  391. }
  392. }
  393. return true;
  394. }