Want more features on Pastebin? Sign Up, it's FREE!
Guest

Pip - List all versions of a specific package

By: a guest on Feb 4th, 2011  |  syntax: Python  |  size: 6.37 KB  |  views: 797  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import posixpath
  2. import pkg_resources
  3. import sys
  4. from pip.download import url_to_path
  5. from pip.exceptions import DistributionNotFound
  6. from pip.index import PackageFinder, Link
  7. from pip.log import logger
  8. from pip.req import InstallRequirement
  9. from pip.util import Inf
  10.  
  11.  
  12. class MyPackageFinder(PackageFinder):
  13.    
  14.     def find_requirement(self, req, upgrade):
  15.         url_name = req.url_name
  16.         # Only check main index if index URL is given:
  17.         main_index_url = None
  18.         if self.index_urls:
  19.             # Check that we have the url_name correctly spelled:
  20.             main_index_url = Link(posixpath.join(self.index_urls[0], url_name))
  21.             # This will also cache the page, so it's okay that we get it again later:
  22.             page = self._get_page(main_index_url, req)
  23.             if page is None:
  24.                 url_name = self._find_url_name(Link(self.index_urls[0]), url_name, req) or req.url_name
  25.  
  26.         # Combine index URLs with mirror URLs here to allow
  27.         # adding more index URLs from requirements files
  28.         all_index_urls = self.index_urls + self.mirror_urls
  29.  
  30.         def mkurl_pypi_url(url):
  31.             loc = posixpath.join(url, url_name)
  32.             # For maximum compatibility with easy_install, ensure the path
  33.             # ends in a trailing slash.  Although this isn't in the spec
  34.             # (and PyPI can handle it without the slash) some other index
  35.             # implementations might break if they relied on easy_install's behavior.
  36.             if not loc.endswith('/'):
  37.                 loc = loc + '/'
  38.             return loc
  39.         if url_name is not None:
  40.             locations = [
  41.                 mkurl_pypi_url(url)
  42.                 for url in all_index_urls] + self.find_links
  43.         else:
  44.             locations = list(self.find_links)
  45.         locations.extend(self.dependency_links)
  46.         for version in req.absolute_versions:
  47.             if url_name is not None and main_index_url is not None:
  48.                 locations = [
  49.                     posixpath.join(main_index_url.url, version)] + locations
  50.  
  51.         file_locations, url_locations = self._sort_locations(locations)
  52.  
  53.         locations = [Link(url) for url in url_locations]
  54.         logger.debug('URLs to search for versions for %s:' % req)
  55.         for location in locations:
  56.             logger.debug('* %s' % location)
  57.         found_versions = []
  58.         found_versions.extend(
  59.             self._package_versions(
  60.                 [Link(url, '-f') for url in self.find_links], req.name.lower()))
  61.         page_versions = []
  62.         for page in self._get_pages(locations, req):
  63.             logger.debug('Analyzing links from page %s' % page.url)
  64.             logger.indent += 2
  65.             try:
  66.                 page_versions.extend(self._package_versions(page.links, req.name.lower()))
  67.             finally:
  68.                 logger.indent -= 2
  69.         dependency_versions = list(self._package_versions(
  70.             [Link(url) for url in self.dependency_links], req.name.lower()))
  71.         if dependency_versions:
  72.             logger.info('dependency_links found: %s' % ', '.join([link.url for parsed, link, version in dependency_versions]))
  73.         file_versions = list(self._package_versions(
  74.                 [Link(url) for url in file_locations], req.name.lower()))
  75.         if not found_versions and not page_versions and not dependency_versions and not file_versions:
  76.             logger.fatal('Could not find any downloads that satisfy the requirement %s' % req)
  77.             raise DistributionNotFound('No distributions at all found for %s' % req)
  78.         if req.satisfied_by is not None:
  79.             found_versions.append((req.satisfied_by.parsed_version, Inf, req.satisfied_by.version))
  80.         if file_versions:
  81.             file_versions.sort(reverse=True)
  82.             logger.info('Local files found: %s' % ', '.join([url_to_path(link.url) for parsed, link, version in file_versions]))
  83.             found_versions = file_versions + found_versions
  84.         all_versions = found_versions + page_versions + dependency_versions
  85.         applicable_versions = []
  86.         for (parsed_version, link, version) in all_versions:
  87.             if version not in req.req:
  88.                 logger.info("Ignoring link %s, version %s doesn't match %s"
  89.                             % (link, version, ','.join([''.join(s) for s in req.req.specs])))
  90.                 continue
  91.             applicable_versions.append((link, version))
  92.         applicable_versions = sorted(applicable_versions, key=lambda v: pkg_resources.parse_version(v[1]), reverse=True)
  93.         existing_applicable = bool([link for link, version in applicable_versions if link is Inf])
  94.         if not upgrade and existing_applicable:
  95.             if applicable_versions[0][1] is Inf:
  96.                 logger.info('Existing installed version (%s) is most up-to-date and satisfies requirement'
  97.                             % req.satisfied_by.version)
  98.             else:
  99.                 logger.info('Existing installed version (%s) satisfies requirement (most up-to-date version is %s)'
  100.                             % (req.satisfied_by.version, applicable_versions[0][1]))
  101.             return None
  102.         if not applicable_versions:
  103.             logger.fatal('Could not find a version that satisfies the requirement %s (from versions: %s)'
  104.                          % (req, ', '.join([version for parsed_version, link, version in found_versions])))
  105.             raise DistributionNotFound('No distributions matching the version for %s' % req)
  106.         if applicable_versions[0][0] is Inf:
  107.             # We have an existing version, and its the best version
  108.             logger.info('Installed version (%s) is most up-to-date (past versions: %s)'
  109.                         % (req.satisfied_by.version, ', '.join([version for link, version in applicable_versions[1:]]) or 'none'))
  110.             return None
  111.         if len(applicable_versions) > 1:
  112.             logger.info('Using version %s (newest of versions: %s)' %
  113.                         (applicable_versions[0][1], ', '.join([version for link, version in applicable_versions])))
  114.         return applicable_versions
  115.  
  116.  
  117. if __name__ == '__main__':
  118.     req = InstallRequirement.from_line(sys.argv[1], None)
  119.     finder = MyPackageFinder([], ['http://pypi.python.org/simple/'])
  120.     versions = finder.find_requirement(req, False)
  121.     print 'Versions of %s' % sys.argv[1]
  122.     for v in versions:
  123.         print v[1]
clone this paste RAW Paste Data