cmCTestP4.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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<cmStdString> args =
  307. cmSystemTools::ParseArguments(opts.c_str());
  308. for(std::vector<cmStdString>::const_iterator ai = args.begin();
  309. ai != args.end(); ++ai)
  310. {
  311. P4Options.push_back(ai->c_str());
  312. }
  313. }
  314. CommandOptions.clear();
  315. for(std::vector<std::string>::iterator i = P4Options.begin();
  316. i != P4Options.end(); ++i)
  317. {
  318. CommandOptions.push_back(i->c_str());
  319. }
  320. }
  321. //----------------------------------------------------------------------------
  322. std::string cmCTestP4::GetWorkingRevision()
  323. {
  324. std::vector<char const*> p4_identify;
  325. SetP4Options(p4_identify);
  326. p4_identify.push_back("changes");
  327. p4_identify.push_back("-m");
  328. p4_identify.push_back("1");
  329. p4_identify.push_back("-t");
  330. std::string source = this->SourceDirectory + "/...#have";
  331. p4_identify.push_back(source.c_str());
  332. p4_identify.push_back(0);
  333. std::string rev;
  334. IdentifyParser out(this, "p4_changes-out> ", rev);
  335. OutputLogger err(this->Log, "p4_changes-err> ");
  336. bool result = RunChild(&p4_identify[0], &out, &err);
  337. // If there was a problem contacting the server return "<unknown>"
  338. if(!result)
  339. {
  340. return "<unknown>";
  341. }
  342. if(rev.empty())
  343. {
  344. return "0";
  345. }
  346. else
  347. {
  348. return rev;
  349. }
  350. }
  351. //----------------------------------------------------------------------------
  352. void cmCTestP4::NoteOldRevision()
  353. {
  354. this->OldRevision = this->GetWorkingRevision();
  355. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Old revision of repository is: "
  356. << this->OldRevision << "\n");
  357. this->PriorRev.Rev = this->OldRevision;
  358. }
  359. //----------------------------------------------------------------------------
  360. void cmCTestP4::NoteNewRevision()
  361. {
  362. this->NewRevision = this->GetWorkingRevision();
  363. cmCTestLog(this->CTest, HANDLER_OUTPUT, " New revision of repository is: "
  364. << this->NewRevision << "\n");
  365. }
  366. //----------------------------------------------------------------------------
  367. void cmCTestP4::LoadRevisions()
  368. {
  369. std::vector<char const*> p4_changes;
  370. SetP4Options(p4_changes);
  371. // Use 'p4 changes ...@old,new' to get a list of changelists
  372. std::string range = this->SourceDirectory + "/...";
  373. // If any revision is unknown it means we couldn't contact the server.
  374. // Do not process updates
  375. if(this->OldRevision == "<unknown>" || this->NewRevision == "<unknown>")
  376. {
  377. cmCTestLog(this->CTest, HANDLER_OUTPUT, " At least one of the revisions "
  378. << "is unknown. No repository changes will be reported.\n");
  379. return;
  380. }
  381. range.append("@").append(this->OldRevision)
  382. .append(",").append(this->NewRevision);
  383. p4_changes.push_back("changes");
  384. p4_changes.push_back(range.c_str());
  385. p4_changes.push_back(0);
  386. ChangesParser out(this, "p4_changes-out> ");
  387. OutputLogger err(this->Log, "p4_changes-err> ");
  388. ChangeLists.clear();
  389. this->RunChild(&p4_changes[0], &out, &err);
  390. if(ChangeLists.size() == 0)
  391. return;
  392. //p4 describe -s ...@1111111,2222222
  393. std::vector<char const*> p4_describe;
  394. for(std::vector<std::string>::reverse_iterator i = ChangeLists.rbegin();
  395. i != ChangeLists.rend(); ++i)
  396. {
  397. SetP4Options(p4_describe);
  398. p4_describe.push_back("describe");
  399. p4_describe.push_back("-s");
  400. p4_describe.push_back(i->c_str());
  401. p4_describe.push_back(0);
  402. DescribeParser outDescribe(this, "p4_describe-out> ");
  403. OutputLogger errDescribe(this->Log, "p4_describe-err> ");
  404. this->RunChild(&p4_describe[0], &outDescribe, &errDescribe);
  405. }
  406. }
  407. //----------------------------------------------------------------------------
  408. void cmCTestP4::LoadModifications()
  409. {
  410. std::vector<char const*> p4_diff;
  411. SetP4Options(p4_diff);
  412. p4_diff.push_back("diff");
  413. //Ideally we would use -Od but not all clients support it
  414. p4_diff.push_back("-dn");
  415. std::string source = this->SourceDirectory + "/...";
  416. p4_diff.push_back(source.c_str());
  417. p4_diff.push_back(0);
  418. DiffParser out(this, "p4_diff-out> ");
  419. OutputLogger err(this->Log, "p4_diff-err> ");
  420. this->RunChild(&p4_diff[0], &out, &err);
  421. }
  422. //----------------------------------------------------------------------------
  423. bool cmCTestP4::UpdateCustom(const std::string& custom)
  424. {
  425. std::vector<std::string> p4_custom_command;
  426. cmSystemTools::ExpandListArgument(custom, p4_custom_command, true);
  427. std::vector<char const*> p4_custom;
  428. for(std::vector<std::string>::const_iterator
  429. i = p4_custom_command.begin(); i != p4_custom_command.end(); ++i)
  430. {
  431. p4_custom.push_back(i->c_str());
  432. }
  433. p4_custom.push_back(0);
  434. OutputLogger custom_out(this->Log, "p4_customsync-out> ");
  435. OutputLogger custom_err(this->Log, "p4_customsync-err> ");
  436. return this->RunUpdateCommand(&p4_custom[0], &custom_out, &custom_err);
  437. }
  438. //----------------------------------------------------------------------------
  439. bool cmCTestP4::UpdateImpl()
  440. {
  441. std::string custom = this->CTest->GetCTestConfiguration("P4UpdateCustom");
  442. if(!custom.empty())
  443. {
  444. return this->UpdateCustom(custom);
  445. }
  446. // If we couldn't get a revision number before updating, abort.
  447. if(this->OldRevision == "<unknown>")
  448. {
  449. this->UpdateCommandLine = "Unknown current revision";
  450. cmCTestLog(this->CTest, ERROR_MESSAGE, " Unknown current revision\n");
  451. return false;
  452. }
  453. std::vector<char const*> p4_sync;
  454. SetP4Options(p4_sync);
  455. p4_sync.push_back("sync");
  456. // Get user-specified update options.
  457. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  458. if(opts.empty())
  459. {
  460. opts = this->CTest->GetCTestConfiguration("P4UpdateOptions");
  461. }
  462. std::vector<cmStdString> args = cmSystemTools::ParseArguments(opts.c_str());
  463. for(std::vector<cmStdString>::const_iterator ai = args.begin();
  464. ai != args.end(); ++ai)
  465. {
  466. p4_sync.push_back(ai->c_str());
  467. }
  468. std::string source = this->SourceDirectory + "/...";
  469. // Specify the start time for nightly testing.
  470. if(this->CTest->GetTestModel() == cmCTest::NIGHTLY)
  471. {
  472. std::string date = this->GetNightlyTime();
  473. //CTest reports the date as YYYY-MM-DD, Perforce needs it as YYYY/MM/DD
  474. std::replace(date.begin(), date.end(), '-', '/');
  475. //Revision specification: /...@"YYYY/MM/DD HH:MM:SS"
  476. source.append("@\"").append(date).append("\"");
  477. }
  478. p4_sync.push_back(source.c_str());
  479. p4_sync.push_back(0);
  480. OutputLogger out(this->Log, "p4_sync-out> ");
  481. OutputLogger err(this->Log, "p4_sync-err> ");
  482. return this->RunUpdateCommand(&p4_sync[0], &out, &err);
  483. }