Skip to content

Commit ae28d9b

Browse files
committed
added retry command option for scanning
1 parent 55a353c commit ae28d9b

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Upcoming changes...
1111

12+
## [1.5.2] - 2023-06-13
13+
### Added
14+
- Added retry limit option (`--retry`) while scanning
15+
- `--retry 0` will fail immediately
16+
1217
## [1.5.1] - 2023-04-21
1318
### Added
1419
- Added support scanning/fingeprinting file contents from STDIN
@@ -231,3 +236,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
231236
[1.4.2]: https://github.com/scanoss/scanoss.py/compare/v1.4.0...v1.4.2
232237
[1.5.0]: https://github.com/scanoss/scanoss.py/compare/v1.4.2...v1.5.0
233238
[1.5.1]: https://github.com/scanoss/scanoss.py/compare/v1.5.0...v1.5.1
239+
[1.5.2]: https://github.com/scanoss/scanoss.py/compare/v1.5.1...v1.5.2

src/scanoss/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
THE SOFTWARE.
2323
"""
2424

25-
__version__ = '1.5.1'
25+
__version__ = '1.5.2'

src/scanoss/cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def setup_args() -> None:
9191
help='Number of kilobytes to limit the post to while scanning (optional - default 64)')
9292
p_scan.add_argument('--timeout', '-M', type=int, default=120,
9393
help='Timeout (in seconds) for API communication (optional - default 120)')
94+
p_scan.add_argument('--retry', '-R', type=int, default=5,
95+
help='Retry limit for API communication (optional - default 5)')
9496
p_scan.add_argument('--no-wfp-output', action='store_true', help='Skip WFP file generation')
9597
p_scan.add_argument('--all-extensions', action='store_true', help='Scan all file extensions')
9698
p_scan.add_argument('--all-folders', action='store_true', help='Scan all folders')
@@ -445,6 +447,8 @@ def scan(parser, args):
445447
print_stderr(f'Changing scanning POST size to: {args.post_size}k...')
446448
if args.timeout != 120:
447449
print_stderr(f'Changing scanning POST timeout to: {args.timeout}...')
450+
if args.retry != 5:
451+
print_stderr(f'Changing scanning POST retry to: {args.retry}...')
448452
if args.obfuscate:
449453
print_stderr("Obfuscating file fingerprints...")
450454
if args.proxy:
@@ -460,6 +464,8 @@ def scan(parser, args):
460464
elif not args.quiet:
461465
if args.timeout < 5:
462466
print_stderr(f'POST timeout (--timeout) too small: {args.timeout}. Reverting to default.')
467+
if args.retry < 0:
468+
print_stderr(f'POST retry (--retry) too small: {args.retry}. Reverting to default.')
463469

464470
if not os.access(os.getcwd(), os.W_OK): # Make sure the current directory is writable. If not disable saving WFP
465471
print_stderr(f'Warning: Current directory is not writable: {os.getcwd()}')
@@ -478,7 +484,7 @@ def scan(parser, args):
478484
scan_options=scan_options, sc_timeout=args.sc_timeout, sc_command=args.sc_command,
479485
grpc_url=args.api2url, obfuscate=args.obfuscate,
480486
ignore_cert_errors=args.ignore_cert_errors, proxy=args.proxy, grpc_proxy=args.grpc_proxy,
481-
pac=pac_file, ca_cert=args.ca_cert
487+
pac=pac_file, ca_cert=args.ca_cert, retry=args.retry
482488
)
483489
if args.wfp:
484490
if not scanner.is_file_or_snippet_scan():

src/scanoss/scanner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __init__(self, wfp: str = None, scan_output: str = None, output_format: str
101101
all_extensions: bool = False, all_folders: bool = False, hidden_files_folders: bool = False,
102102
scan_options: int = 7, sc_timeout: int = 600, sc_command: str = None, grpc_url: str = None,
103103
obfuscate: bool = False, ignore_cert_errors: bool = False, proxy: str = None, grpc_proxy: str = None,
104-
ca_cert: str = None, pac: PACFile = None
104+
ca_cert: str = None, pac: PACFile = None, retry: int = 5
105105
):
106106
"""
107107
Initialise scanning class, including Winnowing, ScanossApi and ThreadedScanning
@@ -125,7 +125,7 @@ def __init__(self, wfp: str = None, scan_output: str = None, output_format: str
125125
self.scanoss_api = ScanossApi(debug=debug, trace=trace, quiet=quiet, api_key=api_key, url=url,
126126
sbom_path=sbom_path, scan_type=scan_type, flags=flags, timeout=timeout,
127127
ver_details=ver_details, ignore_cert_errors=ignore_cert_errors,
128-
proxy=proxy, ca_cert=ca_cert, pac=pac
128+
proxy=proxy, ca_cert=ca_cert, pac=pac, retry=retry
129129
)
130130
sc_deps = ScancodeDeps(debug=debug, quiet=quiet, trace=trace, timeout=sc_timeout, sc_command=sc_command)
131131
grpc_api = ScanossGrpc(url=grpc_url, debug=debug, quiet=quiet, trace=trace, api_key=api_key,

src/scanoss/scanossapi.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ScanossApi(ScanossBase):
5353
def __init__(self, scan_type: str = None, sbom_path: str = None, scan_format: str = None, flags: str = None,
5454
url: str = None, api_key: str = None, debug: bool = False, trace: bool = False, quiet: bool = False,
5555
timeout: int = 120, ver_details: str = None, ignore_cert_errors: bool = False,
56-
proxy: str = None, ca_cert: str = None, pac: PACFile = None):
56+
proxy: str = None, ca_cert: str = None, pac: PACFile = None, retry: int = 5):
5757
"""
5858
Initialise the SCANOSS API
5959
:param scan_type: Scan type (default identify)
@@ -82,6 +82,7 @@ def __init__(self, scan_type: str = None, sbom_path: str = None, scan_format: st
8282
self.sbom_path = sbom_path
8383
self.flags = flags
8484
self.timeout = timeout if timeout > 5 else 120
85+
self.retry_limit = retry if retry >= 0 else 5
8586
self.ignore_cert_errors = ignore_cert_errors
8687
self.headers = {}
8788
if ver_details:
@@ -149,7 +150,7 @@ def scan(self, wfp: str, context: str = None, scan_id: int = None):
149150
headers['x-request-id'] = request_id # send a unique request id for each post
150151
r = None
151152
retry = 0 # Add some retry logic to cater for timeouts, etc.
152-
while retry <= 5:
153+
while retry <= self.retry_limit:
153154
retry += 1
154155
try:
155156
r = None
@@ -163,7 +164,7 @@ def scan(self, wfp: str, context: str = None, scan_id: int = None):
163164
self.print_stderr(f'ERROR: Exception ({e.__class__.__name__}) POSTing data - {e}.')
164165
raise Exception(f"ERROR: The SCANOSS API request failed for {self.url}") from e
165166
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
166-
if retry > 5: # Timed out 5 or more times, fail
167+
if retry > self.retry_limit: # Timed out retry_limit or more times, fail
167168
self.print_stderr(f'ERROR: {e.__class__.__name__} POSTing data ({request_id}) - {e}: {scan_files}')
168169
raise Exception(f"ERROR: The SCANOSS API request timed out ({e.__class__.__name__}) for"
169170
f" {self.url}") from e
@@ -176,7 +177,7 @@ def scan(self, wfp: str, context: str = None, scan_id: int = None):
176177
raise Exception(f"ERROR: The SCANOSS API request failed for {self.url}") from e
177178
else:
178179
if r is None:
179-
if retry > 5: # No response 5 or more times, fail
180+
if retry > self.retry_limit: # No response retry_limit or more times, fail
180181
self.save_bad_req_wfp(scan_files, request_id, scan_id)
181182
raise Exception(f"ERROR: The SCANOSS API request ({request_id}) response object is empty "
182183
f"for {self.url}")
@@ -190,7 +191,7 @@ def scan(self, wfp: str, context: str = None, scan_id: int = None):
190191
raise Exception(f"ERROR: {r.status_code} - The SCANOSS API request ({request_id}) rejected "
191192
f"for {self.url} due to service limits being exceeded.")
192193
elif r.status_code >= 400:
193-
if retry > 5: # No response 5 or more times, fail
194+
if retry > self.retry_limit: # No response retry_limit or more times, fail
194195
self.save_bad_req_wfp(scan_files, request_id, scan_id)
195196
raise Exception(
196197
f"ERROR: The SCANOSS API returned the following error: HTTP {r.status_code}, "

0 commit comments

Comments
 (0)