cmCTestBZR.cxx 16 KB

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