cmCTestP4.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. std::string Path = this->RegexDiff.match(1);
  131. // See if we need to remove the //depot prefix
  132. if(Path.length() > 2 && Path[0] == '/' && Path[1] == '/')
  133. {
  134. size_t found = Path.find('/', 2);
  135. if(found != std::string::npos)
  136. {
  137. Path = Path.substr(found + 1);
  138. }
  139. }
  140. CurrentPath = Path;
  141. AlreadyNotified = false;
  142. }
  143. else
  144. {
  145. if(!AlreadyNotified)
  146. {
  147. P4->DoModification(PathModified, CurrentPath);
  148. AlreadyNotified = true;
  149. }
  150. }
  151. return true;
  152. }
  153. };
  154. //----------------------------------------------------------------------------
  155. cmCTestP4::User cmCTestP4::GetUserData(const std::string& username)
  156. {
  157. std::map<std::string, cmCTestP4::User>::const_iterator it =
  158. Users.find(username);
  159. if(it == Users.end())
  160. {
  161. std::vector<char const*> p4_users;
  162. SetP4Options(p4_users);
  163. p4_users.push_back("users");
  164. p4_users.push_back("-m");
  165. p4_users.push_back("1");
  166. p4_users.push_back(username.c_str());
  167. p4_users.push_back(0);
  168. UserParser out(this, "users-out> ");
  169. OutputLogger err(this->Log, "users-err> ");
  170. RunChild(&p4_users[0], &out, &err);
  171. // The user should now be added to the map. Search again.
  172. it = Users.find(username);
  173. if(it == Users.end())
  174. {
  175. return cmCTestP4::User();
  176. }
  177. }
  178. return it->second;
  179. }
  180. //----------------------------------------------------------------------------
  181. /* Commit format:
  182. Change 1111111 by user@client on 2013/09/26 11:50:36
  183. text
  184. text
  185. Affected files ...
  186. ... //path/to/file#rev edit
  187. ... //path/to/file#rev add
  188. ... //path/to/file#rev delete
  189. ... //path/to/file#rev integrate
  190. */
  191. class cmCTestP4::DescribeParser: public cmCTestVC::LineParser
  192. {
  193. public:
  194. DescribeParser(cmCTestP4* p4, const char* prefix):
  195. LineParser('\n', false), P4(p4), Section(SectionHeader)
  196. {
  197. this->SetLog(&P4->Log, prefix);
  198. this->RegexHeader.compile("^Change ([0-9]+) by (.+)@(.+) on (.*)$");
  199. this->RegexDiff.compile("^\\.\\.\\. (.*)#[0-9]+ ([^ ]+)$");
  200. }
  201. private:
  202. cmsys::RegularExpression RegexHeader;
  203. cmsys::RegularExpression RegexDiff;
  204. cmCTestP4* P4;
  205. typedef cmCTestP4::Revision Revision;
  206. typedef cmCTestP4::Change Change;
  207. std::vector<Change> Changes;
  208. enum SectionType { SectionHeader, SectionBody, SectionDiffHeader,
  209. SectionDiff, SectionCount };
  210. SectionType Section;
  211. Revision Rev;
  212. virtual bool ProcessLine()
  213. {
  214. if(this->Line.empty())
  215. {
  216. this->NextSection();
  217. }
  218. else
  219. {
  220. switch(this->Section)
  221. {
  222. case SectionHeader: this->DoHeaderLine(); break;
  223. case SectionBody: this->DoBodyLine(); break;
  224. case SectionDiffHeader: break; // nothing to do
  225. case SectionDiff: this->DoDiffLine(); break;
  226. case SectionCount: break; // never happens
  227. }
  228. }
  229. return true;
  230. }
  231. void NextSection()
  232. {
  233. if(this->Section == SectionDiff)
  234. {
  235. this->P4->DoRevision(this->Rev, this->Changes);
  236. this->Rev = Revision();
  237. }
  238. this->Section = SectionType((this->Section+1) % SectionCount);
  239. }
  240. void DoHeaderLine()
  241. {
  242. if(this->RegexHeader.find(this->Line))
  243. {
  244. this->Rev.Rev = this->RegexHeader.match(1);
  245. this->Rev.Date = this->RegexHeader.match(4);
  246. cmCTestP4::User user = P4->GetUserData(this->RegexHeader.match(2));
  247. this->Rev.Author = user.Name;
  248. this->Rev.EMail = user.EMail;
  249. this->Rev.Committer = this->Rev.Author;
  250. this->Rev.CommitterEMail = this->Rev.EMail;
  251. this->Rev.CommitDate = this->Rev.Date;
  252. }
  253. }
  254. void DoBodyLine()
  255. {
  256. if(this->Line[0] == '\t')
  257. {
  258. this->Rev.Log += this->Line.substr(1);
  259. }
  260. this->Rev.Log += "\n";
  261. }
  262. void DoDiffLine()
  263. {
  264. if(this->RegexDiff.find(this->Line))
  265. {
  266. Change change;
  267. std::string Path = this->RegexDiff.match(1);
  268. if(Path.length() > 2 && Path[0] == '/' && Path[1] == '/')
  269. {
  270. size_t found = Path.find('/', 2);
  271. if(found != std::string::npos)
  272. {
  273. Path = Path.substr(found + 1);
  274. }
  275. }
  276. change.Path = Path;
  277. std::string action = this->RegexDiff.match(2);
  278. if(action == "add")
  279. {
  280. change.Action = 'A';
  281. }
  282. else if(action == "delete")
  283. {
  284. change.Action = 'D';
  285. }
  286. else if(action == "edit" || action == "integrate")
  287. {
  288. change.Action = 'M';
  289. }
  290. Changes.push_back(change);
  291. }
  292. }
  293. };
  294. //----------------------------------------------------------------------------
  295. void cmCTestP4::SetP4Options(std::vector<char const*> &CommandOptions)
  296. {
  297. if(P4Options.size() == 0)
  298. {
  299. const char* p4 = this->CommandLineTool.c_str();
  300. P4Options.push_back(p4);
  301. //The CTEST_P4_CLIENT variable sets the P4 client used when issuing
  302. //Perforce commands, if it's different from the default one.
  303. std::string client = this->CTest->GetCTestConfiguration("P4Client");
  304. if(!client.empty())
  305. {
  306. P4Options.push_back("-c");
  307. P4Options.push_back(client);
  308. }
  309. //Set the message language to be English, in case the P4 admin
  310. //has localized them
  311. P4Options.push_back("-L");
  312. P4Options.push_back("en");
  313. //The CTEST_P4_OPTIONS variable adds additional Perforce command line
  314. //options before the main command
  315. std::string opts = this->CTest->GetCTestConfiguration("P4Options");
  316. std::vector<cmStdString> args =
  317. cmSystemTools::ParseArguments(opts.c_str());
  318. for(std::vector<cmStdString>::const_iterator ai = args.begin();
  319. ai != args.end(); ++ai)
  320. {
  321. P4Options.push_back(ai->c_str());
  322. }
  323. }
  324. CommandOptions.clear();
  325. for(std::vector<std::string>::iterator i = P4Options.begin();
  326. i != P4Options.end(); ++i)
  327. {
  328. CommandOptions.push_back(i->c_str());
  329. }
  330. }
  331. //----------------------------------------------------------------------------
  332. std::string cmCTestP4::GetWorkingRevision()
  333. {
  334. std::vector<char const*> p4_identify;
  335. SetP4Options(p4_identify);
  336. p4_identify.push_back("changes");
  337. p4_identify.push_back("-m");
  338. p4_identify.push_back("1");
  339. p4_identify.push_back("-t");
  340. std::string source = this->SourceDirectory + "/...#have";
  341. p4_identify.push_back(source.c_str());
  342. p4_identify.push_back(0);
  343. std::string rev;
  344. IdentifyParser out(this, "rev-out> ", rev);
  345. OutputLogger err(this->Log, "rev-err> ");
  346. RunChild(&p4_identify[0], &out, &err);
  347. if(rev.empty())
  348. {
  349. return "0";
  350. }
  351. else
  352. {
  353. return rev;
  354. }
  355. }
  356. //----------------------------------------------------------------------------
  357. void cmCTestP4::NoteOldRevision()
  358. {
  359. this->OldRevision = this->GetWorkingRevision();
  360. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Old revision of repository is: "
  361. << this->OldRevision << "\n");
  362. this->PriorRev.Rev = this->OldRevision;
  363. }
  364. //----------------------------------------------------------------------------
  365. void cmCTestP4::NoteNewRevision()
  366. {
  367. this->NewRevision = this->GetWorkingRevision();
  368. cmCTestLog(this->CTest, HANDLER_OUTPUT, " New revision of repository is: "
  369. << this->NewRevision << "\n");
  370. }
  371. //----------------------------------------------------------------------------
  372. void cmCTestP4::LoadRevisions()
  373. {
  374. std::vector<char const*> p4_changes;
  375. SetP4Options(p4_changes);
  376. // Use 'p4 changes ...@old,new' to get a list of changelists
  377. std::string range = this->SourceDirectory + "/...";
  378. if(this->OldRevision != "0")
  379. {
  380. range.append("@").append(this->OldRevision);
  381. }
  382. if(this->NewRevision != "0")
  383. {
  384. if(this->OldRevision != "0")
  385. {
  386. range.append(",").append(this->NewRevision);
  387. }
  388. else
  389. {
  390. range.append("@").append(this->NewRevision);
  391. }
  392. }
  393. p4_changes.push_back("changes");
  394. p4_changes.push_back(range.c_str());
  395. p4_changes.push_back(0);
  396. ChangesParser out(this, "changes-out> ");
  397. OutputLogger err(this->Log, "changes-err> ");
  398. ChangeLists.clear();
  399. this->RunChild(&p4_changes[0], &out, &err);
  400. if(ChangeLists.size() == 0)
  401. return;
  402. //p4 describe -s ...@1111111,2222222
  403. std::vector<char const*> p4_describe;
  404. for(std::vector<std::string>::reverse_iterator i = ChangeLists.rbegin();
  405. i != ChangeLists.rend(); ++i)
  406. {
  407. SetP4Options(p4_describe);
  408. p4_describe.push_back("describe");
  409. p4_describe.push_back("-s");
  410. p4_describe.push_back(i->c_str());
  411. p4_describe.push_back(0);
  412. DescribeParser outDescribe(this, "describe-out> ");
  413. OutputLogger errDescribe(this->Log, "describe-err> ");
  414. this->RunChild(&p4_describe[0], &outDescribe, &errDescribe);
  415. }
  416. }
  417. //----------------------------------------------------------------------------
  418. void cmCTestP4::LoadModifications()
  419. {
  420. std::vector<char const*> p4_diff;
  421. SetP4Options(p4_diff);
  422. p4_diff.push_back("diff");
  423. //Ideally we would use -Od but not all clients support it
  424. p4_diff.push_back("-dn");
  425. std::string source = this->SourceDirectory + "/...";
  426. p4_diff.push_back(source.c_str());
  427. p4_diff.push_back(0);
  428. DiffParser out(this, "diff-out> ");
  429. OutputLogger err(this->Log, "diff-err> ");
  430. this->RunChild(&p4_diff[0], &out, &err);
  431. }
  432. //----------------------------------------------------------------------------
  433. bool cmCTestP4::UpdateCustom(const std::string& custom)
  434. {
  435. std::vector<std::string> p4_custom_command;
  436. cmSystemTools::ExpandListArgument(custom, p4_custom_command, true);
  437. std::vector<char const*> p4_custom;
  438. for(std::vector<std::string>::const_iterator
  439. i = p4_custom_command.begin(); i != p4_custom_command.end(); ++i)
  440. {
  441. p4_custom.push_back(i->c_str());
  442. }
  443. p4_custom.push_back(0);
  444. OutputLogger custom_out(this->Log, "custom-out> ");
  445. OutputLogger custom_err(this->Log, "custom-err> ");
  446. return this->RunUpdateCommand(&p4_custom[0], &custom_out, &custom_err);
  447. }
  448. //----------------------------------------------------------------------------
  449. bool cmCTestP4::UpdateImpl()
  450. {
  451. std::string custom = this->CTest->GetCTestConfiguration("P4UpdateCustom");
  452. if(!custom.empty())
  453. {
  454. return this->UpdateCustom(custom);
  455. }
  456. std::vector<char const*> p4_sync;
  457. SetP4Options(p4_sync);
  458. p4_sync.push_back("sync");
  459. // Get user-specified update options.
  460. std::string opts = this->CTest->GetCTestConfiguration("UpdateOptions");
  461. if(opts.empty())
  462. {
  463. opts = this->CTest->GetCTestConfiguration("P4UpdateOptions");
  464. }
  465. std::vector<cmStdString> args = cmSystemTools::ParseArguments(opts.c_str());
  466. for(std::vector<cmStdString>::const_iterator ai = args.begin();
  467. ai != args.end(); ++ai)
  468. {
  469. p4_sync.push_back(ai->c_str());
  470. }
  471. std::string source = this->SourceDirectory + "/...";
  472. // Specify the start time for nightly testing.
  473. if(this->CTest->GetTestModel() == cmCTest::NIGHTLY)
  474. {
  475. std::string date = this->GetNightlyTime();
  476. //CTest reports the date as YYYY-MM-DD, Perforce needs it as YYYY/MM/DD
  477. std::replace(date.begin(), date.end(), '-', '/');
  478. //Revision specification: /...@"YYYY/MM/DD HH:MM:SS"
  479. source.append("@\"").append(date).append("\"");
  480. }
  481. p4_sync.push_back(source.c_str());
  482. p4_sync.push_back(0);
  483. OutputLogger out(this->Log, "sync-out> ");
  484. OutputLogger err(this->Log, "sync-err> ");
  485. return this->RunUpdateCommand(&p4_sync[0], &out, &err);
  486. }