cmCTestBZR.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. Rev(rev)
  120. {
  121. this->SetLog(&bzr->Log, prefix);
  122. this->RegexRevno.compile("^([0-9]+)$");
  123. }
  124. private:
  125. std::string& Rev;
  126. cmsys::RegularExpression RegexRevno;
  127. virtual bool ProcessLine()
  128. {
  129. if(this->RegexRevno.find(this->Line))
  130. {
  131. this->Rev = this->RegexRevno.match(1);
  132. }
  133. return true;
  134. }
  135. };
  136. //----------------------------------------------------------------------------
  137. std::string cmCTestBZR::LoadInfo()
  138. {
  139. // Run "bzr info" to get the repository info from the work tree.
  140. const char* bzr = this->CommandLineTool.c_str();
  141. const char* bzr_info[] = {bzr, "info", 0};
  142. InfoParser iout(this, "info-out> ");
  143. OutputLogger ierr(this->Log, "info-err> ");
  144. this->RunChild(bzr_info, &iout, &ierr);
  145. // Run "bzr revno" to get the repository revision number from the work tree.
  146. const char* bzr_revno[] = {bzr, "revno", 0};
  147. std::string rev;
  148. RevnoParser rout(this, "revno-out> ", rev);
  149. OutputLogger rerr(this->Log, "revno-err> ");
  150. this->RunChild(bzr_revno, &rout, &rerr);
  151. return rev;
  152. }
  153. void cmCTestBZR::NoteOldRevision()
  154. {
  155. this->OldRevision = this->LoadInfo();
  156. this->Log << "Revision before update: " << this->OldRevision << "\n";
  157. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Old revision of repository is: "
  158. << this->OldRevision << "\n");
  159. this->PriorRev.Rev = this->OldRevision;
  160. }
  161. //----------------------------------------------------------------------------
  162. void cmCTestBZR::NoteNewRevision()
  163. {
  164. this->NewRevision = this->LoadInfo();
  165. this->Log << "Revision after update: " << this->NewRevision << "\n";
  166. cmCTestLog(this->CTest, HANDLER_OUTPUT, " New revision of repository is: "
  167. << this->NewRevision << "\n");
  168. this->Log << "URL = " << this->URL << "\n";
  169. }
  170. //----------------------------------------------------------------------------
  171. class cmCTestBZR::LogParser: public cmCTestVC::OutputLogger,
  172. private cmXMLParser
  173. {
  174. public:
  175. LogParser(cmCTestBZR* bzr, const char* prefix):
  176. OutputLogger(bzr->Log, prefix), BZR(bzr),
  177. EmailRegex("(.*) <([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+)>")
  178. { this->InitializeParser(); }
  179. ~LogParser() { this->CleanupParser(); }
  180. virtual int InitializeParser()
  181. {
  182. int res = cmXMLParser::InitializeParser();
  183. if (res)
  184. {
  185. XML_SetUnknownEncodingHandler(static_cast<XML_Parser>(this->Parser),
  186. cmBZRXMLParserUnknownEncodingHandler, 0);
  187. }
  188. return res;
  189. }
  190. private:
  191. cmCTestBZR* BZR;
  192. typedef cmCTestBZR::Revision Revision;
  193. typedef cmCTestBZR::Change Change;
  194. Revision Rev;
  195. std::vector<Change> Changes;
  196. Change CurChange;
  197. std::vector<char> CData;
  198. cmsys::RegularExpression EmailRegex;
  199. virtual bool ProcessChunk(const char* data, int length)
  200. {
  201. this->OutputLogger::ProcessChunk(data, length);
  202. this->ParseChunk(data, length);
  203. return true;
  204. }
  205. virtual void StartElement(const char* name, const char**)
  206. {
  207. this->CData.clear();
  208. if(strcmp(name, "log") == 0)
  209. {
  210. this->Rev = Revision();
  211. this->Changes.clear();
  212. }
  213. // affected-files can contain blocks of
  214. // modified, unknown, renamed, kind-changed, removed, conflicts, added
  215. else if(strcmp(name, "modified") == 0
  216. || strcmp(name, "renamed") == 0
  217. || strcmp(name, "kind-changed") == 0)
  218. {
  219. this->CurChange = Change();
  220. this->CurChange.Action = 'M';
  221. }
  222. else if(strcmp(name, "added") == 0)
  223. {
  224. this->CurChange = Change();
  225. this->CurChange = 'A';
  226. }
  227. else if(strcmp(name, "removed") == 0)
  228. {
  229. this->CurChange = Change();
  230. this->CurChange = 'D';
  231. }
  232. else if(strcmp(name, "unknown") == 0
  233. || strcmp(name, "conflicts") == 0)
  234. {
  235. // Should not happen here
  236. this->CurChange = Change();
  237. }
  238. }
  239. virtual void CharacterDataHandler(const char* data, int length)
  240. {
  241. this->CData.insert(this->CData.end(), data, data+length);
  242. }
  243. virtual void EndElement(const char* name)
  244. {
  245. if(strcmp(name, "log") == 0)
  246. {
  247. this->BZR->DoRevision(this->Rev, this->Changes);
  248. }
  249. else if((strcmp(name, "file") == 0 || strcmp(name, "directory") == 0)
  250. && !this->CData.empty())
  251. {
  252. this->CurChange.Path.assign(&this->CData[0], this->CData.size());
  253. cmSystemTools::ConvertToUnixSlashes(this->CurChange.Path);
  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. cmSystemTools::ConvertToUnixSlashes(this->CurChange.Path);
  261. this->Changes.push_back(this->CurChange);
  262. }
  263. else if(strcmp(name, "committer") == 0 && !this->CData.empty())
  264. {
  265. this->Rev.Author.assign(&this->CData[0], this->CData.size());
  266. if(this->EmailRegex.find(this->Rev.Author))
  267. {
  268. this->Rev.Author = this->EmailRegex.match(1);
  269. this->Rev.EMail = this->EmailRegex.match(2);
  270. }
  271. }
  272. else if(strcmp(name, "timestamp") == 0 && !this->CData.empty())
  273. {
  274. this->Rev.Date.assign(&this->CData[0], this->CData.size());
  275. }
  276. else if(strcmp(name, "message") == 0 && !this->CData.empty())
  277. {
  278. this->Rev.Log.assign(&this->CData[0], this->CData.size());
  279. }
  280. else if(strcmp(name, "revno") == 0 && !this->CData.empty())
  281. {
  282. this->Rev.Rev.assign(&this->CData[0], this->CData.size());
  283. }
  284. this->CData.clear();
  285. }
  286. virtual void ReportError(int, int, const char* msg)
  287. {
  288. this->BZR->Log << "Error parsing bzr log xml: " << msg << "\n";
  289. }
  290. };
  291. //----------------------------------------------------------------------------
  292. class cmCTestBZR::UpdateParser: public cmCTestVC::LineParser
  293. {
  294. public:
  295. UpdateParser(cmCTestBZR* bzr, const char* prefix): BZR(bzr)
  296. {
  297. this->SetLog(&bzr->Log, prefix);
  298. this->RegexUpdate.compile("^([-+R?XCP ])([NDKM ])([* ]) +(.+)$");
  299. }
  300. private:
  301. cmCTestBZR* BZR;
  302. cmsys::RegularExpression RegexUpdate;
  303. virtual bool ProcessChunk(const char* first, int length)
  304. {
  305. bool last_is_new_line = (*first == '\r' || *first == '\n');
  306. const char* const last = first + length;
  307. for(const char* c = first; c != last; ++c)
  308. {
  309. if(*c == '\r' || *c == '\n')
  310. {
  311. if(!last_is_new_line)
  312. {
  313. // Log this line.
  314. if(this->Log && this->Prefix)
  315. {
  316. *this->Log << this->Prefix << this->Line << "\n";
  317. }
  318. // Hand this line to the subclass implementation.
  319. if(!this->ProcessLine())
  320. {
  321. this->Line = "";
  322. return false;
  323. }
  324. this->Line = "";
  325. last_is_new_line = true;
  326. }
  327. }
  328. else
  329. {
  330. // Append this character to the line under construction.
  331. this->Line.append(1, *c);
  332. last_is_new_line = false;
  333. }
  334. }
  335. return true;
  336. }
  337. bool ProcessLine()
  338. {
  339. if(this->RegexUpdate.find(this->Line))
  340. {
  341. this->DoPath(this->RegexUpdate.match(1)[0],
  342. this->RegexUpdate.match(2)[0],
  343. this->RegexUpdate.match(3)[0],
  344. this->RegexUpdate.match(4));
  345. }
  346. return true;
  347. }
  348. void DoPath(char c0, char c1, char c2, std::string path)
  349. {
  350. if(path.empty()) return;
  351. cmSystemTools::ConvertToUnixSlashes(path);
  352. const std::string dir = cmSystemTools::GetFilenamePath(path);
  353. const std::string name = cmSystemTools::GetFilenameName(path);
  354. if ( c0=='C' )
  355. {
  356. this->BZR->Dirs[dir][name].Status = PathConflicting;
  357. return;
  358. }
  359. if ( c1=='M' || c1=='K' || c1=='N' || c1=='D' || c2 =='*' )
  360. {
  361. this->BZR->Dirs[dir][name].Status = PathUpdated;
  362. return;
  363. }
  364. }
  365. };
  366. //----------------------------------------------------------------------------
  367. bool cmCTestBZR::UpdateImpl()
  368. {
  369. // Get user-specified update options.
  370. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  371. if(opts.empty())
  372. {
  373. opts = this->CTest->GetCTestConfiguration("BZRUpdateOptions");
  374. }
  375. std::vector<cmStdString> args = cmSystemTools::ParseArguments(opts.c_str());
  376. // TODO: if(this->CTest->GetTestModel() == cmCTest::NIGHTLY)
  377. // Use "bzr pull" to update the working tree.
  378. std::vector<char const*> bzr_update;
  379. bzr_update.push_back(this->CommandLineTool.c_str());
  380. bzr_update.push_back("pull");
  381. for(std::vector<cmStdString>::const_iterator ai = args.begin();
  382. ai != args.end(); ++ai)
  383. {
  384. bzr_update.push_back(ai->c_str());
  385. }
  386. bzr_update.push_back(this->URL.c_str());
  387. bzr_update.push_back(0);
  388. // For some reason bzr uses stderr to display the update status.
  389. OutputLogger out(this->Log, "pull-out> ");
  390. UpdateParser err(this, "pull-err> ");
  391. return this->RunUpdateCommand(&bzr_update[0], &out, &err);
  392. }
  393. //----------------------------------------------------------------------------
  394. void cmCTestBZR::LoadRevisions()
  395. {
  396. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  397. " Gathering version information (one . per revision):\n"
  398. " " << std::flush);
  399. // We are interested in every revision included in the update.
  400. this->Revisions.clear();
  401. std::string revs;
  402. if(atoi(this->OldRevision.c_str()) <= atoi(this->NewRevision.c_str()))
  403. {
  404. // DoRevision takes care of discarding the information about OldRevision
  405. revs = this->OldRevision + ".." + this->NewRevision;
  406. }
  407. else
  408. {
  409. return;
  410. }
  411. // Run "bzr log" to get all global revisions of interest.
  412. const char* bzr = this->CommandLineTool.c_str();
  413. const char* bzr_log[] = {bzr, "log", "-v", "-r", revs.c_str(), "--xml",
  414. this->URL.c_str(), 0};
  415. {
  416. LogParser out(this, "log-out> ");
  417. OutputLogger err(this->Log, "log-err> ");
  418. this->RunChild(bzr_log, &out, &err);
  419. }
  420. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  421. }
  422. //----------------------------------------------------------------------------
  423. class cmCTestBZR::StatusParser: public cmCTestVC::LineParser
  424. {
  425. public:
  426. StatusParser(cmCTestBZR* bzr, const char* prefix): BZR(bzr)
  427. {
  428. this->SetLog(&bzr->Log, prefix);
  429. this->RegexStatus.compile("^([-+R?XCP ])([NDKM ])([* ]) +(.+)$");
  430. }
  431. private:
  432. cmCTestBZR* BZR;
  433. cmsys::RegularExpression RegexStatus;
  434. bool ProcessLine()
  435. {
  436. if(this->RegexStatus.find(this->Line))
  437. {
  438. this->DoPath(this->RegexStatus.match(1)[0],
  439. this->RegexStatus.match(2)[0],
  440. this->RegexStatus.match(3)[0],
  441. this->RegexStatus.match(4));
  442. }
  443. return true;
  444. }
  445. void DoPath(char c0, char c1, char c2, std::string path)
  446. {
  447. if(path.empty()) return;
  448. cmSystemTools::ConvertToUnixSlashes(path);
  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. }