cmCTestUpdateHandler.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. //----------------------------------------------------------------------
  53. //**********************************************************************
  54. class cmCTestUpdateHandlerSVNXMLParser : public cmXMLParser
  55. {
  56. public:
  57. struct t_CommitLog
  58. {
  59. int Revision;
  60. std::string Author;
  61. std::string Date;
  62. std::string Message;
  63. };
  64. cmCTestUpdateHandlerSVNXMLParser(cmCTestUpdateHandler* up)
  65. : cmXMLParser(), UpdateHandler(up), MinRevision(-1), MaxRevision(-1)
  66. {
  67. }
  68. int Parse(const char* str)
  69. {
  70. this->MinRevision = -1;
  71. this->MaxRevision = -1;
  72. int res = this->cmXMLParser::Parse(str);
  73. if ( this->MinRevision == -1 || this->MaxRevision == -1 )
  74. {
  75. return 0;
  76. }
  77. return res;
  78. }
  79. typedef std::vector<t_CommitLog> t_VectorOfCommits;
  80. t_VectorOfCommits* GetCommits() { return &this->Commits; }
  81. int GetMinRevision() { return this->MinRevision; }
  82. int GetMaxRevision() { return this->MaxRevision; }
  83. protected:
  84. void StartElement(const char* name, const char** atts)
  85. {
  86. if ( strcmp(name, "logentry") == 0 )
  87. {
  88. this->CommitLog = t_CommitLog();
  89. const char* rev = this->FindAttribute(atts, "revision");
  90. if ( rev)
  91. {
  92. this->CommitLog.Revision = atoi(rev);
  93. if ( this->MinRevision < 0 ||
  94. this->MinRevision > this->CommitLog.Revision )
  95. {
  96. this->MinRevision = this->CommitLog.Revision;
  97. }
  98. if ( this->MaxRevision < 0 ||
  99. this->MaxRevision < this->CommitLog.Revision )
  100. {
  101. this->MaxRevision = this->CommitLog.Revision;
  102. }
  103. }
  104. }
  105. this->CharacterData.erase(
  106. this->CharacterData.begin(), this->CharacterData.end());
  107. }
  108. void EndElement(const char* name)
  109. {
  110. if ( strcmp(name, "logentry") == 0 )
  111. {
  112. cmCTestLog(this->UpdateHandler->GetCTestInstance(),
  113. HANDLER_VERBOSE_OUTPUT,
  114. "\tRevision: " << this->CommitLog.Revision<< std::endl
  115. << "\tAuthor: " << this->CommitLog.Author.c_str() << std::endl
  116. << "\tDate: " << this->CommitLog.Date.c_str() << std::endl
  117. << "\tMessage: " << this->CommitLog.Message.c_str() << std::endl);
  118. this->Commits.push_back(this->CommitLog);
  119. }
  120. else if ( strcmp(name, "author") == 0 )
  121. {
  122. this->CommitLog.Author.assign(&(*(this->CharacterData.begin())),
  123. this->CharacterData.size());
  124. }
  125. else if ( strcmp(name, "date") == 0 )
  126. {
  127. this->CommitLog.Date.assign(&(*(this->CharacterData.begin())),
  128. this->CharacterData.size());
  129. }
  130. else if ( strcmp(name, "msg") == 0 )
  131. {
  132. this->CommitLog.Message.assign(&(*(this->CharacterData.begin())),
  133. this->CharacterData.size());
  134. }
  135. this->CharacterData.erase(this->CharacterData.begin(),
  136. this->CharacterData.end());
  137. }
  138. void CharacterDataHandler(const char* data, int length)
  139. {
  140. this->CharacterData.insert(this->CharacterData.end(), data, data+length);
  141. }
  142. const char* FindAttribute( const char** atts, const char* attribute )
  143. {
  144. if ( !atts || !attribute )
  145. {
  146. return 0;
  147. }
  148. const char **atr = atts;
  149. while ( *atr && **atr && **(atr+1) )
  150. {
  151. if ( strcmp(*atr, attribute) == 0 )
  152. {
  153. return *(atr+1);
  154. }
  155. atr+=2;
  156. }
  157. return 0;
  158. }
  159. private:
  160. std::vector<char> CharacterData;
  161. cmCTestUpdateHandler* UpdateHandler;
  162. t_CommitLog CommitLog;
  163. t_VectorOfCommits Commits;
  164. int MinRevision;
  165. int MaxRevision;
  166. };
  167. //**********************************************************************
  168. //----------------------------------------------------------------------
  169. class cmCTestUpdateHandlerLocale
  170. {
  171. public:
  172. cmCTestUpdateHandlerLocale();
  173. ~cmCTestUpdateHandlerLocale();
  174. private:
  175. std::string saveLCMessages;
  176. };
  177. cmCTestUpdateHandlerLocale::cmCTestUpdateHandlerLocale()
  178. {
  179. const char* lcmess = cmSystemTools::GetEnv("LC_MESSAGES");
  180. if(lcmess)
  181. {
  182. saveLCMessages = lcmess;
  183. }
  184. // if LC_MESSAGES is not set to C, then
  185. // set it, so that svn/cvs info will be in english ascii
  186. if(! (lcmess && strcmp(lcmess, "C") == 0))
  187. {
  188. cmSystemTools::PutEnv("LC_MESSAGES=C");
  189. }
  190. }
  191. cmCTestUpdateHandlerLocale::~cmCTestUpdateHandlerLocale()
  192. {
  193. // restore the value of LC_MESSAGES after running the version control
  194. // commands
  195. if(saveLCMessages.size())
  196. {
  197. std::string put = "LC_MESSAGES=";
  198. put += saveLCMessages;
  199. cmSystemTools::PutEnv(put.c_str());
  200. }
  201. else
  202. {
  203. cmSystemTools::UnsetEnv("LC_MESSAGES");
  204. }
  205. }
  206. //----------------------------------------------------------------------
  207. cmCTestUpdateHandler::cmCTestUpdateHandler()
  208. {
  209. }
  210. //----------------------------------------------------------------------
  211. void cmCTestUpdateHandler::Initialize()
  212. {
  213. this->Superclass::Initialize();
  214. this->UpdateCommand = "";
  215. this->UpdateType = e_CVS;
  216. }
  217. //----------------------------------------------------------------------
  218. int cmCTestUpdateHandler::DetermineType(const char* cmd, const char* type)
  219. {
  220. cmCTestLog(this->CTest, DEBUG, "Determine update type from command: " << cmd
  221. << " and type: " << type << std::endl);
  222. if ( type && *type )
  223. {
  224. cmCTestLog(this->CTest, DEBUG, "Type specified: " << type << std::endl);
  225. std::string stype = cmSystemTools::LowerCase(type);
  226. if ( stype.find("cvs") != std::string::npos )
  227. {
  228. return cmCTestUpdateHandler::e_CVS;
  229. }
  230. if ( stype.find("svn") != std::string::npos )
  231. {
  232. return cmCTestUpdateHandler::e_SVN;
  233. }
  234. }
  235. else
  236. {
  237. cmCTestLog(this->CTest, DEBUG, "Type not specified, check command: "
  238. << cmd << std::endl);
  239. std::string stype = cmSystemTools::LowerCase(cmd);
  240. if ( stype.find("cvs") != std::string::npos )
  241. {
  242. return cmCTestUpdateHandler::e_CVS;
  243. }
  244. if ( stype.find("svn") != std::string::npos )
  245. {
  246. return cmCTestUpdateHandler::e_SVN;
  247. }
  248. }
  249. return cmCTestUpdateHandler::e_UNKNOWN;
  250. }
  251. //----------------------------------------------------------------------
  252. //clearly it would be nice if this were broken up into a few smaller
  253. //functions and commented...
  254. int cmCTestUpdateHandler::ProcessHandler()
  255. {
  256. int count = 0;
  257. std::string::size_type cc, kk;
  258. std::string goutput;
  259. std::string errors;
  260. // Make sure VCS tool messages are in English so we can parse them.
  261. cmCTestUpdateHandlerLocale fixLocale;
  262. static_cast<void>(fixLocale);
  263. int retVal = 0;
  264. // Get source dir
  265. const char* sourceDirectory = this->GetOption("SourceDirectory");
  266. if ( !sourceDirectory )
  267. {
  268. cmCTestLog(this->CTest, ERROR_MESSAGE,
  269. "Cannot find SourceDirectory key in the DartConfiguration.tcl"
  270. << std::endl);
  271. return -1;
  272. }
  273. cmGeneratedFileStream ofs;
  274. if ( !this->CTest->GetShowOnly() )
  275. {
  276. this->StartLogFile("Update", ofs);
  277. }
  278. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  279. "Updating the repository" << std::endl);
  280. // Make sure the source directory exists.
  281. if(!this->InitialCheckout(ofs))
  282. {
  283. return -1;
  284. }
  285. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Updating the repository: "
  286. << sourceDirectory << std::endl);
  287. if(!this->SelectVCS())
  288. {
  289. return -1;
  290. }
  291. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Use "
  292. << cmCTestUpdateHandlerUpdateToString(this->UpdateType)
  293. << " repository type"
  294. << std::endl;);
  295. // Create an object to interact with the VCS tool.
  296. cmsys::auto_ptr<cmCTestVC> vc;
  297. switch (this->UpdateType)
  298. {
  299. case e_CVS: vc.reset(new cmCTestCVS(this->CTest, ofs)); break;
  300. case e_SVN: vc.reset(new cmCTestSVN(this->CTest, ofs)); break;
  301. default: vc.reset(new cmCTestVC(this->CTest, ofs)); break;
  302. }
  303. vc->SetCommandLineTool(this->UpdateCommand);
  304. vc->SetSourceDirectory(sourceDirectory);
  305. // And update options
  306. std::string updateOptions
  307. = this->CTest->GetCTestConfiguration("UpdateOptions");
  308. if ( updateOptions.empty() )
  309. {
  310. switch (this->UpdateType)
  311. {
  312. case cmCTestUpdateHandler::e_CVS:
  313. updateOptions = this->CTest->GetCTestConfiguration("CVSUpdateOptions");
  314. if ( updateOptions.empty() )
  315. {
  316. updateOptions = "-dP";
  317. }
  318. break;
  319. case cmCTestUpdateHandler::e_SVN:
  320. updateOptions = this->CTest->GetCTestConfiguration("SVNUpdateOptions");
  321. break;
  322. }
  323. }
  324. // Get update time
  325. std::string extra_update_opts;
  326. if ( this->CTest->GetTestModel() == cmCTest::NIGHTLY )
  327. {
  328. std::string today_update_date = vc->GetNightlyTime();
  329. // TODO: SVN
  330. switch ( this->UpdateType )
  331. {
  332. case cmCTestUpdateHandler::e_CVS:
  333. extra_update_opts += "-D \"" + today_update_date +" UTC\"";
  334. break;
  335. case cmCTestUpdateHandler::e_SVN:
  336. extra_update_opts += "-r \"{" + today_update_date +" +0000}\"";
  337. break;
  338. }
  339. }
  340. // Cleanup the working tree.
  341. vc->Cleanup();
  342. bool res = true;
  343. // CVS variables
  344. // SVN variables
  345. int svn_current_revision = 0;
  346. int svn_latest_revision = 0;
  347. int svn_use_status = 0;
  348. // Get initial repository information if that is possible.
  349. vc->MarkOldRevision();
  350. if(this->UpdateType == e_SVN)
  351. {
  352. svn_current_revision =
  353. static_cast<cmCTestSVN*>(vc.get())->GetOldRevision();
  354. }
  355. //
  356. // Now update repository and remember what files were updated
  357. //
  358. cmGeneratedFileStream os;
  359. if(!this->StartResultingXML(cmCTest::PartUpdate, "Update", os))
  360. {
  361. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file"
  362. << std::endl);
  363. return -1;
  364. }
  365. std::string start_time = this->CTest->CurrentTime();
  366. unsigned int start_time_time =
  367. static_cast<unsigned int>(cmSystemTools::GetTime());
  368. double elapsed_time_start = cmSystemTools::GetTime();
  369. std::string command;
  370. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "* Update repository: "
  371. << command.c_str() << std::endl);
  372. if ( !this->CTest->GetShowOnly() )
  373. {
  374. command = "";
  375. switch( this->UpdateType )
  376. {
  377. case cmCTestUpdateHandler::e_CVS:
  378. command = "\""+this->UpdateCommand+"\" -z3 update " + updateOptions +
  379. " " + extra_update_opts;
  380. ofs << "* Update repository: " << std::endl;
  381. ofs << " Command: " << command.c_str() << std::endl;
  382. res = this->CTest->RunCommand(command.c_str(), &goutput, &errors,
  383. &retVal, sourceDirectory, 0 /*this->TimeOut*/);
  384. ofs << " Output: " << goutput.c_str() << std::endl;
  385. ofs << " Errors: " << errors.c_str() << std::endl;
  386. break;
  387. case cmCTestUpdateHandler::e_SVN:
  388. {
  389. std::string partialOutput;
  390. command = "\"" + this->UpdateCommand + "\" update " + updateOptions +
  391. " " + extra_update_opts;
  392. ofs << "* Update repository: " << std::endl;
  393. ofs << " Command: " << command.c_str() << std::endl;
  394. bool res1 = this->CTest->RunCommand(command.c_str(), &partialOutput,
  395. &errors,
  396. &retVal, sourceDirectory, 0 /*this->TimeOut*/);
  397. ofs << " Output: " << partialOutput.c_str() << std::endl;
  398. ofs << " Errors: " << errors.c_str() << std::endl;
  399. goutput = partialOutput;
  400. command = "\"" + this->UpdateCommand + "\" status";
  401. ofs << "* Status repository: " << std::endl;
  402. ofs << " Command: " << command.c_str() << std::endl;
  403. res = this->CTest->RunCommand(command.c_str(), &partialOutput,
  404. &errors, &retVal, sourceDirectory, 0 /*this->TimeOut*/);
  405. ofs << " Output: " << partialOutput.c_str() << std::endl;
  406. ofs << " Errors: " << errors.c_str() << std::endl;
  407. goutput += partialOutput;
  408. res = res && res1;
  409. ofs << " Total output of update: " << goutput.c_str() << std::endl;
  410. }
  411. }
  412. if ( ofs )
  413. {
  414. ofs << "--- Update repository ---" << std::endl;
  415. ofs << goutput << std::endl;
  416. }
  417. }
  418. bool updateProducedError = !res || retVal;
  419. os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  420. << "<Update mode=\"Client\" Generator=\"ctest-"
  421. << cmVersion::GetCMakeVersion() << "\">\n"
  422. << "\t<Site>" << this->CTest->GetCTestConfiguration("Site") << "</Site>\n"
  423. << "\t<BuildName>" << this->CTest->GetCTestConfiguration("BuildName")
  424. << "</BuildName>\n"
  425. << "\t<BuildStamp>" << this->CTest->GetCurrentTag() << "-"
  426. << this->CTest->GetTestModelString() << "</BuildStamp>" << std::endl;
  427. os << "\t<StartDateTime>" << start_time << "</StartDateTime>\n"
  428. << "\t<StartTime>" << start_time_time << "</StartTime>\n"
  429. << "\t<UpdateCommand>" << cmXMLSafe(command)
  430. << "</UpdateCommand>\n"
  431. << "\t<UpdateType>" << cmXMLSafe(
  432. cmCTestUpdateHandlerUpdateToString(this->UpdateType))
  433. << "</UpdateType>\n";
  434. // Even though it failed, we may have some useful information. Try to
  435. // continue...
  436. std::vector<cmStdString> lines;
  437. cmSystemTools::Split(goutput.c_str(), lines);
  438. std::vector<cmStdString> errLines;
  439. cmSystemTools::Split(errors.c_str(), errLines);
  440. lines.insert(lines.end(), errLines.begin(), errLines.end());
  441. // CVS style regular expressions
  442. cmsys::RegularExpression cvs_date_author_regex(
  443. "^date: +([^;]+); +author: +([^;]+); +state: +[^;]+;");
  444. cmsys::RegularExpression cvs_revision_regex("^revision +([^ ]*) *$");
  445. cmsys::RegularExpression cvs_end_of_file_regex(
  446. "^=========================================="
  447. "===================================$");
  448. cmsys::RegularExpression cvs_end_of_comment_regex(
  449. "^----------------------------$");
  450. // Subversion style regular expressions
  451. cmsys::RegularExpression svn_status_line_regex(
  452. "^ *([0-9]+) *([0-9]+) *([^ ]+) *([^ ][^\t\r\n]*)[ \t\r\n]*$");
  453. cmsys::RegularExpression svn_latest_revision_regex(
  454. "(Updated to|At) revision ([0-9]+)\\.");
  455. cmsys::RegularExpression file_removed_line(
  456. "cvs update: `?([^']*)'? is no longer in the repository");
  457. cmsys::RegularExpression file_removed_line2(
  458. "cvs update: warning: `?([^']*)'? is not \\(any longer\\) pertinent");
  459. cmsys::RegularExpression file_update_line("([A-Z]) *(.*)");
  460. std::string current_path = "<no-path>";
  461. bool first_file = true;
  462. std::set<cmStdString> author_set;
  463. int numUpdated = 0;
  464. int numModified = 0;
  465. int numConflicting = 0;
  466. // Get final repository information if that is possible.
  467. vc->MarkNewRevision();
  468. if ( this->UpdateType == cmCTestUpdateHandler::e_SVN )
  469. {
  470. svn_latest_revision =
  471. static_cast<cmCTestSVN*>(vc.get())->GetNewRevision();
  472. }
  473. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  474. " Gathering version information (each . represents one updated file):"
  475. << std::endl);
  476. int file_count = 0;
  477. std::string removed_line;
  478. for ( cc= 0; cc < lines.size(); cc ++ )
  479. {
  480. const char* line = lines[cc].c_str();
  481. if ( file_removed_line.find(line) )
  482. {
  483. removed_line = "D " + file_removed_line.match(1);
  484. line = removed_line.c_str();
  485. }
  486. else if ( file_removed_line2.find(line) )
  487. {
  488. removed_line = "D " + file_removed_line2.match(1);
  489. line = removed_line.c_str();
  490. }
  491. if ( file_update_line.find(line) )
  492. {
  493. if ( file_count == 0 )
  494. {
  495. cmCTestLog(this->CTest, HANDLER_OUTPUT, " " << std::flush);
  496. }
  497. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  498. std::string upChar = file_update_line.match(1);
  499. std::string upFile = file_update_line.match(2);
  500. char mod = upChar[0];
  501. bool notLocallyModified = false;
  502. if ( mod == 'X' || mod == 'L')
  503. {
  504. continue;
  505. }
  506. if ( mod != 'M' && mod != 'C' && mod != 'G' )
  507. {
  508. count ++;
  509. notLocallyModified = true;
  510. }
  511. const char* file = upFile.c_str();
  512. cmCTestLog(this->CTest, DEBUG, "Line" << cc << ": " << mod << " - "
  513. << file << std::endl);
  514. std::string output;
  515. if ( notLocallyModified )
  516. {
  517. std::string logcommand;
  518. switch ( this->UpdateType )
  519. {
  520. case cmCTestUpdateHandler::e_CVS:
  521. logcommand = "\"" + this->UpdateCommand + "\" -z3 log -N \""
  522. + file + "\"";
  523. break;
  524. case cmCTestUpdateHandler::e_SVN:
  525. if ( svn_latest_revision > 0 &&
  526. svn_latest_revision > svn_current_revision )
  527. {
  528. cmOStringStream logCommandStream;
  529. logCommandStream << "\"" << this->UpdateCommand << "\" log -r "
  530. << svn_current_revision << ":" << svn_latest_revision
  531. << " --xml \"" << file << "\"";
  532. logcommand = logCommandStream.str();
  533. }
  534. else
  535. {
  536. logcommand = "\"" + this->UpdateCommand +
  537. "\" status --verbose \"" + file + "\"";
  538. svn_use_status = 1;
  539. }
  540. break;
  541. }
  542. cmCTestLog(this->CTest, DEBUG, "Do log: " << logcommand << std::endl);
  543. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  544. "* Get file update information: " << logcommand.c_str()
  545. << std::endl);
  546. ofs << "* Get log information for file: " << file << std::endl;
  547. ofs << " Command: " << logcommand.c_str() << std::endl;
  548. res = this->CTest->RunCommand(logcommand.c_str(), &output, &errors,
  549. &retVal, sourceDirectory, 0 /*this->TimeOut*/);
  550. ofs << " Output: " << output.c_str() << std::endl;
  551. ofs << " Errors: " << errors.c_str() << std::endl;
  552. if ( ofs )
  553. {
  554. ofs << output << std::endl;
  555. }
  556. }
  557. else
  558. {
  559. res = false;
  560. }
  561. if ( res )
  562. {
  563. cmCTestLog(this->CTest, DEBUG, output << std::endl);
  564. std::string::size_type sline = 0;
  565. std::string srevision1 = "Unknown";
  566. std::string sdate1 = "Unknown";
  567. std::string sauthor1 = "Unknown";
  568. std::string semail1 = "Unknown";
  569. std::string comment1 = "";
  570. std::string srevision2 = "Unknown";
  571. if ( this->UpdateType == cmCTestUpdateHandler::e_CVS )
  572. {
  573. bool have_first = false;
  574. bool have_second = false;
  575. std::vector<cmStdString> ulines;
  576. cmSystemTools::Split(output.c_str(), ulines);
  577. for ( kk = 0; kk < ulines.size(); kk ++ )
  578. {
  579. const char* clp = ulines[kk].c_str();
  580. if ( !have_second && !sline && cvs_revision_regex.find(clp) )
  581. {
  582. if ( !have_first )
  583. {
  584. srevision1 = cvs_revision_regex.match(1);
  585. }
  586. else
  587. {
  588. srevision2 = cvs_revision_regex.match(1);
  589. }
  590. }
  591. else if ( !have_second && !sline &&
  592. cvs_date_author_regex.find(clp) )
  593. {
  594. sline = kk + 1;
  595. if ( !have_first )
  596. {
  597. sdate1 = cvs_date_author_regex.match(1);
  598. sauthor1 = cvs_date_author_regex.match(2);
  599. }
  600. }
  601. else if ( sline && cvs_end_of_comment_regex.find(clp) ||
  602. cvs_end_of_file_regex.find(clp))
  603. {
  604. if ( !have_first )
  605. {
  606. have_first = true;
  607. }
  608. else if ( !have_second )
  609. {
  610. have_second = true;
  611. }
  612. sline = 0;
  613. }
  614. else if ( sline )
  615. {
  616. if ( !have_first )
  617. {
  618. comment1 += clp;
  619. comment1 += "\n";
  620. }
  621. }
  622. }
  623. }
  624. else if ( this->UpdateType == cmCTestUpdateHandler::e_SVN )
  625. {
  626. if ( svn_use_status )
  627. {
  628. cmOStringStream str;
  629. str << svn_current_revision;
  630. srevision1 = str.str();
  631. if (!svn_status_line_regex.find(output))
  632. {
  633. cmCTestLog(this->CTest, ERROR_MESSAGE,
  634. "Bad output from SVN status command: " << output
  635. << std::endl);
  636. }
  637. else if ( svn_status_line_regex.match(4) != file )
  638. {
  639. cmCTestLog(this->CTest, ERROR_MESSAGE,
  640. "Bad output from SVN status command. "
  641. "The file name returned: \""
  642. << svn_status_line_regex.match(4)
  643. << "\" was different than the file specified: \"" << file
  644. << "\"" << std::endl);
  645. }
  646. else
  647. {
  648. srevision1 = svn_status_line_regex.match(2);
  649. int latest_revision = atoi(
  650. svn_status_line_regex.match(2).c_str());
  651. if ( svn_current_revision < latest_revision )
  652. {
  653. srevision2 = str.str();
  654. }
  655. sauthor1 = svn_status_line_regex.match(3);
  656. }
  657. }
  658. else
  659. {
  660. cmCTestUpdateHandlerSVNXMLParser parser(this);
  661. if ( parser.Parse(output.c_str()) )
  662. {
  663. int minrev = parser.GetMinRevision();
  664. int maxrev = parser.GetMaxRevision();
  665. cmCTestUpdateHandlerSVNXMLParser::
  666. t_VectorOfCommits::iterator it;
  667. for ( it = parser.GetCommits()->begin();
  668. it != parser.GetCommits()->end();
  669. ++ it )
  670. {
  671. if ( it->Revision == maxrev )
  672. {
  673. cmOStringStream mRevStream;
  674. mRevStream << maxrev;
  675. srevision1 = mRevStream.str();
  676. sauthor1 = it->Author;
  677. comment1 = it->Message;
  678. sdate1 = it->Date;
  679. }
  680. else if ( it->Revision == minrev )
  681. {
  682. cmOStringStream mRevStream;
  683. mRevStream << minrev;
  684. srevision2 = mRevStream.str();
  685. }
  686. }
  687. }
  688. }
  689. }
  690. if ( mod == 'M' )
  691. {
  692. comment1 = "Locally modified file\n";
  693. sauthor1 = "Local User";
  694. }
  695. if ( mod == 'D' )
  696. {
  697. comment1 += " - Removed file\n";
  698. }
  699. if ( mod == 'C' )
  700. {
  701. comment1 = "Conflict while updating\n";
  702. sauthor1 = "Local User";
  703. }
  704. std::string path = cmSystemTools::GetFilenamePath(file);
  705. std::string fname = cmSystemTools::GetFilenameName(file);
  706. if ( path != current_path )
  707. {
  708. if ( !first_file )
  709. {
  710. os << "\t</Directory>" << std::endl;
  711. }
  712. else
  713. {
  714. first_file = false;
  715. }
  716. os << "\t<Directory>\n"
  717. << "\t\t<Name>" << path << "</Name>" << std::endl;
  718. }
  719. if ( mod == 'C' )
  720. {
  721. numConflicting ++;
  722. os << "\t<Conflicting>" << std::endl;
  723. }
  724. else if ( mod == 'G' )
  725. {
  726. numConflicting ++;
  727. os << "\t<Conflicting>" << std::endl;
  728. }
  729. else if ( mod == 'M' )
  730. {
  731. numModified ++;
  732. os << "\t<Modified>" << std::endl;
  733. }
  734. else
  735. {
  736. numUpdated ++;
  737. os << "\t<Updated>" << std::endl;
  738. }
  739. if ( srevision2 == "Unknown" )
  740. {
  741. srevision2 = srevision1;
  742. }
  743. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "File: "
  744. << path.c_str() << " / " << fname.c_str() << " was updated by "
  745. << sauthor1.c_str() << " to revision: " << srevision1.c_str()
  746. << " from revision: " << srevision2.c_str() << std::endl);
  747. os << "\t\t<File>"
  748. << cmXMLSafe(fname)
  749. << "</File>\n"
  750. << "\t\t<Directory>" << cmXMLSafe(path)
  751. << "</Directory>\n"
  752. << "\t\t<FullName>" << cmXMLSafe(file) << "</FullName>\n"
  753. << "\t\t<CheckinDate>" << cmXMLSafe(sdate1)
  754. << "</CheckinDate>\n"
  755. << "\t\t<Author>" << cmXMLSafe(sauthor1) << "</Author>\n"
  756. << "\t\t<Email>" << cmXMLSafe(semail1) << "</Email>\n"
  757. << "\t\t<Log>" << cmXMLSafe(comment1) << "</Log>\n"
  758. << "\t\t<Revision>" << srevision1 << "</Revision>\n"
  759. << "\t\t<PriorRevision>" << srevision2 << "</PriorRevision>"
  760. << std::endl;
  761. if ( mod == 'C' )
  762. {
  763. os << "\t</Conflicting>" << std::endl;
  764. }
  765. else if ( mod == 'G' )
  766. {
  767. os << "\t</Conflicting>" << std::endl;
  768. }
  769. else if ( mod == 'M' )
  770. {
  771. os << "\t</Modified>" << std::endl;
  772. }
  773. else
  774. {
  775. os << "\t</Updated>" << std::endl;
  776. }
  777. author_set.insert(sauthor1);
  778. current_path = path;
  779. }
  780. file_count ++;
  781. }
  782. }
  783. if ( file_count )
  784. {
  785. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  786. }
  787. if ( numUpdated )
  788. {
  789. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Found " << numUpdated
  790. << " updated files" << std::endl);
  791. }
  792. if ( numModified )
  793. {
  794. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Found " << numModified
  795. << " locally modified files"
  796. << std::endl);
  797. }
  798. if ( numConflicting )
  799. {
  800. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Found " << numConflicting
  801. << " conflicting files"
  802. << std::endl);
  803. }
  804. if ( numModified == 0 && numConflicting == 0 && numUpdated == 0 )
  805. {
  806. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Project is up-to-date"
  807. << std::endl);
  808. }
  809. if ( !first_file )
  810. {
  811. os << "\t</Directory>" << std::endl;
  812. }
  813. // TODO: Skip the author list when submitting to CDash.
  814. for(std::set<cmStdString>::const_iterator ai = author_set.begin();
  815. ai != author_set.end(); ++ai)
  816. {
  817. os << "\t<Author><Name>" << cmXMLSafe(*ai) << "</Name></Author>\n";
  818. }
  819. cmCTestLog(this->CTest, DEBUG, "End" << std::endl);
  820. std::string end_time = this->CTest->CurrentTime();
  821. os << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  822. << "\t<EndTime>" << static_cast<unsigned int>(cmSystemTools::GetTime())
  823. << "</EndTime>\n"
  824. << "<ElapsedMinutes>" <<
  825. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  826. << "</ElapsedMinutes>\n"
  827. << "\t<UpdateReturnStatus>";
  828. if ( numModified > 0 || numConflicting > 0 )
  829. {
  830. os << "Update error: There are modified or conflicting files in the "
  831. "repository";
  832. cmCTestLog(this->CTest, ERROR_MESSAGE,
  833. " There are modified or conflicting files in the repository"
  834. << std::endl);
  835. }
  836. if ( updateProducedError )
  837. {
  838. os << "Update error: ";
  839. cmCTestLog(this->CTest, ERROR_MESSAGE, " Update with command: "
  840. << command << " failed" << std::endl);
  841. }
  842. os << "</UpdateReturnStatus>" << std::endl;
  843. os << "</Update>" << std::endl;
  844. if (! res )
  845. {
  846. cmCTestLog(this->CTest, ERROR_MESSAGE,
  847. "Error(s) when updating the project" << std::endl);
  848. cmCTestLog(this->CTest, ERROR_MESSAGE, "Output: "
  849. << goutput << std::endl);
  850. return -1;
  851. }
  852. return count;
  853. }
  854. //----------------------------------------------------------------------
  855. bool cmCTestUpdateHandler::InitialCheckout(std::ostream& ofs)
  856. {
  857. const char* sourceDirectory = this->GetOption("SourceDirectory");
  858. // Use the user-provided command to create the source tree.
  859. const char* initialCheckoutCommand = this->GetOption("InitialCheckout");
  860. if ( initialCheckoutCommand )
  861. {
  862. std::string goutput;
  863. std::string errors;
  864. int retVal = 0;
  865. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  866. " First perform the initial checkout: " << initialCheckoutCommand
  867. << std::endl);
  868. cmStdString parent = cmSystemTools::GetParentDirectory(sourceDirectory);
  869. if ( parent.empty() )
  870. {
  871. cmCTestLog(this->CTest, ERROR_MESSAGE,
  872. "Something went wrong when trying "
  873. "to determine the parent directory of " << sourceDirectory
  874. << std::endl);
  875. return false;
  876. }
  877. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  878. " Perform checkout in directory: " << parent.c_str() << std::endl);
  879. if ( !cmSystemTools::MakeDirectory(parent.c_str()) )
  880. {
  881. cmCTestLog(this->CTest, ERROR_MESSAGE,
  882. "Cannot create parent directory: " << parent.c_str()
  883. << " of the source directory: " << sourceDirectory << std::endl);
  884. return false;
  885. }
  886. ofs << "* Run initial checkout" << std::endl;
  887. ofs << " Command: " << initialCheckoutCommand << std::endl;
  888. cmCTestLog(this->CTest, DEBUG, " Before: "
  889. << initialCheckoutCommand << std::endl);
  890. bool retic = this->CTest->RunCommand(initialCheckoutCommand, &goutput,
  891. &errors, &retVal, parent.c_str(), 0 /* Timeout */);
  892. cmCTestLog(this->CTest, DEBUG, " After: "
  893. << initialCheckoutCommand << std::endl);
  894. ofs << " Output: " << goutput.c_str() << std::endl;
  895. ofs << " Errors: " << errors.c_str() << std::endl;
  896. if ( !retic || retVal )
  897. {
  898. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initial checkout failed:\n"
  899. << goutput << "\n" << errors << "\n");
  900. }
  901. if(!this->CTest->InitializeFromCommand(this->Command))
  902. {
  903. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  904. " Fatal Error in initialize: "
  905. << std::endl);
  906. cmSystemTools::SetFatalErrorOccured();
  907. return false;
  908. }
  909. }
  910. return true;
  911. }
  912. //----------------------------------------------------------------------
  913. int cmCTestUpdateHandler::DetectVCS(const char* dir)
  914. {
  915. std::string sourceDirectory = dir;
  916. cmCTestLog(this->CTest, DEBUG, "Check directory: "
  917. << sourceDirectory.c_str() << std::endl);
  918. sourceDirectory += "/.svn";
  919. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  920. {
  921. return cmCTestUpdateHandler::e_SVN;
  922. }
  923. sourceDirectory = dir;
  924. sourceDirectory += "/CVS";
  925. if ( cmSystemTools::FileExists(sourceDirectory.c_str()) )
  926. {
  927. return cmCTestUpdateHandler::e_CVS;
  928. }
  929. return cmCTestUpdateHandler::e_UNKNOWN;
  930. }
  931. //----------------------------------------------------------------------
  932. bool cmCTestUpdateHandler::SelectVCS()
  933. {
  934. // Get update command
  935. this->UpdateCommand = this->CTest->GetCTestConfiguration("UpdateCommand");
  936. // Detect the VCS managing the source tree.
  937. this->UpdateType = this->DetectVCS(this->GetOption("SourceDirectory"));
  938. if (this->UpdateType == e_UNKNOWN)
  939. {
  940. // The source tree does not have a recognized VCS. Check the
  941. // configuration value or command name.
  942. this->UpdateType = this->DetermineType(this->UpdateCommand.c_str(),
  943. this->CTest->GetCTestConfiguration("UpdateType").c_str());
  944. }
  945. // If no update command was specified, lookup one for this VCS tool.
  946. if (this->UpdateCommand.empty())
  947. {
  948. const char* key = 0;
  949. switch (this->UpdateType)
  950. {
  951. case e_CVS: key = "CVSCommand"; break;
  952. case e_SVN: key = "SVNCommand"; break;
  953. default: break;
  954. }
  955. if (key)
  956. {
  957. this->UpdateCommand = this->CTest->GetCTestConfiguration(key);
  958. }
  959. if (this->UpdateCommand.empty())
  960. {
  961. cmOStringStream e;
  962. e << "Cannot find UpdateCommand ";
  963. if (key)
  964. {
  965. e << "or " << key;
  966. }
  967. e << " configuration key.";
  968. cmCTestLog(this->CTest, ERROR_MESSAGE, e.str() << std::endl);
  969. return false;
  970. }
  971. }
  972. return true;
  973. }