Coverage for rust2rpm/distgit.py: 100%
3 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 18:36 +0100
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 18:36 +0100
1"""Module containing functionality for interacting with the <https://src.fedoraproject.org> API."""
3from urllib.parse import urljoin
5import requests
7DIST_GIT_API_URL = "https://src.fedoraproject.org/api/0/"
10def get_package_info(package: str) -> dict | None: # pragma nocover: requires internet access
11 """Query src.fedoraproject.org API for information about a package.
13 Arguments:
14 package: The (source) name of the package under the `rpms/` namespace.
16 Returns:
17 Metadata in JSON format if the package exists in Fedora and is not
18 retired in Rawhide, or `None` if the package is not present in Fedora.
20 """
21 # check if the dist-git repository exists
23 url = urljoin(DIST_GIT_API_URL, f"rpms/{package}")
24 req = requests.get(url, headers={"User-Agent": "rust2rpm"}, timeout=10)
25 data = req.json()
27 if "name" not in data:
28 return None
30 # check if there is a spec file on the rawhide branch
32 full_url = data["full_url"]
33 if not full_url.endswith("/"):
34 # ensure the base URL has a trailing slash;
35 # otherwise urljoin mangles the URL
36 full_url += "/"
38 spec_url = urljoin(full_url, f"blob/rawhide/f/{package}.spec")
39 req = requests.head(spec_url, headers={"User-Agent": "rust2rpm"}, timeout=10)
41 if not req.ok:
42 return None
44 return data