Debian Codegraph @

tooling/src/hypothesistooling/installers.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 """Module for obtaining various versions of Python.
12
13 This is a thin shim around ``uv python install`` via the
14 ``ensure-python.sh`` helper script.
15 """
16
17 import os
18 import shutil
19 import subprocess
20
21 from hypothesistooling import scripts
22 from hypothesistooling.junkdrawer import once
23
24 HOME = os.environ["HOME"]
25
26
27 def __python_executable(version):
28 return os.path.join(scripts.SNAKEPIT, version, "bin", "python")
29
30
31 def python_executable(version):
32 ensure_python(version)
33 return __python_executable(version)
34
35
36 PYTHONS = set()
37
38
39 def ensure_python(version):
40 if version in PYTHONS:
41 return
42 scripts.run_script("ensure-python.sh", version)
43 target = __python_executable(version)
44 assert os.path.exists(target), target
45 PYTHONS.add(version)
46
47
48 STACK = os.path.join(HOME, ".local", "bin", "stack")
49 SHELLCHECK = shutil.which("shellcheck") or os.path.join(
50 HOME, ".local", "bin", "shellcheck"
51 )
52
53
54 def ensure_stack():
55 if os.path.exists(STACK):
56 return
57 subprocess.check_call("mkdir -p ~/.local/bin", shell=True)
58 # if you're on macos, this will error with "--wildcards is not supported"
59 # or similar. You should put shellcheck on your PATH with your package
60 # manager of choice; eg `brew install shellcheck`.
61 subprocess.check_call(
62 "curl -L https://www.stackage.org/stack/linux-x86_64 "
63 "| tar xz --wildcards --strip-components=1 -C $HOME"
64 "/.local/bin '*/stack'",
65 shell=True,
66 )
67
68
69 @once
70 def update_stack():
71 ensure_stack()
72 subprocess.check_call([STACK, "update"])
73
74
75 @once
76 def ensure_shellcheck():
77 if os.path.exists(SHELLCHECK):
78 return
79 update_stack()
80 subprocess.check_call([STACK, "install", "ShellCheck"])