Created
July 5, 2018 16:53
-
-
Save b-ryan/53858f83118761af9c22c69a6ce4aad3 to your computer and use it in GitHub Desktop.
Get licenses from all packages in requirements.txt (packages need to be installed locally first)
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
#!/usr/bin/env python | |
import pkg_resources | |
def get_pkg_license(pkg): | |
try: | |
lines = pkg.get_metadata_lines('METADATA') | |
except: | |
lines = pkg.get_metadata_lines('PKG-INFO') | |
for line in lines: | |
if line.startswith('License:'): | |
return line[9:] | |
return '(License not found)' | |
def print_packages_and_licenses(): | |
with open("./requirements.txt") as f: | |
wanted = {x.strip().lower() for x in f.readlines()} | |
out = open("out", "w") | |
for pkg in sorted(pkg_resources.working_set, key=lambda x: str(x).lower()): | |
name = str(pkg).split()[0] | |
search = name.lower() | |
for x in wanted: | |
if search in x: | |
wanted.remove(x) | |
break | |
else: | |
continue | |
out.write(name + "\t" + get_pkg_license(pkg) + "\n") | |
out.close() | |
print("not found", wanted) | |
if __name__ == "__main__": | |
print_packages_and_licenses() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
adapted from https://stackoverflow.com/a/44090218/683436