cmCTestBZR.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. 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 "cmCTestBZR.h"
  14. #include "cmCTest.h"
  15. #include "cmSystemTools.h"
  16. #include "cmXMLParser.h"
  17. #include "cmXMLSafe.h"
  18. #include <cmsys/RegularExpression.hxx>
  19. #include <cm_expat.h>
  20. //----------------------------------------------------------------------------
  21. extern "C"
  22. int cmBZRXMLParserUnknownEncodingHandler(void*,
  23. const XML_Char *name,
  24. XML_Encoding *info)
  25. {
  26. static const int latin1[]=
  27. {
  28. 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
  29. 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
  30. 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
  31. 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F,
  32. 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
  33. 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
  34. 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
  35. 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
  36. 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
  37. 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
  38. 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
  39. 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
  40. 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
  41. 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
  42. 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
  43. 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F,
  44. 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
  45. 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F,
  46. 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
  47. 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178,
  48. 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
  49. 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
  50. 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
  51. 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF,
  52. 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7,
  53. 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
  54. 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7,
  55. 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
  56. 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
  57. 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
  58. 0x00F0, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7,
  59. 0x00F8, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FE, 0x00FF
  60. };
  61. // The BZR xml output plugin can use some encodings that are not
  62. // recognized by expat. This will lead to an error, e.g. "Error
  63. // parsing bzr log xml: unknown encoding", the following is a
  64. // workaround for these unknown encodings.
  65. if(name == std::string("ascii") || name == std::string("cp1252") ||
  66. name == std::string("ANSI_X3.4-1968"))
  67. {
  68. for(unsigned int i=0;i<256;++i) info->map[i] = latin1[i];
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. //----------------------------------------------------------------------------
  74. cmCTestBZR::cmCTestBZR(cmCTest* ct, std::ostream& log):
  75. cmCTestGlobalVC(ct, log)
  76. {
  77. this->PriorRev = this->Unknown;
  78. // Even though it is specified in the documention, with bzr 1.13
  79. // BZR_PROGRESS_BAR has no effect. In the future this bug might be fixed.
  80. // Since it doesn't hurt, we specify this environment variable.
  81. cmSystemTools::PutEnv("BZR_PROGRESS_BAR=none");
  82. }
  83. //----------------------------------------------------------------------------
  84. cmCTestBZR::~cmCTestBZR()
  85. {
  86. }
  87. //----------------------------------------------------------------------------
  88. class cmCTestBZR::InfoParser: public cmCTestVC::LineParser
  89. {
  90. public:
  91. InfoParser(cmCTestBZR* bzr, const char* prefix):
  92. BZR(bzr), CheckOutFound(false)
  93. {
  94. this->SetLog(&bzr->Log, prefix);
  95. this->RegexCheckOut.compile("checkout of branch: *([^\t\r\n]+)$");
  96. this->RegexParent.compile("parent branch: *([^\t\r\n]+)$");
  97. }
  98. private:
  99. cmCTestBZR* BZR;
  100. bool CheckOutFound;
  101. cmsys::RegularExpression RegexCheckOut;
  102. cmsys::RegularExpression RegexParent;
  103. virtual bool ProcessLine()
  104. {
  105. if(this->RegexCheckOut.find(this->Line))
  106. {
  107. this->BZR->URL = this->RegexCheckOut.match(1);
  108. CheckOutFound = true;
  109. }
  110. else if(!CheckOutFound && this->RegexParent.find(this->Line))
  111. {
  112. this->BZR->URL = this->RegexParent.match(1);
  113. }
  114. return true;
  115. }
  116. };
  117. //----------------------------------------------------------------------------
  118. class cmCTestBZR::RevnoParser: public cmCTestVC::LineParser
  119. {
  120. public:
  121. RevnoParser(cmCTestBZR* bzr, const char* prefix, std::string& rev):
  122. BZR(bzr), Rev(rev)
  123. {
  124. this->SetLog(&bzr->Log, prefix);
  125. this->RegexRevno.compile("^([0-9]+)$");
  126. }
  127. private:
  128. cmCTestBZR* BZR;
  129. std::string& Rev;
  130. cmsys::RegularExpression RegexRevno;
  131. virtual bool ProcessLine()
  132. {
  133. if(this->RegexRevno.find(this->Line))
  134. {
  135. this->Rev = this->RegexRevno.match(1);
  136. }
  137. return true;
  138. }
  139. };
  140. //----------------------------------------------------------------------------
  141. std::string cmCTestBZR::LoadInfo()
  142. {
  143. // Run "bzr info" to get the repository info from the work tree.
  144. const char* bzr = this->CommandLineTool.c_str();
  145. const char* bzr_info[] = {bzr, "info", 0};
  146. InfoParser iout(this, "info-out> ");
  147. OutputLogger ierr(this->Log, "info-err> ");
  148. this->RunChild(bzr_info, &iout, &ierr);
  149. // Run "bzr revno" to get the repository revision number from the work tree.
  150. const char* bzr_revno[] = {bzr, "revno", 0};
  151. std::string rev;
  152. RevnoParser rout(this, "revno-out> ", rev);
  153. OutputLogger rerr(this->Log, "revno-err> ");
  154. this->RunChild(bzr_revno, &rout, &rerr);
  155. return rev;
  156. }
  157. void cmCTestBZR::NoteOldRevision()
  158. {
  159. this->OldRevision = this->LoadInfo();
  160. this->Log << "Revision before update: " << this->OldRevision << "\n";
  161. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Old revision of repository is: "
  162. << this->OldRevision << "\n");
  163. this->PriorRev.Rev = this->OldRevision;
  164. }
  165. //----------------------------------------------------------------------------
  166. void cmCTestBZR::NoteNewRevision()
  167. {
  168. this->NewRevision = this->LoadInfo();
  169. this->Log << "Revision after update: " << this->NewRevision << "\n";
  170. cmCTestLog(this->CTest, HANDLER_OUTPUT, " New revision of repository is: "
  171. << this->NewRevision << "\n");
  172. this->Log << "URL = " << this->URL << "\n";
  173. }
  174. //----------------------------------------------------------------------------
  175. class cmCTestBZR::LogParser: public cmCTestVC::OutputLogger,
  176. private cmXMLParser
  177. {
  178. public:
  179. LogParser(cmCTestBZR* bzr, const char* prefix):
  180. OutputLogger(bzr->Log, prefix), BZR(bzr),
  181. EmailRegex("(.*) <([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+)>")
  182. { this->InitializeParser(); }
  183. ~LogParser() { this->CleanupParser(); }
  184. virtual int InitializeParser()
  185. {
  186. int res = cmXMLParser::InitializeParser();
  187. if (res)
  188. {
  189. XML_SetUnknownEncodingHandler(static_cast<XML_Parser>(this->Parser),
  190. cmBZRXMLParserUnknownEncodingHandler, 0);
  191. }
  192. return res;
  193. }
  194. private:
  195. cmCTestBZR* BZR;
  196. typedef cmCTestBZR::Revision Revision;
  197. typedef cmCTestBZR::Change Change;
  198. Revision Rev;
  199. std::vector<Change> Changes;
  200. Change CurChange;
  201. std::vector<char> CData;
  202. cmsys::RegularExpression EmailRegex;
  203. virtual bool ProcessChunk(const char* data, int length)
  204. {
  205. this->OutputLogger::ProcessChunk(data, length);
  206. this->ParseChunk(data, length);
  207. return true;
  208. }
  209. virtual void StartElement(const char* name, const char**)
  210. {
  211. this->CData.clear();
  212. if(strcmp(name, "log") == 0)
  213. {
  214. this->Rev = Revision();
  215. this->Changes.clear();
  216. }
  217. // affected-files can contain blocks of
  218. // modified, unknown, renamed, kind-changed, removed, conflicts, added
  219. else if(strcmp(name, "modified") == 0
  220. || strcmp(name, "renamed") == 0
  221. || strcmp(name, "kind-changed") == 0)
  222. {
  223. this->CurChange = Change();
  224. this->CurChange.Action = 'M';
  225. }
  226. else if(strcmp(name, "added") == 0)
  227. {
  228. this->CurChange = Change();
  229. this->CurChange = 'A';
  230. }
  231. else if(strcmp(name, "removed") == 0)
  232. {
  233. this->CurChange = Change();
  234. this->CurChange = 'D';
  235. }
  236. else if(strcmp(name, "unknown") == 0
  237. || strcmp(name, "conflicts") == 0)
  238. {
  239. // Should not happen here
  240. this->CurChange = Change();
  241. }
  242. }
  243. virtual void CharacterDataHandler(const char* data, int length)
  244. {
  245. this->CData.insert(this->CData.end(), data, data+length);
  246. }
  247. virtual void EndElement(const char* name)
  248. {
  249. if(strcmp(name, "log") == 0)
  250. {
  251. this->BZR->DoRevision(this->Rev, this->Changes);
  252. }
  253. else if((strcmp(name, "file") == 0 || strcmp(name, "directory") == 0)
  254. && !this->CData.empty())
  255. {
  256. this->CurChange.Path.assign(&this->CData[0], this->CData.size());
  257. this->Changes.push_back(this->CurChange);
  258. }
  259. else if(strcmp(name, "symlink") == 0 && !this->CData.empty())
  260. {
  261. // symlinks have an arobase at the end in the log
  262. this->CurChange.Path.assign(&this->CData[0], this->CData.size()-1);
  263. this->Changes.push_back(this->CurChange);
  264. }
  265. else if(strcmp(name, "committer") == 0 && !this->CData.empty())
  266. {
  267. this->Rev.Author.assign(&this->CData[0], this->CData.size());
  268. if(this->EmailRegex.find(this->Rev.Author))
  269. {
  270. this->Rev.Author = this->EmailRegex.match(1);
  271. //email = email_regex.match(2);
  272. }
  273. }
  274. else if(strcmp(name, "timestamp") == 0 && !this->CData.empty())
  275. {
  276. this->Rev.Date.assign(&this->CData[0], this->CData.size());
  277. }
  278. else if(strcmp(name, "message") == 0 && !this->CData.empty())
  279. {
  280. this->Rev.Log.assign(&this->CData[0], this->CData.size());
  281. }
  282. else if(strcmp(name, "revno") == 0 && !this->CData.empty())
  283. {
  284. this->Rev.Rev.assign(&this->CData[0], this->CData.size());
  285. }
  286. this->CData.clear();
  287. }
  288. virtual void ReportError(int, int, const char* msg)
  289. {
  290. this->BZR->Log << "Error parsing bzr log xml: " << msg << "\n";
  291. }
  292. };
  293. //----------------------------------------------------------------------------
  294. class cmCTestBZR::UpdateParser: public cmCTestVC::LineParser
  295. {
  296. public:
  297. UpdateParser(cmCTestBZR* bzr, const char* prefix): BZR(bzr)
  298. {
  299. this->SetLog(&bzr->Log, prefix);
  300. this->RegexUpdate.compile("^([-+R?XCP ])([NDKM ])([* ]) +(.+)$");
  301. }
  302. private:
  303. cmCTestBZR* BZR;
  304. cmsys::RegularExpression RegexUpdate;
  305. virtual bool ProcessChunk(const char* first, int length)
  306. {
  307. bool last_is_new_line = (*first == '\r' || *first == '\n');
  308. const char* const last = first + length;
  309. for(const char* c = first; c != last; ++c)
  310. {
  311. if(*c == '\r' || *c == '\n')
  312. {
  313. if(!last_is_new_line)
  314. {
  315. // Log this line.
  316. if(this->Log && this->Prefix)
  317. {
  318. *this->Log << this->Prefix << this->Line << "\n";
  319. }
  320. // Hand this line to the subclass implementation.
  321. if(!this->ProcessLine())
  322. {
  323. this->Line = "";
  324. return false;
  325. }
  326. this->Line = "";
  327. last_is_new_line = true;
  328. }
  329. }
  330. else
  331. {
  332. // Append this character to the line under construction.
  333. this->Line.append(1, *c);
  334. last_is_new_line = false;
  335. }
  336. }
  337. return true;
  338. }
  339. bool ProcessLine()
  340. {
  341. if(this->RegexUpdate.find(this->Line))
  342. {
  343. this->DoPath(this->RegexUpdate.match(1)[0],
  344. this->RegexUpdate.match(2)[0],
  345. this->RegexUpdate.match(3)[0],
  346. this->RegexUpdate.match(4));
  347. }
  348. return true;
  349. }
  350. void DoPath(char c0, char c1, char c2, std::string const& path)
  351. {
  352. if(path.empty()) return;
  353. const std::string dir = cmSystemTools::GetFilenamePath(path);
  354. const std::string name = cmSystemTools::GetFilenameName(path);
  355. if ( c0=='C' )
  356. {
  357. this->BZR->Dirs[dir][name].Status = PathConflicting;
  358. return;
  359. }
  360. if ( c1=='M' || c1=='K' || c1=='N' || c1=='D' || c2 =='*' )
  361. {
  362. this->BZR->Dirs[dir][name].Status = PathUpdated;
  363. return;
  364. }
  365. }
  366. };
  367. //----------------------------------------------------------------------------
  368. bool cmCTestBZR::UpdateImpl()
  369. {
  370. // Get user-specified update options.
  371. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  372. if(opts.empty())
  373. {
  374. opts = this->CTest->GetCTestConfiguration("BZRUpdateOptions");
  375. }
  376. std::vector<cmStdString> args = cmSystemTools::ParseArguments(opts.c_str());
  377. // TODO: if(this->CTest->GetTestModel() == cmCTest::NIGHTLY)
  378. // Use "bzr pull" to update the working tree.
  379. std::vector<char const*> bzr_update;
  380. bzr_update.push_back(this->CommandLineTool.c_str());
  381. bzr_update.push_back("pull");
  382. for(std::vector<cmStdString>::const_iterator ai = args.begin();
  383. ai != args.end(); ++ai)
  384. {
  385. bzr_update.push_back(ai->c_str());
  386. }
  387. bzr_update.push_back(this->URL.c_str());
  388. bzr_update.push_back(0);
  389. // For some reason bzr uses stderr to display the update status.
  390. OutputLogger out(this->Log, "pull-out> ");
  391. UpdateParser err(this, "pull-err> ");
  392. return this->RunUpdateCommand(&bzr_update[0], &out, &err);
  393. }
  394. //----------------------------------------------------------------------------
  395. void cmCTestBZR::LoadRevisions()
  396. {
  397. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  398. " Gathering version information (one . per revision):\n"
  399. " " << std::flush);
  400. // We are interested in every revision included in the update.
  401. this->Revisions.clear();
  402. std::string revs;
  403. if(atoi(this->OldRevision.c_str()) <= atoi(this->NewRevision.c_str()))
  404. {
  405. // DoRevision takes care of discarding the information about OldRevision
  406. revs = this->OldRevision + ".." + this->NewRevision;
  407. }
  408. else
  409. {
  410. return;
  411. }
  412. // Run "bzr log" to get all global revisions of interest.
  413. const char* bzr = this->CommandLineTool.c_str();
  414. const char* bzr_log[] = {bzr, "log", "-v", "-r", revs.c_str(), "--xml",
  415. this->URL.c_str(), 0};
  416. {
  417. LogParser out(this, "log-out> ");
  418. OutputLogger err(this->Log, "log-err> ");
  419. this->RunChild(bzr_log, &out, &err);
  420. }
  421. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  422. }
  423. //----------------------------------------------------------------------------
  424. class cmCTestBZR::StatusParser: public cmCTestVC::LineParser
  425. {
  426. public:
  427. StatusParser(cmCTestBZR* bzr, const char* prefix): BZR(bzr)
  428. {
  429. this->SetLog(&bzr->Log, prefix);
  430. this->RegexStatus.compile("^([-+R?XCP ])([NDKM ])([* ]) +(.+)$");
  431. }
  432. private:
  433. cmCTestBZR* BZR;
  434. cmsys::RegularExpression RegexStatus;
  435. bool ProcessLine()
  436. {
  437. if(this->RegexStatus.find(this->Line))
  438. {
  439. this->DoPath(this->RegexStatus.match(1)[0],
  440. this->RegexStatus.match(2)[0],
  441. this->RegexStatus.match(3)[0],
  442. this->RegexStatus.match(4));
  443. }
  444. return true;
  445. }
  446. void DoPath(char c0, char c1, char c2, std::string const& path)
  447. {
  448. if(path.empty()) return;
  449. if ( c0=='C' )
  450. {
  451. this->BZR->DoModification(PathConflicting, path);
  452. return;
  453. }
  454. if ( c0 == '+' || c0 == 'R' || c0 == 'P'
  455. || c1=='M' || c1=='K' || c1=='N' || c1=='D'
  456. || c2 =='*' )
  457. {
  458. this->BZR->DoModification(PathModified, path);
  459. return;
  460. }
  461. }
  462. };
  463. //----------------------------------------------------------------------------
  464. void cmCTestBZR::LoadModifications()
  465. {
  466. // Run "bzr status" which reports local modifications.
  467. const char* bzr = this->CommandLineTool.c_str();
  468. const char* bzr_status[] = {bzr, "status", "-SV", 0};
  469. StatusParser out(this, "status-out> ");
  470. OutputLogger err(this->Log, "status-err> ");
  471. this->RunChild(bzr_status, &out, &err);
  472. }