]> git.decadent.org.uk Git - dak.git/blob - daklib/srcformats.py
Split parsing of "Format:" string and validation of it.
[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):
32         format = re_verwithext.search(txt)
33
34         if format is None:
35             raise UnknownFormatError, txt
36
37         format = format.groups()
38
39         if format[1] is None:
40             format = int(float(format[0])), 0, format[2]
41         else:
42             format = int(format[0]), int(format[1]), format[2]
43
44         if format[2] is None:
45             format = format[:2]
46
47         return format
48
49     @classmethod
50     def validate_format(cls, format, is_a_dsc=False, field='files'):
51         if is_a_dsc:
52             if format != (1,0) and \
53                format != (3,0,"quilt") and format != (3,0,"native"):
54                 raise UnknownFormatError, repr(format)
55         else:
56             if (format < (1,5) or format > (1,8)):
57                 raise UnknownFormatError, repr(format)
58             if field != "files" and format < (1,8):
59                 raise UnknownFormatError, repr(format)
60
61 class FormatOne(SourceFormat):
62     __metaclass__ = SourceFormat
63
64     name = '1.0'
65     format = r'1.0'
66
67     requires = ()
68     disallowed = ('debian_tar', 'more_orig_tar')
69
70     @classmethod
71     def reject_msgs(cls, has):
72         if not (has['native_tar_gz'] or (has['orig_tar_gz'] and has['debian_diff'])):
73             yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field."
74         if has['native_tar_gz'] and has['debian_diff']:
75             yield "native package with diff makes no sense"
76         if (has['orig_tar_gz'] != has['orig_tar']) or \
77            (has['native_tar_gz'] != has['native_tar']):
78             yield "contains source files not allowed in format %s" % cls.name
79
80         for msg in super(FormatOne, cls).reject_msgs(has):
81             yield msg
82
83 class FormatThree(SourceFormat):
84     __metaclass__ = SourceFormat
85
86     name = '3.x (native)'
87     format = r'3\.\d+ \(native\)'
88
89     requires = ('native_tar',)
90     disallowed = ('orig_tar', 'debian_diff', 'debian_tar', 'more_orig_tar')
91
92 class FormatThreeQuilt(SourceFormat):
93     __metaclass__ = SourceFormat
94
95     name = '3.x (quilt)'
96     format = r'3\.\d+ \(quilt\)'
97
98     requires = ('orig_tar', 'debian_tar')
99     disallowed = ('debian_diff', 'native_tar')