cmCTestUpdateHandler.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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, " 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. case e_HG: vc.reset(new cmCTestHG(this->CTest, ofs)); break;
  202. default: vc.reset(new cmCTestVC(this->CTest, ofs)); break;
  203. }
  204. vc->SetCommandLineTool(this->UpdateCommand);
  205. vc->SetSourceDirectory(sourceDirectory);
  206. // Cleanup the working tree.
  207. vc->Cleanup();
  208. //
  209. // Now update repository and remember what files were updated
  210. //
  211. cmGeneratedFileStream os;
  212. if(!this->StartResultingXML(cmCTest::PartUpdate, "Update", os))
  213. {
  214. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file"
  215. << std::endl);
  216. return -1;
  217. }
  218. std::string start_time = this->CTest->CurrentTime();
  219. unsigned int start_time_time =
  220. static_cast<unsigned int>(cmSystemTools::GetTime());
  221. double elapsed_time_start = cmSystemTools::GetTime();
  222. bool updated = vc->Update();
  223. os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  224. << "<Update mode=\"Client\" Generator=\"ctest-"
  225. << cmVersion::GetCMakeVersion() << "\">\n"
  226. << "\t<Site>" << this->CTest->GetCTestConfiguration("Site") << "</Site>\n"
  227. << "\t<BuildName>" << this->CTest->GetCTestConfiguration("BuildName")
  228. << "</BuildName>\n"
  229. << "\t<BuildStamp>" << this->CTest->GetCurrentTag() << "-"
  230. << this->CTest->GetTestModelString() << "</BuildStamp>" << std::endl;
  231. os << "\t<StartDateTime>" << start_time << "</StartDateTime>\n"
  232. << "\t<StartTime>" << start_time_time << "</StartTime>\n"
  233. << "\t<UpdateCommand>"
  234. << cmXMLSafe(vc->GetUpdateCommandLine()).Quotes(false)
  235. << "</UpdateCommand>\n"
  236. << "\t<UpdateType>" << cmXMLSafe(
  237. cmCTestUpdateHandlerUpdateToString(this->UpdateType))
  238. << "</UpdateType>\n";
  239. vc->WriteXML(os);
  240. int localModifications = 0;
  241. int numUpdated = vc->GetPathCount(cmCTestVC::PathUpdated);
  242. if(numUpdated)
  243. {
  244. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  245. " Found " << numUpdated << " updated files\n");
  246. }
  247. if(int numModified = vc->GetPathCount(cmCTestVC::PathModified))
  248. {
  249. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  250. " Found " << numModified << " locally modified files\n");
  251. localModifications += numModified;
  252. }
  253. if(int numConflicting = vc->GetPathCount(cmCTestVC::PathConflicting))
  254. {
  255. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  256. " Found " << numConflicting << " conflicting files\n");
  257. localModifications += numConflicting;
  258. }
  259. cmCTestLog(this->CTest, DEBUG, "End" << std::endl);
  260. std::string end_time = this->CTest->CurrentTime();
  261. os << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  262. << "\t<EndTime>" << static_cast<unsigned int>(cmSystemTools::GetTime())
  263. << "</EndTime>\n"
  264. << "<ElapsedMinutes>" <<
  265. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  266. << "</ElapsedMinutes>\n"
  267. << "\t<UpdateReturnStatus>";
  268. if(localModifications)
  269. {
  270. os << "Update error: There are modified or conflicting files in the "
  271. "repository";
  272. cmCTestLog(this->CTest, ERROR_MESSAGE,
  273. " There are modified or conflicting files in the repository"
  274. << std::endl);
  275. }
  276. if(!updated)
  277. {
  278. os << "Update command failed:\n" << vc->GetUpdateCommandLine();
  279. cmCTestLog(this->CTest, ERROR_MESSAGE, " Update command failed: "
  280. << vc->GetUpdateCommandLine() << "\n");
  281. }
  282. os << "</UpdateReturnStatus>" << std::endl;
  283. os << "</Update>" << std::endl;
  284. return numUpdated;
  285. }
  286. //----------------------------------------------------------------------
  287. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  288. {
  289. std::string sourceDirectory = dir;
  290. cmCTestLog(this->CTest, DEBUG, "Check directory: "
  291. << sourceDirectory.c_str() << std::endl);
  292. sourceDirectory += "/.svn";
  293. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  294. {
  295. return cmCTestUpdateHandler::e_SVN;
  296. }
  297. sourceDirectory = dir;
  298. sourceDirectory += "/CVS";
  299. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  300. {
  301. return cmCTestUpdateHandler::e_CVS;
  302. }
  303. sourceDirectory = dir;
  304. sourceDirectory += "/.bzr";
  305. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  306. {
  307. return cmCTestUpdateHandler::e_BZR;
  308. }
  309. sourceDirectory = dir;
  310. sourceDirectory += "/.git";
  311. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  312. {
  313. return cmCTestUpdateHandler::e_GIT;
  314. }
  315. sourceDirectory = dir;
  316. sourceDirectory += "/.hg";
  317. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  318. {
  319. return cmCTestUpdateHandler::e_HG;
  320. }
  321. return cmCTestUpdateHandler::e_UNKNOWN;
  322. }
  323. //----------------------------------------------------------------------
  324. bool cmCTestUpdateHandler::SelectVCS()
  325. {
  326. // Get update command
  327. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  328. // Detect the VCS managing the source tree.
  329. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  330. if (this->UpdateType == e_UNKNOWN)
  331. {
  332. // The source tree does not have a recognized VCS. Check the
  333. // configuration value or command name.
  334. this->UpdateType = this->DetermineType(this->UpdateCommand.c_str(),
  335. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  336. }
  337. // If no update command was specified, lookup one for this VCS tool.
  338. if (this->UpdateCommand.empty())
  339. {
  340. const char* key = 0;
  341. switch (this->UpdateType)
  342. {
  343. case e_CVS: key = "CVSCommand"; break;
  344. case e_SVN: key = "SVNCommand"; break;
  345. case e_BZR: key = "BZRCommand"; break;
  346. case e_GIT: key = "GITCommand"; break;
  347. case e_HG: key = "HGCommand"; break;
  348. default: break;
  349. }
  350. if (key)
  351. {
  352. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  353. }
  354. if (this->UpdateCommand.empty())
  355. {
  356. cmOStringStream e;
  357. e << "Cannot find UpdateCommand ";
  358. if (key)
  359. {
  360. e << "or " << key;
  361. }
  362. e << " configuration key.";
  363. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  364. return false;
  365. }
  366. }
  367. return true;
  368. }