Coverage for daklib/srcformats.py: 97%

55 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2026-01-04 16:18 +0000

1"""Helper functions for the various source formats 

2 

3@contact: Debian FTPMaster <ftpmaster@debian.org> 

4@copyright: 2009, 2010 Joerg Jaspert <joerg@debian.org> 

5@copyright: 2009 Chris Lamb <lamby@debian.org> 

6@license: GNU General Public License version 2 or later 

7""" 

8 

9# This program is free software; you can redistribute it and/or modify 

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

11# the Free Software Foundation; either version 2 of the License, or 

12# (at your option) any later version. 

13 

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

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

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

17# GNU General Public License for more details. 

18 

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

20# along with this program; if not, write to the Free Software 

21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

22 

23################################################################################ 

24 

25# <sgran> hey, I think something's wrong with your git repo 

26# <sgran> when I git pulled this last time, I got something that looked almost 

27# like python instead of dak 

28# <mhy> sgran: slander 

29# <sgran> sorry, I take it back, I've had a better look now 

30 

31################################################################################ 

32 

33import re 

34from typing import ClassVar 

35 

36from .dak_exceptions import UnknownFormatError 

37 

38srcformats: "list[SourceFormat]" = [] 

39 

40 

41def get_format_from_string(txt: str): 

42 """ 

43 Returns the SourceFormat class that corresponds to the specified .changes 

44 Format value. If the string does not match any class, UnknownFormatError 

45 is raised. 

46 """ 

47 

48 for format in srcformats: 

49 if format.re_format.match(txt): 

50 return format 

51 

52 raise UnknownFormatError("Unknown format %r" % txt) 

53 

54 

55class SourceFormat(type): 

56 name: ClassVar[str] 

57 format: ClassVar[str] 

58 

59 requires: ClassVar[tuple[str, ...]] 

60 disallowed: ClassVar[tuple[str, ...]] 

61 

62 re_format: re.Pattern 

63 

64 def __new__(cls, name, bases, attrs): 

65 klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs) 

66 srcformats.append(klass) 

67 

68 assert str(klass.name) 

69 assert iter(klass.requires) 

70 assert iter(klass.disallowed) 

71 

72 klass.re_format = re.compile(klass.format) 

73 

74 return klass 

75 

76 @classmethod 

77 def reject_msgs(cls, has): 

78 if len(cls.requires) != len([x for x in cls.requires if has[x]]): 

79 yield "lack of required files for format %s" % cls.name 

80 

81 for key in cls.disallowed: 

82 if has[key]: 

83 yield "contains source files not allowed in format %s" % cls.name 

84 

85 

86class FormatOne(SourceFormat, metaclass=SourceFormat): 

87 name = "1.0" 

88 format = r"1\.0" 

89 

90 requires = () 

91 disallowed = ("debian_tar", "more_orig_tar") 

92 

93 @classmethod 

94 def reject_msgs(cls, has): 

95 if not (has["native_tar_gz"] or (has["orig_tar_gz"] and has["debian_diff"])): 

96 yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field." 

97 if has["native_tar_gz"] and has["debian_diff"]: 

98 yield "native package with diff makes no sense" 

99 if (has["orig_tar_gz"] != has["orig_tar"]) or ( 99 ↛ 102line 99 didn't jump to line 102 because the condition on line 99 was never true

100 has["native_tar_gz"] != has["native_tar"] 

101 ): 

102 yield "contains source files not allowed in format %s" % cls.name 

103 

104 for msg in super(FormatOne, cls).reject_msgs(has): 

105 yield msg 

106 

107 

108class FormatThree(SourceFormat, metaclass=SourceFormat): 

109 name = "3.x (native)" 

110 format = r"3\.\d+ \(native\)" 

111 

112 requires = ("native_tar",) 

113 disallowed = ("orig_tar", "debian_diff", "debian_tar", "more_orig_tar") 

114 

115 

116class FormatThreeQuilt(SourceFormat, metaclass=SourceFormat): 

117 name = "3.x (quilt)" 

118 format = r"3\.\d+ \(quilt\)" 

119 

120 requires = ("orig_tar", "debian_tar") 

121 disallowed = ("debian_diff", "native_tar")