Skip to content

Commit 257d304

Browse files
committed
Bringing master updates into work branch
Merge branch 'master' into mkumykov-multi-image
2 parents 3c9aa0c + bd36209 commit 257d304

File tree

5 files changed

+570
-21
lines changed

5 files changed

+570
-21
lines changed

blackduck/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
VERSION = (1, 1, 0)
1+
VERSION = (1, 1, 3)
22

33
__version__ = '.'.join(map(str, VERSION))

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())

examples/client/match_snippet.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
Copyright (C) 2024 Synopsys, Inc.
5+
http://www.blackducksoftware.com/
6+
7+
Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing,
18+
software distributed under the License is distributed on an
19+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
KIND, either express or implied. See the License for the
21+
specific language governing permissions and limitations
22+
under the License.
23+
24+
usage: match_snippets.py [-h] --base-url BASE_URL --token-file TOKEN_FILE [--no-verify] [--input INPUT]
25+
26+
options:
27+
-h, --help show this help message and exit
28+
--base-url BASE_URL Hub server URL e.g. https://your.blackduck.url
29+
--token-file TOKEN_FILE
30+
containing access token
31+
--no-verify disable TLS certificate verification
32+
--input INPUT File containing code snippet or stdin
33+
34+
Match a snippet of a code.
35+
This functionality requires 'Generative AI Compliance' option licenses
36+
37+
38+
Examples:
39+
40+
Curl file content from github and match it against Black Duck KB
41+
and format the output using jq utility
42+
curl https://raw.githubusercontent.com/apache/kafka/trunk/shell/src/main/java/org/apache/kafka/shell/state/MetadataShellState.java | \
43+
python3 examples/client/match_snippet.py --base-url=$BD_URL --token-file=<(echo $API_TOKEN) --no-verify | \
44+
jq .
45+
46+
This will produce something like:
47+
{
48+
"snippetMatches": {
49+
"PERMISSIVE": [
50+
{
51+
"projectName": "Apache Kafka",
52+
"releaseVersion": "3.5.0",
53+
"licenseDefinition": {
54+
"name": "Apache License 2.0",
55+
"spdxId": "Apache-2.0",
56+
"ownership": "OPEN_SOURCE",
57+
"licenseDisplayName": "Apache License 2.0"
58+
. . .
59+
60+
'''
61+
import argparse
62+
import json
63+
import logging
64+
import sys
65+
66+
from blackduck import Client
67+
68+
parser = argparse.ArgumentParser('match_snippets.py')
69+
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
70+
parser.add_argument("--token-file", dest='token_file', required=True, help="containing access token")
71+
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
72+
parser.add_argument("--input", required=False, help="File containing code snippet or stdin")
73+
args = parser.parse_args()
74+
75+
76+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
77+
logging.getLogger("requests").setLevel(logging.WARNING)
78+
logging.getLogger("urllib3").setLevel(logging.WARNING)
79+
logging.getLogger("blackduck").setLevel(logging.WARNING)
80+
81+
with open(args.token_file, 'r') as tf:
82+
access_token = tf.readline().strip()
83+
84+
bd = Client(
85+
base_url=args.base_url,
86+
token=access_token,
87+
verify=args.verify
88+
)
89+
90+
if args.input:
91+
with open(args.input, 'r') as content_file:
92+
content = content_file.read()
93+
else:
94+
with sys.stdin as content_file:
95+
content = content_file.read()
96+
97+
endpoint='/api/snippet-matching'
98+
headers = {"Content-Type": "text/plain"}
99+
100+
response = bd.session.post(url=endpoint, headers=headers, data=content)
101+
if response.ok:
102+
data = response.json()
103+
import json
104+
print(json.dumps(data))

0 commit comments

Comments
 (0)