]> git.decadent.org.uk Git - dak.git/blob - daklib/srcformats.py
merge from master
[dak.git] / daklib / srcformats.py
1 import re
2
3 from dak_exceptions import UnknownFormatError
4
5 srcformats = []
6
7 def get_format_from_string(txt):
8     """
9     Returns the SourceFormat class that corresponds to the specified .changes
10     Format value. If the string does not match any class, UnknownFormatError
11     is raised.
12     """
13
14     for format in srcformats:
15         if format.re_format.match(txt):
16             return format
17
18     raise UnknownFormatError, "Unknown format %r" % txt
19
20 class SourceFormat(type):
21     def __new__(cls, name, bases, attrs):
22         klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs)
23         srcformats.append(klass)
24
25         assert str(klass.name)
26         assert iter(klass.requires)
27         assert iter(klass.disallowed)
28
29         klass.re_format = re.compile(klass.format)
30
31         return klass
32
33     @classmethod
34     def reject_msgs(cls, has):
35         if len(cls.requires) != len([x for x in cls.requires if has[x]]):
36             yield "lack of required files for format %s" % cls.name
37
38         for key in cls.disallowed:
39             if has[key]:
40                 yield "contains source files not allowed in format %s" % cls.name
41
42 class FormatOne(SourceFormat):
43     __metaclass__ = SourceFormat
44
45     name = '1.0'
46     format = r'1.0'
47
48     requires = ()
49     disallowed = ('debian_tar', 'more_orig_tar')
50
51     @classmethod
52     def reject_msgs(cls, has):
53         if not (has['native_tar_gz'] or (has['orig_tar_gz'] and has['debian_diff'])):
54             yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field."
55         if has['native_tar_gz'] and has['debian_diff']:
56             yield "native package with diff makes no sense"
57         if (has['orig_tar_gz'] != has['orig_tar']) or \
58            (has['native_tar_gz'] != has['native_tar']):
59             yield "contains source files not allowed in format %s" % cls.name
60
61         for msg in super(FormatOne, cls).reject_msgs(has):
62             yield msg
63
64 class FormatThree(SourceFormat):
65     __metaclass__ = SourceFormat
66
67     name = '3.x (native)'
68     format = r'3\.\d+ \(native\)'
69
70     requires = ('native_tar',)
71     disallowed = ('orig_tar', 'debian_diff', 'debian_tar', 'more_orig_tar')
72
73 class FormatThreeQuilt(SourceFormat):
74     __metaclass__ = SourceFormat
75
76     name = '3.x (quilt)'
77     format = r'3\.\d+ \(quilt\)'
78
79     requires = ('orig_tar', 'debian_tar')
80     disallowed = ('debian_diff', 'native_tar')