cmCTestUpdateHandler.cxx 13 KB

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