Skip to content

Commit a94250a

Browse files
committed
added script to retrieve job stats
1 parent da92fc9 commit a94250a

File tree

3 files changed

+120
-8
lines changed

3 files changed

+120
-8
lines changed

examples/client/batch_delete_project_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Created on April 4, 2023
2+
Created on April 25, 2023
33
@author: kumykov
44
55
Copyright (C) 2023 Synopsys, Inc.

examples/client/get_debug_page.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
'''
2+
Created on April 26, 2023
3+
@author: kumykov
4+
5+
Copyright (C) 2023 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+
This script will read the content of the job page from diagnostic section
26+
in Blackduck.
27+
28+
Data will be written into a file named bd-stats-<isotamestamp>.txt
29+
30+
Requirements
31+
32+
- python3 version 3.8 or newer recommended
33+
- the following packages are used by the script and should be installed
34+
prior to use:
35+
argparse
36+
blackduck
37+
logging
38+
datetime
39+
40+
- Blackduck instance
41+
- API token with sufficient privileges to perform project version phase
42+
change.
43+
44+
Install python packages with the following command:
45+
46+
pip3 install argparse blackduck logging datetime
47+
48+
Using
49+
50+
place the token into a file (token in this example) then execute:
51+
52+
python3 get_debug_page.py -u $BD_URL -t token -nv
53+
54+
55+
usage: python3 get_debug_page.py [-h] -u BASE_URL -t TOKEN_FILE [-nv] [-o OUTPUT_FILE] [--stdout]
56+
57+
options:
58+
-h, --help show this help message and exit
59+
-u BASE_URL, --base-url BASE_URL
60+
Hub server URL e.g. https://your.blackduck.url
61+
-t TOKEN_FILE, --token-file TOKEN_FILE
62+
containing access token
63+
-nv, --no-verify disable TLS certificate verification
64+
-o OUTPUT_FILE, --output-file OUTPUT_FILE
65+
Output file name pattern
66+
--stdout Output to stdout, instead of the file
67+
68+
'''
69+
70+
from blackduck import Client
71+
72+
import argparse
73+
import logging
74+
from pprint import pprint
75+
from datetime import datetime
76+
77+
logging.basicConfig(
78+
level=logging.DEBUG,
79+
format="[%(asctime)s] {%(module)s:%(lineno)d} %(levelname)s - %(message)s"
80+
)
81+
82+
parser = argparse.ArgumentParser("Get a job stats from debug page")
83+
parser.add_argument("-u", "--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
84+
parser.add_argument("-t", "--token-file", dest='token_file', required=True, help="containing access token")
85+
parser.add_argument("-nv", "--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
86+
parser.add_argument("-o", "--output-file", dest='output_file', default='bd-stats', help="Output file name pattern")
87+
parser.add_argument("--stdout", dest='stdout', action='store_true', help='Output to stdout, instead of the file')
88+
args = parser.parse_args()
89+
90+
with open(args.token_file, 'r') as tf:
91+
access_token = tf.readline().strip()
92+
93+
bd = Client(base_url=args.base_url, token=access_token, verify=args.verify)
94+
95+
url = "https://ec2-44-202-86-163.compute-1.amazonaws.com/debug?job=true"
96+
97+
result = bd.session.get(url)
98+
99+
if args.stdout:
100+
print(result.text)
101+
102+
else:
103+
timestamp = datetime.now().isoformat()
104+
filename = f"{args.output_file}-{timestamp}.txt"
105+
f = open(filename,"w")
106+
f.write(result.text)
107+
f.close()

examples/client/read_scan_status.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010

1111
from blackduck import Client
12+
from pprint import pprint
1213

1314
parser = argparse.ArgumentParser("Get all the users")
1415
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
@@ -31,13 +32,17 @@
3132
verify=args.verify
3233
)
3334

34-
headers = {'Accept': 'application/vnd.blackducksoftware.internal-1+json'}
35-
status = {"in_progress": 0, "completed": 0, "error": 0, "skipped": 0}
36-
37-
for i in status.keys():
38-
params = {"filter": ["codeLocationStatus:{}".format(i)]}
39-
codelocations = bd.get_resource('codeLocations', params=params, headers=headers)
40-
status[i] = sum(1 for _ in codelocations)
35+
status = {"STARTED": 0, "SUCCESS": 0, "FAILURE": 0}
36+
codelocations = bd.get_resource('codeLocations')
37+
for codelocation in codelocations:
38+
latest_summaries = bd.get_resource('scans', codelocation)
39+
for summary in latest_summaries:
40+
scan_state = summary['scanState']
41+
if scan_state in status:
42+
status[scan_state] += 1
43+
else:
44+
status[scan_state] =1
45+
break
4146

4247
print (status)
4348

0 commit comments

Comments
 (0)