forked from jrrk2/prjxray
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sort_db.py
executable file
·466 lines (363 loc) · 11.2 KB
/
sort_db.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2020 The Project X-Ray Authors.
#
# Use of this source code is governed by a ISC-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/ISC
#
# SPDX-License-Identifier: ISC
"""
Canonicalize the Project X-Ray database files by sorting. The aim is to reduce
the diff output between runs to make it clearer what has changed.
DB Files
--------
DB files are sorted into "natural" ordering. This is generally the order that a
human would sort them in rather than how they sort as ASCII strings.
For example with tags, a sequence of ABC1 to ABC12 would have the ASCII sort
order of;
ABC1
ABC10
ABC11
ABC12
ABC2
...
ABC9
We instead sort them like the following;
ABC1
ABC2
...
ABC9
ABC10
ABC11
ABC12
For the segbit files, we sort the bit definitions ignoring any leading
exclamation mark. Doing this generally makes it much easier to see patterns in
the bit descriptions and you end up with output like the following for 1-hot
encoded choices,
ABC.CHOICE1 22_15 !22_16 !22_17
ABC.CHOICE2 !22_15 22_16 !22_17
ABC.CHOICE3 !22_15 !22_16 22_17
JSON Files
----------
For the JSON files, we run them through Python's pretty printing module and
sort sets (lists where the order doesn't matter).
"""
import csv
import os
import random
import re
import sys
import json
import utils.xjson as xjson
import utils.cmp as cmp
from prjxray.util import OpenSafeFile
def split_all(s, chars):
"""Split on multiple character values.
>>> split_all('a_b_c_d', '_. ')
['a', 'b', 'c', 'd']
>>> split_all('a b c d', '_. ')
['a', 'b', 'c', 'd']
>>> split_all('a.b.c.d', '_. ')
['a', 'b', 'c', 'd']
>>> split_all('a_b.c d', '_. ')
['a', 'b', 'c', 'd']
>>> split_all('a b_c.d', '_. ')
['a', 'b', 'c', 'd']
"""
chars = list(chars)
o = [s]
while len(chars) > 0:
c = chars.pop(0)
n = []
for i in o:
n += i.split(c)
o = n
return o
NUM_REGEX = re.compile('^(.*?)([0-9]*)$')
def extract_num(i):
"""Extract number from a string to be sorted.
>>> extract_num('BLAH123')
('BLAH', 123)
>>> extract_num('123')
123
>>> extract_num('BLAH')
'BLAH'
>>> extract_num('')
''
"""
g = NUM_REGEX.match(i).groups()
if len(g[-1]) == 0:
return i
i = int(g[-1])
if len(g[0]) == 0:
return i
else:
return (g[0], i)
class bit(tuple):
"""Class representing a bit specifier.
>>> a = bit.parse("02_12")
>>> a
bit(2, 12, True)
>>> b = bit.parse("!02_03")
>>> b
bit(2, 3, False)
>>> b == a
False
>>> b < a
True
>>> str(a)
'02_12'
>>> str(b)
'!02_03'
>>> bit.parseline("!30_04 !31_00 !31_01 31_02")
[bit(30, 4, False), bit(31, 0, False), bit(31, 1, False), bit(31, 2, True)]
>>> bit.parseline("31_02 !31_00 !31_01 !30_04")
[bit(30, 4, False), bit(31, 0, False), bit(31, 1, False), bit(31, 2, True)]
"""
@classmethod
def parse(cls, s):
mode = s[0] != '!'
s = s.replace('!', '')
assert '_' in s, s
a, b = s.split('_', 1)
assert '_' not in b, s
return cls((extract_num(a), extract_num(b), mode))
@classmethod
def parseline(cls, s):
bits = [cls.parse(b) for b in s.split(' ')]
bits.sort()
return bits
def __repr__(self):
return "bit" + tuple.__repr__(self)
def __str__(self):
s = self
return "{}{:02d}_{:02d}".format(['!', ''][s[2]], s[0], s[1])
def convert_bit(i):
"""Convert a bit pattern into sortable form.
>>> convert_bit("02_12")
bit(2, 12, True)
>>> convert_bit("!02_12")
bit(2, 12, False)
>>> convert_bit("!02_02")
bit(2, 2, False)
>>> convert_bit("always")
'always'
"""
if '_' not in i:
return i
return bit.parse(i)
def segbit_line_sort_bits(l):
"""Sort the bit section of a segbit line.
>>> segbit_line_sort_bits("A !28_35 !27_39 27_37")
'A 27_37 !27_39 !28_35'
>>> segbit_line_sort_bits("B !28_35 !27_39 !27_37")
'B !27_37 !27_39 !28_35'
>>> segbit_line_sort_bits("C 28_35 00_00 !27_37")
'C 00_00 !27_37 28_35'
"""
tag, *segbits = l.split()
segbits = [bit.parse(b) for b in segbits]
segbits.sort()
return "{} {}".format(tag, " ".join(str(s) for s in segbits))
def sortable_tag(t):
"""
>>> sortable_tag("CLBLL_L.CLBLL_L_A.CLBLL_L_A1")
('CLBLL', 'L', 'CLBLL', 'L', 'A', 'CLBLL', 'L', ('A', 1))
>>> sortable_tag("CLBLL_L.CLBLL_LOGIC_OUTS23.CLBLL_LL_DMUX")
('CLBLL', 'L', 'CLBLL', 'LOGIC', ('OUTS', 23), 'CLBLL', 'LL', 'DMUX')
>>> sortable_tag("BRAM_L.RAMB18_Y0.INIT_B[9]")
('BRAM', 'L', ('RAMB', 18), ('Y', 0), 'INIT', 'B', 9)
>>> sortable_tag("BRAM_L.RAMB18_Y0.READ_WIDTH_A_18")
('BRAM', 'L', ('RAMB', 18), ('Y', 0), 'READ', 'WIDTH', 'A', 18)
"""
return tuple(extract_num(i) for i in split_all(t, '_.[]') if i != '')
def sortable_line_from_mask(l):
"""Convert a line in a mask_XXXX.db file to something sortable.
Example lines from mask_XXX.db file
>>> a, b = sortable_line_from_mask("bit 00_00")
>>> a
bit(0, 0, True)
>>> b
'bit 00_00'
>>> a, b = sortable_line_from_mask("bit 09_39")
>>> a
bit(9, 39, True)
>>> b
'bit 09_39'
"""
tag, b = l.split(' ', 1)
assert tag == 'bit', tag
return bit.parse(b), l
def sortable_line_from_ppips(l):
"""Convert a line in a ppips_XXX.db file to something sortable.
Example lines from ppips_XXX.db file
>>> a, b = sortable_line_from_ppips("CLBLL_L.CLBLL_L_A.CLBLL_L_A1 hint")
>>> a
(('CLBLL', 'L', 'CLBLL', 'L', 'A', 'CLBLL', 'L', ('A', 1)), 'hint')
>>> b
'CLBLL_L.CLBLL_L_A.CLBLL_L_A1 hint'
>>> a, b = sortable_line_from_ppips("CLBLL_L.CLBLL_LOGIC_OUTS23.CLBLL_LL_DMUX always")
>>> a
(('CLBLL', 'L', 'CLBLL', 'LOGIC', ('OUTS', 23), 'CLBLL', 'LL', 'DMUX'), 'always')
>>> b
'CLBLL_L.CLBLL_LOGIC_OUTS23.CLBLL_LL_DMUX always'
"""
assert ' ' in l, repr(l)
tag, ptype = l.split(' ', 1)
tag = sortable_tag(tag)
return (tag, ptype), l
def sortable_line_from_segbits(l):
"""Convert a line in segbits_XXX.db file to something sortable.
>>> (tag, bits), b = sortable_line_from_segbits("BRAM_L.RAMB18_Y0.INIT_B[9] 27_15")
>>> tag
('BRAM', 'L', ('RAMB', 18), ('Y', 0), 'INIT', 'B', 9)
>>> bits
(bit(27, 15, True),)
>>> b
'BRAM_L.RAMB18_Y0.INIT_B[9] 27_15'
>>> (tag, bits), b = sortable_line_from_segbits("BRAM_L.RAMB18_Y0.READ_WIDTH_A_18 !28_35 !27_39 27_37")
>>> tag
('BRAM', 'L', ('RAMB', 18), ('Y', 0), 'READ', 'WIDTH', 'A', 18)
>>> bits
(bit(27, 37, True), bit(27, 39, False), bit(28, 35, False))
>>> b
'BRAM_L.RAMB18_Y0.READ_WIDTH_A_18 27_37 !27_39 !28_35'
"""
tag, sbit = l.split(' ', 1)
tag = sortable_tag(tag)
bits = bit.parseline(sbit)
l = segbit_line_sort_bits(l)
return (tag, tuple(bits)), l
def sortable_line_from_origin_segbits(l):
tag, origin, sbit = l.split(' ', 2)
tag = sortable_tag(tag)
bits = bit.parseline(sbit)
return (tag, tuple(bits)), l
def sort_db(pathname):
"""Sort a XXX.db file."""
filename = os.path.split(pathname)[-1]
if filename.startswith('segbits_'):
if 'origin_info' in filename:
sortable_line_from_dbfile = sortable_line_from_origin_segbits
else:
sortable_line_from_dbfile = sortable_line_from_segbits
elif 'origin_info' in filename:
return False
elif filename.startswith('ppips_'):
sortable_line_from_dbfile = sortable_line_from_ppips
elif filename.startswith('grid-'):
sortable_line_from_dbfile = sortable_line_from_ppips
elif filename.startswith('mask_'):
sortable_line_from_dbfile = sortable_line_from_mask
else:
return False
with OpenSafeFile(pathname) as f:
lines = f.readlines()
tosort = []
for l in lines:
l = l.strip()
if not l:
continue
tosort.append(sortable_line_from_dbfile(l))
tosort.sort(key=cmp.cmp_key)
with open(pathname, 'w') as f:
for _, l in tosort:
f.write(l)
f.write('\n')
return True
def sort_csv(pathname):
rows = []
fields = []
delimiter = None
with open(pathname, newline='') as f:
if pathname.endswith('.csv'):
delimiter = ','
elif pathname.endswith('.txt'):
delimiter = ' '
reader = csv.DictReader(f, delimiter=delimiter)
fields.extend(reader.fieldnames)
rows.extend(reader)
del reader
def sort_key(r):
v = []
for field in fields:
v.append(sortable_tag(r[field]))
return tuple(v)
rows.sort(key=sort_key)
with open(pathname, 'w', newline='') as f:
writer = csv.DictWriter(
f, fields, delimiter=delimiter, lineterminator='\n')
writer.writeheader()
writer.writerows(rows)
return True
def sort_json(filename):
"""Sort a XXX.json file."""
try:
with OpenSafeFile(filename) as f:
d = json.load(f)
except json.JSONDecodeError as e:
print(e)
return False
with OpenSafeFile(filename, 'w') as f:
xjson.pprint(f, d)
return True
def sort_db_text(n):
rows = []
with OpenSafeFile(n) as f:
for l in f:
rows.append(([extract_num(s) for s in l.split()], l))
rows.sort(key=lambda i: i[0])
with OpenSafeFile(n, 'w') as f:
for l in rows:
f.write(l[-1])
return True
def sort_file(n):
assert os.path.exists(n)
base, ext = os.path.splitext(n)
dirname, base = os.path.split(base)
# Leave db files with fuzzer of origin untouched
if "origin_info" in n and not base.startswith('segbits'):
print("Ignoring file {:45s}".format(n), flush=True)
return
if ext == '.db':
print("Sorting DB file {:45s}".format(n), end=" ", flush=True)
x = sort_db(n)
elif ext == '.json':
print("Sorting JSON file {:45s}".format(n), end=" ", flush=True)
x = sort_json(n)
elif ext in ('.csv', '.txt'):
if n.endswith('-db.txt'):
print("Sorting txt file {:45s}".format(n), end=" ", flush=True)
x = sort_db_text(n)
else:
print("Sorting CSV file {:45s}".format(n), end=" ", flush=True)
x = sort_csv(n)
else:
print("Ignoring file {:45s}".format(n), end=" ", flush=True)
x = True
if x:
print(".. success.")
else:
print(".. failed.")
def sort_dir(dirname):
for n in sorted(os.listdir(dirname)):
n = os.path.join(dirname, n)
if os.path.isdir(n):
print("Entering dir {:45s}".format(n), flush=True)
sort_dir(n)
continue
elif not os.path.isfile(n):
print("Ignoring non-file {:45s}".format(n), flush=True)
continue
sort_file(n)
def main(argv):
if argv[1:]:
for n in argv[1:]:
sort_file(n)
else:
sort_dir('.')
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))