|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import re |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def _find_files(project_root): |
| 10 | + path_exclude_pattern = r"\.git($|\/)|venv|_build" |
| 11 | + file_exclude_pattern = r"fill_template_vars\.py|\.swp$" |
| 12 | + filepaths = [] |
| 13 | + for dir_path, _dir_names, file_names in os.walk(project_root): |
| 14 | + if not re.search(path_exclude_pattern, dir_path): |
| 15 | + for file in file_names: |
| 16 | + if not re.search(file_exclude_pattern, file): |
| 17 | + filepaths.append(str(Path(dir_path, file))) |
| 18 | + |
| 19 | + return filepaths |
| 20 | + |
| 21 | + |
| 22 | +def _replace(pattern, replacement, project_root): |
| 23 | + print(f"Replacing values: {pattern}") |
| 24 | + for file in _find_files(project_root): |
| 25 | + try: |
| 26 | + with open(file) as f: |
| 27 | + content = f.read() |
| 28 | + content = re.sub(pattern, replacement, content) |
| 29 | + with open(file, "w") as f: |
| 30 | + f.write(content) |
| 31 | + except UnicodeDecodeError: |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + project_root = Path(os.path.realpath(sys.argv[0])).parent.parent |
| 37 | + |
| 38 | + module_name = input("What is your python module name? ") |
| 39 | + |
| 40 | + pypi_input = input(f"What is your pypi package name? (default: {module_name}) ") |
| 41 | + pypi_name = pypi_input or module_name |
| 42 | + |
| 43 | + repo_input = input(f"What is your github project name? (default: {pypi_name}) ") |
| 44 | + repo_name = repo_input or pypi_name |
| 45 | + |
| 46 | + rtd_input = input( |
| 47 | + f"What is your readthedocs.org project name? (default: {pypi_name}) " |
| 48 | + ) |
| 49 | + rtd_name = rtd_input or pypi_name |
| 50 | + |
| 51 | + project_input = input( |
| 52 | + f"What is your project name (ex: at the top of the README)? (default: {repo_name}) " |
| 53 | + ) |
| 54 | + project_name = project_input or repo_name |
| 55 | + |
| 56 | + short_description = input("What is a one-liner describing the project? ") |
| 57 | + |
| 58 | + _replace("<MODULE_NAME>", module_name, project_root) |
| 59 | + _replace("<PYPI_NAME>", pypi_name, project_root) |
| 60 | + _replace("<REPO_NAME>", repo_name, project_root) |
| 61 | + _replace("<RTD_NAME>", rtd_name, project_root) |
| 62 | + _replace("<PROJECT_NAME>", project_name, project_root) |
| 63 | + _replace("<SHORT_DESCRIPTION>", short_description, project_root) |
| 64 | + |
| 65 | + os.makedirs(project_root / module_name, exist_ok=True) |
| 66 | + Path(project_root / module_name / "__init__.py").touch() |
| 67 | + Path(project_root / module_name / "py.typed").touch() |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments