-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspecmodel.py
More file actions
497 lines (412 loc) · 20 KB
/
specmodel.py
File metadata and controls
497 lines (412 loc) · 20 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from __future__ import print_function
from abstract_model import BlockTypes, BlockTypeUnknown
from metastring import HeaderTagMetastring, SectionMetastring, ConditionMetastring, MacroConditionMetastring, MacroDefinitionMetastring, CommentMetastring, ChangelogMetastring, PackageMetastring, MMetastring, WhitespacesMetastring, UninterpretedMetastring
# Condition-free layout of a specfile 2.0
#
# Metadata:
# - Tags: []
# - Macros: []
# Packages: []
# Descriptions: []
# Files: []
# Prep: {}
# Install: {}
# Changelog: {}
# OtherSections: {}
#
# Integration of Conditions:
# - register each condition expression in a conditioner table
# - have metastrings of conditions refer to the conditioner table
# - propagation of contidion is managed on a level of the layout (above)
# each block conditions refer corresponding condition expression on the conditioner table
# - the conditioner table also counts the number of allocation of a condition (used during macro evaluations)
#
# Conditioner table
# - if-condition (used to construct AP of a block)
# - if-condition (used to construct AP of a block)
# - !if-condition (artificial condition?)
class ModelTypes:
Tag = 0
Macros = 1
Package = 2
Description = 3
Files = 4
OtherSection = 5
Comment = 6
Condition = 7
Prep = 8
Build = 9
Install = 10
Check = 11
Changelog = 12
Uninterpreted = 13
class SpecModel(object):
def __init__(self):
self._metastrings = []
self._metadata_tags = []
self._metadata_macros = []
self._packages = []
self._descriptions = []
self._files = []
self._prep = {}
self._build = {}
self._install = {}
self._check = {}
self._changelog = {}
self._other_sections = []
self._conditions = []
self._predicates = []
self._comments = []
self._uninterpreted = []
def addTag(self, data):
idx = len(self._metadata_tags)
self._metadata_tags.append(data)
return idx
def getTag(self, idx):
return self._metadata_tags[idx]
def addMacro(self, data):
idx = len(self._metadata_macros)
self._metadata_macros.append(data)
return idx
def getMacro(self, idx):
return self._metadata_macros[idx]
def addPackage(self, data):
idx = len(self._packages)
self._packages.append(data)
return idx
def getPackage(self, idx):
return self._packages[idx]
def addDescription(self, data):
idx = len(self._descriptions)
self._descriptions.append(data)
return idx
def getDescription(self, idx):
return self._descriptions[idx]
def addFiles(self, data):
idx = len(self._files)
self._files.append(data)
return idx
def getFiles(self, idx):
return self._files[idx]
def setPrep(self, data):
self._prep = data
def getPrep(self):
return self._prep
def setBuild(self, data):
self._build = data
def getBuild(self):
return self._build
def setInstall(self, data):
self._install = data
def getInstall(self):
return self._install
def setCheck(self, data):
self._check = data
def getCheck(self):
return self._check
def setChangelog(self, data):
self._changelog = data
def getChangelog(self):
return self._changelog
def addSection(self, data):
idx = len(self._other_sections)
self._other_sections.append(data)
return idx
def getSection(self, idx):
return self._other_sections[idx]
def addCondition(self, data):
idx = len(self._conditions)
self._conditions.append(data)
return idx
def getCondition(self, idx):
return self._conditions[idx]
def addPredicate(self, data):
idx = len(self._predicates)
self._predicates.append(data)
return idx
def getPredicate(self, idx):
return self._predicates[idx]
def addComment(self, data):
idx = len(self._comments)
self._comments.append(data)
return idx
def getComment(self, idx):
return self._comments[idx]
def addUninterpreted(self, data):
idx = len(self._uninterpreted)
self._uninterpreted.append(data)
return idx
def getUninterpreted(self, idx):
return self._uninterpreted[idx]
class SpecModelGenerator(object):
def __init__(self):
# metastring extraction from a raw specfile
self._block_list = []
self._metastrings = []
# abstract specfile modeling
self._spec_model = SpecModel()
def fromRawSpecfile(self, raw):
self._block_list, self._metastrings = self._processBlockList(raw)
self._toAbstractModel(self._metastrings, self._block_list)
#MMetastring(self._metastrings).format(self._spec_model)
self._spec_model._metastrings = self._metastrings
return self._spec_model
def fromJsonInput(self, json):
for attr in json:
setattr(self._spec_model, attr, json[attr])
return self._spec_model
def _processBlockList(self, block_list, predicate_list = []):
processed_blocks = []
generated_metastrings = []
for single_block in block_list:
if single_block.block_type == BlockTypes.HeaderTagType:
generated_metastrings.append( HeaderTagMetastring(single_block.key, single_block.option, single_block.content) )
clean_block = HeaderTagMetastring.cleanBlockType(single_block)
if predicate_list != []:
# TODO(jchaloup): register the AP in the conditioner table as well
clean_block.AP = predicate_list
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.CommentType:
generated_metastrings.append( CommentMetastring(single_block.content) )
clean_block = CommentMetastring.cleanBlockType(single_block)
if predicate_list != []:
# TODO(jchaloup): register the AP in the conditioner table as well
clean_block.AP = predicate_list
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.Whitespaces:
generated_metastrings.append( WhitespacesMetastring(single_block.content) )
clean_block = WhitespacesMetastring.cleanBlockType(single_block)
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.Uninterpreted:
generated_metastrings.append( UninterpretedMetastring(single_block.content) )
clean_block = UninterpretedMetastring.cleanBlockType(single_block)
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.MacroDefinitionType:
metastring = MacroDefinitionMetastring(single_block.keyword, single_block.name, single_block.options, single_block.body)
generated_metastrings.append( metastring )
clean_block = MacroDefinitionMetastring.cleanBlockType(single_block)
if predicate_list != []:
# TODO(jchaloup): register the AP in the conditioner table as well
clean_block.AP = predicate_list
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.PackageTagType:
clean_block = PackageMetastring.cleanBlockType(single_block)
metastring = PackageMetastring(single_block.keyword, single_block.parameters, single_block.subname)
#print(single_block.content)
if single_block.content != []:
clean_block.content, metastring_children = self._processBlockList(single_block.content, predicate_list)
#print(clean_block.content)
#exit(1)
metastring.setContentMetastring(metastring_children)
generated_metastrings.append( metastring )
if predicate_list != []:
# TODO(jchaloup): register the AP in the conditioner table as well
clean_block.AP = predicate_list
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.ChangelogTagType:
generated_metastrings.append( ChangelogMetastring(single_block.keyword, single_block.content) )
clean_block = ChangelogMetastring.cleanBlockType(single_block)
if predicate_list != []:
# TODO(jchaloup): register the AP in the conditioner table as well
clean_block.AP = predicate_list
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.SectionTagType:
clean_block = SectionMetastring.cleanBlockType(single_block)
metastring = SectionMetastring(single_block.keyword, single_block.parameters, single_block.name, single_block.subname)
if single_block.content != []:
clean_block.content, metastring_children = self._processBlockList(single_block.content, predicate_list)
metastring.setContentMetastring(metastring_children)
generated_metastrings.append( metastring )
if predicate_list != []:
# TODO(jchaloup): register the AP in the conditioner table as well
clean_block.AP = predicate_list
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.ConditionType:
clean_block = ConditionMetastring.cleanBlockType(single_block)
ms = ConditionMetastring(single_block.keyword, single_block.expression, single_block.end_keyword, single_block.else_keyword)
if single_block.content != []:
clean_block.content, content_metastring_children = self._processBlockList(single_block.content, predicate_list + [[clean_block.expression, 1, clean_block.keyword]])
ms.setIfBodyMetastring(content_metastring_children)
if single_block.else_body != []:
clean_block.else_body, else_body_metastring_children = self._processBlockList(single_block.else_body, predicate_list + [[clean_block.expression, 0, clean_block.keyword]])
ms.setElseBodyMetastring(else_body_metastring_children)
generated_metastrings.append( ms )
if predicate_list != []:
# TODO(jchaloup): register the AP in the conditioner table as well
clean_block.AP = predicate_list
processed_blocks.append(clean_block)
continue
if single_block.block_type == BlockTypes.MacroConditionType:
metastring = MacroConditionMetastring(single_block.condition, single_block.name, single_block.ending)
clean_block = MacroConditionMetastring.cleanBlockType(single_block)
if single_block.content != []:
if '!' in single_block.condition:
clean_block.content, metastring_children = self._processBlockList(single_block.content, predicate_list + [[clean_block.name, 0, None]])
metastring.setContentMetastring(metastring_children)
else:
clean_block.content, metastring_children = self._processBlockList(single_block.content, predicate_list + [[clean_block.name, 1, None]])
metastring.setContentMetastring(metastring_children)
generated_metastrings.append( metastring )
if predicate_list != []:
# TODO(jchaloup): register the AP in the conditioner table as well
clean_block.AP = predicate_list
processed_blocks.append(clean_block)
continue
raise BlockTypeUnknown("Block type {} unknown".format(single_block.block_type))
return processed_blocks, generated_metastrings
def _toAbstractModel(self, metastrings, block_list):
ms_idx = 0
for _, block in enumerate(block_list):
if block.block_type == BlockTypes.MacroDefinitionType:
metastrings[ms_idx].setBlockIdx(ModelTypes.Macros, self._spec_model.addMacro(block))
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.MacroConditionType:
# TODO(jchaloup): Should it be under the macros or under its own category?
metastrings[ms_idx].setBlockIdx(ModelTypes.Macros, self._spec_model.addMacro(block))
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.CommentType:
metastrings[ms_idx].setBlockIdx(ModelTypes.Comment, self._spec_model.addComment(block))
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.Uninterpreted:
metastrings[ms_idx].setBlockIdx(ModelTypes.Uninterpreted, self._spec_model.addUninterpreted(block))
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.Whitespaces:
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.HeaderTagType:
idx = self._spec_model.addTag(block)
metastrings[ms_idx].setBlockIdx(ModelTypes.Tag, idx)
metastrings[ms_idx].format(self._spec_model)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.ConditionType:
idx = self._spec_model.addCondition(block)
metastrings[ms_idx].setBlockIdx(ModelTypes.Condition, idx)
# metastrings[ms_idx].format(self._spec_model)
self._spec_model.addPredicate(self._spec_model._conditions[idx].expression)
# TODO(jchaloup): distribute the APs properly
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getIfBodyMetastring(), block.content)
if block.else_body != []:
self._toAbstractModel(metastrings[ms_idx].getElseBodyMetastring(), block.else_body)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.PackageTagType:
metastrings[ms_idx].setBlockIdx(ModelTypes.Package, self._spec_model.addPackage(block))
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.ChangelogTagType:
metastrings[ms_idx].setBlockIdx(ModelTypes.Changelog)
self._spec_model.setChangelog(block)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.block_type == BlockTypes.SectionTagType:
if block.keyword == "%description":
metastrings[ms_idx].setBlockIdx(ModelTypes.Description, self._spec_model.addDescription(block))
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.keyword == "%files":
metastrings[ms_idx].setBlockIdx(ModelTypes.Files, self._spec_model.addFiles(block))
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.keyword == "%prep":
metastrings[ms_idx].setBlockIdx(ModelTypes.Prep)
self._spec_model.setPrep(block)
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.keyword == "%build":
metastrings[ms_idx].setBlockIdx(ModelTypes.Build)
self._spec_model.setBuild(block)
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.keyword == "%install":
metastrings[ms_idx].setBlockIdx(ModelTypes.Install)
self._spec_model.setInstall(block)
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
if block.keyword == "%check":
metastrings[ms_idx].setBlockIdx(ModelTypes.Check)
self._spec_model.setCheck(block)
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
metastrings[ms_idx].setBlockIdx(ModelTypes.OtherSection, self._spec_model.addSection(block))
if block.content != []:
self._toAbstractModel(metastrings[ms_idx].getContentMetastring(), block.content)
block.id = metastrings[ms_idx].blockIdx()
ms_idx += 1
continue
raise BlockTypeUnknown("Block type {} unknown".format(block.block_type))
return self
def _metastring_list_to_str(self, metastring_list):
str = ""
for item in metastring_list:
if isinstance(item, list):
str += self._metastring_list_to_str(item)
continue
str += item.to_str()
return str
def _metastring_list_to_json(self, metastring_list):
data = []
for item in metastring_list:
if isinstance(item, list):
data.append(self._metastring_list_to_json(item))
continue
data.append(item.to_json())
return data
def metastrings_to_json(self):
return self._metastring_list_to_json(self._metastrings)
def metastrings_to_str(self):
return self._metastring_list_to_str(self._metastrings)
def model_to_json(self):
return {
"block_list": map(lambda l: l.to_json(), self._block_list),
"metastring": self.metastrings_to_str(),
}
def _generate_spec(self, specModel):
for metastring in specModel._metastrings:
print(metastring.format(specModel), end = '')
def to_spec(self, specModel):
self._generate_spec(specModel)