Skip to content

Instantly share code, notes, and snippets.

@alistair-broomhead
Created February 11, 2015 12:46
Show Gist options
  • Save alistair-broomhead/96bd7534c561b103cad1 to your computer and use it in GitHub Desktop.
Save alistair-broomhead/96bd7534c561b103cad1 to your computer and use it in GitHub Desktop.
Written by fgh@4gh.tv
import asyncio
import aiohttp
import json
from pip.utils import get_installed_distributions
#from pip.commands.search import compare_versions
from distutils.version import StrictVersion, LooseVersion
def package_url(name):
return "http://pypi.python.org/pypi/%s/json/" % name
sem = asyncio.Semaphore(5)
@asyncio.coroutine
def get_package_latest(name):
url = package_url(name)
failed = False
with (yield from sem):
response = yield from aiohttp.request('GET', url)
try:
body = yield from response.read_and_close(decode=True)
latest = body['info']['version']
except:
failed = True
latest = "0.0.0"
return [name, latest, failed]
def compare_versions(version1, version2):
try:
cmpv = StrictVersion(version1) > StrictVersion(version2)
if cmpv:
cmpv = 1
else:
if StrictVersion(version1) == StrictVersion(version2):
cmpv = 0
else:
cmpv = -1
# in case of abnormal version number, fall back to LooseVersion
except ValueError:
cmpv = LooseVersion(version1) > LooseVersion(version2)
if cmpv:
cmpv = 1
else:
if LooseVersion(version1) == LooseVersion(version2):
cmpv = 0
else:
cmpv = -1
return cmpv
packages=get_installed_distributions()
installed_packages=[]
installed_packages_info={}
for package in packages:
loc = package.location
name = package.key
version = package.version
installed_packages.append(name)
installed_packages_info[name]={'version':version, 'location':loc}
loop = asyncio.get_event_loop()
fs = asyncio.wait([get_package_latest(p) for p in installed_packages])
results = loop.run_until_complete(fs)
for r in list(results[0]):
name, latest, failed = r.result()
if failed:
print("failed to find package information for {}".format(name))
continue
version = installed_packages_info[name]['version']
print("name: {}. latest: {}. installed: {}".format(name, latest, version))
verc = compare_versions(version, latest)
if verc == 0:
print(" latest version is installed")
elif verc > 0:
print(" installed version is NEWER than released!")
else:
print(" can update from version {} to released version {}".format(version, latest))
print(" use:")
print(" pip install --upgrade {}".format(name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment