Debian Codegraph @

contrib/automation/automation.py

raw
1 #!/usr/bin/env python3
2 #
3 # automation.py - Perform tasks on remote machines
4 #
5 # Copyright 2019 Gregory Szorc <[email protected]>
6 #
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
9
10 import os
11 import pathlib
12 import subprocess
13 import sys
14 import venv
15
16
17 HERE = pathlib.Path(os.path.abspath(__file__)).parent
18 REQUIREMENTS_TXT = HERE / 'requirements.txt'
19 SOURCE_DIR = HERE.parent.parent
20 VENV = SOURCE_DIR / 'build' / 'venv-automation'
21
22
23 def bootstrap():
24 venv_created = not VENV.exists()
25
26 VENV.parent.mkdir(exist_ok=True)
27
28 venv.create(VENV, with_pip=True)
29
30 if os.name == 'nt':
31 venv_bin = VENV / 'Scripts'
32 pip = venv_bin / 'pip.exe'
33 python = venv_bin / 'python.exe'
34 else:
35 venv_bin = VENV / 'bin'
36 pip = venv_bin / 'pip'
37 python = venv_bin / 'python'
38
39 args = [
40 str(pip),
41 'install',
42 '-r',
43 str(REQUIREMENTS_TXT),
44 '--disable-pip-version-check',
45 ]
46
47 if not venv_created:
48 args.append('-q')
49
50 subprocess.run(args, check=True)
51
52 os.environ['HGAUTOMATION_BOOTSTRAPPED'] = '1'
53 os.environ['PATH'] = '%s%s%s' % (venv_bin, os.pathsep, os.environ['PATH'])
54
55 subprocess.run([str(python), __file__] + sys.argv[1:], check=True)
56
57
58 def run():
59 import hgautomation.cli as cli
60
61 # Need to strip off main Python executable.
62 cli.main()
63
64
65 if __name__ == '__main__':
66 try:
67 if 'HGAUTOMATION_BOOTSTRAPPED' not in os.environ:
68 bootstrap()
69 else:
70 run()
71 except subprocess.CalledProcessError as e:
72 sys.exit(e.returncode)
73 except KeyboardInterrupt:
74 sys.exit(1)