-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
dev.py
84 lines (70 loc) · 2.76 KB
/
dev.py
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
# A script for running pyperformance out of the repo in dev-mode.
import os.path
import sys
REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
VENVS = os.path.join(REPO_ROOT, '.venvs')
def resolve_venv_root(kind='dev', venvsdir=VENVS):
import sysconfig
if sysconfig.is_python_build():
sys.exit('please install your built Python first (or pass it using --python)')
# XXX Handle other implementations too?
base = os.path.join(venvsdir, kind or 'dev')
major, minor = sys.version_info[:2]
pyloc = ((os.path.abspath(sys.executable)
).partition(os.path.sep)[2].lstrip(os.path.sep)
).replace(os.path.sep, '-')
return f'{base}-{major}.{minor}-{pyloc}'
def ensure_venv_ready(venvroot=None, kind='dev', venvsdir=VENVS):
if sys.prefix != sys.base_prefix:
assert os.path.exists(os.path.join(sys.prefix, 'pyvenv.cfg'))
venvroot = sys.prefix
python = sys.executable
readyfile = os.path.join(sys.prefix, 'READY')
isready = os.path.exists(readyfile)
else:
import venv
if not venvroot:
venvroot = resolve_venv_root(kind, venvsdir)
# Make sure the venv exists.
readyfile = os.path.join(venvroot, 'READY')
isready = os.path.exists(readyfile)
if not isready:
relroot = os.path.relpath(venvroot)
if not os.path.exists(venvroot):
print(f'creating venv at {relroot}...')
else:
print(f'venv {relroot} not ready, re-creating...')
venv.create(venvroot, with_pip=True, clear=True)
else:
assert os.path.exists(os.path.join(venvroot, 'pyvenv.cfg'))
# Return the venv's Python executable.
binname = 'Scripts' if os.name == 'nt' else 'bin'
exename = os.path.basename(sys.executable)
python = os.path.join(venvroot, binname, exename)
# Now make sure the venv has pyperformance installed.
if not isready:
import subprocess
relroot = os.path.relpath(venvroot)
print(f'venv {relroot} not ready, installing dependencies...')
proc = subprocess.run(
[python, '-m', 'pip', 'install',
'--upgrade',
'--editable', REPO_ROOT],
)
if proc.returncode != 0:
sys.exit('ERROR: install failed')
with open(readyfile, 'w'):
pass
print('...venv {relroot} ready!')
return venvroot, python
def main(venvroot=None):
_, python = ensure_venv_ready(venvroot)
if python != sys.executable:
# Now re-run using the venv.
os.execv(python, [python, *sys.argv])
# <unreachable>
# Now run pyperformance.
import pyperformance.cli
pyperformance.cli.main()
if __name__ == '__main__':
main()