-
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathpost_gen_project.py
More file actions
103 lines (88 loc) · 3.23 KB
/
Copy pathpost_gen_project.py
File metadata and controls
103 lines (88 loc) · 3.23 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
#!/usr/bin/env python
import shutil
import subprocess
import tomllib
import shlex
from termcolor import cprint, colored
from pathlib import Path
CONDITIONAL_MANIFEST = Path("conditional_files.toml")
REPLACE_MANIFEST = Path("replaceable_files.toml")
def delete_resource(resource: Path):
if resource.is_file():
resource.unlink()
elif resource.is_dir():
shutil.rmtree(resource)
def delete_resources_for_disabled_features():
with CONDITIONAL_MANIFEST.open("rb") as manifest_file:
manifest = tomllib.load(manifest_file)
for feature in manifest["features"]:
enabled = feature["enabled"].lower() != "true"
name = feature["name"]
resources = feature["resources"]
if enabled:
text = "{} resources for disabled feature {}...".format(
colored("Removing", color="red"),
colored(name, color="magenta", attrs=["underline"]),
)
print(text)
for resource in resources:
delete_resource(Path(resource))
delete_resource(CONDITIONAL_MANIFEST)
cprint("cleanup complete!", color="green")
def replace_resources():
print(
"⭐ Placing {} nicely in your {} ⭐".format(
colored("resources", color="green"), colored("new project", color="blue")
)
)
with REPLACE_MANIFEST.open("rb") as replace_manifest:
manifest = tomllib.load(replace_manifest)
for substitution in manifest["sub"]:
target = Path(substitution["target"])
replaces = [Path(path) for path in substitution["replaces"]]
delete_resource(target)
for src_file in replaces:
if src_file.exists():
shutil.move(src_file, target)
delete_resource(REPLACE_MANIFEST)
print(
"Resources are happy to be where {}.".format(
colored("they are needed the most", color="green", attrs=["underline"])
)
)
def run_cmd(cmd: str, ignore_error: bool = False):
out = subprocess.run(
shlex.split(cmd),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if out.returncode != 0 and not ignore_error:
cprint(" WARNING ".center(50, "="))
cprint(
f"[WARN] Command `{cmd}` was not successfull. Check output below.",
"yellow",
)
cprint(
"However, the project was generated. So it could be a false-positive.",
"yellow",
)
cprint(out.stdout.decode(), "red")
cprint(out.stderr.decode(), "red")
exit(1)
def init_repo():
run_cmd("git init")
cprint(" Git repository initialized", "green")
run_cmd("git add .")
cprint("🐍 Installing python dpendencies with UV", "green")
run_cmd("uv sync")
run_cmd("uv run pre-commit install")
cprint("📚🖌️📄📏 Tidying up the project", "green")
for _ in range(2):
run_cmd("uv run pre-commit run -a", ignore_error=True)
run_cmd("git add .")
cprint("🚀Creating your first commit", "green")
run_cmd("git commit -m 'Initial commit'")
if __name__ == "__main__":
delete_resources_for_disabled_features()
replace_resources()
init_repo()