Repository navigation
Expand file tree
/
Copy pathspellforge.py
More file actions
3049 lines (2530 loc) · 114 KB
/
Copy pathspellforge.py
File metadata and controls
3049 lines (2530 loc) · 114 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
spellforge.py - Claude-powered project bootstrapper
Conjures a fully configured Python project from nothing: git, venv,
ruff, detect-secrets, Claude Code, hooks, and all config files.
Verbose + colorful output throughout.
"""
import argparse
import json
import os
import shutil
import subprocess
import sys
from pathlib import Path
# =============================================================================
# PYTHON VERSION TARGET
# Spellforge pins projects to Python 3.13.x. We deliberately do NOT default
# to whatever `python3` happens to be on PATH because:
# - `brew install python3` resolves to the *current* formula (3.14 today),
# and 3.14 is too new for our typical dependency matrix (pandas/pydantic
# wheels lag, native extensions may not have 3.14 builds yet).
# - 3.13 is the current stable release with broad wheel coverage.
# To bump the target, change the two constants below — every version check
# in the script reads from these.
# =============================================================================
PYTHON_TARGET_MINOR = (3, 13)
PYTHON_TARGET_LABEL = f"{PYTHON_TARGET_MINOR[0]}.{PYTHON_TARGET_MINOR[1]}"
PYTHON_TARGET_BIN_NAME = f"python{PYTHON_TARGET_LABEL}" # e.g. "python3.13"
PYTHON_TARGET_BREW_FORMULA = f"python@{PYTHON_TARGET_LABEL}" # e.g. "python@3.13"
# =============================================================================
# PACKAGE MANAGER CHOICE
# Spellforge supports two package managers for venv creation and installs:
# pip (the classic default, always available once Python is) and uv (a much
# faster Rust-based drop-in). The user picks one on the installation menu;
# repair mode auto-detects the prior choice from the existing venv instead
# of re-prompting (see _detect_venv_package_manager()).
# =============================================================================
PACKAGE_MANAGER_PIP = "pip"
PACKAGE_MANAGER_UV = "uv"
# =============================================================================
# ANSI COLOR HELPERS
# These let us print colorful, formatted output to the terminal.
# =============================================================================
class C:
"""Terminal color codes and helper print functions."""
RESET = "\033[0m"
BOLD = "\033[1m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[92m" # bright green - replaces washed-out blue
MAGENTA = "\033[95m"
CYAN = "\033[96m"
WHITE = "\033[97m"
def banner():
"""
Clear the screen and print the Spellforge terminal banner.
Styled to evoke the heraldic logo - runic border, fantasy serif feel,
tool chips listed across the bottom.
"""
# Clear the screen and move cursor to top-left
print("[2J[H", end="")
# Top runic border
print(f"""
{C.MAGENTA}{C.BOLD} 🔨 S P E L L F O R G E 🔨{C.RESET}
{C.WHITE} Project Bootstrapper for Claude Code{C.RESET}
{C.BLUE} git · venv · ruff · detect-secrets · claude code · hooks · pytest · .gitignore · CLAUDE.md · bandit{C.RESET}
""")
def step(emoji, message):
"""Print a step header — makes it easy to follow progress."""
print(f"\n{C.CYAN}{C.BOLD}{'─' * 60}{C.RESET}")
print(f"{C.BOLD}{emoji} {message}{C.RESET}")
print(f"{C.CYAN}{'─' * 60}{C.RESET}")
def info(message):
"""Print an informational message."""
print(f" {C.BLUE}ℹ{C.RESET} {message}")
def success(message):
"""Print a success message."""
print(f" {C.GREEN}✔{C.RESET} {message}")
def warning(message):
"""Print a warning message."""
print(f" {C.YELLOW}⚠{C.RESET} {message}")
def error(message):
"""Print an error message."""
print(f" {C.RED}✘{C.RESET} {message}")
def fatal(message):
"""Print a fatal error and exit the script."""
print(f"\n{C.RED}{C.BOLD}💀 FATAL: {message}{C.RESET}\n")
sys.exit(1)
def press_any_key(prompt: str = " Press any key to continue..."):
"""
Wait for a single keypress without requiring Enter.
Uses raw terminal mode so any key advances the screen.
"""
import termios
import tty
print(f"\n{C.BLUE}{prompt}{C.RESET}", end="", flush=True)
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
print()
# =============================================================================
# SHELL COMMAND RUNNER
# Wraps subprocess so we get consistent output and error handling.
# =============================================================================
def run(cmd, cwd=None, capture=False, check=True, env=None):
"""
Run a shell command.
Args:
cmd: List of command parts e.g. ['git', 'init']
cwd: Working directory to run the command in
capture: If True, return stdout instead of printing it
check: If True, raise an error on non-zero exit code
env: Optional dict to override the subprocess environment. When
supplied, replaces the entire env (so callers should merge with
os.environ first if they want PATH inheritance, etc).
Returns:
CompletedProcess result object
"""
info(f"Running: {C.YELLOW}{' '.join(cmd)}{C.RESET}")
result = subprocess.run(
cmd,
cwd=cwd,
capture_output=capture,
text=True,
check=False, # We handle errors manually for better messages
env=env,
)
if check and result.returncode != 0:
error(f"Command failed: {' '.join(cmd)}")
if result.stderr:
print(f" {C.RED}{result.stderr.strip()}{C.RESET}")
raise SystemExit(1)
return result
# =============================================================================
# STEP 1 — ASK FOR TARGET PROJECT PATH
# =============================================================================
def get_project_path():
"""
Prompt the user for the target project directory.
Creates it if it doesn't exist. Returns a resolved Path object.
"""
step("📁", "Project Location")
home = Path.home()
cwd = Path.cwd()
print(f"""
{C.WHITE}Where should your new project live?{C.RESET}
{C.BLUE}Press Enter to use the current directory, or enter a path.{C.RESET}
{C.BLUE} /absolute/path | ~/relative/to/home | name (creates {home}/name){C.RESET}
{C.BLUE}Note: ~ expands to {home} — do not repeat your username after it.{C.RESET}
""")
while True:
raw = input(
f" {C.BOLD}{C.MAGENTA}➜ Project path [{C.RESET}{C.YELLOW}{cwd}{C.RESET}{C.MAGENTA}]: {C.RESET}"
).strip()
# Empty input → use current directory (no further confirmation needed)
if not raw:
success(f"Using current directory: {C.YELLOW}{cwd}{C.RESET}")
return cwd
# ~ paths and absolute paths resolve as-is; bare names resolve from home.
expanded = Path(raw).expanduser()
path = expanded if expanded.is_absolute() else (home / expanded).resolve()
info(f"Resolved path: {C.YELLOW}{path}{C.RESET}")
# If it already exists, confirm they want to use it
if path.exists():
if path.is_file():
error("That path points to a file, not a directory. Please choose a directory.")
continue
warning(f"Directory already exists: {path}")
confirm = (
input(f" {C.BOLD}Use this existing directory? (Y/n): {C.RESET}").strip().lower()
)
if confirm not in ("y", ""):
info("OK, let's try a different path.")
continue
success(f"Using existing directory: {path}")
else:
# Directory doesn't exist — offer to create it
confirm = (
input(f" {C.BOLD}Directory does not exist. Create it? (Y/n): {C.RESET}")
.strip()
.lower()
)
if confirm not in ("y", ""):
info("OK, let's try a different path.")
continue
path.mkdir(parents=True, exist_ok=True)
success(f"Created directory: {path}")
return path
# =============================================================================
# STEP 2 — HOMEBREW
# Homebrew is our package manager for macOS. Almost everything else depends
# on it, so we check for it first and install if missing.
# =============================================================================
def brew_available() -> bool:
"""
Return True if Homebrew is installed and on PATH.
Used by other functions to decide whether to use brew or fall back to
an alternative install method. Corporate environments often block brew.
"""
return shutil.which("brew") is not None
def ensure_homebrew():
"""
Check if Homebrew is available and report its status.
Homebrew is optional - if not present we fall back to alternative
install methods for each tool. We never force-install brew because
corporate environments may explicitly prohibit it.
"""
step("🍺", "Homebrew")
if brew_available():
result = run(["brew", "--version"], capture=True, check=False)
version = (result.stdout or result.stderr).strip().splitlines()
success(f"Homebrew available: {version[0] if version else 'unknown'}")
info("Will use Homebrew to install system tools.")
# On Apple Silicon, brew lives in /opt/homebrew/bin - ensure it's on PATH
brew_path = "/opt/homebrew/bin"
if brew_path not in os.environ.get("PATH", ""):
os.environ["PATH"] = brew_path + ":" + os.environ.get("PATH", "")
info(f"Added {brew_path} to PATH for this session.")
else:
warning("Homebrew not found - will use fallback install methods for each tool.")
info("This is fine for corporate environments where brew is not permitted.")
info("To install Homebrew manually later: https://brew.sh")
# =============================================================================
# STEP 3 — GIT
# We need git for version control and for the pre-commit secret scanning hook.
# =============================================================================
def ensure_git():
"""
Check if git is installed.
Install via Homebrew if available, otherwise prompt the user to install
Xcode Command Line Tools which includes git on macOS.
"""
step("🗂️", "Git")
if shutil.which("git"):
result = run(["git", "--version"], capture=True, check=False)
success(f"Git already installed: {(result.stdout or result.stderr).strip()}")
return
if brew_available():
warning("Git not found - installing via Homebrew...")
run(["brew", "install", "git"])
success("Git installed successfully!")
else:
# On macOS, git ships with Xcode Command Line Tools
warning("Git not found and Homebrew is not available.")
info("Attempting to install via Xcode Command Line Tools...")
result = subprocess.run(
["xcode-select", "--install"], capture_output=True, text=True, check=False
)
# xcode-select --install exits non-zero if already installed, that's fine
info("If a dialog appeared, complete the installation then re-run Spellforge.")
info("Alternatively, download git from: https://git-scm.com/download/mac")
# Check if git is now available
if not shutil.which("git"):
fatal(
"Git is required but could not be installed automatically. "
"Please install git manually and re-run Spellforge."
)
success("Git is now available!")
def init_git_repo(project_path: Path):
"""
Initialize a git repository in the project directory, but only if
one doesn't already exist. We never clobber an existing repo.
Args:
project_path: The resolved Path to the project root directory.
"""
git_dir = project_path / ".git"
if git_dir.exists():
# Already a git repo — leave it alone
warning(f".git already exists in {project_path} - skipping git init.")
return
info("No .git directory found - initializing a new git repository...")
run(["git", "init", str(project_path)])
success("Git repository initialized!")
# =============================================================================
# STEP 4 — PYTHON + VIRTUAL ENVIRONMENT
# We use the system python3 (whatever is active via brew) and create a
# project-local venv so packages don't pollute the global environment.
# =============================================================================
# The base packages to install in every new project venv.
# datetime and os are Python builtins — no install needed for those.
BASE_PACKAGES = [
"pandas", # Data manipulation and analysis
"requests", # HTTP requests made simple
"python-dotenv", # Load .env files into environment variables
"pydantic", # Data validation using Python type hints
"pypdf", # Read and write PDF files
"ruff", # Fast Python linter and formatter (also used by Claude hook)
"pytest", # Testing framework - required by Definition of Done
"pytest-cov", # Coverage reporting - enforces 80% minimum per CLAUDE.md
"rich", # Beautiful terminal output - colors, tables, progress bars
"loguru", # Simple powerful logging - timestamps, color by level, zero config
]
def _find_pinned_python() -> str | None:
"""
Locate a Python interpreter matching PYTHON_TARGET_LABEL on disk.
Searches, in order:
1. The pinned-version bin name on PATH (e.g. `python3.13`).
2. Homebrew's keg-only formula path on Apple Silicon
(/opt/homebrew/opt/python@3.13/bin/python3.13).
3. Homebrew's keg-only formula path on Intel Macs
(/usr/local/opt/python@3.13/bin/python3.13).
Returns the absolute path to the interpreter, or None if not found.
We search the keg paths explicitly because `python@3.13` is keg-only and
may not be linked into /opt/homebrew/bin on machines where the user has
a different default Python pinned.
"""
# 1) Direct PATH lookup for the pinned-version binary
found = shutil.which(PYTHON_TARGET_BIN_NAME)
if found:
return found
# 2 & 3) Common brew keg locations for python@X.Y
candidate_paths = [
Path(f"/opt/homebrew/opt/{PYTHON_TARGET_BREW_FORMULA}/bin/{PYTHON_TARGET_BIN_NAME}"),
Path(f"/usr/local/opt/{PYTHON_TARGET_BREW_FORMULA}/bin/{PYTHON_TARGET_BIN_NAME}"),
]
for candidate in candidate_paths:
if candidate.exists() and os.access(candidate, os.X_OK):
return str(candidate)
# 4) Last resort: ask brew directly where the formula lives
if brew_available():
try:
result = subprocess.run(
["brew", "--prefix", PYTHON_TARGET_BREW_FORMULA],
capture_output=True,
text=True,
check=False,
)
if result.returncode == 0:
prefix = result.stdout.strip()
candidate = Path(prefix) / "bin" / PYTHON_TARGET_BIN_NAME
if candidate.exists():
return str(candidate)
except FileNotFoundError:
pass
return None
def ensure_python():
"""
Ensure Python PYTHON_TARGET_LABEL (e.g. 3.13) is available, and return
its absolute path.
Order of operations:
1. Look for an existing python3.13 binary in standard locations.
2. If absent and Homebrew is available, `brew install python@3.13`.
3. If brew isn't available, fatal with instructions for python.org.
We deliberately DO NOT fall back to a generic `python3` on PATH — the
whole point of pinning is to refuse silent drift to whatever Python the
user happens to have.
"""
step("🐍", f"Python {PYTHON_TARGET_LABEL}")
# ── First pass: is the pinned version already on disk somewhere? ──────────
existing = _find_pinned_python()
if existing:
result = run([existing, "--version"], capture=True, check=False)
version = (result.stdout or result.stderr).strip()
success(f"Python {PYTHON_TARGET_LABEL} already installed: {version}")
info(f"Using interpreter: {C.YELLOW}{existing}{C.RESET}")
return existing
# ── Not found — install via brew if possible ──────────────────────────────
if brew_available():
warning(
f"{PYTHON_TARGET_BIN_NAME} not found - installing via Homebrew "
f"({PYTHON_TARGET_BREW_FORMULA})..."
)
run(["brew", "install", PYTHON_TARGET_BREW_FORMULA])
# Re-resolve after install
installed = _find_pinned_python()
if installed:
success(f"Python {PYTHON_TARGET_LABEL} installed successfully!")
info(f"Using interpreter: {C.YELLOW}{installed}{C.RESET}")
return installed
fatal(
f"Homebrew reported success but {PYTHON_TARGET_BIN_NAME} was not\n"
f" found in any expected location. Try:\n"
f" brew reinstall {PYTHON_TARGET_BREW_FORMULA}\n"
f" Then re-run Spellforge."
)
# ── No brew, no pinned Python — manual install required ───────────────────
fatal(
f"Python {PYTHON_TARGET_LABEL} is required but not found, and Homebrew\n"
f" is not available to install it automatically.\n\n"
f" Options:\n"
f" • Install Homebrew (https://brew.sh) and re-run Spellforge\n"
f" • Install Python {PYTHON_TARGET_LABEL} from python.org:\n"
f" https://www.python.org/downloads/\n"
f" • Install via pyenv: pyenv install {PYTHON_TARGET_LABEL}\n\n"
f" Spellforge deliberately does not fall back to a different Python\n"
f" version - dependency wheel availability and tooling support are\n"
f" pinned to {PYTHON_TARGET_LABEL}.x."
)
def ensure_uv() -> str:
"""
Ensure the uv package manager is available, and return its absolute path.
Only called when the user chose uv on the installation menu. Installs via
Homebrew if available; otherwise fatals with a manual-install pointer,
matching ensure_python()'s no-brew fallback style. We deliberately do not
auto-run uv's `curl | sh` install script.
Returns:
Absolute path to the uv executable.
"""
step("⚡", "uv (Package Manager)")
existing = shutil.which("uv")
if existing:
result = run([existing, "--version"], capture=True, check=False)
success(f"uv already installed: {(result.stdout or result.stderr).strip()}")
return existing
if brew_available():
warning("uv not found - installing via Homebrew...")
run(["brew", "install", "uv"])
installed = shutil.which("uv")
if installed:
success("uv installed successfully!")
return installed
fatal("Homebrew reported success but uv was not found on PATH. Try: brew reinstall uv")
fatal(
"uv is required (you chose it as your package manager) but was not\n"
" found, and Homebrew is not available to install it automatically.\n\n"
" Options:\n"
" • Install Homebrew (https://brew.sh) and re-run Spellforge\n"
" • Install uv manually: https://docs.astral.sh/uv/getting-started/installation/\n"
" • Re-run Spellforge and choose pip instead"
)
def _venv_python_version(venv_path: Path) -> tuple[int, int] | None:
"""
Return the (major, minor) version tuple of the Python inside an existing
venv, or None if it can't be determined (binary missing, won't execute).
"""
venv_python = venv_path / "bin" / "python3"
if not venv_python.exists():
return None
result = subprocess.run(
[str(venv_python), "-c", "import sys; print(sys.version_info[0], sys.version_info[1])"],
capture_output=True,
text=True,
check=False,
)
if result.returncode != 0:
return None
try:
parts = result.stdout.strip().split()
return (int(parts[0]), int(parts[1]))
except (ValueError, IndexError):
return None
def _detect_venv_package_manager(venv_path: Path) -> str:
"""
Detect which package manager created an existing venv, by reading its
pyvenv.cfg. uv stamps an extra `uv = <version>` line into pyvenv.cfg that
the stdlib `venv` module never writes; its presence is the signal.
Used by repair mode, which is non-interactive and has no InstallChoices
to read the original selection from — this lets `--repair` re-target the
same package manager the project was originally bootstrapped with instead
of silently defaulting back to pip.
Args:
venv_path: Path to the venv directory (containing pyvenv.cfg).
Returns:
PACKAGE_MANAGER_UV if pyvenv.cfg has a `uv = ` line, else
PACKAGE_MANAGER_PIP (also the fallback when pyvenv.cfg is missing).
"""
pyvenv_cfg = venv_path / "pyvenv.cfg"
if not pyvenv_cfg.exists():
return PACKAGE_MANAGER_PIP
for line in pyvenv_cfg.read_text().splitlines():
if line.strip().startswith("uv "):
return PACKAGE_MANAGER_UV
return PACKAGE_MANAGER_PIP
def create_venv(project_path: Path, python_bin: str, package_manager: str = PACKAGE_MANAGER_PIP):
"""
Create a Python virtual environment inside the project directory.
Skips creation if a venv already exists AND it's the right Python version.
If an existing venv is the wrong version (e.g. left over from a prior
bootstrap that used a different Python), we fatal with instructions to
delete and rerun — silently reusing a stale venv is the bug that lets
the pre-commit hook point at a ruff binary that may or may not exist.
Args:
project_path: The resolved Path to the project root directory.
python_bin: Absolute path to the pinned-version python3 executable.
package_manager: PACKAGE_MANAGER_PIP or PACKAGE_MANAGER_UV. Selects
`python -m venv` vs `uv venv` for creation. A uv-created venv has
no pip installed by default — package installs go through uv
instead (see _install_packages()).
Returns:
Path to the venv's python3 executable (used to derive all other venv
binary paths and to drive package installs).
"""
step("📦", "Python Virtual Environment")
venv_path = project_path / ".venv"
venv_python_bin = venv_path / "bin" / "python3"
if venv_path.exists():
existing_version = _venv_python_version(venv_path)
if existing_version is None:
fatal(
f"Found existing .venv at {venv_path} but its Python is broken\n"
f" (cannot determine version). Delete it and re-run:\n"
f" rm -rf {venv_path}"
)
if existing_version != PYTHON_TARGET_MINOR:
fatal(
f"Found existing .venv at {venv_path} but it uses Python "
f"{existing_version[0]}.{existing_version[1]}, not the target "
f"{PYTHON_TARGET_LABEL}.\n"
f" Spellforge will not silently reuse a wrong-version venv.\n"
f" Delete it and re-run:\n"
f" rm -rf {venv_path}"
)
warning(
f".venv already exists at {venv_path} with Python "
f"{existing_version[0]}.{existing_version[1]} - reusing."
)
else:
info(f"Creating virtual environment at {venv_path} (using {python_bin})...")
if package_manager == PACKAGE_MANAGER_UV:
run(["uv", "venv", "--python", python_bin, str(venv_path)])
else:
run([python_bin, "-m", "venv", str(venv_path)])
success("Virtual environment created!")
return str(venv_python_bin)
# =============================================================================
# PIP HARDENING HELPERS
# Even when you invoke a venv's pip by absolute path, pip honors user-level
# config files (~/.config/pip/pip.conf, pip.ini) and these environment
# variables: PIP_USER, PIP_TARGET, PIP_PREFIX, PIP_INDEX_URL. On managed
# corporate Macs, IT sometimes pre-configures PIP_USER=1 or PIP_TARGET=...
# which silently redirects installs OUT of the venv. We defend against this
# by (a) invoking pip via `python -m pip` from the venv's interpreter
# (strict venv isolation), (b) passing --no-user --isolated, and (c)
# stripping the redirecting env vars from the subprocess environment.
# =============================================================================
def _isolated_pip_env() -> dict:
"""
Return an environment dict for pip subprocesses that defeats user-level
redirection. Inherits PATH and other vars from the parent process but
clobbers the pip-specific knobs that could send installs astray.
"""
env = os.environ.copy()
# Defeat PIP_USER=1 (which would install to ~/.local instead of the venv)
env["PIP_USER"] = "0"
# Defeat PIP_TARGET / PIP_PREFIX (which would redirect to alternate dirs)
env.pop("PIP_TARGET", None)
env.pop("PIP_PREFIX", None)
# Tell pip to ignore site-packages from outside the venv when resolving
env["PIP_REQUIRE_VIRTUALENV"] = "true"
return env
def _install_packages(
venv_python_bin: str,
packages: list[str],
package_manager: str = PACKAGE_MANAGER_PIP,
upgrade_pip_first: bool = False,
):
"""
Install packages into the venv using the chosen package manager.
pip: `<venv>/bin/python3 -m pip install --no-user --isolated <packages>`
with PIP_USER=0 and PIP_TARGET/PIP_PREFIX stripped, so the install lands
in the venv's site-packages regardless of user-level pip config.
uv: `uv pip install --python <venv>/bin/python3 <packages>`. uv targets
an arbitrary venv by interpreter path rather than by being invoked from
inside it, and has no equivalent user-config redirection to defend
against, so no isolation env is needed.
Args:
venv_python_bin: Path to the venv's python3 executable.
packages: List of package specifiers to install.
package_manager: PACKAGE_MANAGER_PIP or PACKAGE_MANAGER_UV.
upgrade_pip_first: If True (pip only), run `pip install --upgrade pip`
first. uv has no pip to upgrade, so this is a no-op under uv.
"""
if package_manager == PACKAGE_MANAGER_UV:
run(["uv", "pip", "install", "--python", venv_python_bin] + packages)
return
env = _isolated_pip_env()
if upgrade_pip_first:
run(
[
venv_python_bin,
"-m",
"pip",
"install",
"--no-user",
"--isolated",
"--upgrade",
"pip",
],
env=env,
)
run(
[venv_python_bin, "-m", "pip", "install", "--no-user", "--isolated"] + packages,
env=env,
)
def install_base_packages(
venv_python_bin: str, project_path: Path, package_manager: str = PACKAGE_MANAGER_PIP
):
"""
Install the base package set into the project venv.
Uses strict pip isolation (see _install_packages) so installs land in the
venv regardless of user-level pip.conf or PIP_USER environment variables.
Args:
venv_python_bin: Path to the venv's python3 executable.
project_path: Used for display/context only.
package_manager: PACKAGE_MANAGER_PIP or PACKAGE_MANAGER_UV.
"""
step("📚", "Installing Base Packages")
info(f"Packages to install: {C.YELLOW}{', '.join(BASE_PACKAGES)}{C.RESET}")
info(f"Installing base packages via {package_manager}...")
_install_packages(venv_python_bin, BASE_PACKAGES, package_manager, upgrade_pip_first=True)
success(f"All {len(BASE_PACKAGES)} base packages installed!")
# Show what's installed so the user has a clear record.
# uv-created venvs have no pip binary, so freeze the same way we installed.
info("Installed package versions:")
if package_manager == PACKAGE_MANAGER_UV:
freeze_cmd = ["uv", "pip", "freeze", "--python", venv_python_bin]
else:
freeze_cmd = [venv_python_bin, "-m", "pip", "freeze"]
result = run(freeze_cmd, capture=True, check=False)
for line in result.stdout.strip().splitlines():
# Only show our base packages, not pip's own deps
if any(pkg.lower().replace("-", "_") in line.lower() for pkg in BASE_PACKAGES):
print(f" {C.GREEN}{line}{C.RESET}")
# =============================================================================
# STEP 5 — DETECT-SECRETS
# Scans commits for accidentally included secrets (API keys, passwords, etc.)
# Prefer the Homebrew version; fall back to pip if brew install fails.
# =============================================================================
def install_detect_secrets(venv_python_bin: str, package_manager: str = PACKAGE_MANAGER_PIP):
"""
Install detect-secrets for pre-commit secret scanning.
Tries Homebrew first (preferred, system-wide) when brew is available.
Falls back to the venv (with isolation) otherwise.
Args:
venv_python_bin: Path to the venv's python3 executable, used as
fallback install target.
package_manager: PACKAGE_MANAGER_PIP or PACKAGE_MANAGER_UV.
Returns:
Path string to the detect-secrets executable that was installed.
"""
step("🕵️", "detect-secrets (Secret Scanning)")
# ── Try Homebrew first — but only if brew exists ──────────────────────────
# Previously this called `brew install` unconditionally, which raises
# FileNotFoundError on machines without Homebrew (crash, not graceful
# fallback). The guard below restores the intended behavior.
if brew_available():
info("Attempting to install detect-secrets via Homebrew (preferred)...")
brew_result = run(["brew", "install", "detect-secrets"], check=False)
if brew_result.returncode == 0:
detect_secrets_bin = shutil.which("detect-secrets")
if detect_secrets_bin:
success(f"detect-secrets installed via Homebrew: {detect_secrets_bin}")
return detect_secrets_bin
warning(
"brew install succeeded but detect-secrets not found on PATH - falling back to pip."
)
else:
warning("Homebrew install failed - falling back to pip install into venv.")
else:
info("Homebrew not available - installing detect-secrets into venv.")
# ── Fallback: install into the venv ────────────────────────────────────────
info(f"Installing detect-secrets into venv via {package_manager}...")
_install_packages(venv_python_bin, ["detect-secrets"], package_manager)
# The venv bin directory is one level up from python3 itself
venv_bin_dir = Path(venv_python_bin).parent
detect_secrets_bin = str(venv_bin_dir / "detect-secrets")
if not Path(detect_secrets_bin).exists():
fatal(
f"detect-secrets install failed via both Homebrew and {package_manager}. Cannot continue."
)
success(f"detect-secrets installed via {package_manager}: {detect_secrets_bin}")
return detect_secrets_bin
def _resolve_detect_secrets_bin(venv_python_bin: str) -> str:
"""
Resolve an existing detect-secrets binary path WITHOUT triggering an install.
Used by repair mode, which assumes the project was bootstrapped before and
just needs to point the pre-commit hook at whichever detect-secrets is
actually on disk now. Checks Homebrew first (matches install_detect_secrets'
preference), then the venv. Fatals if neither exists.
Args:
venv_python_bin: Path to the venv's python3 executable — used to derive
the venv bin directory where an installed detect-secrets would live.
"""
brew_path = shutil.which("detect-secrets")
if brew_path:
return brew_path
venv_path = str(Path(venv_python_bin).parent / "detect-secrets")
if Path(venv_path).exists():
return venv_path
fatal(
"detect-secrets not found on PATH or in the venv. "
"Run `spellforge.py` (without --repair) to install it, or "
"`brew install detect-secrets` manually."
)
def init_secrets_baseline(project_path: Path, detect_secrets_bin: str):
"""
Create the initial .secrets.baseline file that detect-secrets uses
to track known secrets in the repo. This is required before the
pre-commit hook will work correctly.
Args:
project_path: The resolved Path to the project root directory.
detect_secrets_bin: Path to the detect-secrets executable.
"""
baseline_path = project_path / ".secrets.baseline"
if baseline_path.exists():
warning(".secrets.baseline already exists - skipping baseline creation.")
return
info("Generating initial .secrets.baseline file...")
# Exclude the baseline itself and generated files that legitimately contain
# high-entropy strings (Alembic migration revision IDs). This must match the
# EXCLUDE_PATTERN in the pre-commit hook, or the baseline and the hook will
# disagree about what counts as a finding.
exclude_pattern = r"(\.secrets\.baseline$|/versions/.*\.py$|^alembic/versions/)"
result = run(
[detect_secrets_bin, "scan", "--exclude-files", exclude_pattern],
cwd=str(project_path),
capture=True,
check=False,
)
if result.returncode != 0:
warning(f"detect-secrets scan returned an error: {result.stderr.strip()}")
warning("Continuing - you can run 'detect-secrets scan > .secrets.baseline' manually.")
return
# Write the JSON output from detect-secrets scan to the baseline file
baseline_path.write_text(result.stdout)
success(f".secrets.baseline created at {baseline_path}")
# =============================================================================
# STEP 6 — RUFF CONFIGURATION
# Ruff is our linter and formatter. We write a pyproject.toml into the project
# root so ruff knows how to check this project's code.
# =============================================================================
# Full pyproject.toml content written to the project root.
# Includes:
# [project] — package metadata and declared dependencies
# [tool.ruff] — linter/formatter settings matching CLAUDE.md coding standards
# [tool.pytest] — test discovery and coverage enforcement
PYPROJECT_CONTENT = """
[project]
name = "{project_name}"
version = "0.1.0"
description = ""
requires-python = ">={target},<{next_major_minor}"
dependencies = [
"pandas",
"requests",
"python-dotenv",
"pydantic",
"pypdf",
]
[tool.ruff]
# Use tabs for indentation — matches project coding standards
indent-width = 4
line-length = 100
[tool.ruff.format]
# Enforce tabs, not spaces, as required by coding guidelines
indent-style = "tab"
[tool.ruff.lint]
# E/W = pycodestyle, F = pyflakes, I = isort import sorting
select = ["E", "F", "I", "W"]
# Rules we deliberately ignore:
# E501 — line too long (we trust developers on long strings/comments)
# E303 — too many blank lines (occasionally useful for readability)
ignore = ["E501", "E303", "W191", "E101"]
[tool.pytest.ini_options]
# Discover all tests/ directories recursively
testpaths = ["tests"]
# Enforce 80% minimum coverage on every run — matches Definition of Done
addopts = "--cov=. --cov-report=term-missing --cov-fail-under=80"
# AI NOTE: This coverage config is for the projects Spellforge creates, not for Spellforge itself.
# Spellforge is a bootstrapper script — it has no application code to cover.
""".strip()
def configure_pyproject(project_path: Path, project_name: str):
"""
Write the full pyproject.toml into the project root, including:
- [project] metadata section with declared dependencies
- [tool.ruff] linter/formatter settings
- [tool.pytest.ini_options] test discovery and coverage enforcement
If pyproject.toml already exists and contains [tool.ruff], skips.
If it exists but lacks [tool.ruff], appends only the tool sections.
Args:
project_path: The resolved Path to the project root directory.
project_name: The project name to embed in the [project] section.
"""
step("🔍", "pyproject.toml - Project Config + Ruff + Pytest")
pyproject_path = project_path / "pyproject.toml"
# Substitute the project name and pinned Python version into the template.
# next_major_minor reads e.g. "3.14" when target is "3.13", giving us the
# exclusive upper bound for requires-python.
next_major_minor = f"{PYTHON_TARGET_MINOR[0]}.{PYTHON_TARGET_MINOR[1] + 1}"
rendered = PYPROJECT_CONTENT.format(
project_name=project_name,
target=PYTHON_TARGET_LABEL,
next_major_minor=next_major_minor,
)
if pyproject_path.exists():
existing = pyproject_path.read_text()
if "[tool.ruff]" in existing:
warning("pyproject.toml already contains [tool.ruff] - skipping.")
return
# File exists but no ruff config — append just the tool sections
info("pyproject.toml exists - appending ruff + pytest config...")
tool_section = rendered[rendered.index("[tool.ruff]") :]
with pyproject_path.open("a") as f:
f.write(f"\n\n{tool_section}\n")
else:
# Fresh project — write the full file including [project] metadata
info("Creating pyproject.toml with project metadata + ruff + pytest config...")
pyproject_path.write_text(rendered + "\n")
success(f"pyproject.toml written: {pyproject_path}")
info(f" Project name : {project_name}")
info(" Ruff : tabs, line length 100, pycodestyle + pyflakes + isort")
info(" Pytest : tests/ directory, 80% coverage enforced")
# =============================================================================
# STEP 7 — CLAUDE CODE
# Install Claude Code globally via npm. Claude Code requires Node.js,
# so we install that via Homebrew first if it's missing.
# =============================================================================
def ensure_node():
"""
Check if Node.js (and npm) is available.
Install via Homebrew if available, otherwise direct the user to nodejs.org.
Claude Code is an npm package and requires Node to run.
"""
if shutil.which("node"):
result = run(["node", "--version"], capture=True, check=False)
success(f"Node.js already installed: {(result.stdout or result.stderr).strip()}")
return
if brew_available():
warning("Node.js not found - attempting install via Homebrew...")
result = run(["brew", "install", "node"], check=False)
if result.returncode == 0 and shutil.which("node"):
success("Node.js installed!")
return
warning(
"Homebrew install failed (corporate Workbrew restrictions?). Try installing Node.js manually."