2012年4月7日星期六

查詢已安裝的組件的更新

即是query latest Python package update,如下:

from pkg_resources import parse_version
import pip.util

if __name__ == '__main__':
    try:
        import xmlrpclib as client
    except ImportError:
        from xmlrpc import client
    c = client.ServerProxy('http://pypi.python.org/pypi')
    print('{:40} {:15} {:15} {}'.format('Package', 'Installed', 'Latest', 'Upgrade'))
    l = pip.util.get_installed_distributions()
    for i in sorted((i.project_name, i.version) for i in l):
        try:
            latest_ver = c.package_releases(i[0])[0]
            install_ver = i[1]
            print('{2:40} {3:15} {0:15} {1}'.format(latest_ver, 'True' if
                parse_version(install_ver) < parse_version(latest_ver) else '', *i))
        except IndexError:
            print('{:40} {} '.format(*i))

沒有留言:

發佈留言