Coverage for daklib/srcformats.py: 98%

56 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2026-05-10 21:38 +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, override 

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 @override 

95 def reject_msgs(cls, has): 

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

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

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

99 yield "native package with diff makes no sense" 

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

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

102 ): 

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

104 

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

106 yield msg 

107 

108 

109class FormatThree(SourceFormat, metaclass=SourceFormat): 

110 name = "3.x (native)" 

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

112 

113 requires = ("native_tar",) 

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

115 

116 

117class FormatThreeQuilt(SourceFormat, metaclass=SourceFormat): 

118 name = "3.x (quilt)" 

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

120 

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

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