Skip to content

Instantly share code, notes, and snippets.

@b-ryan
Created July 5, 2018 16:53
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 b-ryan/53858f83118761af9c22c69a6ce4aad3 to your computer and use it in GitHub Desktop.
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)
#!/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()
@b-ryan
Copy link
Author

b-ryan commented Jul 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment