Created
March 30, 2018 00:04
-
-
Save eloquence/a8e9a02472723be11432104a7a2cc3bd to your computer and use it in GitHub Desktop.
Get package licenses for the current virtualenv
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/python3 | |
import pkg_resources | |
import copy | |
def get_packages_info(): | |
KEY_MAP = { | |
"Name": 'name', | |
"Version": 'version', | |
"License": 'license', | |
} | |
empty_info = {} | |
for key, name in KEY_MAP.items(): | |
empty_info[name] = "" | |
packages = pkg_resources.working_set.by_key | |
infos = [] | |
for pkg_name, pkg in packages.items(): | |
info = copy.deepcopy(empty_info) | |
try: | |
lines = pkg.get_metadata_lines('METADATA') | |
except (KeyError, IOError): | |
lines = pkg.get_metadata_lines('PKG-INFO') | |
for line in lines: | |
try: | |
key, value = line.split(': ', 1) | |
if key in KEY_MAP: | |
info[KEY_MAP[key]] = value | |
except ValueError: | |
pass | |
infos += [info] | |
return "name,version,license\n%s" % "\n".join(['"%s","%s","%s"' % (info['name'], info['version'], info['license']) for info in sorted(infos, key=(lambda item: item['name'].lower()))]) | |
print(get_packages_info()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://stackoverflow.com/a/43644140 for credits.