cmCTestVC.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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::string const& arg : args) {
  51. vc_co.push_back(arg.c_str());
  52. }
  53. vc_co.push_back(nullptr);
  54. // Run the initial checkout command and log its output.
  55. this->Log << "--- Begin Initial Checkout ---\n";
  56. OutputLogger out(this->Log, "co-out> ");
  57. OutputLogger err(this->Log, "co-err> ");
  58. bool result = this->RunChild(&vc_co[0], &out, &err, parent.c_str());
  59. this->Log << "--- End Initial Checkout ---\n";
  60. if (!result) {
  61. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initial checkout failed!"
  62. << std::endl);
  63. }
  64. return result;
  65. }
  66. bool cmCTestVC::RunChild(char const* const* cmd, OutputParser* out,
  67. OutputParser* err, const char* workDir,
  68. Encoding encoding)
  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, encoding);
  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, Encoding encoding)
  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, nullptr, encoding);
  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. result = this->NoteOldRevision() && result;
  131. this->Log << "--- Begin Update ---\n";
  132. result = this->UpdateImpl() && result;
  133. this->Log << "--- End Update ---\n";
  134. }
  135. result = this->NoteNewRevision() && result;
  136. return result;
  137. }
  138. bool cmCTestVC::NoteOldRevision()
  139. {
  140. // We do nothing by default.
  141. return true;
  142. }
  143. bool cmCTestVC::NoteNewRevision()
  144. {
  145. // We do nothing by default.
  146. return true;
  147. }
  148. bool cmCTestVC::UpdateImpl()
  149. {
  150. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  151. "* Unknown VCS tool, not updating!" << std::endl);
  152. return true;
  153. }
  154. bool cmCTestVC::WriteXML(cmXMLWriter& xml)
  155. {
  156. this->Log << "--- Begin Revisions ---\n";
  157. bool result = this->WriteXMLUpdates(xml);
  158. this->Log << "--- End Revisions ---\n";
  159. return result;
  160. }
  161. bool cmCTestVC::WriteXMLUpdates(cmXMLWriter& /*unused*/)
  162. {
  163. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  164. "* CTest cannot extract updates for this VCS tool.\n");
  165. return true;
  166. }
  167. void cmCTestVC::WriteXMLEntry(cmXMLWriter& xml, std::string const& path,
  168. std::string const& name, std::string const& full,
  169. File const& f)
  170. {
  171. static const char* desc[3] = { "Updated", "Modified", "Conflicting" };
  172. Revision const& rev = f.Rev ? *f.Rev : this->Unknown;
  173. std::string prior = f.PriorRev ? f.PriorRev->Rev : std::string("Unknown");
  174. xml.StartElement(desc[f.Status]);
  175. xml.Element("File", name);
  176. xml.Element("Directory", path);
  177. xml.Element("FullName", full);
  178. xml.Element("CheckinDate", rev.Date);
  179. xml.Element("Author", rev.Author);
  180. xml.Element("Email", rev.EMail);
  181. xml.Element("Committer", rev.Committer);
  182. xml.Element("CommitterEmail", rev.CommitterEMail);
  183. xml.Element("CommitDate", rev.CommitDate);
  184. xml.Element("Log", rev.Log);
  185. xml.Element("Revision", rev.Rev);
  186. xml.Element("PriorRevision", prior);
  187. xml.EndElement();
  188. ++this->PathCount[f.Status];
  189. }