Skip to content

Commit c07209c

Browse files
committed
example to list project versions uses as components in other projects
1 parent 0c0dcf7 commit c07209c

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

examples/client/get_subprojects.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
Copyright (C) 2023 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+
List project versions that are used as subprojects (components)
25+
26+
usage: get_subprojects.py [-h] -u BASE_URL -t TOKEN_FILE [-nv]
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+
containing access token
34+
-nv, --no-verify disable TLS certificate verification
35+
36+
'''
37+
import argparse
38+
import json
39+
import logging
40+
import sys
41+
import arrow
42+
43+
from blackduck import Client
44+
from pprint import pprint
45+
46+
parser = argparse.ArgumentParser("Get all the users")
47+
parser.add_argument("-u", "--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
48+
parser.add_argument("-t", "--token-file", dest='token_file', required=True, help="containing access token")
49+
parser.add_argument("-nv", "--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
50+
args = parser.parse_args()
51+
52+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
53+
logging.getLogger("requests").setLevel(logging.WARNING)
54+
logging.getLogger("urllib3").setLevel(logging.WARNING)
55+
logging.getLogger("blackduck").setLevel(logging.WARNING)
56+
57+
with open(args.token_file, 'r') as tf:
58+
access_token = tf.readline().strip()
59+
60+
bd = Client(
61+
base_url=args.base_url,
62+
token=access_token,
63+
verify=args.verify
64+
)
65+
66+
headers = {}
67+
projects = bd.get_resource('projects')
68+
69+
for project in projects:
70+
project_name = project['name']
71+
versions = bd.get_resource('versions',project)
72+
for version in versions:
73+
version_name = version['versionName']
74+
version_url = version['_meta']['href']
75+
references_url = version_url.replace("/projects/","/components/") + "/references"
76+
references = bd.session.get(references_url)
77+
json_data = references.json()
78+
reference_count = json_data['totalCount']
79+
print (f"{project_name:30} {version_name:30} is ised in {reference_count:4} projects")
80+
if reference_count > 0:
81+
items = json_data['items']
82+
for item in items:
83+
referencing_version = bd.get_resource('href', item, items=False)
84+
referencing_project = bd.get_resource('project', referencing_version, items=False)
85+
print (f" {project_name:30} {version_name:30} is ised in {referencing_project['name']} {referencing_version['versionName']}")

0 commit comments

Comments
 (0)