|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import logging |
| 6 | +import sys |
| 7 | + |
| 8 | +from blackduck.HubRestApi import HubInstance |
| 9 | + |
| 10 | + |
| 11 | +parser = argparse.ArgumentParser("Retrieve licenses from the Black Duck KB") |
| 12 | +parser.add_argument("-k", "--keyword", help="Find licenses whose name include the keyword (case-insensitive)") |
| 13 | +parser.add_argument("-l", "--limit", type=int, help="Set a limit on the number of licenses to retrieve (default: 10)") |
| 14 | + |
| 15 | +args = parser.parse_args() |
| 16 | + |
| 17 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 18 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 19 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 20 | + |
| 21 | +hub = HubInstance() |
| 22 | + |
| 23 | +parameters = {} |
| 24 | + |
| 25 | +if args.keyword: |
| 26 | + parameters.update({"q": f"name:{args.keyword}"}) |
| 27 | + |
| 28 | +if args.limit: |
| 29 | + parameters.update({"limit": args.limit}) |
| 30 | + |
| 31 | +logging.debug(f"Retrieving licenses using parameters={parameters}") |
| 32 | +licenses = hub.get_licenses(parameters=parameters).get('items', []) |
| 33 | + |
| 34 | +logging.debug(f"Found {len(licenses)} licenses") |
| 35 | + |
| 36 | +for license in licenses: |
| 37 | + logging.debug(f"Retrieving license text, terms, and obligations info for license \"{license['name']}\"") |
| 38 | + text_url = hub.get_link(license, "text") |
| 39 | + license['text'] = hub.execute_get(text_url).json().get('text') |
| 40 | + |
| 41 | + terms_url = hub.get_link(license, 'license-terms') |
| 42 | + license['terms'] = hub.execute_get(terms_url).json().get('items', []) |
| 43 | + |
| 44 | + obligations_url = hub.get_link(license, 'license-obligations') |
| 45 | + license['obligations'] = hub.execute_get(obligations_url).json().get('items', []) |
| 46 | + |
| 47 | +print(json.dumps(licenses)) |
0 commit comments