-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenamer.py
139 lines (110 loc) · 3.57 KB
/
renamer.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
#!/usr/bin/env python3.9
"""
Copyright (c) 2021 JMcB
Not for use with any cryptocurrency or blockchain that is any of the following:
legitimate, commercial, high proof-of-work
"""
import hashlib
import subprocess
import sys
import concurrent.futures
from pathlib import Path
from typing import List, Callable
REPLACES = {
'bitcoin': 'bubcoin',
'btc': 'bub',
}
def casereplace(text: str, old: str, new: str) -> str:
cases = [
str.lower,
str.upper,
str.capitalize,
str.title
]
for case in cases:
text = text.replace(case(old), case(new))
return text
def multireplace(
text: str, mapping: dict, replacer: Callable = casereplace
) -> str:
for key, value in mapping.items():
text = replacer(text, key, value)
return text
def filereplace(
filepath: Path, ignore_content: dict, mapping: dict = REPLACES
):
with open(filepath, encoding='utf-8') as file:
content = file.read()
content_replaced = multireplace(content, ignore_content, str.replace)
content_replaced = multireplace(content_replaced, mapping)
content_replaced = multireplace(
content_replaced,
{v: k for k, v in ignore_content.items()},
str.replace
)
if content != content_replaced:
print(f'Content: {filepath}')
with open(filepath, 'w', encoding='utf-8') as file:
file.write(content_replaced)
def pathrename(filepath: Path, mapping: dict = REPLACES) -> Path:
filename_replaced = multireplace(filepath.name, mapping)
if filepath.name != filename_replaced:
print(f'Rename: {filepath}')
filepath = filepath.rename(filepath.with_name(filename_replaced))
return filepath
def check_ignore(files: List[Path]) -> List[Path]:
command = ' '.join(['git check-ignore'] + [str(f) for f in files])
try:
result = subprocess.check_output(
command, shell=True, encoding='utf-8'
)
except subprocess.CalledProcessError as error:
result = error.output
ignored_files = result.splitlines()
ignored_paths = [Path(f.strip('"')).resolve() for f in ignored_files]
return ignored_paths
def recurse(
directory: Path, threadpool: concurrent.futures.ThreadPoolExecutor,
ignore: List[Path], ignore_content: dict
):
iterdir_list = list(directory.iterdir())
ignored_paths = check_ignore(iterdir_list) + ignore
for path in iterdir_list:
print(path)
if path.resolve() in ignored_paths:
print(f'Ignore: {path}')
continue
path = pathrename(path)
if path.is_dir():
recurse(path, threadpool, ignore, ignore_content)
else:
threadpool.submit(filereplace, path, ignore_content)
def main():
for i in range(1):
if input('Are you sure? y/N\n') != 'y':
sys.exit(1)
threadpool = concurrent.futures.ThreadPoolExecutor()
cwd = Path().resolve()
ignore_str = [
__file__,
'.git',
]
ignore_path = [Path(p).resolve() for p in ignore_str]
ignore_content = [
'The Bitcoin Core developers',
'Bitcoin Developers',
'BtcDrak',
'btcdrak',
'AZ9XVV1BtcgNzbCRR'
]
ignore_content_hashes = {}
for i in ignore_content:
h = hashlib.md5()
h.update(i.encode('utf_8'))
ignore_content_hashes[i] = h.digest().hex()
# always do gitignore first
filereplace(Path('.gitignore'), {})
recurse(cwd, threadpool, ignore_path, ignore_content_hashes)
threadpool.shutdown()
if __name__ == '__main__':
main()