subversion.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  4. import logging
  5. import os
  6. import re
  7. from pip._internal.utils.logging import indent_log
  8. from pip._internal.utils.misc import (
  9. display_path,
  10. is_console_interactive,
  11. rmtree,
  12. split_auth_from_netloc,
  13. )
  14. from pip._internal.utils.subprocess import make_command
  15. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  16. from pip._internal.vcs.versioncontrol import VersionControl, vcs
  17. _svn_xml_url_re = re.compile('url="([^"]+)"')
  18. _svn_rev_re = re.compile(r'committed-rev="(\d+)"')
  19. _svn_info_xml_rev_re = re.compile(r'\s*revision="(\d+)"')
  20. _svn_info_xml_url_re = re.compile(r'<url>(.*)</url>')
  21. if MYPY_CHECK_RUNNING:
  22. from typing import Optional, Tuple
  23. from pip._internal.utils.subprocess import CommandArgs
  24. from pip._internal.utils.misc import HiddenText
  25. from pip._internal.vcs.versioncontrol import AuthInfo, RevOptions
  26. logger = logging.getLogger(__name__)
  27. class Subversion(VersionControl):
  28. name = 'svn'
  29. dirname = '.svn'
  30. repo_name = 'checkout'
  31. schemes = ('svn', 'svn+ssh', 'svn+http', 'svn+https', 'svn+svn')
  32. @classmethod
  33. def should_add_vcs_url_prefix(cls, remote_url):
  34. return True
  35. @staticmethod
  36. def get_base_rev_args(rev):
  37. return ['-r', rev]
  38. @classmethod
  39. def get_revision(cls, location):
  40. """
  41. Return the maximum revision for all files under a given location
  42. """
  43. # Note: taken from setuptools.command.egg_info
  44. revision = 0
  45. for base, dirs, files in os.walk(location):
  46. if cls.dirname not in dirs:
  47. dirs[:] = []
  48. continue # no sense walking uncontrolled subdirs
  49. dirs.remove(cls.dirname)
  50. entries_fn = os.path.join(base, cls.dirname, 'entries')
  51. if not os.path.exists(entries_fn):
  52. # FIXME: should we warn?
  53. continue
  54. dirurl, localrev = cls._get_svn_url_rev(base)
  55. if base == location:
  56. base = dirurl + '/' # save the root url
  57. elif not dirurl or not dirurl.startswith(base):
  58. dirs[:] = []
  59. continue # not part of the same svn tree, skip it
  60. revision = max(revision, localrev)
  61. return revision
  62. @classmethod
  63. def get_netloc_and_auth(cls, netloc, scheme):
  64. """
  65. This override allows the auth information to be passed to svn via the
  66. --username and --password options instead of via the URL.
  67. """
  68. if scheme == 'ssh':
  69. # The --username and --password options can't be used for
  70. # svn+ssh URLs, so keep the auth information in the URL.
  71. return super(Subversion, cls).get_netloc_and_auth(netloc, scheme)
  72. return split_auth_from_netloc(netloc)
  73. @classmethod
  74. def get_url_rev_and_auth(cls, url):
  75. # type: (str) -> Tuple[str, Optional[str], AuthInfo]
  76. # hotfix the URL scheme after removing svn+ from svn+ssh:// readd it
  77. url, rev, user_pass = super(Subversion, cls).get_url_rev_and_auth(url)
  78. if url.startswith('ssh://'):
  79. url = 'svn+' + url
  80. return url, rev, user_pass
  81. @staticmethod
  82. def make_rev_args(username, password):
  83. # type: (Optional[str], Optional[HiddenText]) -> CommandArgs
  84. extra_args = [] # type: CommandArgs
  85. if username:
  86. extra_args += ['--username', username]
  87. if password:
  88. extra_args += ['--password', password]
  89. return extra_args
  90. @classmethod
  91. def get_remote_url(cls, location):
  92. # In cases where the source is in a subdirectory, not alongside
  93. # setup.py we have to look up in the location until we find a real
  94. # setup.py
  95. orig_location = location
  96. while not os.path.exists(os.path.join(location, 'setup.py')):
  97. last_location = location
  98. location = os.path.dirname(location)
  99. if location == last_location:
  100. # We've traversed up to the root of the filesystem without
  101. # finding setup.py
  102. logger.warning(
  103. "Could not find setup.py for directory %s (tried all "
  104. "parent directories)",
  105. orig_location,
  106. )
  107. return None
  108. return cls._get_svn_url_rev(location)[0]
  109. @classmethod
  110. def _get_svn_url_rev(cls, location):
  111. from pip._internal.exceptions import InstallationError
  112. entries_path = os.path.join(location, cls.dirname, 'entries')
  113. if os.path.exists(entries_path):
  114. with open(entries_path) as f:
  115. data = f.read()
  116. else: # subversion >= 1.7 does not have the 'entries' file
  117. data = ''
  118. if (data.startswith('8') or
  119. data.startswith('9') or
  120. data.startswith('10')):
  121. data = list(map(str.splitlines, data.split('\n\x0c\n')))
  122. del data[0][0] # get rid of the '8'
  123. url = data[0][3]
  124. revs = [int(d[9]) for d in data if len(d) > 9 and d[9]] + [0]
  125. elif data.startswith('<?xml'):
  126. match = _svn_xml_url_re.search(data)
  127. if not match:
  128. raise ValueError(
  129. 'Badly formatted data: {data!r}'.format(**locals()))
  130. url = match.group(1) # get repository URL
  131. revs = [int(m.group(1)) for m in _svn_rev_re.finditer(data)] + [0]
  132. else:
  133. try:
  134. # subversion >= 1.7
  135. # Note that using get_remote_call_options is not necessary here
  136. # because `svn info` is being run against a local directory.
  137. # We don't need to worry about making sure interactive mode
  138. # is being used to prompt for passwords, because passwords
  139. # are only potentially needed for remote server requests.
  140. xml = cls.run_command(
  141. ['info', '--xml', location],
  142. show_stdout=False,
  143. )
  144. url = _svn_info_xml_url_re.search(xml).group(1)
  145. revs = [
  146. int(m.group(1)) for m in _svn_info_xml_rev_re.finditer(xml)
  147. ]
  148. except InstallationError:
  149. url, revs = None, []
  150. if revs:
  151. rev = max(revs)
  152. else:
  153. rev = 0
  154. return url, rev
  155. @classmethod
  156. def is_commit_id_equal(cls, dest, name):
  157. """Always assume the versions don't match"""
  158. return False
  159. def __init__(self, use_interactive=None):
  160. # type: (bool) -> None
  161. if use_interactive is None:
  162. use_interactive = is_console_interactive()
  163. self.use_interactive = use_interactive
  164. # This member is used to cache the fetched version of the current
  165. # ``svn`` client.
  166. # Special value definitions:
  167. # None: Not evaluated yet.
  168. # Empty tuple: Could not parse version.
  169. self._vcs_version = None # type: Optional[Tuple[int, ...]]
  170. super(Subversion, self).__init__()
  171. def call_vcs_version(self):
  172. # type: () -> Tuple[int, ...]
  173. """Query the version of the currently installed Subversion client.
  174. :return: A tuple containing the parts of the version information or
  175. ``()`` if the version returned from ``svn`` could not be parsed.
  176. :raises: BadCommand: If ``svn`` is not installed.
  177. """
  178. # Example versions:
  179. # svn, version 1.10.3 (r1842928)
  180. # compiled Feb 25 2019, 14:20:39 on x86_64-apple-darwin17.0.0
  181. # svn, version 1.7.14 (r1542130)
  182. # compiled Mar 28 2018, 08:49:13 on x86_64-pc-linux-gnu
  183. version_prefix = 'svn, version '
  184. version = self.run_command(['--version'], show_stdout=False)
  185. if not version.startswith(version_prefix):
  186. return ()
  187. version = version[len(version_prefix):].split()[0]
  188. version_list = version.split('.')
  189. try:
  190. parsed_version = tuple(map(int, version_list))
  191. except ValueError:
  192. return ()
  193. return parsed_version
  194. def get_vcs_version(self):
  195. # type: () -> Tuple[int, ...]
  196. """Return the version of the currently installed Subversion client.
  197. If the version of the Subversion client has already been queried,
  198. a cached value will be used.
  199. :return: A tuple containing the parts of the version information or
  200. ``()`` if the version returned from ``svn`` could not be parsed.
  201. :raises: BadCommand: If ``svn`` is not installed.
  202. """
  203. if self._vcs_version is not None:
  204. # Use cached version, if available.
  205. # If parsing the version failed previously (empty tuple),
  206. # do not attempt to parse it again.
  207. return self._vcs_version
  208. vcs_version = self.call_vcs_version()
  209. self._vcs_version = vcs_version
  210. return vcs_version
  211. def get_remote_call_options(self):
  212. # type: () -> CommandArgs
  213. """Return options to be used on calls to Subversion that contact the server.
  214. These options are applicable for the following ``svn`` subcommands used
  215. in this class.
  216. - checkout
  217. - export
  218. - switch
  219. - update
  220. :return: A list of command line arguments to pass to ``svn``.
  221. """
  222. if not self.use_interactive:
  223. # --non-interactive switch is available since Subversion 0.14.4.
  224. # Subversion < 1.8 runs in interactive mode by default.
  225. return ['--non-interactive']
  226. svn_version = self.get_vcs_version()
  227. # By default, Subversion >= 1.8 runs in non-interactive mode if
  228. # stdin is not a TTY. Since that is how pip invokes SVN, in
  229. # call_subprocess(), pip must pass --force-interactive to ensure
  230. # the user can be prompted for a password, if required.
  231. # SVN added the --force-interactive option in SVN 1.8. Since
  232. # e.g. RHEL/CentOS 7, which is supported until 2024, ships with
  233. # SVN 1.7, pip should continue to support SVN 1.7. Therefore, pip
  234. # can't safely add the option if the SVN version is < 1.8 (or unknown).
  235. if svn_version >= (1, 8):
  236. return ['--force-interactive']
  237. return []
  238. def export(self, location, url):
  239. # type: (str, HiddenText) -> None
  240. """Export the svn repository at the url to the destination location"""
  241. url, rev_options = self.get_url_rev_options(url)
  242. logger.info('Exporting svn repository %s to %s', url, location)
  243. with indent_log():
  244. if os.path.exists(location):
  245. # Subversion doesn't like to check out over an existing
  246. # directory --force fixes this, but was only added in svn 1.5
  247. rmtree(location)
  248. cmd_args = make_command(
  249. 'export', self.get_remote_call_options(),
  250. rev_options.to_args(), url, location,
  251. )
  252. self.run_command(cmd_args, show_stdout=False)
  253. def fetch_new(self, dest, url, rev_options):
  254. # type: (str, HiddenText, RevOptions) -> None
  255. rev_display = rev_options.to_display()
  256. logger.info(
  257. 'Checking out %s%s to %s',
  258. url,
  259. rev_display,
  260. display_path(dest),
  261. )
  262. cmd_args = make_command(
  263. 'checkout', '-q', self.get_remote_call_options(),
  264. rev_options.to_args(), url, dest,
  265. )
  266. self.run_command(cmd_args)
  267. def switch(self, dest, url, rev_options):
  268. # type: (str, HiddenText, RevOptions) -> None
  269. cmd_args = make_command(
  270. 'switch', self.get_remote_call_options(), rev_options.to_args(),
  271. url, dest,
  272. )
  273. self.run_command(cmd_args)
  274. def update(self, dest, url, rev_options):
  275. # type: (str, HiddenText, RevOptions) -> None
  276. cmd_args = make_command(
  277. 'update', self.get_remote_call_options(), rev_options.to_args(),
  278. dest,
  279. )
  280. self.run_command(cmd_args)
  281. vcs.register(Subversion)