|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# updates 'usage' for a list of components on a given project and version. Component list file must be one component per line |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import logging |
| 8 | +import sys |
| 9 | + |
| 10 | +from blackduck.HubRestApi import HubInstance, object_id |
| 11 | + |
| 12 | +usages = [ |
| 13 | + 'DYNAMICALLY_LINKED', |
| 14 | + 'SOURCE_CODE', |
| 15 | + 'STATICALLY_LINKED', |
| 16 | + 'SEPARATE_WORK', |
| 17 | + 'MERELY_AGGREGATED', |
| 18 | + 'IMPLEMENTATION_OF_STANDARD', |
| 19 | + 'PREREQUISITE', |
| 20 | + 'DEV_TOOL_EXCLUDED' |
| 21 | +] |
| 22 | + |
| 23 | + |
| 24 | +def readComponentList(compFile): |
| 25 | + compList = [] |
| 26 | + try: |
| 27 | + fp = open(compFile, 'r') |
| 28 | + line = fp.readline() |
| 29 | + while line: |
| 30 | + compList.append(line.strip() ) |
| 31 | + line = fp.readline() |
| 32 | + fp.close() |
| 33 | + except: |
| 34 | + logging.error(f"Could not open component list file: {compFile}") |
| 35 | + sys.exit(-1) |
| 36 | + |
| 37 | + |
| 38 | + return compList |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +parser = argparse.ArgumentParser("Set the Usage for a component list read from file in a project version to the specified usage") |
| 43 | +parser.add_argument("project") |
| 44 | +parser.add_argument("version") |
| 45 | +parser.add_argument("new_usage", choices=usages) |
| 46 | +parser.add_argument("component_list_file") |
| 47 | + |
| 48 | +args = parser.parse_args() |
| 49 | + |
| 50 | + |
| 51 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 52 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 53 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 54 | +logging.getLogger("blackduck").setLevel(logging.WARNING) |
| 55 | + |
| 56 | +hub = HubInstance() |
| 57 | + |
| 58 | +version = hub.get_project_version_by_name(args.project, args.version) |
| 59 | + |
| 60 | +if version: |
| 61 | + components_url = hub.get_link(version, "components") + "?limit=9999" |
| 62 | + components = hub.execute_get(components_url).json().get('items', []) |
| 63 | + logging.debug(f"Found {len(components)} components in {args.project}-{args.version}") |
| 64 | + change_comps = readComponentList(args.component_list_file) |
| 65 | + for change in change_comps: |
| 66 | + for component in components: |
| 67 | + if change == component['componentName']: |
| 68 | + #logging.info(f"Change component: {change} matches {component['componentName']}") |
| 69 | + component_url = component['_meta']['href'] |
| 70 | + component['usages'] = [args.new_usage] |
| 71 | + result = hub.execute_put(component_url, data=component) |
| 72 | + # import pdb; pdb.set_trace() |
| 73 | + if result.status_code == 200: |
| 74 | + logging.info(f"Set usage for {component['componentName']}-{component.get('componentVersionName', '?')} to {args.new_usage}") |
| 75 | + else: |
| 76 | + logging.warning(f"Failed to set usage for {component['componentName']}-{component.get('componentVersionName', '?')} to {args.new_usage}. Status code was {result.status_code}") |
| 77 | + |
| 78 | +else: |
| 79 | + logging.error(f"Did not find the project-version: {args.project}-{args.version}") |
0 commit comments