cmCTestUpdateHandler.cxx 12 KB

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