cmCTestUpdateHandler.cxx 13 KB

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