Advertisement
furas

Python - pip - get all available versions

Mar 25th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4. from pip.basecommand import Command
  5. from pip.index import PackageFinder
  6.  
  7. class AllVersionsCommand(Command):
  8.  
  9.     name = 'all_versions'
  10.  
  11.     def run(self, package_name):
  12.  
  13.         # PackageFinder requires session which requires options
  14.  
  15.         options, args = self.parse_args([])
  16.         session = self._build_session(options=options)
  17.        
  18.         finder = PackageFinder(
  19.             find_links=[],
  20.             index_urls=["https://pypi.python.org/simple/"],
  21.             session=session,
  22.         )
  23.        
  24.         candidates = finder.find_all_candidates(package_name)
  25.        
  26.         # set() to remove repeated versions - ie. matplotlib
  27.         versions = sorted(set(c.version for c in candidates))
  28.        
  29.         print('\n'.join(map(str, versions)))
  30.  
  31. if __name__ == '__main__':
  32.     AllVersionsCommand().run(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement