]> git.decadent.org.uk Git - dak.git/blob - daklib/srcformats.py
Add by-hash support
[dak.git] / daklib / srcformats.py
1 #!/usr/bin/python
2
3 """ Helper functions for the various source formats
4
5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2009, 2010  Joerg Jaspert <joerg@debian.org>
7 @copyright: 2009  Chris Lamb <lamby@debian.org>
8 @license: GNU General Public License version 2 or later
9 """
10
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 ################################################################################
26
27 # <sgran> hey, I think something's wrong with your git repo
28 # <sgran> when I git pulled this last time, I got something that looked almost
29 #         like python instead of dak
30 # <mhy> sgran: slander
31 # <sgran> sorry, I take it back, I've had a better look now
32
33 ################################################################################
34 import re
35
36 from dak_exceptions import UnknownFormatError
37
38 srcformats = []
39
40 def get_format_from_string(txt):
41     """
42     Returns the SourceFormat class that corresponds to the specified .changes
43     Format value. If the string does not match any class, UnknownFormatError
44     is raised.
45     """
46
47     for format in srcformats:
48         if format.re_format.match(txt):
49             return format
50
51     raise UnknownFormatError("Unknown format %r" % txt)
52
53 class SourceFormat(type):
54     def __new__(cls, name, bases, attrs):
55         klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs)
56         srcformats.append(klass)
57
58         assert str(klass.name)
59         assert iter(klass.requires)
60         assert iter(klass.disallowed)
61
62         klass.re_format = re.compile(klass.format)
63
64         return klass
65
66     @classmethod
67     def reject_msgs(cls, has):
68         if len(cls.requires) != len([x for x in cls.requires if has[x]]):
69             yield "lack of required files for format %s" % cls.name
70
71         for key in cls.disallowed:
72             if has[key]:
73                 yield "contains source files not allowed in format %s" % cls.name
74
75 class FormatOne(SourceFormat):
76     __metaclass__ = SourceFormat
77
78     name = '1.0'
79     format = r'1\.0'
80
81     requires = ()
82     disallowed = ('debian_tar', 'more_orig_tar')
83
84     @classmethod
85     def reject_msgs(cls, has):
86         if not (has['native_tar_gz'] or (has['orig_tar_gz'] and has['debian_diff'])):
87             yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field."
88         if has['native_tar_gz'] and has['debian_diff']:
89             yield "native package with diff makes no sense"
90         if (has['orig_tar_gz'] != has['orig_tar']) or \
91            (has['native_tar_gz'] != has['native_tar']):
92             yield "contains source files not allowed in format %s" % cls.name
93
94         for msg in super(FormatOne, cls).reject_msgs(has):
95             yield msg
96
97 class FormatThree(SourceFormat):
98     __metaclass__ = SourceFormat
99
100     name = '3.x (native)'
101     format = r'3\.\d+ \(native\)'
102
103     requires = ('native_tar',)
104     disallowed = ('orig_tar', 'debian_diff', 'debian_tar', 'more_orig_tar')
105
106 class FormatThreeQuilt(SourceFormat):
107     __metaclass__ = SourceFormat
108
109     name = '3.x (quilt)'
110     format = r'3\.\d+ \(quilt\)'
111
112     requires = ('orig_tar', 'debian_tar')
113     disallowed = ('debian_diff', 'native_tar')