Skip to content

Commit bacd2a5

Browse files
authored
enable installation for other python versions (#9)
* enable installation for other python versions * fix other basepython discovery
1 parent 219d82f commit bacd2a5

File tree

2 files changed

+78
-22
lines changed

2 files changed

+78
-22
lines changed

tests/test_plugin.py

+53-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import shutil
2+
13
import pytest
24

35
from light_the_torch.computation_backend import CPUBackend
@@ -8,10 +10,9 @@ def patch_extract_dists(mocker):
810
def patch_extract_dists_(return_value=None):
911
if return_value is None:
1012
return_value = []
11-
return mocker.patch(
12-
"tox_ltt.plugin.ltt.extract_dists", return_value=return_value
13-
)
14-
return mocker.patch()
13+
return mocker.patch(
14+
"tox_ltt.plugin.ltt.extract_dists", return_value=return_value
15+
)
1516

1617
return patch_extract_dists_
1718

@@ -21,10 +22,7 @@ def patch_find_links(mocker):
2122
def patch_find_links_(return_value=None):
2223
if return_value is None:
2324
return_value = []
24-
return mocker.patch(
25-
"tox_ltt.plugin.ltt.find_links", return_value=return_value
26-
)
27-
return mocker.patch()
25+
return mocker.patch("tox_ltt.plugin.ltt.find_links", return_value=return_value)
2826

2927
return patch_find_links_
3028

@@ -65,6 +63,7 @@ def get_setup_cfg(name, version, install_requires=None, extra_requires=None):
6563

6664

6765
def get_tox_ini(
66+
basepython=None,
6867
disable_light_the_torch=None,
6968
force_cpu=None,
7069
deps=None,
@@ -80,6 +79,8 @@ def get_tox_ini(
8079

8180
lines.extend(("[testenv]", "requires = ", "\ttox-ltt",))
8281

82+
if basepython is not None:
83+
lines.append(f"basepython = {basepython}")
8384
if skip_install:
8485
lines.append("skip_install = True")
8586
if extra:
@@ -100,6 +101,7 @@ def tox_ltt_initproj(initproj):
100101
def tox_ltt_initproj_(
101102
name="foo",
102103
version="1.2.3",
104+
basepython=None,
103105
install_requires=None,
104106
extra_requires=None,
105107
disable_light_the_torch=None,
@@ -116,6 +118,7 @@ def tox_ltt_initproj_(
116118
extra_requires=extra_requires,
117119
),
118120
"tox.ini": get_tox_ini(
121+
basepython=basepython,
119122
skip_install=skip_install,
120123
extra=extra_requires is not None,
121124
disable_light_the_torch=disable_light_the_torch,
@@ -269,3 +272,45 @@ def test_tox_ltt_project_extra_pytorch_dists(
269272

270273
args, _ = mock.call_args
271274
assert set(args[0]) == dists
275+
276+
277+
@pytest.fixture
278+
def other_basepythons(current_tox_py):
279+
current_minor = int(current_tox_py[-1])
280+
basepythons = (f"python3.{minor}" for minor in {6, 7, 8} - {current_minor})
281+
return [
282+
basepython for basepython in basepythons if shutil.which(basepython) is not None
283+
]
284+
285+
286+
@pytest.mark.slow
287+
def test_tox_ltt_other_basepython(
288+
subtests,
289+
mock_venv,
290+
patch_extract_dists,
291+
patch_find_links,
292+
install_mock,
293+
tox_ltt_initproj,
294+
cmd,
295+
other_basepythons,
296+
):
297+
def canonical_to_tox(version):
298+
major, minor, _ = version.split(".")
299+
return f"python{major}.{minor}"
300+
301+
deps = ["torch"]
302+
patch_extract_dists(return_value=deps)
303+
mock = patch_find_links()
304+
305+
for basepython in other_basepythons:
306+
mock.reset()
307+
308+
with subtests.test(basepython=basepython):
309+
tox_ltt_initproj(basepython=basepython, deps=deps)
310+
311+
result = cmd()
312+
result.assert_success()
313+
314+
_, kwargs = mock.call_args
315+
python_version = kwargs["python_version"]
316+
assert canonical_to_tox(python_version) == basepython

tox_ltt/plugin.py

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import subprocess
12
from typing import Any, List, Optional, Sequence, cast
23

34
import tox
45
from tox import reporter
56
from tox.action import Action
6-
from tox.config import Parser
7+
from tox.config import Parser, TestenvConfig
78
from tox.venv import VirtualEnv
89

910
import light_the_torch as ltt
@@ -83,19 +84,11 @@ def tox_testenv_install_deps(venv: VirtualEnv, action: Action) -> None:
8384
)
8485
return None
8586

86-
computation_backend: Optional[CPUBackend]
87-
if envconfig.force_cpu:
88-
reporter.verbosity1(
89-
(
90-
"Using CPU as computation backend instead of auto-detecting since "
91-
"'force_cpu = True' is configured."
92-
),
93-
)
94-
computation_backend = CPUBackend()
95-
else:
96-
computation_backend = None
97-
98-
links = ltt.find_links(dists, computation_backend=computation_backend)
87+
links = ltt.find_links(
88+
dists,
89+
computation_backend=get_computation_backend(envconfig),
90+
python_version=get_python_version(envconfig),
91+
)
9992

10093
action.setactivity("installdeps-light-the-torch", ", ".join(links))
10194
venv.run_install_command(links, action)
@@ -104,3 +97,21 @@ def tox_testenv_install_deps(venv: VirtualEnv, action: Action) -> None:
10497
# TODO: this should probably implemented in light-the-torch
10598
def remove_extras(dists: List[str]) -> List[str]:
10699
return [dist.split(";")[0] for dist in dists]
100+
101+
102+
def get_computation_backend(envconfig: TestenvConfig) -> Optional[CPUBackend]:
103+
if not envconfig.force_cpu:
104+
return None
105+
106+
reporter.verbosity1(
107+
(
108+
"Using CPU as computation backend instead of auto-detecting since "
109+
"'force_cpu = True' is configured."
110+
),
111+
)
112+
return CPUBackend()
113+
114+
115+
def get_python_version(envconfig: TestenvConfig) -> str:
116+
output = subprocess.check_output((envconfig.basepython, "--version"))
117+
return output.decode("utf-8").strip()[7:]

0 commit comments

Comments
 (0)