Created
February 9, 2018 21:28
-
-
Save melizeche/387f9b2ee05738e175b60d4a8c1fa6d4 to your computer and use it in GitHub Desktop.
Get licenses of all packages in use
This file contains hidden or 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
import pkg_resources | |
import prettytable | |
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 '(Licence not found)' | |
def print_packages_and_licenses(): | |
t = prettytable.PrettyTable(['Library', 'License']) | |
for pkg in sorted(pkg_resources.working_set, key=lambda x: str(x).lower()): | |
t.add_row((str(pkg), get_pkg_license(pkg))) | |
print(t) | |
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