|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# FederatedCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/nexB/federatedcode for support or download. |
| 7 | +# See https://aboutcode.org for more information about AboutCode.org OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import os |
| 11 | +from typing import Union |
| 12 | +from urllib.parse import urljoin |
| 13 | + |
| 14 | +import requests |
| 15 | +from aboutcode.hashid import get_package_base_dir |
| 16 | +from dotenv import load_dotenv |
| 17 | +from packageurl import PackageURL |
| 18 | + |
| 19 | +load_dotenv() |
| 20 | + |
| 21 | +FEDERATEDCODE_GITHUB_ACCOUNT_NAME = os.getenv("FEDERATEDCODE_GITHUB_ACCOUNT_NAME") |
| 22 | + |
| 23 | + |
| 24 | +class ScanNotAvailableError(Exception): |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +def get_package_scan(purl: Union[PackageURL, str]): |
| 29 | + """Return the package scan result for a PURL from the FederatedCode Git repository.""" |
| 30 | + |
| 31 | + if not FEDERATEDCODE_GITHUB_ACCOUNT_NAME: |
| 32 | + raise ValueError("Provide ``FEDERATEDCODE_GITHUB_ACCOUNT_NAME`` in .env file.") |
| 33 | + |
| 34 | + if isinstance(purl, str): |
| 35 | + purl = PackageURL.from_string(purl) |
| 36 | + |
| 37 | + if not purl.version: |
| 38 | + raise ValueError("Missing version in PURL.") |
| 39 | + |
| 40 | + package_path = get_package_base_dir(purl=purl) |
| 41 | + package_path_parts = package_path.parts |
| 42 | + |
| 43 | + repo_name = f"{package_path_parts[0]}/refs/heads/main" |
| 44 | + package_dir_path = "/".join(package_path_parts[1:]) |
| 45 | + version = purl.version |
| 46 | + file_name = "scancodeio.json" |
| 47 | + |
| 48 | + url_parts = [FEDERATEDCODE_GITHUB_ACCOUNT_NAME, repo_name, package_dir_path, version, file_name] |
| 49 | + |
| 50 | + file_url = urljoin("https://raw.githubusercontent.com", "/".join(url_parts)) |
| 51 | + |
| 52 | + try: |
| 53 | + response = requests.get(file_url) |
| 54 | + response.raise_for_status() |
| 55 | + return response.json() |
| 56 | + except requests.exceptions.HTTPError as err: |
| 57 | + if response.status_code == 404: |
| 58 | + raise ScanNotAvailableError(f"No scan available for {purl!s}") |
| 59 | + raise err |
0 commit comments