-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfigure.py
More file actions
executable file
·177 lines (148 loc) · 4.75 KB
/
Copy pathconfigure.py
File metadata and controls
executable file
·177 lines (148 loc) · 4.75 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
#!/usr/bin/env python3
"""Configure the local machine for use with this dev-env repository."""
from __future__ import annotations
import argparse
import os
import re
import subprocess
import sys
from pathlib import Path
from shutil import which
MARKER = "# dev-env configure.py"
SCRIPT_DIR = Path(__file__).resolve().parent
def detect_shell() -> str:
shell = os.environ.get("SHELL", "")
if shell:
name = Path(shell).name
if name:
return name
return "zsh"
def expand_path(path: str) -> Path:
return Path(os.path.expanduser(path)).resolve()
def shell_config_path(shell_name: str) -> Path:
return Path.home() / f".{shell_name}rc"
def prompt_notes_dir(default: str) -> Path:
try:
response = input(f"Notes directory [{default}]: ").strip()
except (EOFError, KeyboardInterrupt):
print(file=sys.stderr)
sys.exit(1)
if not response:
response = default
return expand_path(response)
def write_benvrc(
notes_dir: Path,
dev_env_home: Path,
git_remote: str,
work_registry: str,
) -> Path:
notes_dir.mkdir(parents=True, exist_ok=True)
benvrc_path = notes_dir / ".benvrc"
benvrc_path.write_text(
"\n".join(
[
MARKER,
f'export DEFAULT_GIT_REMOTE="{git_remote}"',
f'export DEV_ENV_HOME="{dev_env_home}"',
f'export WORK_REGISTRY="{work_registry}"',
'source "${DEV_ENV_HOME}/.bash_profile"',
"",
]
)
)
return benvrc_path
def update_shell_config(shell_config: Path, benvrc_path: Path) -> None:
source_line = f'source "{benvrc_path}"'
block = f"{MARKER}\n{source_line}\n"
pattern = re.compile(
rf"{re.escape(MARKER)}\nsource \".*?\"\n?",
re.MULTILINE,
)
if shell_config.exists():
content = shell_config.read_text()
if pattern.search(content):
content = pattern.sub(block, content)
elif MARKER in content:
content = content.rstrip() + "\n\n" + block
else:
if content and not content.endswith("\n"):
content += "\n"
content += "\n" + block
else:
content = block
shell_config.parent.mkdir(parents=True, exist_ok=True)
shell_config.write_text(content)
def configure_git(name: str, email: str) -> None:
if not which("git"):
print("git not found; skipping global user.name and user.email.", file=sys.stderr)
return
subprocess.run(
["git", "config", "--global", "user.name", name],
check=True,
)
subprocess.run(
["git", "config", "--global", "user.email", email],
check=True,
)
print(f"Set global git user.name to {name!r}")
print(f"Set global git user.email to {email!r}")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Configure your machine for use with this dev-env repository.",
)
parser.add_argument(
"--notes",
help="Notes directory for .benvrc (prompted if omitted, default: ~/notes)",
)
parser.add_argument(
"--default-git-remote",
default="origin",
help="Value for DEFAULT_GIT_REMOTE (default: origin)",
)
parser.add_argument(
"--work-registry",
default="beverts312",
help="Value for WORK_REGISTRY (default: beverts312)",
)
parser.add_argument(
"--dev-env-home",
default=str(SCRIPT_DIR),
help="Value for DEV_ENV_HOME (default: directory containing this script)",
)
parser.add_argument(
"--default-shell",
default=None,
help="Shell whose rc file to update (default: invoker's shell, else zsh)",
)
parser.add_argument(
"--git-name",
default="beverts312",
help="Global git user.name (default: beverts312)",
)
parser.add_argument(
"--git-email",
default="texas312@gmail.com",
help="Global git user.email (default: texas312@gmail.com)",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
notes_dir = expand_path(args.notes) if args.notes else prompt_notes_dir("~/notes")
dev_env_home = expand_path(args.dev_env_home)
shell_name = args.default_shell or detect_shell()
shell_config = shell_config_path(shell_name)
benvrc_path = write_benvrc(
notes_dir,
dev_env_home,
args.default_git_remote,
args.work_registry,
)
update_shell_config(shell_config, benvrc_path)
configure_git(args.git_name, args.git_email)
print(f"Wrote {benvrc_path}")
print(f"Updated {shell_config}")
print()
print("Restart your shell or run:")
print(f' source "{benvrc_path}"')
if __name__ == "__main__":
main()