cmCTestBZR.cxx 15 KB

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