3 from regexes import re_verwithext
4 from dak_exceptions import UnknownFormatError
8 def get_format_from_string(txt):
10 Returns the SourceFormat class that corresponds to the specified .changes
11 Format value. If the string does not match any class, UnknownFormatError
15 for format in srcformats:
16 if format.re_format.match(txt):
19 raise UnknownFormatError, "Unknown format %r" % txt
21 def parse_format(txt):
23 Parse a .changes Format string into a tuple representation for easy
26 >>> parse_format('1.0')
28 >>> parse_format('8.4 (hardy)')
31 If the format doesn't match these forms, raises UnknownFormatError.
34 format = re_verwithext.search(txt)
37 raise UnknownFormatError, txt
39 format = format.groups()
42 format = int(float(format[0])), 0, format[2]
44 format = int(format[0]), int(format[1]), format[2]
51 class SourceFormat(type):
52 def __new__(cls, name, bases, attrs):
53 klass = super(SourceFormat, cls).__new__(cls, name, bases, attrs)
54 srcformats.append(klass)
56 assert str(klass.name)
57 assert iter(klass.requires)
58 assert iter(klass.disallowed)
60 klass.re_format = re.compile(klass.format)
65 def reject_msgs(cls, has):
66 if len(cls.requires) != len([x for x in cls.requires if has[x]]):
67 yield "lack of required files for format %s" % cls.name
69 for key in cls.disallowed:
71 yield "contains source files not allowed in format %s" % cls.name
74 def validate_format(cls, format, is_a_dsc=False, field='files'):
76 Raises UnknownFormatError if the specified format tuple is not valid for
77 this format (for example, the format (1, 0) is not valid for the
78 "3.0 (quilt)" format). Return value is undefined in all other cases.
82 class FormatOne(SourceFormat):
83 __metaclass__ = SourceFormat
89 disallowed = ('debian_tar', 'more_orig_tar')
92 def reject_msgs(cls, has):
93 if not (has['native_tar_gz'] or (has['orig_tar_gz'] and has['debian_diff'])):
94 yield "no .tar.gz or .orig.tar.gz+.diff.gz in 'Files' field."
95 if has['native_tar_gz'] and has['debian_diff']:
96 yield "native package with diff makes no sense"
97 if (has['orig_tar_gz'] != has['orig_tar']) or \
98 (has['native_tar_gz'] != has['native_tar']):
99 yield "contains source files not allowed in format %s" % cls.name
101 for msg in super(FormatOne, cls).reject_msgs(has):
105 def validate_format(cls, format, is_a_dsc=False, field='files'):
106 msg = "Invalid format %s definition: %r" % (cls.name, format)
110 raise UnknownFormatError, msg
112 if (format < (1,5) or format > (1,8)):
113 raise UnknownFormatError, msg
114 if field != "files" and format < (1,8):
115 raise UnknownFormatError, msg
117 class FormatThree(SourceFormat):
118 __metaclass__ = SourceFormat
120 name = '3.x (native)'
121 format = r'3\.\d+ \(native\)'
123 requires = ('native_tar',)
124 disallowed = ('orig_tar', 'debian_diff', 'debian_tar', 'more_orig_tar')
127 def validate_format(cls, format, **kwargs):
128 if format != (3, 0, 'native'):
129 raise UnknownFormatError, "Invalid format %s definition: %r" % \
132 class FormatThreeQuilt(SourceFormat):
133 __metaclass__ = SourceFormat
136 format = r'3\.\d+ \(quilt\)'
138 requires = ('orig_tar', 'debian_tar')
139 disallowed = ('debian_diff', 'native_tar')
142 def validate_format(cls, format, **kwargs):
143 if format != (3, 0, 'quilt'):
144 raise UnknownFormatError, "Invalid format %s definition: %r" % \