You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

292 lines
10 KiB

  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. import re
  5. from pip._vendor.six.moves.urllib import parse as urllib_parse
  6. from pip.index import Link
  7. from pip.utils import rmtree, display_path
  8. from pip.utils.logging import indent_log
  9. from pip.vcs import vcs, VersionControl
  10. _svn_xml_url_re = re.compile('url="([^"]+)"')
  11. _svn_rev_re = re.compile('committed-rev="(\d+)"')
  12. _svn_url_re = re.compile(r'URL: (.+)')
  13. _svn_revision_re = re.compile(r'Revision: (.+)')
  14. _svn_info_xml_rev_re = re.compile(r'\s*revision="(\d+)"')
  15. _svn_info_xml_url_re = re.compile(r'<url>(.*)</url>')
  16. logger = logging.getLogger(__name__)
  17. class Subversion(VersionControl):
  18. name = 'svn'
  19. dirname = '.svn'
  20. repo_name = 'checkout'
  21. schemes = ('svn', 'svn+ssh', 'svn+http', 'svn+https', 'svn+svn')
  22. def get_info(self, location):
  23. """Returns (url, revision), where both are strings"""
  24. assert not location.rstrip('/').endswith(self.dirname), \
  25. 'Bad directory: %s' % location
  26. output = self.run_command(
  27. ['info', location],
  28. show_stdout=False,
  29. extra_environ={'LANG': 'C'},
  30. )
  31. match = _svn_url_re.search(output)
  32. if not match:
  33. logger.warning(
  34. 'Cannot determine URL of svn checkout %s',
  35. display_path(location),
  36. )
  37. logger.debug('Output that cannot be parsed: \n%s', output)
  38. return None, None
  39. url = match.group(1).strip()
  40. match = _svn_revision_re.search(output)
  41. if not match:
  42. logger.warning(
  43. 'Cannot determine revision of svn checkout %s',
  44. display_path(location),
  45. )
  46. logger.debug('Output that cannot be parsed: \n%s', output)
  47. return url, None
  48. return url, match.group(1)
  49. def export(self, location):
  50. """Export the svn repository at the url to the destination location"""
  51. url, rev = self.get_url_rev()
  52. rev_options = get_rev_options(url, rev)
  53. logger.info('Exporting svn repository %s to %s', url, location)
  54. with indent_log():
  55. if os.path.exists(location):
  56. # Subversion doesn't like to check out over an existing
  57. # directory --force fixes this, but was only added in svn 1.5
  58. rmtree(location)
  59. self.run_command(
  60. ['export'] + rev_options + [url, location],
  61. show_stdout=False)
  62. def switch(self, dest, url, rev_options):
  63. self.run_command(['switch'] + rev_options + [url, dest])
  64. def update(self, dest, rev_options):
  65. self.run_command(['update'] + rev_options + [dest])
  66. def obtain(self, dest):
  67. url, rev = self.get_url_rev()
  68. rev_options = get_rev_options(url, rev)
  69. if rev:
  70. rev_display = ' (to revision %s)' % rev
  71. else:
  72. rev_display = ''
  73. if self.check_destination(dest, url, rev_options, rev_display):
  74. logger.info(
  75. 'Checking out %s%s to %s',
  76. url,
  77. rev_display,
  78. display_path(dest),
  79. )
  80. self.run_command(['checkout', '-q'] + rev_options + [url, dest])
  81. def get_location(self, dist, dependency_links):
  82. for url in dependency_links:
  83. egg_fragment = Link(url).egg_fragment
  84. if not egg_fragment:
  85. continue
  86. if '-' in egg_fragment:
  87. # FIXME: will this work when a package has - in the name?
  88. key = '-'.join(egg_fragment.split('-')[:-1]).lower()
  89. else:
  90. key = egg_fragment
  91. if key == dist.key:
  92. return url.split('#', 1)[0]
  93. return None
  94. def get_revision(self, location):
  95. """
  96. Return the maximum revision for all files under a given location
  97. """
  98. # Note: taken from setuptools.command.egg_info
  99. revision = 0
  100. for base, dirs, files in os.walk(location):
  101. if self.dirname not in dirs:
  102. dirs[:] = []
  103. continue # no sense walking uncontrolled subdirs
  104. dirs.remove(self.dirname)
  105. entries_fn = os.path.join(base, self.dirname, 'entries')
  106. if not os.path.exists(entries_fn):
  107. # FIXME: should we warn?
  108. continue
  109. dirurl, localrev = self._get_svn_url_rev(base)
  110. if base == location:
  111. base_url = dirurl + '/' # save the root url
  112. elif not dirurl or not dirurl.startswith(base_url):
  113. dirs[:] = []
  114. continue # not part of the same svn tree, skip it
  115. revision = max(revision, localrev)
  116. return revision
  117. def get_url_rev(self):
  118. # hotfix the URL scheme after removing svn+ from svn+ssh:// readd it
  119. url, rev = super(Subversion, self).get_url_rev()
  120. if url.startswith('ssh://'):
  121. url = 'svn+' + url
  122. return url, rev
  123. def get_url(self, location):
  124. # In cases where the source is in a subdirectory, not alongside
  125. # setup.py we have to look up in the location until we find a real
  126. # setup.py
  127. orig_location = location
  128. while not os.path.exists(os.path.join(location, 'setup.py')):
  129. last_location = location
  130. location = os.path.dirname(location)
  131. if location == last_location:
  132. # We've traversed up to the root of the filesystem without
  133. # finding setup.py
  134. logger.warning(
  135. "Could not find setup.py for directory %s (tried all "
  136. "parent directories)",
  137. orig_location,
  138. )
  139. return None
  140. return self._get_svn_url_rev(location)[0]
  141. def _get_svn_url_rev(self, location):
  142. from pip.exceptions import InstallationError
  143. with open(os.path.join(location, self.dirname, 'entries')) as f:
  144. data = f.read()
  145. if (data.startswith('8') or
  146. data.startswith('9') or
  147. data.startswith('10')):
  148. data = list(map(str.splitlines, data.split('\n\x0c\n')))
  149. del data[0][0] # get rid of the '8'
  150. url = data[0][3]
  151. revs = [int(d[9]) for d in data if len(d) > 9 and d[9]] + [0]
  152. elif data.startswith('<?xml'):
  153. match = _svn_xml_url_re.search(data)
  154. if not match:
  155. raise ValueError('Badly formatted data: %r' % data)
  156. url = match.group(1) # get repository URL
  157. revs = [int(m.group(1)) for m in _svn_rev_re.finditer(data)] + [0]
  158. else:
  159. try:
  160. # subversion >= 1.7
  161. xml = self.run_command(
  162. ['info', '--xml', location],
  163. show_stdout=False,
  164. )
  165. url = _svn_info_xml_url_re.search(xml).group(1)
  166. revs = [
  167. int(m.group(1)) for m in _svn_info_xml_rev_re.finditer(xml)
  168. ]
  169. except InstallationError:
  170. url, revs = None, []
  171. if revs:
  172. rev = max(revs)
  173. else:
  174. rev = 0
  175. return url, rev
  176. def get_tag_revs(self, svn_tag_url):
  177. stdout = self.run_command(['ls', '-v', svn_tag_url], show_stdout=False)
  178. results = []
  179. for line in stdout.splitlines():
  180. parts = line.split()
  181. rev = int(parts[0])
  182. tag = parts[-1].strip('/')
  183. results.append((tag, rev))
  184. return results
  185. def find_tag_match(self, rev, tag_revs):
  186. best_match_rev = None
  187. best_tag = None
  188. for tag, tag_rev in tag_revs:
  189. if (tag_rev > rev and
  190. (best_match_rev is None or best_match_rev > tag_rev)):
  191. # FIXME: Is best_match > tag_rev really possible?
  192. # or is it a sign something is wacky?
  193. best_match_rev = tag_rev
  194. best_tag = tag
  195. return best_tag
  196. def get_src_requirement(self, dist, location, find_tags=False):
  197. repo = self.get_url(location)
  198. if repo is None:
  199. return None
  200. parts = repo.split('/')
  201. # FIXME: why not project name?
  202. egg_project_name = dist.egg_name().split('-', 1)[0]
  203. rev = self.get_revision(location)
  204. if parts[-2] in ('tags', 'tag'):
  205. # It's a tag, perfect!
  206. full_egg_name = '%s-%s' % (egg_project_name, parts[-1])
  207. elif parts[-2] in ('branches', 'branch'):
  208. # It's a branch :(
  209. full_egg_name = '%s-%s-r%s' % (dist.egg_name(), parts[-1], rev)
  210. elif parts[-1] == 'trunk':
  211. # Trunk :-/
  212. full_egg_name = '%s-dev_r%s' % (dist.egg_name(), rev)
  213. if find_tags:
  214. tag_url = '/'.join(parts[:-1]) + '/tags'
  215. tag_revs = self.get_tag_revs(tag_url)
  216. match = self.find_tag_match(rev, tag_revs)
  217. if match:
  218. logger.info(
  219. 'trunk checkout %s seems to be equivalent to tag %s',
  220. match,
  221. )
  222. repo = '%s/%s' % (tag_url, match)
  223. full_egg_name = '%s-%s' % (egg_project_name, match)
  224. else:
  225. # Don't know what it is
  226. logger.warning(
  227. 'svn URL does not fit normal structure (tags/branches/trunk): '
  228. '%s',
  229. repo,
  230. )
  231. full_egg_name = '%s-dev_r%s' % (egg_project_name, rev)
  232. return 'svn+%s@%s#egg=%s' % (repo, rev, full_egg_name)
  233. def get_rev_options(url, rev):
  234. if rev:
  235. rev_options = ['-r', rev]
  236. else:
  237. rev_options = []
  238. r = urllib_parse.urlsplit(url)
  239. if hasattr(r, 'username'):
  240. # >= Python-2.5
  241. username, password = r.username, r.password
  242. else:
  243. netloc = r[1]
  244. if '@' in netloc:
  245. auth = netloc.split('@')[0]
  246. if ':' in auth:
  247. username, password = auth.split(':', 1)
  248. else:
  249. username, password = auth, None
  250. else:
  251. username, password = None, None
  252. if username:
  253. rev_options += ['--username', username]
  254. if password:
  255. rev_options += ['--password', password]
  256. return rev_options
  257. vcs.register(Subversion)