]> git.decadent.org.uk Git - dak.git/blob - daklib/srcformats.py
Move "Format:" field parsing into srcformats.py
[dak.git] / daklib / srcformats.py
1 import re
2
3 from regexes import re_verwithext
4 from dak_exceptions import UnknownFormatError
5
6 srcformats = []
7
8 class SourceFormat(type):
9     def __new__(cls, name, bases, attrs):
10         klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs)
11         srcformats.append(klass)
12
13         assert str(klass.name)
14         assert iter(klass.requires)
15         assert iter(klass.disallowed)
16
17         klass.re_format = re.compile(klass.format)
18
19         return klass
20
21     @classmethod
22     def reject_msgs(cls, has):
23         if len(cls.requires) != len([x for x in cls.requires if has[x]]):
24             yield "lack of required files for format %s" % cls.name
25
26         for key in cls.disallowed:
27             if has[key]:
28                 yield "contains source files not allowed in format %s" % cls.name
29
30     @classmethod
31     def parse_format(cls, txt, is_a_dsc=False, field='files'):
32         format = re_verwithext.search(txt)
33         if not format:
34             raise UnknownFormatError, txt
35
36         format = format.groups()
37         if format[1] == None:
38             format = int(float(format[0])), 0, format[2]
39         else:
40             format = int(format[0]), int(format[1]), format[2]
41         if format[2] == None:
42             format = format[:2]
43
44         if is_a_dsc:
45             # format = (0,0) are missing format headers of which we still
46             # have some in the archive.
47             if format != (1,0) and format != (0,0) and \
48                format != (3,0,"quilt") and format != (3,0,"native"):
49                 raise UnknownFormatError, txt
50         else:
51             if (format < (1,5) or format > (1,8)):
52                 raise UnknownFormatError, txt
53             if field != "files" and format < (1,8):
54                 raise UnknownFormatError, txt
55
56         return format
57
58 class FormatOne(SourceFormat):
59     __metaclass__ = SourceFormat
60
61     name = '1.0'
62     format = r'1.0'
63
64     requires = ()
65     disallowed = ('debian_tar', 'more_orig_tar')
66
67     @classmethod
68     def reject_msgs(cls, has):
69         if not (has['native_tar_gz'] or (has['orig_tar_gz'] and has['debian_diff'])):
70             yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field."
71         if has['native_tar_gz'] and has['debian_diff']:
72             yield "native package with diff makes no sense"
73         if (has['orig_tar_gz'] != has['orig_tar']) or \
74            (has['native_tar_gz'] != has['native_tar']):
75             yield "contains source files not allowed in format %s" % cls.name
76
77         for msg in super(FormatOne, cls).reject_msgs(has):
78             yield msg
79
80 class FormatThree(SourceFormat):
81     __metaclass__ = SourceFormat
82
83     name = '3.x (native)'
84     format = r'3\.\d+ \(native\)'
85
86     requires = ('native_tar',)
87     disallowed = ('orig_tar', 'debian_diff', 'debian_tar', 'more_orig_tar')
88
89 class FormatThreeQuilt(SourceFormat):
90     __metaclass__ = SourceFormat
91
92     name = '3.x (quilt)'
93     format = r'3\.\d+ \(quilt\)'
94
95     requires = ('orig_tar', 'debian_tar')
96     disallowed = ('debian_diff', 'native_tar')