cmCTestP4.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2013 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 "cmCTestP4.h"
  11. #include "cmCTest.h"
  12. #include "cmSystemTools.h"
  13. #include <cmsys/Process.h>
  14. #include <cmsys/RegularExpression.hxx>
  15. #include <ctype.h>
  16. #include <sys/types.h>
  17. #include <time.h>
  18. cmCTestP4::cmCTestP4(cmCTest* ct, std::ostream& log)
  19. : cmCTestGlobalVC(ct, log)
  20. {
  21. this->PriorRev = this->Unknown;
  22. }
  23. cmCTestP4::~cmCTestP4()
  24. {
  25. }
  26. class cmCTestP4::IdentifyParser : public cmCTestVC::LineParser
  27. {
  28. public:
  29. IdentifyParser(cmCTestP4* p4, const char* prefix, std::string& rev)
  30. : Rev(rev)
  31. {
  32. this->SetLog(&p4->Log, prefix);
  33. this->RegexIdentify.compile("^Change ([0-9]+) on");
  34. }
  35. private:
  36. std::string& Rev;
  37. cmsys::RegularExpression RegexIdentify;
  38. bool ProcessLine() CM_OVERRIDE
  39. {
  40. if (this->RegexIdentify.find(this->Line)) {
  41. this->Rev = this->RegexIdentify.match(1);
  42. return false;
  43. }
  44. return true;
  45. }
  46. };
  47. class cmCTestP4::ChangesParser : public cmCTestVC::LineParser
  48. {
  49. public:
  50. ChangesParser(cmCTestP4* p4, const char* prefix)
  51. : P4(p4)
  52. {
  53. this->SetLog(&P4->Log, prefix);
  54. this->RegexIdentify.compile("^Change ([0-9]+) on");
  55. }
  56. private:
  57. cmsys::RegularExpression RegexIdentify;
  58. cmCTestP4* P4;
  59. bool ProcessLine() CM_OVERRIDE
  60. {
  61. if (this->RegexIdentify.find(this->Line)) {
  62. P4->ChangeLists.push_back(this->RegexIdentify.match(1));
  63. }
  64. return true;
  65. }
  66. };
  67. class cmCTestP4::UserParser : public cmCTestVC::LineParser
  68. {
  69. public:
  70. UserParser(cmCTestP4* p4, const char* prefix)
  71. : P4(p4)
  72. {
  73. this->SetLog(&P4->Log, prefix);
  74. this->RegexUser.compile("^(.+) <(.*)> \\((.*)\\) accessed (.*)$");
  75. }
  76. private:
  77. cmsys::RegularExpression RegexUser;
  78. cmCTestP4* P4;
  79. bool ProcessLine() CM_OVERRIDE
  80. {
  81. if (this->RegexUser.find(this->Line)) {
  82. User NewUser;
  83. NewUser.UserName = this->RegexUser.match(1);
  84. NewUser.EMail = this->RegexUser.match(2);
  85. NewUser.Name = this->RegexUser.match(3);
  86. NewUser.AccessTime = this->RegexUser.match(4);
  87. P4->Users[this->RegexUser.match(1)] = NewUser;
  88. return false;
  89. }
  90. return true;
  91. }
  92. };
  93. /* Diff format:
  94. ==== //depot/file#rev - /absolute/path/to/file ====
  95. (diff data)
  96. ==== //depot/file2#rev - /absolute/path/to/file2 ====
  97. (diff data)
  98. ==== //depot/file3#rev - /absolute/path/to/file3 ====
  99. ==== //depot/file4#rev - /absolute/path/to/file4 ====
  100. (diff data)
  101. */
  102. class cmCTestP4::DiffParser : public cmCTestVC::LineParser
  103. {
  104. public:
  105. DiffParser(cmCTestP4* p4, const char* prefix)
  106. : P4(p4)
  107. , AlreadyNotified(false)
  108. {
  109. this->SetLog(&P4->Log, prefix);
  110. this->RegexDiff.compile("^==== (.*)#[0-9]+ - (.*)");
  111. }
  112. private:
  113. cmCTestP4* P4;
  114. bool AlreadyNotified;
  115. std::string CurrentPath;
  116. cmsys::RegularExpression RegexDiff;
  117. bool ProcessLine() CM_OVERRIDE
  118. {
  119. if (!this->Line.empty() && this->Line[0] == '=' &&
  120. this->RegexDiff.find(this->Line)) {
  121. CurrentPath = this->RegexDiff.match(1);
  122. AlreadyNotified = false;
  123. } else {
  124. if (!AlreadyNotified) {
  125. P4->DoModification(PathModified, CurrentPath);
  126. AlreadyNotified = true;
  127. }
  128. }
  129. return true;
  130. }
  131. };
  132. cmCTestP4::User cmCTestP4::GetUserData(const std::string& username)
  133. {
  134. std::map<std::string, cmCTestP4::User>::const_iterator it =
  135. Users.find(username);
  136. if (it == Users.end()) {
  137. std::vector<char const*> p4_users;
  138. SetP4Options(p4_users);
  139. p4_users.push_back("users");
  140. p4_users.push_back("-m");
  141. p4_users.push_back("1");
  142. p4_users.push_back(username.c_str());
  143. p4_users.push_back(CM_NULLPTR);
  144. UserParser out(this, "users-out> ");
  145. OutputLogger err(this->Log, "users-err> ");
  146. RunChild(&p4_users[0], &out, &err);
  147. // The user should now be added to the map. Search again.
  148. it = Users.find(username);
  149. if (it == Users.end()) {
  150. return cmCTestP4::User();
  151. }
  152. }
  153. return it->second;
  154. }
  155. /* Commit format:
  156. Change 1111111 by user@client on 2013/09/26 11:50:36
  157. text
  158. text
  159. Affected files ...
  160. ... //path/to/file#rev edit
  161. ... //path/to/file#rev add
  162. ... //path/to/file#rev delete
  163. ... //path/to/file#rev integrate
  164. */
  165. class cmCTestP4::DescribeParser : public cmCTestVC::LineParser
  166. {
  167. public:
  168. DescribeParser(cmCTestP4* p4, const char* prefix)
  169. : LineParser('\n', false)
  170. , P4(p4)
  171. , Section(SectionHeader)
  172. {
  173. this->SetLog(&P4->Log, prefix);
  174. this->RegexHeader.compile("^Change ([0-9]+) by (.+)@(.+) on (.*)$");
  175. this->RegexDiff.compile("^\\.\\.\\. (.*)#[0-9]+ ([^ ]+)$");
  176. }
  177. private:
  178. cmsys::RegularExpression RegexHeader;
  179. cmsys::RegularExpression RegexDiff;
  180. cmCTestP4* P4;
  181. typedef cmCTestP4::Revision Revision;
  182. typedef cmCTestP4::Change Change;
  183. std::vector<Change> Changes;
  184. enum SectionType
  185. {
  186. SectionHeader,
  187. SectionBody,
  188. SectionDiffHeader,
  189. SectionDiff,
  190. SectionCount
  191. };
  192. SectionType Section;
  193. Revision Rev;
  194. bool ProcessLine() CM_OVERRIDE
  195. {
  196. if (this->Line.empty()) {
  197. this->NextSection();
  198. } else {
  199. switch (this->Section) {
  200. case SectionHeader:
  201. this->DoHeaderLine();
  202. break;
  203. case SectionBody:
  204. this->DoBodyLine();
  205. break;
  206. case SectionDiffHeader:
  207. break; // nothing to do
  208. case SectionDiff:
  209. this->DoDiffLine();
  210. break;
  211. case SectionCount:
  212. break; // never happens
  213. }
  214. }
  215. return true;
  216. }
  217. void NextSection()
  218. {
  219. if (this->Section == SectionDiff) {
  220. this->P4->DoRevision(this->Rev, this->Changes);
  221. this->Rev = Revision();
  222. }
  223. this->Section = SectionType((this->Section + 1) % SectionCount);
  224. }
  225. void DoHeaderLine()
  226. {
  227. if (this->RegexHeader.find(this->Line)) {
  228. this->Rev.Rev = this->RegexHeader.match(1);
  229. this->Rev.Date = this->RegexHeader.match(4);
  230. cmCTestP4::User user = P4->GetUserData(this->RegexHeader.match(2));
  231. this->Rev.Author = user.Name;
  232. this->Rev.EMail = user.EMail;
  233. this->Rev.Committer = this->Rev.Author;
  234. this->Rev.CommitterEMail = this->Rev.EMail;
  235. this->Rev.CommitDate = this->Rev.Date;
  236. }
  237. }
  238. void DoBodyLine()
  239. {
  240. if (this->Line[0] == '\t') {
  241. this->Rev.Log += this->Line.substr(1);
  242. }
  243. this->Rev.Log += "\n";
  244. }
  245. void DoDiffLine()
  246. {
  247. if (this->RegexDiff.find(this->Line)) {
  248. Change change;
  249. std::string Path = this->RegexDiff.match(1);
  250. if (Path.length() > 2 && Path[0] == '/' && Path[1] == '/') {
  251. size_t found = Path.find('/', 2);
  252. if (found != std::string::npos) {
  253. Path = Path.substr(found + 1);
  254. }
  255. }
  256. change.Path = Path;
  257. std::string action = this->RegexDiff.match(2);
  258. if (action == "add") {
  259. change.Action = 'A';
  260. } else if (action == "delete") {
  261. change.Action = 'D';
  262. } else if (action == "edit" || action == "integrate") {
  263. change.Action = 'M';
  264. }
  265. Changes.push_back(change);
  266. }
  267. }
  268. };
  269. void cmCTestP4::SetP4Options(std::vector<char const*>& CommandOptions)
  270. {
  271. if (P4Options.empty()) {
  272. const char* p4 = this->CommandLineTool.c_str();
  273. P4Options.push_back(p4);
  274. // The CTEST_P4_CLIENT variable sets the P4 client used when issuing
  275. // Perforce commands, if it's different from the default one.
  276. std::string client = this->CTest->GetCTestConfiguration("P4Client");
  277. if (!client.empty()) {
  278. P4Options.push_back("-c");
  279. P4Options.push_back(client);
  280. }
  281. // Set the message language to be English, in case the P4 admin
  282. // has localized them
  283. P4Options.push_back("-L");
  284. P4Options.push_back("en");
  285. // The CTEST_P4_OPTIONS variable adds additional Perforce command line
  286. // options before the main command
  287. std::string opts = this->CTest->GetCTestConfiguration("P4Options");
  288. std::vector<std::string> args =
  289. cmSystemTools::ParseArguments(opts.c_str());
  290. P4Options.insert(P4Options.end(), args.begin(), args.end());
  291. }
  292. CommandOptions.clear();
  293. for (std::vector<std::string>::iterator i = P4Options.begin();
  294. i != P4Options.end(); ++i) {
  295. CommandOptions.push_back(i->c_str());
  296. }
  297. }
  298. std::string cmCTestP4::GetWorkingRevision()
  299. {
  300. std::vector<char const*> p4_identify;
  301. SetP4Options(p4_identify);
  302. p4_identify.push_back("changes");
  303. p4_identify.push_back("-m");
  304. p4_identify.push_back("1");
  305. p4_identify.push_back("-t");
  306. std::string source = this->SourceDirectory + "/...#have";
  307. p4_identify.push_back(source.c_str());
  308. p4_identify.push_back(CM_NULLPTR);
  309. std::string rev;
  310. IdentifyParser out(this, "p4_changes-out> ", rev);
  311. OutputLogger err(this->Log, "p4_changes-err> ");
  312. bool result = RunChild(&p4_identify[0], &out, &err);
  313. // If there was a problem contacting the server return "<unknown>"
  314. if (!result) {
  315. return "<unknown>";
  316. }
  317. if (rev.empty()) {
  318. return "0";
  319. } else {
  320. return rev;
  321. }
  322. }
  323. void cmCTestP4::NoteOldRevision()
  324. {
  325. this->OldRevision = this->GetWorkingRevision();
  326. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Old revision of repository is: "
  327. << this->OldRevision << "\n");
  328. this->PriorRev.Rev = this->OldRevision;
  329. }
  330. void cmCTestP4::NoteNewRevision()
  331. {
  332. this->NewRevision = this->GetWorkingRevision();
  333. cmCTestLog(this->CTest, HANDLER_OUTPUT, " New revision of repository is: "
  334. << this->NewRevision << "\n");
  335. }
  336. void cmCTestP4::LoadRevisions()
  337. {
  338. std::vector<char const*> p4_changes;
  339. SetP4Options(p4_changes);
  340. // Use 'p4 changes ...@old,new' to get a list of changelists
  341. std::string range = this->SourceDirectory + "/...";
  342. // If any revision is unknown it means we couldn't contact the server.
  343. // Do not process updates
  344. if (this->OldRevision == "<unknown>" || this->NewRevision == "<unknown>") {
  345. cmCTestLog(this->CTest, HANDLER_OUTPUT, " At least one of the revisions "
  346. << "is unknown. No repository changes will be reported.\n");
  347. return;
  348. }
  349. range.append("@")
  350. .append(this->OldRevision)
  351. .append(",")
  352. .append(this->NewRevision);
  353. p4_changes.push_back("changes");
  354. p4_changes.push_back(range.c_str());
  355. p4_changes.push_back(CM_NULLPTR);
  356. ChangesParser out(this, "p4_changes-out> ");
  357. OutputLogger err(this->Log, "p4_changes-err> ");
  358. ChangeLists.clear();
  359. this->RunChild(&p4_changes[0], &out, &err);
  360. if (ChangeLists.empty()) {
  361. return;
  362. }
  363. // p4 describe -s ...@1111111,2222222
  364. std::vector<char const*> p4_describe;
  365. for (std::vector<std::string>::reverse_iterator i = ChangeLists.rbegin();
  366. i != ChangeLists.rend(); ++i) {
  367. SetP4Options(p4_describe);
  368. p4_describe.push_back("describe");
  369. p4_describe.push_back("-s");
  370. p4_describe.push_back(i->c_str());
  371. p4_describe.push_back(CM_NULLPTR);
  372. DescribeParser outDescribe(this, "p4_describe-out> ");
  373. OutputLogger errDescribe(this->Log, "p4_describe-err> ");
  374. this->RunChild(&p4_describe[0], &outDescribe, &errDescribe);
  375. }
  376. }
  377. void cmCTestP4::LoadModifications()
  378. {
  379. std::vector<char const*> p4_diff;
  380. SetP4Options(p4_diff);
  381. p4_diff.push_back("diff");
  382. // Ideally we would use -Od but not all clients support it
  383. p4_diff.push_back("-dn");
  384. std::string source = this->SourceDirectory + "/...";
  385. p4_diff.push_back(source.c_str());
  386. p4_diff.push_back(CM_NULLPTR);
  387. DiffParser out(this, "p4_diff-out> ");
  388. OutputLogger err(this->Log, "p4_diff-err> ");
  389. this->RunChild(&p4_diff[0], &out, &err);
  390. }
  391. bool cmCTestP4::UpdateCustom(const std::string& custom)
  392. {
  393. std::vector<std::string> p4_custom_command;
  394. cmSystemTools::ExpandListArgument(custom, p4_custom_command, true);
  395. std::vector<char const*> p4_custom;
  396. for (std::vector<std::string>::const_iterator i = p4_custom_command.begin();
  397. i != p4_custom_command.end(); ++i) {
  398. p4_custom.push_back(i->c_str());
  399. }
  400. p4_custom.push_back(CM_NULLPTR);
  401. OutputLogger custom_out(this->Log, "p4_customsync-out> ");
  402. OutputLogger custom_err(this->Log, "p4_customsync-err> ");
  403. return this->RunUpdateCommand(&p4_custom[0], &custom_out, &custom_err);
  404. }
  405. bool cmCTestP4::UpdateImpl()
  406. {
  407. std::string custom = this->CTest->GetCTestConfiguration("P4UpdateCustom");
  408. if (!custom.empty()) {
  409. return this->UpdateCustom(custom);
  410. }
  411. // If we couldn't get a revision number before updating, abort.
  412. if (this->OldRevision == "<unknown>") {
  413. this->UpdateCommandLine = "Unknown current revision";
  414. cmCTestLog(this->CTest, ERROR_MESSAGE, " Unknown current revision\n");
  415. return false;
  416. }
  417. std::vector<char const*> p4_sync;
  418. SetP4Options(p4_sync);
  419. p4_sync.push_back("sync");
  420. // Get user-specified update options.
  421. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  422. if (opts.empty()) {
  423. opts = this->CTest->GetCTestConfiguration("P4UpdateOptions");
  424. }
  425. std::vector<std::string> args = cmSystemTools::ParseArguments(opts.c_str());
  426. for (std::vector<std::string>::const_iterator ai = args.begin();
  427. ai != args.end(); ++ai) {
  428. p4_sync.push_back(ai->c_str());
  429. }
  430. std::string source = this->SourceDirectory + "/...";
  431. // Specify the start time for nightly testing.
  432. if (this->CTest->GetTestModel() == cmCTest::NIGHTLY) {
  433. std::string date = this->GetNightlyTime();
  434. // CTest reports the date as YYYY-MM-DD, Perforce needs it as YYYY/MM/DD
  435. std::replace(date.begin(), date.end(), '-', '/');
  436. // Revision specification: /...@"YYYY/MM/DD HH:MM:SS"
  437. source.append("@\"").append(date).append("\"");
  438. }
  439. p4_sync.push_back(source.c_str());
  440. p4_sync.push_back(CM_NULLPTR);
  441. OutputLogger out(this->Log, "p4_sync-out> ");
  442. OutputLogger err(this->Log, "p4_sync-err> ");
  443. return this->RunUpdateCommand(&p4_sync[0], &out, &err);
  444. }