forked from SUSE/kernel-source
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheader.py
More file actions
executable file
·478 lines (429 loc) · 15.8 KB
/
header.py
File metadata and controls
executable file
·478 lines (429 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env python3
# vim: sw=4 ts=4 et si:
import sys
import re
from optparse import OptionParser
from . import patch
from io import StringIO
diffstart = re.compile("^(---|\*\*\*|Index:|\+\+\+)[ \t][^ \t]\S+/|^diff -")
tag_regex = re.compile("(\S+):[ \t]*(.*)")
tag_map = {
'Patch-mainline' : {
'required' : True,
'required_on_kabi' : False,
'accepted' : [
{
# In mainline repo, tagged for release
'name' : 'Version',
'match': 'v?(\d+\.)+\d+(-rc\d+)?\s*$',
'requires' : [ 'Git-commit' ],
'excludes' : [ 'Git-repo' ],
}, { # In mainline repo but not tagged yet
'name' : 'Version',
'match': 'v?(\d+\.)+\d+(-rc\d+)?\s+or\s+v?(\d+\.)+\d+(-rc\d+)?\s+\(next release\)\s*$',
'requires' : [ 'Git-commit' ],
'excludes' : [ 'Git-repo' ],
}, {
# Queued in subsystem maintainer repo
'name' : 'Queued',
'match' : 'Queued(.*)',
'requires' : [ 'Git-commit', 'Git-repo' ],
}, {
# Depends on another non-upstream patch
'name' : 'Depends',
'match' : 'Depends on\s+.+',
'excludes' : [ 'Git-commit', 'Git-repo' ],
}, {
# No, typically used for patches that have review issues
# but are not fundamentally rejected
'name' : 'No',
'match' : 'No,?\s+.+',
'excludes' : [ 'Git-commit', 'Git-repo' ],
'error' : "Please use 'Not yet' or 'Never'",
}, {
# Never, typically used for patches that have been rejected
# for upstream inclusion but still have a compelling reason
# for inclusion in a SUSE release or are otherwise
# inappropriate for upstream inclusion (packaging, kABI,
# SLES-only feature.)
'name' : 'Never',
'match' : 'Never,?\s+.+',
'excludes' : [ 'Git-commit', 'Git-repo' ],
}, {
# Submitted upstream. Description should provide either
# a date and a mailing list or a URL to an archived post.
'name' : 'Submitted',
'match' : 'Submitted,?\s+.+',
'excludes' : [ 'Git-commit', 'Git-repo' ],
}, {
# Catch a frequent misuse of 'Not yet'.
'match' : 'Not yet,\s+submitted',
'error' : "Please use 'Submitted'",
'excludes' : [ 'Git-commit', 'Git-repo' ],
}, {
# Should be used rarely. Description should provide
# reason for the patch not being accepted upstream.
'name' : 'Not yet',
'match' : 'Not yet,?\s+.+',
'excludes' : [ 'Git-commit', 'Git-repo' ],
}, {
'match' : 'Submitted\s*$',
'error' : 'Requires a description: <date> <list> or <url>',
'excludes' : [ 'Git-commit', 'Git-repo' ],
}, {
'match' : 'Yes\s+$',
'error' : 'Exact version required',
}, {
'match' : 'Yes.*(\d+\.)+\d+',
'error' : '`Yes\' keyword is invalid',
'excludes' : [ 'Git-commit', 'Git-repo' ],
}, {
'match' : '(Never|Not yet|No)\s*$',
'error' : 'Requires a reason',
'excludes' : [ 'Git-commit', 'Git-repo' ],
},
],
'requires_any' : [ 'Signed-off-by:SUSE', 'Acked-by:SUSE', 'From:SUSE',
'Reviewed-by:SUSE' ],
},
'Git-commit' : {
'multi' : True,
'accepted' : [
{
# 40-character SHA1 hash with optional partial tag
'match' : '([0-9a-fA-F]){40}(\s+\(partial\))?',
}
],
'requires_any' : [ 'Patch-mainline:Version', 'Patch-mainline:Queued' ],
'error' : "requires full SHA1 hash and optional (partial) tag",
},
'Alt-commit' : {
'multi' : True,
'accepted' : [
{
# 40-character SHA1 hash
'match' : '([0-9a-fA-F]){40}$',
}
],
'error' : "requires one full SHA1 hash without trailing spaces",
},
'Git-repo' : {
'accepted' : [
{
# Weak match for URL. Abbreviated names are not acceptable.
'match' : '.*/.*',
}
],
'requires' : [ 'Git-commit' ],
'error' : "must contain URL pointing to repository containing the commit (and branch, if not master)",
},
'Signed-off-by' : {
'multi' : True,
'accepted' : [
{
'name' : 'SUSE',
'match' : '.*@suse\.(com|de|cz)',
},
{
'match' : '.*',
}
],
},
'Acked-by' : {
'multi' : True,
'accepted' : [
{
'name' : 'SUSE',
'match' : '.*@suse\.(com|de|cz)',
},
{
'match' : '.*',
}
],
},
'Reviewed-by' : {
'multi' : True,
'accepted' : [
{
'name' : 'SUSE',
'match' : '.*@suse\.(com|de|cz)',
},
{
'match' : '.*',
}
],
},
'From' : {
'multi' : True,
'required' : True,
'accepted' : [
{
'name' : 'SUSE',
'match' : '.*@suse\.(com|de|cz)',
},
{
'match' : '.*',
}
],
},
'Subject' : {
'required' : True,
'accepted' : [
{
'match' : '\S+',
},
],
},
'References' : {
'required' : True,
'required_on_update' : False,
'multi' : True,
'accepted' : [
{
'name' : 'SUSE',
'match' : '((bsc|boo|bnc|fate)#\d+|jsc#\w+-\d+)',
},
{
'match' : '\S+',
},
],
'error' : "must contain list of references",
# Enable to require real References tags
# 'requires' : ['References:SUSE'],
}
}
class ValidationError(patch.ValidationError):
def __init__(self, name, msg):
super(ValidationError, self).__init__(msg)
self.name = name
class FormatError(ValidationError):
def __init__(self, name, value, error=None):
name = name.capitalize()
if error is None:
error = "invalid value"
msg = "%s: `%s': %s." % (name, value, error)
super(FormatError, self).__init__(name, msg)
class MissingTagError(ValidationError):
def __init__(self, tag, requires):
if not tag:
tag = Tag("Policy", None)
msg = "%s%s requires %s%s." % (tag.name, \
" (%s)" % tag.tagtype if tag.tagtype else "", \
requires['name'], \
" (%s)" % requires['type'] if 'type' in requires else "")
self.target = [requires]
super(MissingTagError, self).__init__(tag.name, msg)
class MissingMultiTagError(MissingTagError):
def __init__(self, tag, requires):
if not tag:
tag = Tag("Policy", None)
msg = "%s%s requires %s." % (tag.name, \
" (%s)" % tag.tagtype if tag.tagtype else "", \
" or ".join(["%s%s" % (req['name'], \
" (%s)" % req['type'] if 'type' in req else "") for req in requires]))
self.target = requires
super(MissingTagError, self).__init__(tag.name, msg)
class ExcludedTagError(ValidationError):
def __init__(self, tag, excludes):
msg = "%s%s excludes %s%s." % (tag.name,
" (%s)" % tag.tagtype if tag.tagtype else "", \
excludes['name'], \
" (%s)" % excludes['type'] if 'type' in excludes else "")
super(ExcludedTagError, self).__init__(tag.name, msg)
class DuplicateTagError(ValidationError):
def __init__(self, name):
name = name.capitalize()
msg = "%s must only be used once, even if it is identical." % name
super(DuplicateTagError, self).__init__(name, msg)
pass
class EmptyTagError(ValidationError):
def __init__(self, name):
name = name.capitalize()
msg = "%s: Value cannot be empty." % name
super(EmptyTagError, self).__init__(name, msg)
class HeaderException(patch.PatchException):
def tag_is_missing(self, name):
try:
for err in self._errors:
if isinstance(err, MissingTagError):
for tag in err.target:
if tag['name'].lower() == name.lower():
return True
except KeyError as e:
pass
return False
class Tag:
def __init__(self, name, value):
self.name = name.lower().capitalize()
self.value = value
self.tagtype = None
self.valid = False
def __str__(self):
return "%s: %s" % (self.name, self.value)
def __repr__(self):
type = "<none>"
if self.tagtype:
type = self.tagtype
valid = "No"
if self.valid:
valid = "Yes"
return "<Tag: name=%s value='%s' type='%s' valid='%s'>" % \
(self.name, self.value, type, valid)
def match_req(self, req):
if self.name == req['name']:
if 'type' not in req or self.tagtype == req['type']:
if self.valid:
return True
return False
class HeaderChecker(patch.PatchChecker):
def __init__(self, stream, updating=False, filename="<unknown>"):
patch.PatchChecker.__init__(self)
self.updating = updating
self.filename = filename
self.kabi = re.match("^patches[.]kabi/", self.filename)
if isinstance(stream, str):
stream = StringIO(stream)
self.stream = stream
self.requires = {}
self.requires_any = {}
self.excludes = {}
self.tags = []
self.errors = []
self.do_patch()
def get_rulename(self, ruleset, rulename):
if rulename in ruleset:
if self.kabi:
kabi_rule = "%s_on_kabi" % rulename
if kabi_rule in ruleset:
return kabi_rule
if self.updating:
updating_rule = "%s_on_update" % rulename
if updating_rule in ruleset:
return updating_rule
return rulename
return None
def handle_requires(self, tag, ruleset, rulename):
target = getattr(self, rulename)
rulename = self.get_rulename(ruleset, rulename)
if not rulename:
return
if isinstance(tag, str):
tag = Tag(tag, None)
for req in ruleset[rulename]:
s = req.split(':')
new_req = {
'name' : s[0]
}
if len(s) > 1:
new_req['type'] = s[1]
if not tag in target:
target[tag] = []
target[tag].append(new_req)
def do_patch(self):
for line in self.stream.readlines():
if diffstart.match(line):
break
m = tag_regex.match(line)
if m:
tag = Tag(m.group(1), m.group(2))
if tag.name not in tag_map:
continue;
if re.match("\s*$", tag.value):
self.errors.append(EmptyTagError(tag.name))
continue
mapping = tag_map[tag.name]
try:
multi = mapping['multi']
except KeyError as e:
multi = False
for t in self.tags:
if tag.name == t.name and not multi:
self.errors.append(DuplicateTagError(tag.name))
continue
# No rules to process
if 'accepted' not in mapping:
self.tags.append(tag)
continue
match = False
error = False
for rule in mapping['accepted']:
if not re.match(rule['match'], tag.value, re.I):
continue
match = True
if 'name' in rule:
tag.tagtype = rule['name']
if 'error' in rule:
error = True
self.errors.append(FormatError(tag.name, tag.value,
rule['error']))
break
tag.valid = True
# Handle rule-level dependencies
self.handle_requires(tag, rule, 'requires')
self.handle_requires(tag, rule, 'requires_any')
self.handle_requires(tag, rule, 'excludes')
break
# Handle tag-level dependencies
if tag.valid:
self.handle_requires(tag, mapping, 'requires')
self.handle_requires(tag, mapping, 'requires_any')
self.handle_requires(tag, mapping, 'excludes')
self.tags.append(tag)
if error:
continue
if not match:
errmsg = None
if 'error' in mapping:
errmsg = mapping['error']
self.errors.append(FormatError(tag.name, tag.value, errmsg))
continue
for reqtag in self.requires:
for req in self.requires[reqtag]:
found = False
for tag in self.tags:
found = tag.match_req(req)
if found:
break
if not found:
self.errors.append(MissingTagError(reqtag, req))
for reqtag in self.requires_any:
found = False
for req in self.requires_any[reqtag]:
for tag in self.tags:
found = tag.match_req(req)
if found:
break
if found:
break
if not found:
self.errors.append(
MissingMultiTagError(reqtag, self.requires_any[reqtag]))
for reqtag in self.excludes:
for req in self.excludes[reqtag]:
found = False
for tag in self.tags:
found = tag.match_req(req)
if found:
break
if found:
self.errors.append(ExcludedTagError(reqtag, req))
for entry in tag_map:
if 'required' in tag_map[entry]:
found = False
for tag in self.tags:
if entry == tag.name:
found = True
if not found:
required = True
if self.kabi and 'required_on_kabi' in tag_map[entry]:
if not tag_map[entry]['required_on_kabi']:
required = False
if self.updating and 'required_on_update' in tag_map[entry]:
if not tag_map[entry]['required_on_update']:
required = False
if required:
self.errors.append(MissingTagError(None,
{ 'name' : entry }))
if self.errors:
raise HeaderException(self.errors)
Checker = HeaderChecker