-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreorder.py
executable file
·68 lines (54 loc) · 1.84 KB
/
reorder.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
#!/usr/bin/python3
'''
Scan all test cases ('*.c'), rename each test case to the format
'{id}_name.c', and make sure they all have unique and continuous ids.
Usage:
reorder.py # reorder all files in directories in the configuration
reorder.py DIR # reorder all files in DIR
reorder.py DIR COUNT # reorder all files in DIR, start counting from COUNT
'''
import os
import re
from math import log10, ceil
# ========== configurations begin ==========
test_case_dirs = [
'functional',
# 'performance',
]
# ========== configurations end ==========
__NAME_PATTERN = re.compile(r'(\d+_)?(.+).c')
def get_id(zero_count: int, count: int) -> str:
id = str(count)
assert zero_count >= len(id)
return (zero_count - len(id)) * '0' + id
def rename_file(root: str, old_name: str, new_name: str):
cur_file = os.path.join(root, old_name)
new_file = os.path.join(root, new_name)
os.rename(cur_file, new_file)
def scan_and_rename(dir: str, start_count: int = 0):
for root, _, files in os.walk(dir):
cases = [f for f in sorted(files) if f.endswith('.c')]
zeros = len(str(len(cases) + start_count))
count = start_count
for f in cases:
cur_id = get_id(zeros, count)
# rename test case
case_name = __NAME_PATTERN.findall(f)[0][1]
new_case_name = f'{cur_id}_{case_name}.c'
rename_file(root, f, new_case_name)
# check & rename test input
in_name = f'{f[:-2]}.in'
if os.path.exists(os.path.join(root, in_name)):
new_in_name = f'{cur_id}_{case_name}.in'
rename_file(root, in_name, new_in_name)
count += 1
if __name__ == '__main__':
import sys
if len(sys.argv) >= 2:
start_count = 0
if len(sys.argv) >= 3:
start_count = int(sys.argv[2])
scan_and_rename(sys.argv[1], start_count)
else:
for i in test_case_dirs:
scan_and_rename(i)