PageRenderTime 52ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/qtwebkit/Tools/Scripts/webkitpy/common/checkout/scm/scm.py

https://gitlab.com/x33n/phantomjs
Python | 249 lines | 144 code | 61 blank | 44 comment | 13 complexity | a0de91a8c3d67d8d1d803af994706c2b MD5 | raw file
  1. # Copyright (c) 2009, Google Inc. All rights reserved.
  2. # Copyright (c) 2009 Apple Inc. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #
  30. # Python module for interacting with an SCM system (like SVN or Git)
  31. import logging
  32. import re
  33. import sys
  34. from webkitpy.common.system.executive import Executive, ScriptError
  35. from webkitpy.common.system.filesystem import FileSystem
  36. _log = logging.getLogger(__name__)
  37. class CheckoutNeedsUpdate(ScriptError):
  38. def __init__(self, script_args, exit_code, output, cwd):
  39. ScriptError.__init__(self, script_args=script_args, exit_code=exit_code, output=output, cwd=cwd)
  40. # FIXME: Should be moved onto SCM
  41. def commit_error_handler(error):
  42. if re.search("resource out of date", error.output):
  43. raise CheckoutNeedsUpdate(script_args=error.script_args, exit_code=error.exit_code, output=error.output, cwd=error.cwd)
  44. Executive.default_error_handler(error)
  45. class AuthenticationError(Exception):
  46. def __init__(self, server_host, prompt_for_password=False):
  47. self.server_host = server_host
  48. self.prompt_for_password = prompt_for_password
  49. # SCM methods are expected to return paths relative to self.checkout_root.
  50. class SCM:
  51. def __init__(self, cwd, executive=None, filesystem=None):
  52. self.cwd = cwd
  53. self._executive = executive or Executive()
  54. self._filesystem = filesystem or FileSystem()
  55. self.checkout_root = self.find_checkout_root(self.cwd)
  56. # A wrapper used by subclasses to create processes.
  57. def run(self, args, cwd=None, input=None, error_handler=None, return_exit_code=False, return_stderr=True, decode_output=True):
  58. # FIXME: We should set cwd appropriately.
  59. return self._executive.run_command(args,
  60. cwd=cwd,
  61. input=input,
  62. error_handler=error_handler,
  63. return_exit_code=return_exit_code,
  64. return_stderr=return_stderr,
  65. decode_output=decode_output)
  66. # SCM always returns repository relative path, but sometimes we need
  67. # absolute paths to pass to rm, etc.
  68. def absolute_path(self, repository_relative_path):
  69. return self._filesystem.join(self.checkout_root, repository_relative_path)
  70. # FIXME: This belongs in Checkout, not SCM.
  71. def scripts_directory(self):
  72. return self._filesystem.join(self.checkout_root, "Tools", "Scripts")
  73. # FIXME: This belongs in Checkout, not SCM.
  74. def script_path(self, script_name):
  75. return self._filesystem.join(self.scripts_directory(), script_name)
  76. def run_status_and_extract_filenames(self, status_command, status_regexp):
  77. filenames = []
  78. # We run with cwd=self.checkout_root so that returned-paths are root-relative.
  79. for line in self.run(status_command, cwd=self.checkout_root).splitlines():
  80. match = re.search(status_regexp, line)
  81. if not match:
  82. continue
  83. # status = match.group('status')
  84. filename = match.group('filename')
  85. filenames.append(filename)
  86. return filenames
  87. def strip_r_from_svn_revision(self, svn_revision):
  88. match = re.match("^r(?P<svn_revision>\d+)", unicode(svn_revision))
  89. if (match):
  90. return match.group('svn_revision')
  91. return svn_revision
  92. def svn_revision_from_commit_text(self, commit_text):
  93. match = re.search(self.commit_success_regexp(), commit_text, re.MULTILINE)
  94. return match.group('svn_revision')
  95. @staticmethod
  96. def _subclass_must_implement():
  97. raise NotImplementedError("subclasses must implement")
  98. @classmethod
  99. def in_working_directory(cls, path, executive=None):
  100. SCM._subclass_must_implement()
  101. def find_checkout_root(self, path):
  102. SCM._subclass_must_implement()
  103. @staticmethod
  104. def commit_success_regexp():
  105. SCM._subclass_must_implement()
  106. def status_command(self):
  107. self._subclass_must_implement()
  108. def add(self, path):
  109. self.add_list([path])
  110. def add_list(self, paths):
  111. self._subclass_must_implement()
  112. def delete(self, path):
  113. self.delete_list([path])
  114. def delete_list(self, paths):
  115. self._subclass_must_implement()
  116. def exists(self, path):
  117. self._subclass_must_implement()
  118. def changed_files(self, git_commit=None):
  119. self._subclass_must_implement()
  120. def changed_files_for_revision(self, revision):
  121. self._subclass_must_implement()
  122. def revisions_changing_file(self, path, limit=5):
  123. self._subclass_must_implement()
  124. def added_files(self):
  125. self._subclass_must_implement()
  126. def conflicted_files(self):
  127. self._subclass_must_implement()
  128. def display_name(self):
  129. self._subclass_must_implement()
  130. def head_svn_revision(self):
  131. return self.svn_revision(self.checkout_root)
  132. def svn_revision(self, path):
  133. """Returns the latest svn revision found in the checkout."""
  134. self._subclass_must_implement()
  135. def timestamp_of_revision(self, path, revision):
  136. self._subclass_must_implement()
  137. def create_patch(self, git_commit=None, changed_files=None):
  138. self._subclass_must_implement()
  139. def committer_email_for_revision(self, revision):
  140. self._subclass_must_implement()
  141. def contents_at_revision(self, path, revision):
  142. self._subclass_must_implement()
  143. def diff_for_revision(self, revision):
  144. self._subclass_must_implement()
  145. def diff_for_file(self, path, log=None):
  146. self._subclass_must_implement()
  147. def show_head(self, path):
  148. self._subclass_must_implement()
  149. def apply_reverse_diff(self, revision):
  150. self._subclass_must_implement()
  151. def revert_files(self, file_paths):
  152. self._subclass_must_implement()
  153. def commit_with_message(self, message, username=None, password=None, git_commit=None, force_squash=False, changed_files=None):
  154. self._subclass_must_implement()
  155. def svn_commit_log(self, svn_revision):
  156. self._subclass_must_implement()
  157. def last_svn_commit_log(self):
  158. self._subclass_must_implement()
  159. def svn_blame(self, path):
  160. self._subclass_must_implement()
  161. def has_working_directory_changes(self):
  162. self._subclass_must_implement()
  163. def discard_working_directory_changes(self):
  164. self._subclass_must_implement()
  165. #--------------------------------------------------------------------------
  166. # Subclasses must indicate if they support local commits,
  167. # but the SCM baseclass will only call local_commits methods when this is true.
  168. @staticmethod
  169. def supports_local_commits():
  170. SCM._subclass_must_implement()
  171. def local_commits(self):
  172. return []
  173. def has_local_commits(self):
  174. return len(self.local_commits()) > 0
  175. def discard_local_commits(self):
  176. return
  177. def remote_merge_base(self):
  178. SCM._subclass_must_implement()
  179. def commit_locally_with_message(self, message):
  180. _log.error("Your source control manager does not support local commits.")
  181. sys.exit(1)
  182. def local_changes_exist(self):
  183. return (self.supports_local_commits() and self.has_local_commits()) or self.has_working_directory_changes()
  184. def discard_local_changes(self):
  185. if self.has_working_directory_changes():
  186. self.discard_working_directory_changes()
  187. if self.has_local_commits():
  188. self.discard_local_commits()