Skip to content

Commit

Permalink
Merge pull request #21 from wayfair-incubator/cantonellis_implement_p…
Browse files Browse the repository at this point in the history
…ython_version

implemented ability to select python version
  • Loading branch information
chrisantonellis authored Feb 18, 2021
2 parents 5731590 + 664829c commit 3e5c1e4
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 8 deletions.
8 changes: 7 additions & 1 deletion little_cheesemonger/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from little_cheesemonger._constants import ( # noqa
PYTHON_BINARIES,
Architecture,
Platform,
PythonVersion,
)
from little_cheesemonger._errors import LittleCheesemongerError # noqa
from little_cheesemonger._run import run # noqa

__version__ = "0.1.0rc1"
__version__ = "0.1.0rc2"
1 change: 1 addition & 0 deletions little_cheesemonger/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ class PythonVersion(str, Enum):
"environment_variables": None,
"system_dependencies": None,
"python_dependencies": None,
"python_versions": None,
"steps": None,
}
26 changes: 23 additions & 3 deletions little_cheesemonger/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

from little_cheesemonger._constants import PythonVersion
from little_cheesemonger._errors import LittleCheesemongerError
from little_cheesemonger._loader import load_configuration
from little_cheesemonger._platform import get_python_binaries
Expand Down Expand Up @@ -35,7 +36,9 @@ def run(
install_system_dependencies(configuration["system_dependencies"])

if configuration["python_dependencies"] is not None:
install_python_dependencies(configuration["python_dependencies"])
install_python_dependencies(
configuration["python_dependencies"], configuration["python_versions"]
)

if configuration["steps"] is not None:
execute_steps(configuration["steps"])
Expand Down Expand Up @@ -66,10 +69,27 @@ def install_system_dependencies(dependencies: List[str]) -> None:
run_subprocess(["yum", "install", "-y"] + dependencies)


def install_python_dependencies(dependencies: List[str]) -> None:
def install_python_dependencies(
dependencies: List[str], python_versions: Optional[List[str]] = None
) -> None:
"""Install Python dependencies."""

for binaries in get_python_binaries().values():
all_binaries = get_python_binaries()

if python_versions is None:
binaries_paths = list(
all_binaries.values()
) # NOTE: cast to list for mypy, fix this
else:
try:
version_keys = [PythonVersion[version] for version in python_versions]
binaries_paths = [all_binaries[version] for version in version_keys]
except KeyError as e:
raise LittleCheesemongerError(
f"A Python version from specified versions {python_versions} is not installed on this image: {e}"
)

for binaries in binaries_paths:
run_subprocess([str(binaries / "pip"), "install"] + dependencies)


Expand Down
1 change: 1 addition & 0 deletions little_cheesemonger/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class ConfigurationType(TypedDict):
environment_variables: Optional[List[str]]
system_dependencies: Optional[List[str]]
python_dependencies: Optional[List[str]]
python_versions: Optional[List[str]]
steps: Optional[List[str]]
8 changes: 7 additions & 1 deletion tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class Platform(str, Enum):
platform = PLATFORM


class PythonVersion(str, Enum):
foo38 = PYTHON_VERSION


PYTHON_BINARIES = {
Architecture.architecture: {
Platform.platform: {PYTHON_VERSION: PYTHON_BINARIES_PATH}
Platform.platform: {PythonVersion.foo38: PYTHON_BINARIES_PATH}
}
}

Expand All @@ -29,10 +33,12 @@ class Platform(str, Enum):
ENVIRONMENT_VARIABLES = ["foo=bar"]
SYSTEM_DEPENDENCIES = ["foo-1.0.0"]
PYTHON_DEPENDENCIES = ["foo==1.0.0"]
PYTHON_VERSIONS = [PYTHON_VERSION]
STEPS = ["foo"]
CONFIGURATION = {
"environment_variables": ENVIRONMENT_VARIABLES,
"system_dependencies": SYSTEM_DEPENDENCIES,
"python_dependencies": PYTHON_DEPENDENCIES,
"python_versions": PYTHON_VERSIONS,
"steps": STEPS,
}
18 changes: 18 additions & 0 deletions tests/integration/assets/example_package/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ environment_variables = [
system_dependencies = [
"atlas"
]
python_versions = [
"cp36-cp36m",
"cp36-cp37m",
"cp36-cp38",
"cp36-cp39"
]
python_dependencies = [
"nyancat==0.1.2"
]
Expand All @@ -19,6 +25,12 @@ environment_variables = [
system_dependencies = [
"atlas"
]
python_versions = [
"cp36-cp36m",
"cp36-cp37m",
"cp36-cp38",
"cp36-cp39"
]
python_dependencies = [
"nyancat==0.1.2"
]
Expand All @@ -33,6 +45,12 @@ environment_variables = [
system_dependencies = [
"atlas"
]
python_versions = [
"cp36-cp36m",
"cp36-cp37m",
"cp36-cp38",
"cp36-cp39"
]
python_dependencies = [
"nyancat==0.1.2"
]
Expand Down
40 changes: 37 additions & 3 deletions tests/unit/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
PYTHON_BINARIES,
PYTHON_DEPENDENCIES,
PYTHON_VERSION,
PYTHON_VERSIONS,
STEPS,
SYSTEM_DEPENDENCIES,
)
from tests.constants import PythonVersion as PythonVersionTesting

DEBUG = False

Expand Down Expand Up @@ -72,6 +74,11 @@ def get_python_binaries(mocker):
)


@pytest.fixture(autouse=True)
def python_versions_enum(mocker):
return mocker.patch("little_cheesemonger._run.PythonVersion", PythonVersionTesting)


@pytest.fixture
def os_mock(mocker):
return mocker.patch("little_cheesemonger._run.os")
Expand Down Expand Up @@ -178,7 +185,9 @@ def test_run__python_dependencies_set_in_configuration__install_python_dependenc

run(DIRECTORY, LOADER_IMPORT_PATH, LOADER_ARGS, LOADER_KWARGS, DEBUG)

install_python_dependencies_mock.assert_called_once_with(PYTHON_DEPENDENCIES)
install_python_dependencies_mock.assert_called_once_with(
PYTHON_DEPENDENCIES, PYTHON_VERSIONS
)


def test_run__python_dependencies_not_set_in_configuration__install_python_dependencies_not_called(
Expand Down Expand Up @@ -254,11 +263,36 @@ def test_install_system_dependencies__run_subprocess_called_with_command(
)


def test_install_python_dependencies__run_subprocess_called_with_command(
def test_install_python_dependencies__python_versions_set__run_subprocess_called_with_command(
run_subprocess_mock, get_python_binaries
):

install_python_dependencies(PYTHON_DEPENDENCIES, PYTHON_VERSIONS)

run_subprocess_mock.assert_called_once_with(
[
str(PYTHON_BINARIES[ARCHITECTURE][PLATFORM][PYTHON_VERSION] / "pip"),
"install",
]
+ PYTHON_DEPENDENCIES
)


def test_install_python_dependencies__python_versions_invalid__raise_LittleCheesemongerError(
run_subprocess_mock, get_python_binaries
):

with pytest.raises(
LittleCheesemongerError, match=r"A Python version from specified versions .*"
):
install_python_dependencies(PYTHON_DEPENDENCIES, ["invalid"])


def test_install_python_dependencies__python_versions_not_set__run_subprocess_called_with_command(
run_subprocess_mock, get_python_binaries
):

install_python_dependencies(PYTHON_DEPENDENCIES)
install_python_dependencies(PYTHON_DEPENDENCIES, None)

run_subprocess_mock.assert_called_once_with(
[
Expand Down

0 comments on commit 3e5c1e4

Please sign in to comment.