cmCTestVC.cxx 6.4 KB

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