我试图审计一个具有大量依赖项的Python项目,虽然我可以手动查找每个项目的主页/许可条款,但似乎大多数OSS包都应该已经在其元数据中包含许可证名称和版本。
不幸的是,我在pip或easy_install中找不到比软件包名和安装版本更多的选项(通过pip冻结)。
有人有指向工具的指针来列出Python包的许可元数据吗?
发布于 2019-04-18 21:33:33
根据pip show -v的输出,每个包的许可信息有两个可能的位置。
下面是一些示例:
$ pip show django -v | grep -i license
License: BSD
  License :: OSI Approved :: BSD License
$ pip show setuptools -v | grep -i license
License: UNKNOWN
  License :: OSI Approved :: MIT License
$ pip show python-dateutil -v | grep -i license
License: Dual License
  License :: OSI Approved :: BSD License
  License :: OSI Approved :: Apache Software License
$ pip show ipdb -v | grep -i license
License: BSD下面的代码返回一个迭代器,它包含包的所有可能的许可证,使用setuptools
from itertools import chain, compress
from pkg_resources import get_distribution
def filters(line):
    return compress(
        (line[9:], line[39:]),
        (line.startswith('License:'), line.startswith('Classifier: License')),
    )
def get_pkg_license(pkg):
    distribution = get_distribution(pkg)
    try:
        lines = distribution.get_metadata_lines('METADATA')
    except OSError:
        lines = distribution.get_metadata_lines('PKG-INFO')
    return tuple(chain.from_iterable(map(filters, lines)))以下是研究结果:
>>> tuple(get_pkg_license(get_distribution('django')))
('BSD', 'BSD License')
>>> tuple(get_pkg_license(get_distribution('setuptools')))
('UNKNOWN', 'MIT License')
>>> tuple(get_pkg_license(get_distribution('python-dateutil')))
('Dual License', 'BSD License', 'Apache Software License')
>>> tuple(get_pkg_license(get_distribution('ipdb')))
('BSD',)最后,要从已安装的应用程序获得所有许可证:
>>> {
        p.project_name: get_pkg_license(p) 
        for p in pkg_resources.working_set
    } https://stackoverflow.com/questions/19086030
复制相似问题