Python Easy_Install更新报告

时间:2011-03-04 15:30:09

标签: python easy-install

是否有一种简单的方法可以获得通过easy_install安装的所有Python库的报告,这些库具有更新版本?我不想简单地在已知安装的库列表上重新运行easy_install,因为较新的库可能具有非向后兼容的更改。我想获得一个列表,以便我快速查看更改内容,并检查新版本以审查任何可能存在冲突的更改。

1 个答案:

答案 0 :(得分:5)

这是一个快速脚本,用于扫描easy-install.pth文件并打印已安装软件包的较新版本列表。您可以自定义它以仅显示可用的最新版本(取最大parsed_version),调整输出格式等:

#!/usr/bin/env python
import os, sys
from distutils import sysconfig
from pkg_resources import Requirement
from setuptools.package_index import PackageIndex

index = PackageIndex()
root = sysconfig.get_python_lib()
path = os.path.join(root, 'easy-install.pth')
if not os.path.exists(path):
    sys.exit(1)
for line in open(path, 'rb'):
    if line.startswith('import sys'):
        continue
    path = os.path.join(root, line.strip(), 'EGG-INFO', 'PKG-INFO')
    if not os.path.exists(path):
        continue
    lines = [r.split(':', 1) for r in open(path, 'rb').readlines() if ':' in r]
    info = dict((k.strip(), v.strip()) for k, v in lines)
    print '%s %s updates..' % (info['Name'], info['Version'])
    spec = Requirement.parse(info['Name'] + '>' + info['Version'])
    index.find_packages(spec)
    versions = set([
        (d.parsed_version, d.version) for d in index[spec.key] if d in spec
        ])
    if versions:
        for _, version in sorted(versions):
            print '\t', version
    else:
        print '\tnone'

用法:

% easy_install networkx==1.3
% easy_install gdata==2.0.5
% ./pkgreport
networkx 1.3 updates..
        1.4rc1
        1.4
gdata 2.0.5 updates..
        2.0.6
        2.0.7
        2.0.8
        2.0.9
        2.0.14