PageRenderTime 6830ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/site-packages/pip/_internal/vcs/subversion.py

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