-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdev.py
More file actions
82 lines (69 loc) · 2.89 KB
/
dev.py
File metadata and controls
82 lines (69 loc) · 2.89 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
"""
Cookiecutter PyPackage Development Watcher
This script watches for changes in the template directory and
automatically regenerates python_boilerplate/ when changes are detected.
Usage:
1. Run this script from the cookiecutter-pypackage repo root directory
2. Make changes to files in {{cookiecutter.package_name}}/
3. The script will automatically regenerate python_boilerplate/ with your changes
4. Press Ctrl+C to stop watching
The generated python-boilerplate/ directory will be created in the repo root.
"""
import io
import shutil
import sys
import time
from pathlib import Path
# Flush prints immediately, even when stdout is piped or redirected
if isinstance(sys.stdout, io.TextIOWrapper):
sys.stdout.reconfigure(line_buffering=True)
from cookiecutter.main import cookiecutter
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
class ChangeHandler(FileSystemEventHandler):
def __init__(self):
self.last_run = 0
self.debounce_period = 2 # seconds
def on_any_event(self, event):
if event.is_directory:
return
src = Path(event.src_path)
# Ignore changes to dev.py itself
if src.name == "dev.py":
return
# For root-level watches, only react to cookiecutter.json
if src.parent == Path(".").resolve() and src.name != "cookiecutter.json":
return
current_time = time.time()
if current_time - self.last_run > self.debounce_period:
self.last_run = current_time
print(f"Detected change in {event.src_path}. Running cookiecutter...")
try:
# Output dir matches package_name in cookiecutter.json
output_dir = Path("python-boilerplate")
if output_dir.exists() and output_dir.is_dir():
print(f"Removing existing directory: {output_dir}")
shutil.rmtree(output_dir)
# The template is the current directory, output to repo root
cookiecutter(".", no_input=True, output_dir=".")
print("Cookiecutter finished successfully.")
except Exception as e:
print(f"Error running cookiecutter: {e}")
print("Waiting for next change...")
if __name__ == "__main__":
# Watch the template directory and cookiecutter.json
template_dir = "{{cookiecutter.package_name}}"
event_handler = ChangeHandler()
observer = Observer()
observer.schedule(event_handler, template_dir, recursive=True)
observer.schedule(event_handler, ".", recursive=False)
observer.start()
print(f"Watching for file changes in '{template_dir}/' and 'cookiecutter.json'...")
print("Press Ctrl+C to stop.")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Stopping watcher.")
observer.stop()
observer.join()