From 542fa099b86c7eb13779ead26b5b85a63b28441b Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Wed, 24 Feb 2021 10:28:13 -0500 Subject: [PATCH 1/2] ENH: Add template renaming script --- tools/rename_template.py | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 tools/rename_template.py diff --git a/tools/rename_template.py b/tools/rename_template.py new file mode 100755 index 0000000..2b6b13b --- /dev/null +++ b/tools/rename_template.py @@ -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)) From 803f86665ad48b5ddef05bf28e593a5b2dd3e927 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Wed, 24 Feb 2021 10:28:43 -0500 Subject: [PATCH 2/2] Ignore VIM swap files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 846dc44..7fa3887 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,6 @@ dmypy.json # Pycharm .idea + +# Vim +.*.sw[op]