Last active
April 11, 2017 10:02
-
-
Save mmmeri/3b71dbebcf18a7e5266ea78828dc530b to your computer and use it in GitHub Desktop.
python code that allows to get a list of all versions of a package that are available on PyPi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contextlib | |
import pip | |
import re | |
@contextlib.contextmanager | |
def capture(): | |
import sys | |
from io import StringIO | |
oldout,olderr = sys.stdout, sys.stderr | |
try: | |
out=[StringIO(), StringIO()] | |
sys.stdout,sys.stderr = out | |
yield out | |
finally: | |
sys.stdout,sys.stderr = oldout, olderr | |
out[0] = out[0].getvalue() | |
out[1] = out[1].getvalue() | |
def all_versions_of_package(package_name): | |
with capture() as out: | |
pip.main(['install', '{}=='.format(package_name)]) | |
_, err_msg = out | |
dirty_msg, *_ = err_msg.split('\n') | |
versions = re.findall(r'\d+\.\d+\.\d+', dirty_msg) | |
return versions | |
print(all_versions_of_package('django')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment