Skip to content

Instantly share code, notes, and snippets.

@Bjwebb
Last active September 25, 2017 10:12
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 Bjwebb/054f585501198f4c9feb292c1ebae97a to your computer and use it in GitHub Desktop.
Save Bjwebb/054f585501198f4c9feb292c1ebae97a to your computer and use it in GitHub Desktop.
import pkg_resources
# https://stackoverflow.com/questions/19086030/can-pip-or-setuptools-distribute-etc-list-the-license-used-by-each-install
def get_pkg_licenses(pkg):
try:
lines = pkg.get_metadata_lines('METADATA')
except (KeyError, IOError):
lines = pkg.get_metadata_lines('PKG-INFO')
license = None
license_classifiers = []
for line in lines:
if not license and ': ' in line:
(k, v) = line.split(': ', 1)
if k == 'License':
license = v
if ':: ' in line:
(k, v) = line.split(':: ', 1)
if k == 'Classifier: License ':
license_classifiers.append(v)
if len(license_classifiers) == 1 and license_classifiers[0] in [
'OSI Approved :: Apache Software License',
'OSI Approved :: GNU General Public License (GPL)',
]:
return [license]
return license_classifiers or [license]
for pkg_name, pkg in pkg_resources.working_set.by_key.items():
licenses = get_pkg_licenses(pkg)
# TODO deal with multiple classifiers properly...
if all(license in [
'OSI Approved :: BSD License',
'OSI Approved :: MIT License',
'OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
'OSI Approved :: GNU General Public License v3 (GPLv3)',
'OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)',
'OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
'OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)',
'OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)',
'OSI Approved :: GNU Affero General Public License v3',
'OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
'OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)',
'OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
'OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)',
'OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)',
'OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
'OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
'OSI Approved :: Python Software Foundation License',
'Apache License 2.0',
'Apache License, Version 2.0',
'Apache 2.0',
'BSD',
'MIT',
'MPL-2.0',
'Public Domain',
] for license in licenses):
continue
print(pkg_name, licenses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment