cmCTestP4.cxx 15 KB

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