Skip to content

Commit

Permalink
Merge pull request #13 from effigies/enh/renamer
Browse files Browse the repository at this point in the history
ENH: Add template renaming script
  • Loading branch information
tclose authored Mar 16, 2024
2 parents 843d381 + ed5f9ba commit 34a9a7b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ dmypy.json
# Pycharm
.idea

# Vim
.*.sw[op]

# VS Code
.vscode

Expand Down
45 changes: 45 additions & 0 deletions tools/rename_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
import sys
import os
import re
import fnmatch
import functools
from pathlib import Path

PACKAGE_ROOT = Path(__file__).absolute().parent.parent

@functools.lru_cache()
def load_gitignore(repo):
gitignore = repo / '.gitignore'
ignore = [fnmatch.translate(".git/"), fnmatch.translate(Path(__file__).name)]
if gitignore.exists():
ignore.extend(
fnmatch.translate(line.strip())
for line in gitignore.read_text().splitlines()
if line.strip() and not line[0] == '#'
)
return re.compile('|'.join(ignore))

cmd, new_name, *_ = sys.argv

for root, dirs, files in os.walk(PACKAGE_ROOT):
ignore = load_gitignore(PACKAGE_ROOT).search
for d in [d for d in dirs if ignore(f"{d}/")]:
dirs.remove(d)
for f in [f for f in files if ignore(f)]:
files.remove(f)

root = Path(root)
for src in list(dirs):
if 'TODO' in src:
dst = src.replace("TODO", new_name)
print(f"Renaming: {root / src} -> {root / dst}")
os.rename(root / src, root / dst)
dirs.remove(src)
dirs.append(dst)
for fname in files:
f = root / fname
text = Path.read_text(root / fname)
if 'TODO' in text:
print(f"Rewriting: {root / fname}")
Path.write_text(root / fname, text.replace("TODO", new_name))

0 comments on commit 34a9a7b

Please sign in to comment.