-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrename.py
More file actions
50 lines (45 loc) · 1.95 KB
/
rename.py
File metadata and controls
50 lines (45 loc) · 1.95 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
import os
import argparse
def replace_in_file(file_path, replacements):
with open(file_path, 'r') as f:
content = f.read()
for old, new in replacements.items():
content = content.replace(old, new)
with open(file_path, 'w') as f:
f.write(content)
def replace_in_dir(dir_path, replacements):
path = dir_path
for root, dirs, files in os.walk(path):
for file in files:
file_path = os.path.join(root, file)
replace_in_file(file_path, replacements)
for dir in dirs:
path = os.path.join(root, dir)
replace_in_dir(path, replacements)
for root, dirs, files in os.walk(dir_path):
for dir_name in dirs:
for old, new in replacements.items():
if old in dir_name:
new_dir_name = dir_name.replace(old, new)
os.rename(os.path.join(root, dir_name), os.path.join(root, new_dir_name))
for file_name in files:
for old, new in replacements.items():
if old in file_name:
new_file_name = file_name.replace(old, new)
os.rename(os.path.join(root, file_name), os.path.join(root, new_file_name))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Replace patterns in directory')
parser.add_argument('new_vendor_name', type=str, help='New vendor name')
parser.add_argument('new_board_name', type=str, help='New board name')
parser.add_argument('new_chip_name', type=str, help='New chip name')
args = parser.parse_args()
replacements = {
"vendor_name": args.new_vendor_name,
"board_name": args.new_board_name,
"chip_name": args.new_chip_name,
"VENDOR_NAME": args.new_vendor_name.upper(),
"BOARD_NAME": args.new_board_name.upper(),
"CHIP_NAME": args.new_chip_name.upper()
}
replace_in_dir("boards", replacements)
replace_in_dir("chips", replacements)