from requests.auth import HTTPBasicAuth from urllib.parse import urlparse import argparse import base64 import json import os import re import requests import subprocess import sys # in case you get rate limited AUTH = HTTPBasicAuth('username', 'token') AUTH = None def get_default_branch(repo): r = requests.get('https://api.github.com/repos/{}'.format(repo), auth=AUTH) j = r.json() return j['default_branch'] def get_license(repo_url, MIT=False): u = urlparse(repo_url) if u.netloc == 'github.com': path = u.path.lstrip('/') path = '/'.join(path.split('/')[:2]) if path.endswith('.git'): path = path.rsplit('.', 1)[0] branch = get_default_branch(path) license_mit = 'https://raw.githubusercontent.com/{}/{}/LICENSE-MIT'.format(path, branch) r = requests.get(license_mit) if r.status_code == requests.codes.ok: return r.text license_url = 'https://api.github.com/repos/{}/license'.format(path) r = requests.get(license_url, auth=AUTH) if r.status_code == requests.codes.ok: j = r.json() license_text = base64.b64decode(j['content']) return license_text.decode('utf8') return None def get_licenses(path): os.chdir(path) p = subprocess.Popen(['cargo', 'metadata', '--format-version', '1'], stdout=subprocess.PIPE) out, _ = p.communicate() j = json.loads(out) for pkg in sorted(j['packages'], key=lambda x: x['name']): name = pkg['name'] version = pkg['version'] authors = pkg['authors'] license = pkg['license'] or '' repo_url = pkg['repository'] if re.search(r'\bMIT\b', license): license = 'MIT' if not license.strip(): print('[!] WARNING: package {} has no license, skipping'.format(name), file=sys.stderr) continue license_text = get_license(repo_url) if license_text is None: print('[!] WARNING: package {} has no license, skipping'.format(name), file=sys.stderr) continue print('-' * 80) print('{} {}: {}'.format(name, version, repo_url)) print('authors: {}'.format(', '.join(authors))) print() print(license_text.strip()) print() return '' if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('path') args = parser.parse_args() print('=' * 80) print('======== RUST LICENSES ========') print('=' * 80) print() print(get_licenses(args.path)) print('=' * 80) print('======== END RUST LICENSES ========') print('=' * 80)