Coverage for rust2rpm/distgit.py: 100%

3 statements  

« 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.""" 

2 

3from urllib.parse import urljoin 

4 

5import requests 

6 

7DIST_GIT_API_URL = "https://src.fedoraproject.org/api/0/" 

8 

9 

10def get_package_info(package: str) -> dict | None: # pragma nocover: requires internet access 

11 """Query src.fedoraproject.org API for information about a package. 

12 

13 Arguments: 

14 package: The (source) name of the package under the `rpms/` namespace. 

15 

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. 

19 

20 """ 

21 # check if the dist-git repository exists 

22 

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() 

26 

27 if "name" not in data: 

28 return None 

29 

30 # check if there is a spec file on the rawhide branch 

31 

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 += "/" 

37 

38 spec_url = urljoin(full_url, f"blob/rawhide/f/{package}.spec") 

39 req = requests.head(spec_url, headers={"User-Agent": "rust2rpm"}, timeout=10) 

40 

41 if not req.ok: 

42 return None 

43 

44 return data