|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import sys |
| 5 | +from datetime import timedelta |
| 6 | +from pprint import pprint |
| 7 | + |
| 8 | +import arrow |
| 9 | +from blackduck import Client |
| 10 | +from blackduck.Utils import get_resource_name |
| 11 | + |
| 12 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 13 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 14 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 15 | +logging.getLogger("blackduck").setLevel(logging.DEBUG) |
| 16 | + |
| 17 | +def main(): |
| 18 | + upstream_copyright_data_file='copyright_data.txt' |
| 19 | + |
| 20 | + parser = argparse.ArgumentParser("Enumerate BOM componets without copyrigth statements. retrieve copyright statements form upstream channel and/or version") |
| 21 | + parser.add_argument("-u", "--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url") |
| 22 | + parser.add_argument("-t", "--token-file", dest='token_file', required=True, help="containing access token") |
| 23 | + parser.add_argument("-nv", "--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification") |
| 24 | + parser.add_argument("-o", "--outputdir", dest='outputdir', default='outdir', help="disable TLS certificate verification") |
| 25 | + args = parser.parse_args() |
| 26 | + |
| 27 | + with open(args.token_file, 'r') as tf: |
| 28 | + access_token = tf.readline().strip() |
| 29 | + |
| 30 | + global bd |
| 31 | + bd = Client(base_url=args.base_url, token=access_token, verify=args.verify, timeout = 60.0) |
| 32 | + outdir=None |
| 33 | + if args.outputdir: |
| 34 | + outdir = args.outputdir |
| 35 | + import os |
| 36 | + if not os.path.exists(outdir): |
| 37 | + os.mkdir(outdir) |
| 38 | + projects = bd.get_resource('projects') |
| 39 | + for project in projects: |
| 40 | + versions = bd.get_resource('versions',project) |
| 41 | + for version in versions: |
| 42 | + codelocations = bd.get_resource('codelocations', version) |
| 43 | + for codelocation in codelocations: |
| 44 | + resources = bd.list_resources(codelocation) |
| 45 | + url = resources['scan-data'] |
| 46 | + filename = url.split('/')[6] |
| 47 | + response = bd.session.get(url, stream=True, verify=False) |
| 48 | + print (response.status_code) |
| 49 | + print (response.ok) |
| 50 | + print (url) |
| 51 | + if response.ok: |
| 52 | + pathname = os.path.join(outdir, filename) |
| 53 | + with open(pathname, "wb") as f: |
| 54 | + for data in response.iter_content(): |
| 55 | + f.write(data) |
| 56 | + print(f"{filename}, {pathname}") |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + sys.exit(main()) |
| 60 | + |
0 commit comments