cmCTestVC.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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)) {
  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. vc_co.reserve(args.size() + 1);
  51. for (std::string const& arg : args) {
  52. vc_co.push_back(arg.c_str());
  53. }
  54. vc_co.push_back(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. Encoding encoding)
  70. {
  71. this->Log << this->ComputeCommandLine(cmd) << "\n";
  72. cmsysProcess* cp = cmsysProcess_New();
  73. cmsysProcess_SetCommand(cp, cmd);
  74. workDir = workDir ? workDir : this->SourceDirectory.c_str();
  75. cmsysProcess_SetWorkingDirectory(cp, workDir);
  76. this->RunProcess(cp, out, err, encoding);
  77. int result = cmsysProcess_GetExitValue(cp);
  78. cmsysProcess_Delete(cp);
  79. return result == 0;
  80. }
  81. std::string cmCTestVC::ComputeCommandLine(char const* const* cmd)
  82. {
  83. std::ostringstream line;
  84. const char* sep = "";
  85. for (const char* const* arg = cmd; *arg; ++arg) {
  86. line << sep << "\"" << *arg << "\"";
  87. sep = " ";
  88. }
  89. return line.str();
  90. }
  91. bool cmCTestVC::RunUpdateCommand(char const* const* cmd, OutputParser* out,
  92. OutputParser* err, Encoding encoding)
  93. {
  94. // Report the command line.
  95. this->UpdateCommandLine = this->ComputeCommandLine(cmd);
  96. if (this->CTest->GetShowOnly()) {
  97. this->Log << this->UpdateCommandLine << "\n";
  98. return true;
  99. }
  100. // Run the command.
  101. return this->RunChild(cmd, out, err, nullptr, encoding);
  102. }
  103. std::string cmCTestVC::GetNightlyTime()
  104. {
  105. // Get the nightly start time corresponding to the current dau.
  106. struct tm* t = this->CTest->GetNightlyTime(
  107. this->CTest->GetCTestConfiguration("NightlyStartTime"),
  108. this->CTest->GetTomorrowTag());
  109. char current_time[1024];
  110. sprintf(current_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900,
  111. t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  112. return std::string(current_time);
  113. }
  114. void cmCTestVC::Cleanup()
  115. {
  116. this->Log << "--- Begin Cleanup ---\n";
  117. this->CleanupImpl();
  118. this->Log << "--- End Cleanup ---\n";
  119. }
  120. void cmCTestVC::CleanupImpl()
  121. {
  122. // We do no cleanup by default.
  123. }
  124. bool cmCTestVC::Update()
  125. {
  126. bool result = true;
  127. // if update version only is on then do not actually update,
  128. // just note the current version and finish
  129. if (!cmSystemTools::IsOn(
  130. this->CTest->GetCTestConfiguration("UpdateVersionOnly").c_str())) {
  131. result = this->NoteOldRevision() && result;
  132. this->Log << "--- Begin Update ---\n";
  133. result = this->UpdateImpl() && result;
  134. this->Log << "--- End Update ---\n";
  135. }
  136. result = this->NoteNewRevision() && result;
  137. return result;
  138. }
  139. bool cmCTestVC::NoteOldRevision()
  140. {
  141. // We do nothing by default.
  142. return true;
  143. }
  144. bool cmCTestVC::NoteNewRevision()
  145. {
  146. // We do nothing by default.
  147. return true;
  148. }
  149. bool cmCTestVC::UpdateImpl()
  150. {
  151. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  152. "* Unknown VCS tool, not updating!" << std::endl);
  153. return true;
  154. }
  155. bool cmCTestVC::WriteXML(cmXMLWriter& xml)
  156. {
  157. this->Log << "--- Begin Revisions ---\n";
  158. bool result = this->WriteXMLUpdates(xml);
  159. this->Log << "--- End Revisions ---\n";
  160. return result;
  161. }
  162. bool cmCTestVC::WriteXMLUpdates(cmXMLWriter& /*unused*/)
  163. {
  164. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  165. "* CTest cannot extract updates for this VCS tool.\n");
  166. return true;
  167. }
  168. void cmCTestVC::WriteXMLEntry(cmXMLWriter& xml, std::string const& path,
  169. std::string const& name, std::string const& full,
  170. File const& f)
  171. {
  172. static const char* desc[3] = { "Updated", "Modified", "Conflicting" };
  173. Revision const& rev = f.Rev ? *f.Rev : this->Unknown;
  174. std::string prior = f.PriorRev ? f.PriorRev->Rev : std::string("Unknown");
  175. xml.StartElement(desc[f.Status]);
  176. xml.Element("File", name);
  177. xml.Element("Directory", path);
  178. xml.Element("FullName", full);
  179. xml.Element("CheckinDate", rev.Date);
  180. xml.Element("Author", rev.Author);
  181. xml.Element("Email", rev.EMail);
  182. xml.Element("Committer", rev.Committer);
  183. xml.Element("CommitterEmail", rev.CommitterEMail);
  184. xml.Element("CommitDate", rev.CommitDate);
  185. xml.Element("Log", rev.Log);
  186. xml.Element("Revision", rev.Rev);
  187. xml.Element("PriorRevision", prior);
  188. xml.EndElement();
  189. ++this->PathCount[f.Status];
  190. }