Skip to content

Instantly share code, notes, and snippets.

@kk6
Created August 18, 2011 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kk6/1153625 to your computer and use it in GitHub Desktop.
Save kk6/1153625 to your computer and use it in GitHub Desktop.
gist: 1153590 のPython 2.5以下用
#-*- coding:utf-8 -*-
"""
This script compare a package installed by pip and the latest version
in PyPI. And it is also possible to upgrade it then and there.
For now work on Windows and Linux.
And I'm not MACer.(Probably run.)
"""
from pip import get_installed_distributions, call_subprocess
from subprocess import call
from string import capitalize
import xmlrpclib
def check_versions(pypi_ver, installed):
if installed.has_version():
print "Checking: ** %s **" % installed.project_name
print "IN PyPI: " + str(pypi_ver[0])
print "INSTALLED: " + installed.version
if pypi_ver[0] == installed.version:
print 'Using latest version.\n'
else:
print "Versions don't match.\n"
pip_upgrade(installed)
else:
print "%s doesn't have version.\n" % installed.project_name
def pip_upgrade(dist):
qstring1 = 'Do you want to upgrade "%s"?: (y/n)'
qstring2 = 'Really? (y/n):'
upgrade_string = 'pip install --upgrade %s'
ans1 = raw_input(qstring1 % dist.project_name)
if ans1 == 'y':
ans2 = raw_input(qstring2)
if ans2 == 'y':
try:
# Windows
call_subprocess(upgrade_string % dist.project_name)
except OSError:
# Linux
call(upgrade_string % dist.project_name, shell=True)
finally:
print 'Done.\n'
else:
print "Upgrading Canceled: %s.\n" % dist.project_name
else:
print "Upgrading Canceled: %s.\n" % dist.project_name
def main():
client = xmlrpclib.ServerProxy('http://pypi.python.org/pypi')
for installed in get_installed_distributions():
pypi_ver = client.package_releases(installed.project_name)
if len(pypi_ver) > 0:
check_versions(pypi_ver, installed)
else:
capitalized = capitalize(installed.project_name)
pypi_ver = client.package_releases(capitalized)
if pypi_ver:
check_versions(pypi_ver, installed)
else:
print '"%s" is not found in PyPI\n' % \
installed.project_name
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment