cmCTestVC.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. cmCTestVC::cmCTestVC(cmCTest* ct, std::ostream& log)
  16. : CTest(ct)
  17. , Log(log)
  18. {
  19. this->PathCount[PathUpdated] = 0;
  20. this->PathCount[PathModified] = 0;
  21. this->PathCount[PathConflicting] = 0;
  22. this->Unknown.Date = "Unknown";
  23. this->Unknown.Author = "Unknown";
  24. this->Unknown.Rev = "Unknown";
  25. }
  26. cmCTestVC::~cmCTestVC()
  27. {
  28. }
  29. void cmCTestVC::SetCommandLineTool(std::string const& tool)
  30. {
  31. this->CommandLineTool = tool;
  32. }
  33. void cmCTestVC::SetSourceDirectory(std::string const& dir)
  34. {
  35. this->SourceDirectory = dir;
  36. }
  37. bool cmCTestVC::InitialCheckout(const char* command)
  38. {
  39. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  40. " First perform the initial checkout: " << command << "\n");
  41. // Make the parent directory in which to perform the checkout.
  42. std::string parent = cmSystemTools::GetFilenamePath(this->SourceDirectory);
  43. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  44. " Perform checkout in directory: " << parent << "\n");
  45. if (!cmSystemTools::MakeDirectory(parent.c_str())) {
  46. cmCTestLog(this->CTest, ERROR_MESSAGE,
  47. "Cannot create directory: " << parent << std::endl);
  48. return false;
  49. }
  50. // Construct the initial checkout command line.
  51. std::vector<std::string> args = cmSystemTools::ParseArguments(command);
  52. std::vector<char const*> vc_co;
  53. for (std::vector<std::string>::const_iterator ai = args.begin();
  54. ai != args.end(); ++ai) {
  55. vc_co.push_back(ai->c_str());
  56. }
  57. vc_co.push_back(CM_NULLPTR);
  58. // Run the initial checkout command and log its output.
  59. this->Log << "--- Begin Initial Checkout ---\n";
  60. OutputLogger out(this->Log, "co-out> ");
  61. OutputLogger err(this->Log, "co-err> ");
  62. bool result = this->RunChild(&vc_co[0], &out, &err, parent.c_str());
  63. this->Log << "--- End Initial Checkout ---\n";
  64. if (!result) {
  65. cmCTestLog(this->CTest, ERROR_MESSAGE, "Initial checkout failed!"
  66. << std::endl);
  67. }
  68. return result;
  69. }
  70. bool cmCTestVC::RunChild(char const* const* cmd, OutputParser* out,
  71. OutputParser* err, const char* workDir)
  72. {
  73. this->Log << this->ComputeCommandLine(cmd) << "\n";
  74. cmsysProcess* cp = cmsysProcess_New();
  75. cmsysProcess_SetCommand(cp, cmd);
  76. workDir = workDir ? workDir : this->SourceDirectory.c_str();
  77. cmsysProcess_SetWorkingDirectory(cp, workDir);
  78. this->RunProcess(cp, out, err);
  79. int result = cmsysProcess_GetExitValue(cp);
  80. cmsysProcess_Delete(cp);
  81. return result == 0;
  82. }
  83. std::string cmCTestVC::ComputeCommandLine(char const* const* cmd)
  84. {
  85. std::ostringstream line;
  86. const char* sep = "";
  87. for (const char* const* arg = cmd; *arg; ++arg) {
  88. line << sep << "\"" << *arg << "\"";
  89. sep = " ";
  90. }
  91. return line.str();
  92. }
  93. bool cmCTestVC::RunUpdateCommand(char const* const* cmd, OutputParser* out,
  94. OutputParser* err)
  95. {
  96. // Report the command line.
  97. this->UpdateCommandLine = this->ComputeCommandLine(cmd);
  98. if (this->CTest->GetShowOnly()) {
  99. this->Log << this->UpdateCommandLine << "\n";
  100. return true;
  101. }
  102. // Run the command.
  103. return this->RunChild(cmd, out, err);
  104. }
  105. std::string cmCTestVC::GetNightlyTime()
  106. {
  107. // Get the nightly start time corresponding to the current dau.
  108. struct tm* t = this->CTest->GetNightlyTime(
  109. this->CTest->GetCTestConfiguration("NightlyStartTime"),
  110. this->CTest->GetTomorrowTag());
  111. char current_time[1024];
  112. sprintf(current_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900,
  113. t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  114. return std::string(current_time);
  115. }
  116. void cmCTestVC::Cleanup()
  117. {
  118. this->Log << "--- Begin Cleanup ---\n";
  119. this->CleanupImpl();
  120. this->Log << "--- End Cleanup ---\n";
  121. }
  122. void cmCTestVC::CleanupImpl()
  123. {
  124. // We do no cleanup by default.
  125. }
  126. bool cmCTestVC::Update()
  127. {
  128. bool result = true;
  129. // if update version only is on then do not actually update,
  130. // just note the current version and finish
  131. if (!cmSystemTools::IsOn(
  132. this->CTest->GetCTestConfiguration("UpdateVersionOnly").c_str())) {
  133. this->NoteOldRevision();
  134. this->Log << "--- Begin Update ---\n";
  135. result = this->UpdateImpl();
  136. this->Log << "--- End Update ---\n";
  137. }
  138. this->NoteNewRevision();
  139. return result;
  140. }
  141. void cmCTestVC::NoteOldRevision()
  142. {
  143. // We do nothing by default.
  144. }
  145. void cmCTestVC::NoteNewRevision()
  146. {
  147. // We do nothing by default.
  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&)
  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. }