-
Notifications
You must be signed in to change notification settings - Fork 16
/
bootstrap
executable file
·420 lines (333 loc) · 13 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python3
# Only use builtin libraries for this script
import re
import readline
import shutil
import subprocess # nosec
import sys
import urllib.error
import urllib.request
from argparse import ArgumentParser
from datetime import date
from enum import Enum
from pathlib import Path
from typing import List, Union
parser = ArgumentParser(description="Prepares python project repository.")
parser.add_argument("--no-verify", action="store_true", help="Disable input verification.")
cli_args = parser.parse_args()
POETRY_BIN = shutil.which("poetry")
REPO = Path(__file__).parent
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."""
class DocFormat(Enum):
MARKDOWN = "md"
RESTRUCTURED_TEXT = "rst"
@classmethod
def from_str(cls, label):
label = label.lower()
if label in ("markdown", "md"):
return cls.MARKDOWN
if label in ("restructuredtext", "rst"):
return cls.RESTRUCTURED_TEXT
raise BadResponseError("is not a valid choice.")
def _check_pypi_name_available(package_name) -> Union[None, bool]:
try:
urllib.request.urlopen(f"https://pypi.org/pypi/{package_name}/json") # noqa: S310
except urllib.error.HTTPError as e:
if e.code == 404:
return True # Package does not exist (name is available)
except urllib.error.URLError as e:
print(f"Failed to reach PyPI server. Reason: {e.reason}")
else:
return False # If we reach this point, the package exists (name is not available)
return None # Unable to determine
def is_pypi_name_available(package_name):
availability = _check_pypi_name_available(package_name)
if availability:
return
elif availability is None:
msg = "Failed to check PyPI for name availability. Continue? [default: no]"
else:
msg = f"{package_name!r} is not available on PyPI. Continue? [default: no]"
response = validate_input(msg, is_boolean, default="no")
if response not in yes_terms:
raise BadResponseError
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)
response = subprocess.check_output(["poetry", "self", "show", "plugins"]).decode() # noqa: S603, S607
if "poetry-dynamic-versioning" not in response:
# Add necessary poetry 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 or cli_args.no_verify:
break
if callable(validate):
validate = [validate]
try:
for v in validate:
v(response)
break
except BadResponseError as e:
if str(e):
print(f'"{response}" {e}')
print('!!! To disable these validation checks, run "./bootstrap --no-verify"')
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 add_to_pyproject(after, new_line, replace=False):
if not new_line.endswith("\n"):
new_line += "\n"
if not after.endswith("\n"):
after += "\n"
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 == after:
if not replace:
f.write(line)
line = new_line
f.write(line)
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, is_pypi_name_available))
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
doc_format = DocFormat.from_str(
validate_input(
"Do you want README to be markdown or restructuredtext? [default: markdown]",
DocFormat.from_str,
default="markdown",
)
)
replacements["README.rst"] = f"README.{doc_format.value}"
######################
# 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()
Path("pythontemplate/_c_extension.pyi").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_to_pyproject(
'requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.1"]',
'requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.1", "Cython>=3.0.0", "setuptools>=61.0"]',
replace=True,
)
add_to_pyproject(
"[tool.poetry.group.dev.dependencies]",
'cython = ">=1.0.0"',
)
add_to_pyproject(
"[tool.poetry.build]",
"script = 'build.py'",
)
else:
# Delete cython-related files
paths_to_delete: List[Union[str, Path]] = [
".github/workflows/build_wheels.yaml",
"build.py",
"pythontemplate/_c_src",
"pythontemplate/_c_extension.pyx",
"pythontemplate/cpythontemplate.pxd",
# Do not include "pythontemplate/_c_extension.pyi" here,
# it is removed in the ``is_typed`` section above.
]
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))
(REPO / "README.rst").unlink() # Delete python-template's README
if doc_format == DocFormat.MARKDOWN:
(REPO / "README_TEMPLATE.md").replace(REPO / "README.md")
(REPO / "README_TEMPLATE.rst").unlink()
add_to_pyproject(
"[tool.poetry.group.docs.dependencies]",
'myst-parser = {extras = ["linkify"], version = "^3.0.1"}',
)
replacements[":start-after: inclusion-marker-do-not-remove"] = ":parser: myst_parser.sphinx_"
elif doc_format == DocFormat.RESTRUCTURED_TEXT:
(REPO / "README_TEMPLATE.rst").replace(REPO / "README.rst")
(REPO / "README_TEMPLATE.md").unlink()
else:
raise NotImplementedError
def replace(string):
"""Replace whole words only."""
def _replace(match):
return replacements[match.group(0)]
# notice that the 'this' in 'thistle' is not matched
pattern = "|".join(rf"\b{re.escape(s)}\b" if " " not in s else re.escape(s) for s in replacements)
return re.sub(pattern, _replace, string)
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.extend(REPO.rglob("*.md"))
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"])
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
Path(__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()