Skip to content

Commit 662903b

Browse files
committed
get project data
1 parent b5d4563 commit 662903b

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

examples/client/get_project_data.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python3
2+
'''
3+
Created: Apr 2, 2024
4+
Author: @kumykov
5+
6+
Copyright (c) 2024, Synopsys, Inc.
7+
http://www.synopsys.com/
8+
9+
Licensed to the Apache Software Foundation (ASF) under one
10+
or more contributor license agreements. See the NOTICE file
11+
distributed with this work for additional information
12+
regarding copyright ownership. The ASF licenses this file
13+
to you under the Apache License, Version 2.0 (the
14+
"License"); you may not use this file except in compliance
15+
with the License. You may obtain a copy of the License at
16+
17+
http://www.apache.org/licenses/LICENSE-2.0
18+
19+
Unless required by applicable law or agreed to in writing,
20+
software distributed under the License is distributed on an
21+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22+
KIND, either express or implied. See the License for the
23+
specific language governing permissions and limitations
24+
under the License.
25+
26+
usage: get_project_data.py [-h] -u BASE_URL -t TOKEN_FILE [-nv] -p SOURCE_PROJECT -v SOURCE_VERSION
27+
28+
options:
29+
-h, --help show this help message and exit
30+
-u BASE_URL, --base-url BASE_URL
31+
Hub server URL e.g. https://your.blackduck.url
32+
-t TOKEN_FILE, --token-file TOKEN_FILE
33+
File containing access token
34+
-nv, --no-verify Disable TLS certificate verification
35+
-p SOURCE_PROJECT, --project SOURCE_PROJECT
36+
Project Name
37+
-v SOURCE_VERSION, --version SOURCE_VERSION
38+
Project Version Name
39+
40+
Black Duck examples collection
41+
42+
43+
'''
44+
import argparse
45+
import io
46+
import json
47+
import sys
48+
import logging
49+
import time
50+
51+
from blackduck import Client
52+
from pprint import pprint
53+
54+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
55+
logging.getLogger("requests").setLevel(logging.WARNING)
56+
logging.getLogger("urllib3").setLevel(logging.WARNING)
57+
logging.getLogger("blackduck").setLevel(logging.WARNING)
58+
59+
60+
def find_project_by_name(bd, project_name):
61+
params = {
62+
'q': [f"name:{project_name}"]
63+
}
64+
projects = [p for p in bd.get_resource('projects', params=params) if p['name'].casefold() == project_name.casefold()]
65+
if len(projects) == 1:
66+
return projects[0]
67+
else:
68+
return None
69+
70+
def find_project_version_by_name(bd, project, version_name):
71+
params = {
72+
'q': [f"versionName:{version_name}"]
73+
}
74+
versions = [v for v in bd.get_resource('versions', project, params=params) if v['versionName'] == version_name]
75+
if len(versions) == 1:
76+
return versions[0]
77+
else:
78+
return None
79+
80+
def get_project_data(bd, args):
81+
project = find_project_by_name(bd, args.project)
82+
version = find_project_version_by_name(bd, project, args.version)
83+
if not version:
84+
logging.error(f"Source project {args.project} : {args.version} not found. Exiting.")
85+
sys.exit(1)
86+
logging.info(f"Located source project {args.project} : {args.version}")
87+
return bd.get_resource('components', version)
88+
89+
90+
def parse_command_args():
91+
parser = argparse.ArgumentParser(prog = "get_project_data.py", description="Generate and download SBOM and upload to the target project version", epilog="Blackduck examples collection")
92+
parser.add_argument("-u", "--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
93+
parser.add_argument("-t", "--token-file", required=True, help="File containing access token")
94+
parser.add_argument("-nv", "--no-verify", action='store_false', help="Disable TLS certificate verification")
95+
parser.add_argument("-p", "--project", required=True, help="Project Name")
96+
parser.add_argument("-v", "--version", required=True, help="Project Version Name")
97+
98+
return parser.parse_args()
99+
100+
def main():
101+
args = parse_command_args()
102+
with open(args.token_file, 'r') as tf:
103+
access_token = tf.readline().strip()
104+
bd = Client(base_url=args.base_url, token=access_token, verify=args.no_verify, timeout=60.0, retries=4)
105+
components = get_project_data(bd, args)
106+
for component in components:
107+
# pprint (component)
108+
print (f"{component['componentName']} {component['componentVersionName']} {component['licenses'][0]['licenseDisplay']}")
109+
110+
if __name__ == "__main__":
111+
sys.exit(main())

0 commit comments

Comments
 (0)