Coverage for src/gitlabracadabra/packages/raw.py: 93%

1#

2# Copyright (C) 2019-2025 Mathieu Parent <math.parent@gmail.com>

3#

4# This program is free software: you can redistribute it and/or modify

5# it under the terms of the GNU Lesser General Public License as published by

6# the Free Software Foundation, either version 3 of the License, or

7# (at your option) any later version.

8#

9# This program is distributed in the hope that it will be useful,

10# but WITHOUT ANY WARRANTY; without even the implied warranty of

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

12# GNU Lesser General Public License for more details.

13#

14# You should have received a copy of the GNU Lesser General Public License

15# along with this program. If not, see <http://www.gnu.org/licenses/>.

16

17 from __future__ import annotations

18

19 from typing import TYPE_CHECKING

20

21 from gitlabracadabra.packages.package_file import PackageFile

22 from gitlabracadabra.packages.source import Source

23

24 if TYPE_CHECKING:

25 from typing import TypedDict

26

27 from gitlabracadabra.packages.destination import Destination

28

29 class PackageFileArgs(TypedDict, total=False):

30 url: str

31 package_name: str

32 package_version: str

33 file_name: str

34

35

36 class RawSource(Source):

37 """Raw urls repository."""

38

39 def __init__(

40 self,

41 *,

42 log_prefix: str = "",

43 default_url: str,

44 default_package_name: str | None = None,

45 default_package_version: str | None = None,

46 package_files: list[PackageFileArgs] | None = None,

47 ) -> None:

48 """Initialize a Raw Source object.

49

50 Args:

51 log_prefix: Log prefix.

52 default_url: Default package file URL.

53 default_package_name: Default package name.

54 default_package_version: Default package version.

55 package_files: Package files.

56 """

57 super().__init__()

58 self._log_prefix = log_prefix

59 self._default_url = default_url

60 self._default_package_name = default_package_name or "unknown"

61 self._default_package_version = default_package_version or "0"

62 self._package_files: list[PackageFileArgs] = package_files or [{}]

63

64 def __str__(self) -> str:

65 """Return string representation.

66

67 Returns:

68 A string.

69 """

70 return f"Raw repository (default_url={self._default_url})"

71

72 def package_files(

73 self,

74 destination: Destination, # noqa: ARG002

75 ) -> list[PackageFile]:

76 """Return list of package files.

77

78 Returns:

79 List of package files.

80 """

81 package_files: list[PackageFile] = []

82 for package_file_args in self._package_files:

83 package_file = self._package_file(package_file_args)

84 if package_file: 84 ↛ 82line 84 didn't jump to line 82 because the condition on line 84 was always true

85 package_files.append(package_file)

86 return package_files

87

88 def _package_file(self, package_file_args: PackageFileArgs) -> PackageFile | None:

89 url = package_file_args.get("url") or self._default_url

90 if not url: 90 ↛ 91line 90 didn't jump to line 91 because the condition on line 90 was never true

91 return None

92 package_name = package_file_args.get("package_name") or self._default_package_name

93 package_version = package_file_args.get("package_version") or self._default_package_version

94 file_name = package_file_args.get("file_name")

95 default_url = self._default_url.format(

96 default_package_name=self._default_package_name,

97 default_package_version=self._default_package_version,

98 package_name=package_name,

99 package_version=package_version,

100 file_name=file_name or "{file_name}",

101 )

102 url = url.format(

103 default_url=default_url,

104 default_package_name=self._default_package_name,

105 default_package_version=self._default_package_version,

106 package_name=package_name,

107 package_version=package_version,

108 file_name=file_name or "{file_name}",

109 )

110 if not file_name:

111 file_name = url.split("/").pop()

112 return PackageFile(

113 url,

114 "generic",

115 package_name,

116 package_version,

117 file_name,

118 )