cmCTestBZR.cxx 16 KB

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