Skip to content

Commit 58f05cd

Browse files
author
Glenn Snyder
committed
extending to check for snippet scans
1 parent c162f48 commit 58f05cd

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

examples/parse_detect_log_and_wait_for_scans.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626

2727
parser = argparse.ArgumentParser("Parse the Synopsys Detect log, load status.json, and wait for all scan processing to complete")
2828
parser.add_argument("-d", "--detect_log", help="By default, this script will read the detect log from stdin, but you can alternatively supply a detect log filename")
29-
# parser.add_argument('-m', '--max_checks', type=int, default=10, help="Set the maximum number of checks before quitting")
30-
# parser.add_argument('-t', '--time_between_checks', type=int, default=5, help="Set the number of seconds to wait in-between checks")
31-
# parser.add_argument('-s', '--snippet_scan', action='store_true', help="Select this option if you want to wait for a snippet scan to complete along with it's corresponding component scan.")
29+
parser.add_argument("-m", "--max_checks", default=10, type=int, help="Set the maximum number of checks before timing out. Applies to each code/scan location")
30+
parser.add_argument("-c", "--check_delay", default=5, type=int, help="The number of seconds between each check")
3231
args = parser.parse_args()
3332

3433
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
@@ -40,7 +39,7 @@
4039
else:
4140
detect_log = sys.stdin
4241

43-
snippet_scan = False
42+
snippet_scan_option_set = False
4443
status_file_path = None
4544
start_time = None
4645

@@ -60,7 +59,7 @@
6059
snippet_matching_re = re.search(".*detect.blackduck.signature.scanner.snippet.matching = (.*)", line)
6160
if snippet_matching_re:
6261
if 'SNIPPET' in snippet_matching_re[1]:
63-
snippet_scan = True
62+
snippet_scan_option_set = True
6463
logging.debug("Found snippet scanning option")
6564

6665
status_file_re = re.search(".*Creating status file: (.*)", line)
@@ -71,7 +70,7 @@
7170
assert start_time, "Hmm, not sure how that happened but we need a start time"
7271

7372
logging.debug(f"detect start time: {start_time}")
74-
logging.debug(f"snippet_scan: {snippet_scan}")
73+
logging.debug(f"snippet_scan_option_set: {snippet_scan_option_set}")
7574
logging.debug(f"status.json path: {status_file_path}")
7675

7776
hub = HubInstance()
@@ -80,13 +79,25 @@
8079
status_info = json.load(status_file)
8180

8281
# Monitoring status serially cause it's simpler (i.e. than spawning multiple threads and waiting for them)
82+
scan_results = []
8383
for code_location in status_info['codeLocations']:
8484
logging.debug(f"Waiting for scan to finish at scan/code location {code_location['codeLocationName']}")
85+
is_signature_scan = code_location['codeLocationName'].endswith("scan")
86+
snippet_scan = is_signature_scan and snippet_scan_option_set
87+
logging.debug(f"is_signature_scan: {is_signature_scan}, snippet_scan: {snippet_scan}")
8588
scan_monitor = ScanMonitor(
8689
hub,
8790
start_time=start_time,
8891
scan_location_name=code_location['codeLocationName'],
89-
snippet_scan=snippet_scan)
90-
scan_monitor.wait_for_scan_completion()
92+
snippet_scan=snippet_scan,
93+
max_checks=args.max_checks,
94+
check_delay=args.check_delay)
95+
scan_result = scan_monitor.wait_for_scan_completion()
96+
logging.debug(f"scan result for {code_location['codeLocationName']} was {scan_result}")
97+
98+
if sum(scan_results) > 0:
99+
sys.exit(1) # failure
100+
else:
101+
sys.exit(0)
91102

92103

0 commit comments

Comments
 (0)