cmCTestUpdateHandler.cxx 11 KB

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