cmCTestUpdateHandler.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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.c_str());
  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. os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  235. << "<Update mode=\"Client\" Generator=\"ctest-"
  236. << cmVersion::GetCMakeVersion() << "\">\n"
  237. << "\t<Site>" << this->CTest->GetCTestConfiguration("Site") << "</Site>\n"
  238. << "\t<BuildName>" << this->CTest->GetCTestConfiguration("BuildName")
  239. << "</BuildName>\n"
  240. << "\t<BuildStamp>" << this->CTest->GetCurrentTag() << "-"
  241. << this->CTest->GetTestModelString() << "</BuildStamp>" << std::endl;
  242. os << "\t<StartDateTime>" << start_time << "</StartDateTime>\n"
  243. << "\t<StartTime>" << start_time_time << "</StartTime>\n"
  244. << "\t<UpdateCommand>"
  245. << cmXMLSafe(vc->GetUpdateCommandLine()).Quotes(false)
  246. << "</UpdateCommand>\n"
  247. << "\t<UpdateType>" << cmXMLSafe(
  248. cmCTestUpdateHandlerUpdateToString(this->UpdateType))
  249. << "</UpdateType>\n";
  250. vc->WriteXML(os);
  251. int localModifications = 0;
  252. int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  253. if(numUpdated)
  254. {
  255. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  256. " Found " << numUpdated << " updated files\n");
  257. }
  258. if(int numModified = vc->GetPathCount(cmCTestVC::PathModified))
  259. {
  260. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  261. " Found " << numModified << " locally modified files\n");
  262. localModifications += numModified;
  263. }
  264. if(int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting))
  265. {
  266. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  267. " Found " << numConflicting << " conflicting files\n");
  268. localModifications += numConflicting;
  269. }
  270. cmCTestLog(this->CTest, DEBUG, "End" << std::endl);
  271. std::string end_time = this->CTest->CurrentTime();
  272. os << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  273. << "\t<EndTime>" << static_cast<unsigned int>(cmSystemTools::GetTime())
  274. << "</EndTime>\n"
  275. << "<ElapsedMinutes>" <<
  276. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  277. << "</ElapsedMinutes>\n"
  278. << "\t<UpdateReturnStatus>";
  279. if(localModifications)
  280. {
  281. os << "Update error: There are modified or conflicting files in the "
  282. "repository";
  283. cmCTestLog(this->CTest, ERROR_MESSAGE,
  284. " There are modified or conflicting files in the repository"
  285. << std::endl);
  286. }
  287. if(!updated)
  288. {
  289. os << "Update command failed:\n" << vc->GetUpdateCommandLine();
  290. cmCTestLog(this->CTest, ERROR_MESSAGE, " Update command failed: "
  291. << vc->GetUpdateCommandLine() << "\n");
  292. }
  293. os << "</UpdateReturnStatus>" << std::endl;
  294. os << "</Update>" << std::endl;
  295. return numUpdated;
  296. }
  297. //----------------------------------------------------------------------
  298. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  299. {
  300. std::string sourceDirectory = dir;
  301. cmCTestLog(this->CTest, DEBUG, "Check directory: "
  302. << sourceDirectory.c_str() << std::endl);
  303. sourceDirectory += "/.svn";
  304. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  305. {
  306. return cmCTestUpdateHandler::e_SVN;
  307. }
  308. sourceDirectory = dir;
  309. sourceDirectory += "/CVS";
  310. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  311. {
  312. return cmCTestUpdateHandler::e_CVS;
  313. }
  314. sourceDirectory = dir;
  315. sourceDirectory += "/.bzr";
  316. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  317. {
  318. return cmCTestUpdateHandler::e_BZR;
  319. }
  320. sourceDirectory = dir;
  321. sourceDirectory += "/.git";
  322. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  323. {
  324. return cmCTestUpdateHandler::e_GIT;
  325. }
  326. sourceDirectory = dir;
  327. sourceDirectory += "/.hg";
  328. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  329. {
  330. return cmCTestUpdateHandler::e_HG;
  331. }
  332. sourceDirectory = dir;
  333. sourceDirectory += "/.p4";
  334. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  335. {
  336. return cmCTestUpdateHandler::e_P4;
  337. }
  338. sourceDirectory = dir;
  339. sourceDirectory += "/.p4config";
  340. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  341. {
  342. return cmCTestUpdateHandler::e_P4;
  343. }
  344. return cmCTestUpdateHandler::e_UNKNOWN;
  345. }
  346. //----------------------------------------------------------------------
  347. bool cmCTestUpdateHandler::SelectVCS()
  348. {
  349. // Get update command
  350. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  351. // Detect the VCS managing the source tree.
  352. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  353. if (this->UpdateType == e_UNKNOWN)
  354. {
  355. // The source tree does not have a recognized VCS. Check the
  356. // configuration value or command name.
  357. this->UpdateType = this->DetermineType(this->UpdateCommand.c_str(),
  358. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  359. }
  360. // If no update command was specified, lookup one for this VCS tool.
  361. if (this->UpdateCommand.empty())
  362. {
  363. const char* key = 0;
  364. switch (this->UpdateType)
  365. {
  366. case e_CVS: key = "CVSCommand"; break;
  367. case e_SVN: key = "SVNCommand"; break;
  368. case e_BZR: key = "BZRCommand"; break;
  369. case e_GIT: key = "GITCommand"; break;
  370. case e_HG: key = "HGCommand"; break;
  371. case e_P4: key = "P4Command"; break;
  372. default: break;
  373. }
  374. if (key)
  375. {
  376. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  377. }
  378. if (this->UpdateCommand.empty())
  379. {
  380. cmOStringStream e;
  381. e << "Cannot find UpdateCommand ";
  382. if (key)
  383. {
  384. e << "or " << key;
  385. }
  386. e << " configuration key.";
  387. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  388. return false;
  389. }
  390. }
  391. return true;
  392. }