Checking for Python Package Updates at PyPI
I frequently find myself wondering if a bug in a Python package has been fixed and whether there is an upgrade for that package that might fix the bug. So I find that I end up running pip freeze
and then having to compare the package versions to those on PyPI manually. Well, anytime you say "run X manually", you're being a chump.
I just saw down and wrote a script to get the list of currently installed packages in the current environment (so it works with virtualenv). Then it checks to see what the latest version of the package is on PyPI and prints out the status. If you work with Python and packages, this is awesomesauce.
I added the script to my dotfiles on github.
#!/usr/bin/env python | |
import xmlrpclib | |
import pip | |
import argparse | |
from pkg_resources import parse_version | |
def version_number_compare(version1, version2): | |
return cmp(parse_version(version1), parse_version(version2)) | |
def print_status(package, message): | |
package_str = '{package.project_name} {package.version}'.format(package=package) | |
print '{package:40} {message}'.format(package=package_str, message=message) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('-a', '--all', dest='all', action='store_true', default=False) | |
parser.add_argument('-m', '--mirror', dest='mirror', default='http://pypi.python.org/pypi') | |
args = parser.parse_args() | |
if not args: | |
exit(-1) | |
pypi = xmlrpclib.ServerProxy(args.mirror) | |
for dist in pip.get_installed_distributions(): | |
available = pypi.package_releases(dist.project_name) | |
if not available: | |
# Try the capitalized package name | |
available = pypi.package_releases(dist.project_name.capitalize()) | |
upgrade_available = True | |
if not available: | |
print_status(dist, 'no releases at pypi') | |
continue | |
comparison = version_number_compare(available[0], dist.version) | |
if comparison == 0: | |
if not args.all: | |
continue | |
print_status(dist, 'up to date') | |
elif comparison < 0: | |
print_status(dist, 'older version on pypi') | |
else: | |
print_status(dist, '%s available' % available[0]) |
Assuming the script is at ~/bin/pyupgrades.py
, you can run ...