|
| 1 | +""" |
| 2 | + SPDX-License-Identifier: MIT |
| 3 | +
|
| 4 | + Copyright (c) 2023, SCANOSS |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + THE SOFTWARE. |
| 23 | +""" |
| 24 | +import json |
| 25 | +import os |
| 26 | +import sys |
| 27 | +from typing import TextIO |
| 28 | + |
| 29 | +from pypac.parser import PACFile |
| 30 | + |
| 31 | +from .scanner import Scanner |
| 32 | +from .scanossbase import ScanossBase |
| 33 | +from .scanossgrpc import ScanossGrpc |
| 34 | + |
| 35 | + |
| 36 | +class Components(ScanossBase): |
| 37 | + """ |
| 38 | + Class for Component functionality |
| 39 | + """ |
| 40 | + |
| 41 | + def __init__(self, debug: bool = False, trace: bool = False, quiet: bool = False, |
| 42 | + grpc_url: str = None, api_key: str = None, timeout: int = 600, |
| 43 | + proxy: str = None, grpc_proxy: str = None, ca_cert: str = None, pac: PACFile = None |
| 44 | + ): |
| 45 | + """ |
| 46 | + Handle all component style requests |
| 47 | +
|
| 48 | + :param debug: Debug |
| 49 | + :param trace: Trace |
| 50 | + :param quiet: Quiet |
| 51 | + :param grpc_url: gRPC URL |
| 52 | + :param api_key: API Key |
| 53 | + :param timeout: Timeout for requests (default 600) |
| 54 | + :param proxy: Proxy to use (optional) |
| 55 | + :param grpc_proxy: Specific gRPC proxy (optional) |
| 56 | + :param ca_cert: TLS client certificate (optional) |
| 57 | + :param pac: Proxy Auto-Config file (optional) |
| 58 | + """ |
| 59 | + super().__init__(debug, trace, quiet) |
| 60 | + ver_details = Scanner.version_details() |
| 61 | + self.grpc_api = ScanossGrpc(url=grpc_url, debug=debug, quiet=quiet, trace=trace, api_key=api_key, |
| 62 | + ver_details=ver_details, ca_cert=ca_cert, proxy=proxy, pac=pac, |
| 63 | + grpc_proxy=grpc_proxy, timeout=timeout) |
| 64 | + |
| 65 | + def load_purls(self, json_file: str = None, purls: [] = None) -> dict: |
| 66 | + """ |
| 67 | + Load the specified purls and return a dictionary |
| 68 | +
|
| 69 | + :param json_file: JSON PURL file (optional) |
| 70 | + :param purls: list of PURLs (optional) |
| 71 | + :return: PURL Request dictionary |
| 72 | + """ |
| 73 | + if json_file: |
| 74 | + if not os.path.isfile(json_file) or not os.access(json_file, os.R_OK): |
| 75 | + self.print_stderr(f'ERROR: JSON file does not exist, is not a file, or is not readable: {json_file}') |
| 76 | + return None |
| 77 | + with open(json_file, 'r') as f: |
| 78 | + try: |
| 79 | + purl_request = json.loads(f.read()) |
| 80 | + except Exception as e: |
| 81 | + self.print_stderr(f'ERROR: Problem parsing input JSON: {e}') |
| 82 | + return None |
| 83 | + elif purls: |
| 84 | + parsed_purls = [] |
| 85 | + for p in purls: |
| 86 | + parsed_purls.append({'purl': p}) |
| 87 | + purl_request = {'purls': parsed_purls} |
| 88 | + else: |
| 89 | + self.print_stderr('ERROR: No purls specified to process.') |
| 90 | + return None |
| 91 | + purl_count = len(purl_request.get('purls', [])) |
| 92 | + self.print_debug(f'Parsed Purls ({purl_count}): {purl_request}') |
| 93 | + if purl_count == 0: |
| 94 | + self.print_stderr('ERROR: No PURLs parsed from request.') |
| 95 | + return None |
| 96 | + return purl_request |
| 97 | + |
| 98 | + def _open_file_or_sdtout(self, filename): |
| 99 | + """ |
| 100 | + Open the given filename if requested, otherwise return STDOUT |
| 101 | +
|
| 102 | + :param filename: |
| 103 | + :return: |
| 104 | + """ |
| 105 | + file = sys.stdout |
| 106 | + if filename: |
| 107 | + try: |
| 108 | + file = open(filename, 'w') |
| 109 | + except OSError as e: |
| 110 | + self.print_stderr(f'ERROR: Failed to open output file {filename}: {e}') |
| 111 | + return None |
| 112 | + return file |
| 113 | + |
| 114 | + def _close_file(self, filename: str = None, file: TextIO = None) -> None: |
| 115 | + """ |
| 116 | + Close the file descriptor if its defined |
| 117 | +
|
| 118 | + :param filename: filename |
| 119 | + :param file: file IO object |
| 120 | + :return: None |
| 121 | + """ |
| 122 | + if filename and file: |
| 123 | + self.print_trace(f'Closing file: {filename}') |
| 124 | + file.close() |
| 125 | + |
| 126 | + def get_crypto_details(self, json_file: str = None, purls: [] = None, output_file: str = None) -> bool: |
| 127 | + """ |
| 128 | + Retrieve the cryptographic details for the supplied PURLs |
| 129 | +
|
| 130 | + :param json_file: PURL JSON request file (optional) |
| 131 | + :param purls: PURL request array (optional) |
| 132 | + :param output_file: output filename (optional). Default: STDOUT |
| 133 | + :return: True on success, False otherwise |
| 134 | + """ |
| 135 | + success = False |
| 136 | + purls_request = self.load_purls(json_file, purls) |
| 137 | + if purls_request is None or len(purls_request) == 0: |
| 138 | + return False |
| 139 | + file = self._open_file_or_sdtout(output_file) |
| 140 | + if file is None: |
| 141 | + return False |
| 142 | + self.print_msg('Sending PURLs to Crypto API for decoration...') |
| 143 | + response = self.grpc_api.get_crypto_json(purls_request) |
| 144 | + if response: |
| 145 | + print(json.dumps(response, indent=2, sort_keys=True), file=file) |
| 146 | + success = True |
| 147 | + if output_file: |
| 148 | + self.print_msg(f'Results written to: {output_file}') |
| 149 | + self._close_file(output_file, file) |
| 150 | + return success |
0 commit comments