-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
99 lines (72 loc) · 2.96 KB
/
Copy path__init__.py
File metadata and controls
99 lines (72 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# FederatedCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/federatedcode for support or download.
# See https://aboutcode.org for more information about AboutCode.org OSS projects.
#
import os
from typing import Union
from urllib.parse import quote
from urllib.parse import urljoin
import requests
from dotenv import load_dotenv
from packageurl import PackageURL
from aboutcode.hashid import get_package_base_dir
load_dotenv()
FEDERATEDCODE_GIT_RAW_URL = os.getenv(
"FEDERATEDCODE_GIT_RAW_URL",
"https://raw.githubusercontent.com/aboutcode-org/",
)
FEDERATEDCODE_AP_HOST = os.getenv(
"FEDERATEDCODE_AP_HOST",
"http://localhost:8000/",
)
class ScanNotAvailableError(Exception):
pass
def get_package_scan(purl: Union[PackageURL, str]):
"""Return the package scan result for a PURL from the FederatedCode Git repository."""
if not FEDERATEDCODE_GIT_RAW_URL:
raise ValueError("Provide ``FEDERATEDCODE_GIT_RAW_URL`` in .env file.")
if isinstance(purl, str):
purl = PackageURL.from_string(purl)
if not purl.version:
raise ValueError("Missing version in PURL.")
package_path = get_package_base_dir(purl=purl)
package_path_parts = package_path.parts
repo_name = f"{package_path_parts[0]}/refs/heads/main"
package_dir_path = "/".join(package_path_parts[1:])
version = purl.version
file_name = "scancodeio.json"
url_parts = [repo_name, package_dir_path, version, file_name]
file_url = urljoin(FEDERATEDCODE_GIT_RAW_URL, "/".join(url_parts))
try:
response = requests.get(file_url)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as err:
if response.status_code == 404:
raise ScanNotAvailableError(f"No scan available for {purl!s}")
raise err
def subscribe_package(federatedcode_host, remote_username, purl):
"""Subscribe package for their metadata update from FederatedCode."""
url_path = f"api/v0/users/@{remote_username}/subscribe/?purl={quote(purl)}"
return requests.get(urljoin(federatedcode_host, url_path))
def discover_package_in_ap_server(purl: Union[str, PackageURL]):
"""Return package profile if PURL exists in AP server."""
if not FEDERATEDCODE_AP_HOST:
raise ValueError("Provide ``FEDERATEDCODE_AP_HOST`` in .env file.")
if isinstance(purl, str):
purl = PackageURL.from_string(purl)
if purl.version or purl.subpath or purl.qualifiers:
purl = PackageURL(
type=purl.type,
namespace=purl.name,
name=purl.name,
)
package = quote(str(purl), safe=":/")
url = urljoin(FEDERATEDCODE_AP_HOST, f"/purls/@{package}")
response = requests.head(url, allow_redirects=True)
if response.status_code == 200:
return url