Created
August 3, 2015 21:22
-
-
Save kscottz/1f399ccc2b0b36d7877b to your computer and use it in GitHub Desktop.
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 argparse | |
import os.path as osp | |
import csv | |
def get_pkg_license(pkgname): | |
""" | |
Given a package reference (as from requirements.txt), | |
return license listed in package metadata. | |
""" | |
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 | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('file', metavar='f', type=str, | |
help='a list of requirements files.') | |
parser.add_argument('output', metavar='f', type=str, | |
help='a csv to stash the results') | |
args = parser.parse_args() | |
output = {} | |
with open(args.file, 'r') as targets: | |
for apath in targets: | |
apath = osp.realpath(apath) | |
apath = "".join(apath.split()) | |
with open(apath, 'r') as f: | |
for line in f: | |
try: | |
line = "".join(line.split()) | |
line = line.split("=")[0] | |
try: | |
l = get_pkg_license(line) | |
if l is None: | |
continue | |
except Exception as e: | |
print "No license for {1} in {0}.".format(apath,line) | |
if output.has_key(line): | |
output[line].append([l,apath]) | |
else: | |
output[line] = [] | |
output[line].append([l,apath]) | |
except Exception as e: | |
print "failed on {0} in {1} because: {2}.".format(apath,line,e) | |
continue | |
print output | |
with open(args.output, 'w') as csvfile: | |
fieldnames = ['library','license','location'] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for k,v in output.items(): | |
stuff = {} | |
stuff["library"] = k | |
for places in v: | |
stuff["license"]=places[0] | |
stuff["location"]=places[1] | |
writer.writerow(stuff) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment