]> git.decadent.org.uk Git - dak.git/blob - daklib/srcformats.py
Split out parse_format to module-level and test it explicitly.
[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 def parse_format(txt):
9     """
10     Parse a .changes Format string into a tuple representation for easy
11     comparison.
12
13     >>> parse_format('1.0')
14     (1, 0)
15     >>> parse_format('8.4 (hardy)')
16     (8, 4, 'hardy')
17
18     If the format doesn't match these forms, raises UnknownFormatError.
19     """
20
21     format = re_verwithext.search(txt)
22
23     if format is None:
24         raise UnknownFormatError, txt
25
26     format = format.groups()
27
28     if format[1] is None:
29         format = int(float(format[0])), 0, format[2]
30     else:
31         format = int(format[0]), int(format[1]), format[2]
32
33     if format[2] is None:
34         format = format[:2]
35
36     return format
37
38 class SourceFormat(type):
39     def __new__(cls, name, bases, attrs):
40         klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs)
41         srcformats.append(klass)
42
43         assert str(klass.name)
44         assert iter(klass.requires)
45         assert iter(klass.disallowed)
46
47         klass.re_format = re.compile(klass.format)
48
49         return klass
50
51     @classmethod
52     def reject_msgs(cls, has):
53         if len(cls.requires) != len([x for x in cls.requires if has[x]]):
54             yield "lack of required files for format %s" % cls.name
55
56         for key in cls.disallowed:
57             if has[key]:
58                 yield "contains source files not allowed in format %s" % cls.name
59
60     @classmethod
61     def validate_format(cls, format, is_a_dsc=False, field='files'):
62         if is_a_dsc:
63             if format != (1,0) and \
64                format != (3,0,"quilt") and format != (3,0,"native"):
65                 raise UnknownFormatError, repr(format)
66         else:
67             if (format < (1,5) or format > (1,8)):
68                 raise UnknownFormatError, repr(format)
69             if field != "files" and format < (1,8):
70                 raise UnknownFormatError, repr(format)
71
72 class FormatOne(SourceFormat):
73     __metaclass__ = SourceFormat
74
75     name = '1.0'
76     format = r'1.0'
77
78     requires = ()
79     disallowed = ('debian_tar', 'more_orig_tar')
80
81     @classmethod
82     def reject_msgs(cls, has):
83         if not (has['native_tar_gz'] or (has['orig_tar_gz'] and has['debian_diff'])):
84             yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field."
85         if has['native_tar_gz'] and has['debian_diff']:
86             yield "native package with diff makes no sense"
87         if (has['orig_tar_gz'] != has['orig_tar']) or \
88            (has['native_tar_gz'] != has['native_tar']):
89             yield "contains source files not allowed in format %s" % cls.name
90
91         for msg in super(FormatOne, cls).reject_msgs(has):
92             yield msg
93
94 class FormatThree(SourceFormat):
95     __metaclass__ = SourceFormat
96
97     name = '3.x (native)'
98     format = r'3\.\d+ \(native\)'
99
100     requires = ('native_tar',)
101     disallowed = ('orig_tar', 'debian_diff', 'debian_tar', 'more_orig_tar')
102
103 class FormatThreeQuilt(SourceFormat):
104     __metaclass__ = SourceFormat
105
106     name = '3.x (quilt)'
107     format = r'3\.\d+ \(quilt\)'
108
109     requires = ('orig_tar', 'debian_tar')
110     disallowed = ('debian_diff', 'native_tar')