-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup_project.py
More file actions
180 lines (148 loc) · 6.66 KB
/
setup_project.py
File metadata and controls
180 lines (148 loc) · 6.66 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
"""Setup a new Python project from the template.
This script copies template files from .opencode/templates/ to the project root,
substituting templated fields with the provided parameters.
Usage:
python setup_project.py run --github-username <username> --project-name <name>
--project-description <desc> [--author-name <name>]
[--author-email <email>] [--package-name <name>]
[--module-name <name>]
python setup_project.py detect-fields
"""
import logging
import shutil
import sys
from datetime import datetime, timezone
from pathlib import Path
import fire
# Configure logging for user feedback
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger(__name__)
TEMPLATES_DIR = Path(__file__).parent / ".opencode" / "templates"
ROOT_DIR = Path(__file__).parent
ORIGINAL_PROJECT_NAME = "python-project-template"
ORIGINAL_PACKAGE_NAME = "python_package_template"
ORIGINAL_MODULE_NAME = "python_module_template"
ORIGINAL_GITHUB_USERNAME = "nullhack"
ORIGINAL_AUTHOR_NAME = "eol"
ORIGINAL_AUTHOR_EMAIL = "nullhack@users.noreply.github.com"
def substitute_values(content: str, replacements: dict) -> str:
"""Replace all values in content based on replacements dict."""
result = content
for original, replacement in replacements.items():
result = result.replace(original, replacement)
return result
def copy_and_rename_package(src_name: str, dst_name: str) -> None:
"""Copy package directory and rename references inside."""
src_dir = ROOT_DIR / src_name
dst_dir = ROOT_DIR / dst_name
if src_dir.exists():
if dst_dir.exists():
shutil.rmtree(dst_dir)
shutil.copytree(src_dir, dst_dir, dirs_exist_ok=True)
logger.info("Copied package: %s -> %s", src_name, dst_name)
for py_file in dst_dir.rglob("*.py"):
content = py_file.read_text(encoding="utf-8")
content = content.replace(src_name, dst_name)
py_file.write_text(content, encoding="utf-8")
logger.info(" Renamed in: %s", py_file.relative_to(ROOT_DIR))
def detect_fields() -> None:
"""Show what fields would need changing."""
today = datetime.now(timezone.utc).strftime("%Y%m%d")
logger.info("\nFields that would need to be changed in templates:")
logger.info("-" * 50)
logger.info(" 1. GitHub Username: %s", ORIGINAL_GITHUB_USERNAME)
logger.info(" 2. Project Name: %s", ORIGINAL_PROJECT_NAME)
logger.info(" 3. Project Description: 'Python template...'")
logger.info(" 4. Author Name: %s", ORIGINAL_AUTHOR_NAME)
logger.info(" 5. Author Email: %s", ORIGINAL_AUTHOR_EMAIL)
logger.info(" 6. Package Name: %s", ORIGINAL_PACKAGE_NAME)
logger.info(" 7. Module Name: %s", ORIGINAL_MODULE_NAME)
logger.info(" 8. Version: starts with 0.1.%s", today)
def copy_directory_structure(src_dir: Path, dst_dir: Path, replacements: dict) -> None:
"""Copy directory structure recursively, processing template files."""
if dst_dir.exists():
shutil.rmtree(dst_dir)
dst_dir.mkdir(parents=True, exist_ok=True)
for item in src_dir.rglob("*"):
if item.is_file():
# Calculate relative path and destination
rel_path = item.relative_to(src_dir)
dst_path = dst_dir / rel_path
dst_path.parent.mkdir(parents=True, exist_ok=True)
# Process template files
if item.suffix == ".template":
content = item.read_text(encoding="utf-8")
content = substitute_values(content, replacements)
# Remove .template extension
dst_path = dst_path.with_suffix("")
dst_path.write_text(content, encoding="utf-8")
logger.info("Created: %s", dst_path.relative_to(ROOT_DIR))
else:
# Copy non-template files as-is
shutil.copy2(item, dst_path)
logger.info("Copied: %s", dst_path.relative_to(ROOT_DIR))
def run(
github_username: str,
project_name: str,
project_description: str,
author_name: str = "Your Name",
author_email: str = "[EMAIL]",
package_name: str | None = None,
module_name: str | None = None,
) -> None:
"""Run the setup script with provided parameters."""
if package_name is None:
package_name = project_name.replace("-", "_")
if module_name is None:
module_name = package_name
replacements = {
ORIGINAL_GITHUB_USERNAME: github_username,
ORIGINAL_AUTHOR_NAME: author_name,
ORIGINAL_AUTHOR_EMAIL: author_email,
ORIGINAL_PROJECT_NAME: project_name,
ORIGINAL_PACKAGE_NAME: package_name,
ORIGINAL_MODULE_NAME: module_name,
}
today = datetime.now(timezone.utc).strftime("%Y%m%d")
replacements["0.1.20260411"] = f"0.1.{today}"
replacements[
"Python template with some awesome tools to quickstart any Python project"
] = project_description
logger.info("\nSetting up project: %s", project_name)
logger.info("Description: %s", project_description)
logger.info("GitHub: github.com/%s/%s", github_username, project_name)
logger.info("Package: %s", package_name)
logger.info("Module: %s", module_name)
logger.info("")
# Process root-level template files
for template_file in TEMPLATES_DIR.glob("*.template"):
content = template_file.read_text(encoding="utf-8")
content = substitute_values(content, replacements)
dst_name = template_file.stem
dst_path = ROOT_DIR / dst_name
dst_path.write_text(content, encoding="utf-8")
logger.info("Created: %s", dst_path.relative_to(ROOT_DIR))
# Process .github directory structure
github_templates_dir = TEMPLATES_DIR / ".github"
if github_templates_dir.exists():
github_dst_dir = ROOT_DIR / ".github"
copy_directory_structure(github_templates_dir, github_dst_dir, replacements)
# Copy and rename package directory
if package_name != ORIGINAL_PACKAGE_NAME:
copy_and_rename_package(ORIGINAL_PACKAGE_NAME, package_name)
logger.info("\nProject setup complete!")
logger.info("\nNext steps:")
logger.info(" 1. Review and update README.md with project-specific content")
logger.info(" 2. Run: uv venv && uv pip install -e '.[dev]'")
logger.info(" 3. Run: task test && task lint && task static-check")
logger.info(
" 4. Initialize secrets baseline: "
"uv run detect-secrets scan --baseline .secrets.baseline"
)
if __name__ == "__main__":
fire.Fire({"run": run, "detect-fields": detect_fields})