Skip to content

Instantly share code, notes, and snippets.

@ninowalker
Created September 25, 2014 04:33
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 ninowalker/830e08858d928a14e6ab to your computer and use it in GitHub Desktop.
Save ninowalker/830e08858d928a14e6ab to your computer and use it in GitHub Desktop.
import pkg_resources
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('PKG-INFO'):
(k, v) = line.split(': ', 1)
if k == "License":
return v
return None
for w in pkg_resources.working_set:
try:
print w, get_pkg_license(str(w).split(" ")[0])
except:
print "UNKNOWN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment