Skip to content

Commit 542fa09

Browse files
committed
ENH: Add template renaming script
1 parent 3b17036 commit 542fa09

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tools/rename_template.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import os
4+
import re
5+
import fnmatch
6+
import functools
7+
from pathlib import Path
8+
9+
PACKAGE_ROOT = Path(__file__).absolute().parent.parent
10+
11+
@functools.lru_cache()
12+
def load_gitignore(repo):
13+
gitignore = repo / '.gitignore'
14+
ignore = [fnmatch.translate(".git/"), fnmatch.translate(Path(__file__).name)]
15+
if gitignore.exists():
16+
ignore.extend(
17+
fnmatch.translate(line.strip())
18+
for line in gitignore.read_text().splitlines()
19+
if line.strip() and not line[0] == '#'
20+
)
21+
return re.compile('|'.join(ignore))
22+
23+
cmd, new_name, *_ = sys.argv
24+
25+
for root, dirs, files in os.walk(PACKAGE_ROOT):
26+
ignore = load_gitignore(PACKAGE_ROOT).search
27+
for d in [d for d in dirs if ignore(f"{d}/")]:
28+
dirs.remove(d)
29+
for f in [f for f in files if ignore(f)]:
30+
files.remove(f)
31+
32+
root = Path(root)
33+
for src in list(dirs):
34+
if 'TODO' in src:
35+
dst = src.replace("TODO", new_name)
36+
print(f"Renaming: {root / src} -> {root / dst}")
37+
os.rename(root / src, root / dst)
38+
dirs.remove(src)
39+
dirs.append(dst)
40+
for fname in files:
41+
f = root / fname
42+
text = Path.read_text(root / fname)
43+
if 'TODO' in text:
44+
print(f"Rewriting: {root / fname}")
45+
Path.write_text(root / fname, text.replace("TODO", new_name))

0 commit comments

Comments
 (0)