Skip to content

Commit c2b4a28

Browse files
Fix several failures. (#163)
A few bugs have crept in with some of my recent changes. Part of the problem is that there hasn't been great test coverage for a bunch of pyperformance code. In this PR I've added a bunch of testing. I've also fixed a number of problems.
1 parent 24d9012 commit c2b4a28

20 files changed

+995
-612
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ pyperformance.egg-info/
1616
venv/
1717
.venvs/
1818

19+
# Create during tests
20+
pyperformance/tests/data/cpython/
21+
1922
# Created by the tox program
2023
.tox/

dev.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@
88
VENVS = os.path.join(REPO_ROOT, '.venvs')
99

1010

11-
def ensure_venv_ready(venvroot=None, kind='dev'):
11+
def resolve_venv_root(kind='dev', venvsdir=VENVS):
12+
import sysconfig
13+
if sysconfig.is_python_build():
14+
sys.exit('please install your built Python first (or pass it using --python)')
15+
# XXX Handle other implementations too?
16+
base = os.path.join(venvsdir, kind or 'dev')
17+
major, minor = sys.version_info[:2]
18+
pyloc = ((os.path.abspath(sys.executable)
19+
).partition(os.path.sep)[2].lstrip(os.path.sep)
20+
).replace(os.path.sep, '-')
21+
return f'{base}-{major}.{minor}-{pyloc}'
22+
23+
24+
def ensure_venv_ready(venvroot=None, kind='dev', venvsdir=VENVS):
1225
if sys.prefix != sys.base_prefix:
1326
assert os.path.exists(os.path.join(sys.prefix, 'pyvenv.cfg'))
1427
venvroot = sys.prefix
@@ -18,16 +31,7 @@ def ensure_venv_ready(venvroot=None, kind='dev'):
1831
else:
1932
import venv
2033
if not venvroot:
21-
import sysconfig
22-
if sysconfig.is_python_build():
23-
sys.exit('please install your built Python first (or pass it using --python)')
24-
# XXX Handle other implementations too?
25-
base = os.path.join(VENVS, kind or 'dev')
26-
major, minor = sys.version_info[:2]
27-
pyloc = ((os.path.abspath(sys.executable)
28-
).partition(os.path.sep)[2].lstrip(os.path.sep)
29-
).replace(os.path.sep, '-')
30-
venvroot = f'{base}-{major}.{minor}-{pyloc}'
34+
venvroot = resolve_venv_root(kind, venvsdir)
3135
# Make sure the venv exists.
3236
readyfile = os.path.join(venvroot, 'READY')
3337
isready = os.path.exists(readyfile)
@@ -61,11 +65,11 @@ def ensure_venv_ready(venvroot=None, kind='dev'):
6165
pass
6266
print('...venv {relroot} ready!')
6367

64-
return python
68+
return venvroot, python
6569

6670

6771
def main(venvroot=None):
68-
python = ensure_venv_ready(venvroot)
72+
_, python = ensure_venv_ready(venvroot)
6973
if python != sys.executable:
7074
# Now re-run using the venv.
7175
os.execv(python, [python, *sys.argv])

pyperformance/_venv.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,13 @@ class VirtualEnvironment:
125125

126126
@classmethod
127127
def create(cls, root=None, python=sys.executable, **kwargs):
128-
if not python or isinstance(python, str):
129-
info = _pythoninfo.get_info(python)
128+
if not python:
129+
python = sys.executable
130+
if isinstance(python, str):
131+
try:
132+
info = _pythoninfo.get_info(python)
133+
except FileNotFoundError:
134+
info = None
130135
else:
131136
info = python
132137
if not root:
@@ -139,12 +144,14 @@ def create(cls, root=None, python=sys.executable, **kwargs):
139144
try:
140145
venv_python = create_venv(
141146
root,
142-
info,
147+
info or python,
143148
**kwargs
144149
)
145150
except BaseException:
146151
_utils.safe_rmtree(root)
147152
raise # re-raise
153+
if not info:
154+
info = _pythoninfo.get_info(python)
148155
self = cls(root, base=info)
149156
self._python = venv_python
150157
return self

pyperformance/cli.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44
import sys
55

66
from pyperformance import _utils, is_installed, is_dev
7-
from pyperformance.venv import cmd_venv
7+
from pyperformance.commands import (
8+
cmd_list,
9+
cmd_list_groups,
10+
cmd_venv_create,
11+
cmd_venv_recreate,
12+
cmd_venv_remove,
13+
cmd_venv_show,
14+
cmd_run,
15+
cmd_compile,
16+
cmd_compile_all,
17+
cmd_upload,
18+
cmd_show,
19+
cmd_compare,
20+
)
821

922

1023
def comma_separated(values):
@@ -243,41 +256,51 @@ def _main():
243256
parser, options = parse_args()
244257

245258
if options.action == 'venv':
246-
if options.venv_action in ('create', 'recreate'):
259+
from . import _pythoninfo, _venv
260+
261+
if not options.venv:
262+
info = _pythoninfo.get_info(options.python)
263+
root = _venv.get_venv_root(python=info)
264+
else:
265+
root = options.venv
266+
info = None
267+
268+
action = options.venv_action
269+
if action == 'create':
247270
benchmarks = _benchmarks_from_options(options)
271+
cmd_venv_create(options, root, info, benchmarks)
272+
elif action == 'recreate':
273+
benchmarks = _benchmarks_from_options(options)
274+
cmd_venv_recreate(options, root, info, benchmarks)
275+
elif action == 'remove':
276+
cmd_venv_remove(options, root)
277+
elif action == 'show':
278+
cmd_venv_show(options, root)
248279
else:
249-
benchmarks = None
250-
cmd_venv(options, benchmarks)
251-
sys.exit()
280+
print(f'ERROR: unsupported venv command action {action!r}')
281+
parser.print_help()
282+
sys.exit(1)
252283
elif options.action == 'compile':
253-
from pyperformance.compile import cmd_compile
254284
cmd_compile(options)
255285
sys.exit()
256286
elif options.action == 'compile_all':
257-
from pyperformance.compile import cmd_compile_all
258287
cmd_compile_all(options)
259288
sys.exit()
260289
elif options.action == 'upload':
261-
from pyperformance.compile import cmd_upload
262290
cmd_upload(options)
263291
sys.exit()
264292
elif options.action == 'show':
265-
from pyperformance.compare import cmd_show
266293
cmd_show(options)
267294
sys.exit()
268295
elif options.action == 'run':
269-
from pyperformance.cli_run import cmd_run
270296
benchmarks = _benchmarks_from_options(options)
271297
cmd_run(options, benchmarks)
272298
elif options.action == 'compare':
273-
from pyperformance.compare import cmd_compare
274299
cmd_compare(options)
275300
elif options.action == 'list':
276-
from pyperformance.cli_run import cmd_list
277301
benchmarks = _benchmarks_from_options(options)
278302
cmd_list(options, benchmarks)
279303
elif options.action == 'list_groups':
280-
from pyperformance.cli_run import cmd_list_groups
281304
manifest = _manifest_from_options(options)
282305
cmd_list_groups(manifest)
283306
else:

pyperformance/cli_run.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)