Debian Codegraph @

tooling/src/hypothesistooling/scripts.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 import os
12 import re
13 import shlex
14 import subprocess
15 import sys
16
17 from hypothesistooling import ROOT
18
19
20 def print_command(command, args):
21 args = list(args)
22 ranges = []
23 for i, v in enumerate(args):
24 if os.path.exists(v):
25 if not ranges or ranges[-1][-1] < i - 1:
26 ranges.append([i, i])
27 elif ranges[-1][-1] + 1 == i:
28 ranges[-1][-1] += 1
29 for i, j in ranges:
30 if j > i:
31 args[i] = "..."
32 for k in range(i + 1, j + 1):
33 args[k] = None
34 args = [v for v in args if v is not None]
35 print(command, *map(shlex.quote, args))
36
37
38 def run_script(script, *args, **kwargs):
39 print_command(script, args)
40 return subprocess.check_call([os.path.join(SCRIPTS, script), *args], **kwargs)
41
42
43 SCRIPTS = ROOT / "tooling" / "scripts"
44 COMMON = SCRIPTS / "common.sh"
45
46
47 def __calc_script_variables():
48 exports = re.compile(r"^export ([A-Z_]+)(=|$)", flags=re.MULTILINE)
49
50 common = COMMON.read_text(encoding="utf-8")
51
52 for name, _ in exports.findall(common):
53 globals()[name] = os.environ[name]
54
55
56 try:
57 __calc_script_variables()
58 except Exception:
59 pass
60
61
62 def tool_path(name):
63 return os.path.join(os.path.dirname(sys.executable), name)
64
65
66 def pip_tool(name, *args, **kwargs):
67 args = [str(arg) for arg in args]
68 print_command(name, args)
69 r = subprocess.call([tool_path(name), *args], **kwargs)
70
71 if r != 0:
72 sys.exit(r)