forked from BrianPugh/python-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap
executable file
·322 lines (247 loc) · 9.63 KB
/
bootstrap
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
#!/usr/bin/env python3
# Only use builtin libraries for this script
import re
import readline
import shutil
import subprocess # nosec
import sys
from datetime import date
from pathlib import Path
from typing import List, Union
POETRY_BIN = shutil.which("poetry")
class col: # noqa: N801
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
class BadResponseError(Exception):
"""User gave a response that does not agree with rule set."""
def run(cmds, **kwargs):
"""Run bash cmd without capturing stdout."""
subprocess.run(cmds, capture_output=False, check=True, **kwargs) # noqa: S603
def check_and_install_poetry():
global POETRY_BIN
if not POETRY_BIN:
print("poetry not found. Installing...")
run(
"curl -sSL https://install.python-poetry.org | python3 -",
shell=True, # noqa: S604
)
POETRY_BIN = str(Path("~/.local/bin/poetry").expanduser())
# Check if the poetry installation is potentially broken; see issue #46
try:
subprocess.check_output(["poetry", "--version"]) # noqa: S603, S607
except subprocess.CalledProcessError:
print(
f"{col.FAIL}\n\nPoetry installation failed healthcheck. Manual debugging required. It is unlikely that python-template's bootstrapping is at fault.\n{col.ENDC}"
)
sys.exit(1)
# Add necessary plugins
run(["poetry", "self", "add", "poetry-dynamic-versioning[plugin]"])
def camel_to_snake(s):
return "".join(["_" + c.lower() if c.isupper() else c for c in s]).lstrip("_")
def validate_input(prompt, validate=None, default=""):
"""Prompts user until response passes ``validate``."""
while True:
response = input(prompt + ": ")
if not response:
response = default
if validate is None:
break
try:
validate(response)
break
except BadResponseError as e:
print(f'"{response}" {e}')
return response
def git(*args):
return subprocess.check_output(["git"] + list(args)) # noqa: S603
yes_terms = {"yes", "y", "true", "t", "1"}
no_terms = {"no", "n", "false", "f", "0"}
def is_boolean(response):
response = response.lower()
if response not in yes_terms.union(no_terms):
raise BadResponseError
def is_identifier(response):
if not response.isidentifier():
raise BadResponseError("is not a valid python identifier.")
def is_lower(response):
if response.lower() != response:
raise BadResponseError("should be all lower case.")
def is_not_empty(response):
if not response:
raise BadResponseError("Cannot be empty.")
def good_module_name(response):
is_identifier(response)
is_lower(response)
if "_" in response:
raise BadResponseError("should not contain an underscore _")
if len(response) > 20:
raise BadResponseError("is too long (max 20 char limit).")
def good_class_name(response):
is_identifier(response)
if not response[0].isupper():
raise BadResponseError("first letter should be capitalized.")
def remove_lines_in_file(file, *lines_to_remove):
file = Path(file)
lines_to_remove = {line + "\n" for line in lines_to_remove}
with file.open("r+") as f:
lines = f.readlines()
f.seek(0)
lines = [line for line in lines if line not in lines_to_remove]
# Remove all trailing newlines
while lines[-1] == "\n":
lines.pop()
for line in lines:
f.write(line)
f.truncate()
def git_remote_to_username_repo(git_remote_url):
ssh_prefix = "[email protected]:"
https_prefix = "https://github.com/"
if git_remote_url.startswith(ssh_prefix):
username_repo = git_remote_url[len(ssh_prefix) : -4]
elif git_remote_url.startswith(https_prefix):
username_repo = git_remote_url[len(https_prefix) : -4]
else:
raise ValueError(f"Unknown remote url {git_remote_url}")
return username_repo.split("/", 1)
def main():
check_and_install_poetry()
replacements: dict[str, str] = {
"CURRENT_YEAR_HERE": str(date.today().year),
}
git_remote_url = git("config", "--get", "remote.origin.url").decode().strip()
try:
(
replacements["GIT_USERNAME"],
replacements["GIT_REPONAME"],
) = git_remote_to_username_repo(git_remote_url)
except ValueError:
print("Unable to get github URL. You will manually have to replace GIT_USERNAME and GIT_REPONAME in README.rst")
########################
# User Questions Begin #
########################
replacements["YOUR_NAME_HERE"] = validate_input("Enter your name", is_not_empty)
replacements["pythontemplate"] = validate_input("Python Module Name", good_module_name)
replacements["cpythontemplate"] = "c" + replacements["pythontemplate"]
is_library = validate_input(
"Is this going to be a library? [default: yes]",
is_boolean,
default="yes",
)
is_library = is_library in yes_terms
is_typed = validate_input(
"Are you going to add type hints? [default: yes]",
is_boolean,
default="yes",
)
is_typed = is_typed in yes_terms
include_cli = validate_input(
"Would you like to include a CLI using Cyclopts? [default: yes]",
is_boolean,
default="yes",
)
include_cli = include_cli in yes_terms
use_cython = validate_input(
"Will your project use Cython? [default: no]",
is_boolean,
default="no",
)
use_cython = use_cython in yes_terms
######################
# User Questions End #
######################
if is_library:
# Don't need docker for library.
Path("Dockerfile").unlink()
Path(".dockerignore").unlink()
Path(".github/workflows/docker.yaml").unlink()
if not is_typed:
# Remove the py.typed file
Path("pythontemplate/py.typed").unlink()
if not include_cli:
# Delete CLI-replated files & config
Path("pythontemplate/__main__.py").unlink()
shutil.rmtree("pythontemplate/cli")
remove_lines_in_file("pyproject.toml", 'pythontemplate = "pythontemplate.cli.main:run_app"')
if use_cython:
# Forgo nice simple poetry action for CIBuildWheel.
Path(".github/workflows/deploy.yaml").unlink()
# Add Cython-related configurations to pyproject.toml
with Path("pyproject.toml").open() as f:
pyproject_contents = f.readlines()
with Path("pyproject.toml").open("w") as f:
for line in pyproject_contents:
if line == 'requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.1"]\n':
line = 'requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.1", "Cython>=3.0.0", "setuptools>=61.0"]\n'
f.write(line)
if line == "[tool.poetry.group.dev.dependencies]\n":
f.write('cython = ">=1.0.0"\n')
if line == "[tool.poetry.build]\n":
f.write("script = 'build.py'\n")
else:
# Delete cython-related files
paths_to_delete: List[Union[str, Path]] = [
".github/workflows/build_wheels.yaml",
"build.py",
"pythontemplate/_c_src",
]
paths_to_delete.extend(Path("pythontemplate").rglob("*.pyx"))
paths_to_delete.extend(Path("pythontemplate").rglob("*.pxd"))
for path_to_delete in paths_to_delete:
path_to_delete = Path(path_to_delete)
if path_to_delete.is_file():
path_to_delete.unlink()
else:
shutil.rmtree(str(path_to_delete))
def replace(string):
"""Replace whole words only."""
def _replace(match):
return replacements[match.group(0)]
# notice that the 'this' in 'thistle' is not matched
return re.sub("|".join(r"\b%s\b" % re.escape(s) for s in replacements), _replace, string)
repo = Path(__file__).parent
bootstrap_file = repo / "bootstrap"
files: list[Path] = list(repo.rglob("*.py"))
files.extend(repo.rglob("*.pyx"))
files.extend(repo.rglob("*.pxd"))
files.extend(repo.rglob("*.yaml"))
files.extend(repo.rglob("*.rst"))
files.append(Path("pyproject.toml"))
files.append(Path(".gitignore"))
if not is_library:
files.append(Path("Dockerfile"))
for file in files:
contents = file.read_text()
contents = replace(contents)
file.write_text(contents)
if file.stem in replacements:
dst = file.with_name(replacements[file.stem] + file.suffix)
file.replace(dst)
# Move the app folder
(repo / "pythontemplate").replace(repo / replacements["pythontemplate"])
# Move README_TEMPLATE.rst
(repo / "README_TEMPLATE.rst").replace(repo / "README.rst")
run([POETRY_BIN, "config", "virtualenvs.in-project", "true", "--local"])
if not include_cli:
run([POETRY_BIN, "remove", "cyclopts"])
run([POETRY_BIN, "remove", "rich"])
run([POETRY_BIN, "update"])
run([POETRY_BIN, "install"])
run(["poetry", "run", "python", "-m", "pre_commit", "install"])
run(["poetry", "run", "python", "-m", "pre_commit", "autoupdate"])
# Delete this script
bootstrap_file.unlink()
git("add", "-A")
git("commit", "-m", "bootstrap from template", "--no-verify")
print(col.OKGREEN)
print("Bootstrapping complete. Changes committed. Please run:")
print(" git push")
print(col.ENDC)
if __name__ == "__main__":
main()