cmCTestVC.cxx 8.1 KB

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