-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_proj.py
executable file
·103 lines (82 loc) · 2.58 KB
/
init_proj.py
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 python3
import os
import pathlib
import re
import subprocess
from typing import Dict
import functools
_TARGETS = {
"<<name>>": "Name for git config",
"<<email>>": "Email for git config and/or slurm begin/end/fail notices",
"<<drac-account>>": "DRAC Account (def-xxxx)",
"<<working-dir>>": "Full path to working directory",
"<<project-name>>": "Name of project and python package",
"<<repo-url>>": "The URL of the project on github",
"<<wandb-project>>": "wandb project",
"<<wandb-entity>>": "wandb entity",
"<<wandb-api-key>>": "wandb API key",
}
_TEMPLATE_CONFIG = {
"<<poetry-version>>": "1.8.1",
"<<user>>": os.environ["USER"],
"<<uid>>": str(os.geteuid()),
"<<gid>>": str(os.getegid()),
}
_SKIP_DIRS = {
".git",
"data",
}
_SKIP_FILES = {".env.template", "init_proj.py"}
def patch_token(match, inputs):
return inputs[match.group(0)]
def patch_file(child, inputs):
if child.name in _SKIP_FILES:
return
with open(child, "r") as handle:
file_text = handle.read()
for k in inputs.keys():
file_text = re.sub(
k, functools.partial(patch_token, inputs=inputs), file_text
)
with open(child, "w") as handle:
handle.write(file_text)
handle.flush()
return
def walk_dir(dir: pathlib.Path, inputs: Dict[str, str]):
if dir.name in _SKIP_DIRS:
return
for child in dir.iterdir():
if child.is_dir():
walk_dir(child, inputs)
elif child.is_file():
patch_file(child, inputs)
else:
raise RuntimeError("Not a dir or a file!?")
def generate_env_file():
subprocess.run(["cp", ".env.template", ".env"])
def rename_python_package(inputs: Dict[str, str]):
p = pathlib.Path.cwd() / "src" / "<<project-name>>"
p.rename(pathlib.Path.cwd() / "src" / inputs["<<project-name>>"])
def get_inputs():
inputs = {}
for k, v in _TARGETS.items():
inputs[k] = str(input(f"Please input your {v}: "))
# We don't want extra dir delimiter in working-dir
inputs["<<working-dir>>"] = inputs["<<working-dir>>"].rstrip("/")
print(f"You inputted: ")
for k, v in inputs.items():
print(f"{k}: {v}")
is_correct = input(f"Is this correct? y/n: ")
if is_correct.lower() == "y":
inputs.update(_TEMPLATE_CONFIG)
return inputs
else:
get_inputs()
def main():
inputs = get_inputs()
generate_env_file()
walk_dir(pathlib.Path.cwd(), inputs)
rename_python_package(inputs)
return
if __name__ == "__main__":
main()