cmCTestBZR.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. cmSystemTools::ConvertToUnixSlashes(this->CurChange.Path);
  255. this->Changes.push_back(this->CurChange);
  256. }
  257. else if(strcmp(name, "symlink") == 0 && !this->CData.empty())
  258. {
  259. // symlinks have an arobase at the end in the log
  260. this->CurChange.Path.assign(&this->CData[0], this->CData.size()-1);
  261. cmSystemTools::ConvertToUnixSlashes(this->CurChange.Path);
  262. this->Changes.push_back(this->CurChange);
  263. }
  264. else if(strcmp(name, "committer") == 0 && !this->CData.empty())
  265. {
  266. this->Rev.Author.assign(&this->CData[0], this->CData.size());
  267. if(this->EmailRegex.find(this->Rev.Author))
  268. {
  269. this->Rev.Author = this->EmailRegex.match(1);
  270. this->Rev.EMail = this->EmailRegex.match(2);
  271. }
  272. }
  273. else if(strcmp(name, "timestamp") == 0 && !this->CData.empty())
  274. {
  275. this->Rev.Date.assign(&this->CData[0], this->CData.size());
  276. }
  277. else if(strcmp(name, "message") == 0 && !this->CData.empty())
  278. {
  279. this->Rev.Log.assign(&this->CData[0], this->CData.size());
  280. }
  281. else if(strcmp(name, "revno") == 0 && !this->CData.empty())
  282. {
  283. this->Rev.Rev.assign(&this->CData[0], this->CData.size());
  284. }
  285. this->CData.clear();
  286. }
  287. virtual void ReportError(int, int, const char* msg)
  288. {
  289. this->BZR->Log << "Error parsing bzr log xml: " << msg << "\n";
  290. }
  291. };
  292. //----------------------------------------------------------------------------
  293. class cmCTestBZR::UpdateParser: public cmCTestVC::LineParser
  294. {
  295. public:
  296. UpdateParser(cmCTestBZR* bzr, const char* prefix): BZR(bzr)
  297. {
  298. this->SetLog(&bzr->Log, prefix);
  299. this->RegexUpdate.compile("^([-+R?XCP ])([NDKM ])([* ]) +(.+)$");
  300. }
  301. private:
  302. cmCTestBZR* BZR;
  303. cmsys::RegularExpression RegexUpdate;
  304. virtual bool ProcessChunk(const char* first, int length)
  305. {
  306. bool last_is_new_line = (*first == '\r' || *first == '\n');
  307. const char* const last = first + length;
  308. for(const char* c = first; c != last; ++c)
  309. {
  310. if(*c == '\r' || *c == '\n')
  311. {
  312. if(!last_is_new_line)
  313. {
  314. // Log this line.
  315. if(this->Log && this->Prefix)
  316. {
  317. *this->Log << this->Prefix << this->Line << "\n";
  318. }
  319. // Hand this line to the subclass implementation.
  320. if(!this->ProcessLine())
  321. {
  322. this->Line = "";
  323. return false;
  324. }
  325. this->Line = "";
  326. last_is_new_line = true;
  327. }
  328. }
  329. else
  330. {
  331. // Append this character to the line under construction.
  332. this->Line.append(1, *c);
  333. last_is_new_line = false;
  334. }
  335. }
  336. return true;
  337. }
  338. bool ProcessLine()
  339. {
  340. if(this->RegexUpdate.find(this->Line))
  341. {
  342. this->DoPath(this->RegexUpdate.match(1)[0],
  343. this->RegexUpdate.match(2)[0],
  344. this->RegexUpdate.match(3)[0],
  345. this->RegexUpdate.match(4));
  346. }
  347. return true;
  348. }
  349. void DoPath(char c0, char c1, char c2, std::string path)
  350. {
  351. if(path.empty()) return;
  352. cmSystemTools::ConvertToUnixSlashes(path);
  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 path)
  447. {
  448. if(path.empty()) return;
  449. cmSystemTools::ConvertToUnixSlashes(path);
  450. if ( c0=='C' )
  451. {
  452. this->BZR->DoModification(PathConflicting, path);
  453. return;
  454. }
  455. if ( c0 == '+' || c0 == 'R' || c0 == 'P'
  456. || c1=='M' || c1=='K' || c1=='N' || c1=='D'
  457. || c2 =='*' )
  458. {
  459. this->BZR->DoModification(PathModified, path);
  460. return;
  461. }
  462. }
  463. };
  464. //----------------------------------------------------------------------------
  465. void cmCTestBZR::LoadModifications()
  466. {
  467. // Run "bzr status" which reports local modifications.
  468. const char* bzr = this->CommandLineTool.c_str();
  469. const char* bzr_status[] = {bzr, "status", "-SV", 0};
  470. StatusParser out(this, "status-out> ");
  471. OutputLogger err(this->Log, "status-err> ");
  472. this->RunChild(bzr_status, &out, &err);
  473. }