|
| 1 | +''' |
| 2 | +Created on June 8, 2022 |
| 3 | +@author: kumykov |
| 4 | +
|
| 5 | +Copyright (C) 2021 Synopsys, Inc. |
| 6 | +http://www.blackducksoftware.com/ |
| 7 | +
|
| 8 | +Licensed to the Apache Software Foundation (ASF) under one |
| 9 | +or more contributor license agreements. See the NOTICE file |
| 10 | +distributed with this work for additional information |
| 11 | +regarding copyright ownership. The ASF licenses this file |
| 12 | +to you under the Apache License, Version 2.0 (the |
| 13 | +"License"); you may not use this file except in compliance |
| 14 | +with the License. You may obtain a copy of the License at |
| 15 | +
|
| 16 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +
|
| 18 | +Unless required by applicable law or agreed to in writing, |
| 19 | +software distributed under the License is distributed on an |
| 20 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 21 | +KIND, either express or implied. See the License for the |
| 22 | +specific language governing permissions and limitations |
| 23 | +under the License. |
| 24 | +
|
| 25 | +
|
| 26 | +''' |
| 27 | +import csv |
| 28 | +import sys |
| 29 | +import argparse |
| 30 | +import json |
| 31 | +import logging |
| 32 | +from urllib import response |
| 33 | +from wsgiref import headers |
| 34 | +import arrow |
| 35 | +import re |
| 36 | +import urllib |
| 37 | +from pprint import pprint |
| 38 | + |
| 39 | +from itertools import islice |
| 40 | +from datetime import timedelta |
| 41 | +from datetime import datetime |
| 42 | +from blackduck import Client |
| 43 | + |
| 44 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 45 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 46 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 47 | +logging.getLogger("blackduck").setLevel(logging.DEBUG) |
| 48 | + |
| 49 | +def find_kb_component(component_name): |
| 50 | + url = bd.base_url + "/api/search/kb-components" |
| 51 | + query = { "q": component_name } |
| 52 | + url = "{}?{}&limit=100".format(url,urllib.parse.urlencode(query)) |
| 53 | + print (url) |
| 54 | + headers = { |
| 55 | + "Accept": "application/vnd.blackducksoftware.internal-1+json, application/json, */*;q=0.8" |
| 56 | + } |
| 57 | + response = bd.session.get(url, headers=headers) |
| 58 | + print (response) |
| 59 | + items = response.json()['items'] |
| 60 | + hit_counts = dict() |
| 61 | + for item in items: |
| 62 | + for hit in item['hits']: |
| 63 | + if component_name.strip() in hit['fields']['name']: |
| 64 | + hit_counts[hit['_meta']['href']] = hit['fields']['release_count'] |
| 65 | + return hit_counts |
| 66 | + |
| 67 | +def add_component_version(component_version, project_version): |
| 68 | + bom_url = project_version['_meta']['href'] + "/components" |
| 69 | + headers = { "Content-Type": "application/vnd.blackducksoftware.bill-of-materials-6+json" } |
| 70 | + data = { |
| 71 | + "component" : component_version['_meta']['href'] |
| 72 | + } |
| 73 | + pprint (data) |
| 74 | + print(bom_url) |
| 75 | + response = bd.session.post(bom_url, json=data, headers=headers) |
| 76 | + print (response) |
| 77 | + |
| 78 | +def get_matching_component_versions(url, component_version): |
| 79 | + print (url) |
| 80 | + response = bd.session.get(url) |
| 81 | + component = response.json() |
| 82 | + params = {"q": [ f"versionName:{component_version}" ]} |
| 83 | + versions = bd.get_resource('versions', component, params=params) |
| 84 | + for version in versions: |
| 85 | + print (url) |
| 86 | + print (version['versionName']) |
| 87 | + if version['versionName'] == component_version: |
| 88 | + add_component_version (version, project_version) |
| 89 | + |
| 90 | +def parse_command_args(): |
| 91 | + |
| 92 | + parser = argparse.ArgumentParser("Print copyrights for BOM using upstream origin or prior version if not available.") |
| 93 | + parser.add_argument("-u", "--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url") |
| 94 | + parser.add_argument("-t", "--token-file", required=True, help="File containing access token") |
| 95 | + parser.add_argument("-nv", "--no-verify", action='store_false', help="Disable TLS certificate verification") |
| 96 | + parser.add_argument("project_name") |
| 97 | + parser.add_argument("version") |
| 98 | + parser.add_argument("component_input_file", help="Supply a file with components listed as foundry:componentname/version/arch one per line") |
| 99 | + |
| 100 | + return parser.parse_args() |
| 101 | + |
| 102 | +def main(): |
| 103 | + args = parse_command_args() |
| 104 | + with open(args.token_file, 'r') as tf: |
| 105 | + access_token = tf.readline().strip() |
| 106 | + global bd |
| 107 | + bd = Client(base_url=args.base_url, token=access_token, verify=args.no_verify, timeout=60.0, retries=4) |
| 108 | + |
| 109 | + global project_version |
| 110 | + |
| 111 | + project_version = None |
| 112 | + params = { "q": f"name:{args.project_name}"} |
| 113 | + |
| 114 | + project_match = None |
| 115 | + version_match = None |
| 116 | + projects = bd.get_resource('projects', params=params) |
| 117 | + for project in projects: |
| 118 | + print (project['name']) |
| 119 | + if args.project_name == project['name']: |
| 120 | + project_match = project |
| 121 | + |
| 122 | + if project_match: |
| 123 | + params = {"q": f"versionName{args.version}"} |
| 124 | + versions = bd.get_resource('versions', project, params=params) |
| 125 | + for version in versions: |
| 126 | + if args.version == version['versionName']: |
| 127 | + project_version = version |
| 128 | + |
| 129 | + if not project_version: |
| 130 | + logging.error (f"Project {args.project_name} version {args.version} not found, exiting") |
| 131 | + sys.exit() |
| 132 | + |
| 133 | + with open(args.component_input_file,"r") as f: |
| 134 | + inputdata = f.readlines() |
| 135 | + for line in inputdata: |
| 136 | + (name,version) = line.strip().split(" ") |
| 137 | + logging.info (f"Processing componemt name {name} version {version}") |
| 138 | + component_hits = find_kb_component(name) |
| 139 | + for hit in component_hits: |
| 140 | + get_matching_component_versions(hit, version) |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + sys.exit(main()) |
0 commit comments