Created
January 16, 2016 06:00
-
-
Save python1981/ba0b7a3d473853187468 to your computer and use it in GitHub Desktop.
Show licenses for all packages in requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Usage: python show_licenses_for_requirements.py ./requirements.txt | |
# Setup: | |
# pip install requirements-parser | |
import sys | |
import pkg_resources | |
import requirements | |
def get_modules(requirements_file_path): | |
reqfile = open(requirements_file_path,'r') | |
reqs = [] | |
for req in requirements.parse(reqfile): | |
#reqs.name, reqs.specs, reqs.extras | |
reqs.append(req) | |
return reqs | |
def get_pkg_license(pkgname): | |
""" | |
Given a package reference (as from requirements.txt), | |
return license listed in package metadata. | |
NOTE: This function does no error checking and is for | |
demonstration purposes only. | |
""" | |
pkgs = pkg_resources.require(pkgname) | |
pkg = pkgs[0] | |
for line in pkg.get_metadata_lines('METADATA'): | |
(k, v) = line.split(': ', 1) | |
if k == "License": | |
return v | |
return None | |
requirements = get_modules(sys.argv[1]) | |
for index, requirement in enumerate(requirements): | |
try: | |
license = get_pkg_license(requirement.name) | |
except: | |
license = 'UNKNOWN' | |
print(requirement.name + ' ('+''.join(list(requirement.specs[0]))+')' + ': ' + license) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment