cmCTestVC.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestVC.h"
  4. #include "cmCTest.h"
  5. #include "cmSystemTools.h"
  6. #include "cmXMLWriter.h"
  7. #include <cmsys/Process.h>
  8. #include <sstream>
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <vector>
  12. cmCTestVC::cmCTestVC(cmCTest* ct, std::ostream& log)
  13. : CTest(ct)
  14. , Log(log)
  15. {
  16. this->PathCount[PathUpdated] = 0;
  17. this->PathCount[PathModified] = 0;
  18. this->PathCount[PathConflicting] = 0;
  19. this->Unknown.Date = "Unknown";
  20. this->Unknown.Author = "Unknown";
  21. this->Unknown.Rev = "Unknown";
  22. }
  23. cmCTestVC::~cmCTestVC()
  24. {
  25. }
  26. void cmCTestVC::SetCommandLineTool(std::string const& tool)
  27. {
  28. this->CommandLineTool = tool;
  29. }
  30. void cmCTestVC::SetSourceDirectory(std::string const& dir)
  31. {
  32. this->SourceDirectory = dir;
  33. }
  34. bool cmCTestVC::InitialCheckout(const char* command)
  35. {
  36. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  37. " First perform the initial checkout: " << command << "\n");
  38. // Make the parent directory in which to perform the checkout.
  39. std::string parent = cmSystemTools::GetFilenamePath(this->SourceDirectory);
  40. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  41. " Perform checkout in directory: " << parent << "\n");
  42. if (!cmSystemTools::MakeDirectory(parent.c_str())) {
  43. cmCTestLog(this->CTest, ERROR_MESSAGE,
  44. "Cannot create directory: " << parent << std::endl);
  45. return false;
  46. }
  47. // Construct the initial checkout command line.
  48. std::vector<std::string> args = cmSystemTools::ParseArguments(command);
  49. std::vector<char const*> vc_co;
  50. for (std::vector<std::string>::const_iterator ai = args.begin();
  51. ai != args.end(); ++ai) {
  52. vc_co.push_back(ai->c_str());
  53. }
  54. vc_co.push_back(CM_NULLPTR);
  55. // Run the initial checkout command and log its output.
  56. this->Log << "--- Begin Initial Checkout ---\n";
  57. OutputLogger out(this->Log, "co-out> ");
  58. OutputLogger err(this->Log, "co-err> ");
  59. bool result = this->RunChild(&vc_co[0], &out, &err, parent.c_str());
  60. this->Log << "--- End Initial Checkout ---\n";
  61. if (!result) {
  62. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initial checkout failed!"
  63. << std::endl);
  64. }
  65. return result;
  66. }
  67. bool cmCTestVC::RunChild(char const* const* cmd, OutputParser* out,
  68. OutputParser* err, const char* workDir)
  69. {
  70. this->Log << this->ComputeCommandLine(cmd) << "\n";
  71. cmsysProcess* cp = cmsysProcess_New();
  72. cmsysProcess_SetCommand(cp, cmd);
  73. workDir = workDir ? workDir : this->SourceDirectory.c_str();
  74. cmsysProcess_SetWorkingDirectory(cp, workDir);
  75. this->RunProcess(cp, out, err);
  76. int result = cmsysProcess_GetExitValue(cp);
  77. cmsysProcess_Delete(cp);
  78. return result == 0;
  79. }
  80. std::string cmCTestVC::ComputeCommandLine(char const* const* cmd)
  81. {
  82. std::ostringstream line;
  83. const char* sep = "";
  84. for (const char* const* arg = cmd; *arg; ++arg) {
  85. line << sep << "\"" << *arg << "\"";
  86. sep = " ";
  87. }
  88. return line.str();
  89. }
  90. bool cmCTestVC::RunUpdateCommand(char const* const* cmd, OutputParser* out,
  91. OutputParser* err)
  92. {
  93. // Report the command line.
  94. this->UpdateCommandLine = this->ComputeCommandLine(cmd);
  95. if (this->CTest->GetShowOnly()) {
  96. this->Log << this->UpdateCommandLine << "\n";
  97. return true;
  98. }
  99. // Run the command.
  100. return this->RunChild(cmd, out, err);
  101. }
  102. std::string cmCTestVC::GetNightlyTime()
  103. {
  104. // Get the nightly start time corresponding to the current dau.
  105. struct tm* t = this->CTest->GetNightlyTime(
  106. this->CTest->GetCTestConfiguration("NightlyStartTime"),
  107. this->CTest->GetTomorrowTag());
  108. char current_time[1024];
  109. sprintf(current_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900,
  110. t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  111. return std::string(current_time);
  112. }
  113. void cmCTestVC::Cleanup()
  114. {
  115. this->Log << "--- Begin Cleanup ---\n";
  116. this->CleanupImpl();
  117. this->Log << "--- End Cleanup ---\n";
  118. }
  119. void cmCTestVC::CleanupImpl()
  120. {
  121. // We do no cleanup by default.
  122. }
  123. bool cmCTestVC::Update()
  124. {
  125. bool result = true;
  126. // if update version only is on then do not actually update,
  127. // just note the current version and finish
  128. if (!cmSystemTools::IsOn(
  129. this->CTest->GetCTestConfiguration("UpdateVersionOnly").c_str())) {
  130. this->NoteOldRevision();
  131. this->Log << "--- Begin Update ---\n";
  132. result = this->UpdateImpl();
  133. this->Log << "--- End Update ---\n";
  134. }
  135. this->NoteNewRevision();
  136. return result;
  137. }
  138. void cmCTestVC::NoteOldRevision()
  139. {
  140. // We do nothing by default.
  141. }
  142. void cmCTestVC::NoteNewRevision()
  143. {
  144. // We do nothing by default.
  145. }
  146. bool cmCTestVC::UpdateImpl()
  147. {
  148. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  149. "* Unknown VCS tool, not updating!" << std::endl);
  150. return true;
  151. }
  152. bool cmCTestVC::WriteXML(cmXMLWriter& xml)
  153. {
  154. this->Log << "--- Begin Revisions ---\n";
  155. bool result = this->WriteXMLUpdates(xml);
  156. this->Log << "--- End Revisions ---\n";
  157. return result;
  158. }
  159. bool cmCTestVC::WriteXMLUpdates(cmXMLWriter& /*unused*/)
  160. {
  161. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  162. "* CTest cannot extract updates for this VCS tool.\n");
  163. return true;
  164. }
  165. void cmCTestVC::WriteXMLEntry(cmXMLWriter& xml, std::string const& path,
  166. std::string const& name, std::string const& full,
  167. File const& f)
  168. {
  169. static const char* desc[3] = { "Updated", "Modified", "Conflicting" };
  170. Revision const& rev = f.Rev ? *f.Rev : this->Unknown;
  171. std::string prior = f.PriorRev ? f.PriorRev->Rev : std::string("Unknown");
  172. xml.StartElement(desc[f.Status]);
  173. xml.Element("File", name);
  174. xml.Element("Directory", path);
  175. xml.Element("FullName", full);
  176. xml.Element("CheckinDate", rev.Date);
  177. xml.Element("Author", rev.Author);
  178. xml.Element("Email", rev.EMail);
  179. xml.Element("Committer", rev.Committer);
  180. xml.Element("CommitterEmail", rev.CommitterEMail);
  181. xml.Element("CommitDate", rev.CommitDate);
  182. xml.Element("Log", rev.Log);
  183. xml.Element("Revision", rev.Rev);
  184. xml.Element("PriorRevision", prior);
  185. xml.EndElement();
  186. ++this->PathCount[f.Status];
  187. }