|
| 1 | +# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- |
| 2 | +# |
| 3 | +# Copyright 2024 Canonical Ltd. |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU Lesser General Public |
| 7 | +# License version 3 as published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +# Lesser General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Lesser General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +from pathlib import Path |
| 17 | +from textwrap import dedent |
| 18 | +from typing import Literal |
| 19 | + |
| 20 | +import pytest |
| 21 | +from craft_parts import Part, PartInfo, ProjectInfo |
| 22 | +from craft_parts.plugins.base import BasePythonPlugin, PluginProperties |
| 23 | +from overrides import override |
| 24 | + |
| 25 | + |
| 26 | +class FakePythonPluginProperties(PluginProperties, frozen=True): |
| 27 | + plugin: Literal["fakepy"] = "fakepy" |
| 28 | + source: str # pyright: ignore[reportGeneralTypeIssues] |
| 29 | + |
| 30 | + |
| 31 | +class FakePythonPlugin(BasePythonPlugin): |
| 32 | + """A really awesome Python plugin""" |
| 33 | + |
| 34 | + properties_class = FakePythonPluginProperties |
| 35 | + _options: FakePythonPluginProperties |
| 36 | + |
| 37 | + @override |
| 38 | + def _get_package_install_commands(self) -> list[str]: |
| 39 | + return ['"${PARTS_PYTHON_INTERPRETER}" -m fake_pip --install'] |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def plugin(new_dir): |
| 44 | + properties = FakePythonPlugin.properties_class.unmarshal({"source": "."}) |
| 45 | + info = ProjectInfo(application_name="test", cache_dir=new_dir) |
| 46 | + part_info = PartInfo(project_info=info, part=Part("p1", {})) |
| 47 | + |
| 48 | + return FakePythonPlugin(properties=properties, part_info=part_info) |
| 49 | + |
| 50 | + |
| 51 | +def get_python_build_commands( |
| 52 | + new_dir: Path, *, should_remove_symlinks: bool = False |
| 53 | +) -> list[str]: |
| 54 | + if should_remove_symlinks: |
| 55 | + postfix = [ |
| 56 | + f"echo Removing python symlinks in {new_dir}/parts/p1/install/bin", |
| 57 | + f'rm "{new_dir}/parts/p1/install"/bin/python*', |
| 58 | + ] |
| 59 | + else: |
| 60 | + postfix = ['ln -sf "${symlink_target}" "${PARTS_PYTHON_VENV_INTERP_PATH}"'] |
| 61 | + |
| 62 | + return [ |
| 63 | + dedent( |
| 64 | + f"""\ |
| 65 | + # look for a provisioned python interpreter |
| 66 | + opts_state="$(set +o|grep errexit)" |
| 67 | + set +e |
| 68 | + install_dir="{new_dir}/parts/p1/install/usr/bin" |
| 69 | + stage_dir="{new_dir}/stage/usr/bin" |
| 70 | +
|
| 71 | + # look for the right Python version - if the venv was created with python3.10, |
| 72 | + # look for python3.10 |
| 73 | + basename=$(basename $(readlink -f ${{PARTS_PYTHON_VENV_INTERP_PATH}})) |
| 74 | + echo Looking for a Python interpreter called \\"${{basename}}\\" in the payload... |
| 75 | + payload_python=$(find "$install_dir" "$stage_dir" -type f -executable -name "${{basename}}" -print -quit 2>/dev/null) |
| 76 | +
|
| 77 | + if [ -n "$payload_python" ]; then |
| 78 | + # We found a provisioned interpreter, use it. |
| 79 | + echo Found interpreter in payload: \\"${{payload_python}}\\" |
| 80 | + installed_python="${{payload_python##{new_dir}/parts/p1/install}}" |
| 81 | + if [ "$installed_python" = "$payload_python" ]; then |
| 82 | + # Found a staged interpreter. |
| 83 | + symlink_target="..${{payload_python##{new_dir}/stage}}" |
| 84 | + else |
| 85 | + # The interpreter was installed but not staged yet. |
| 86 | + symlink_target="..$installed_python" |
| 87 | + fi |
| 88 | + else |
| 89 | + # Otherwise use what _get_system_python_interpreter() told us. |
| 90 | + echo "Python interpreter not found in payload." |
| 91 | + symlink_target="$(readlink -f "$(which "${{PARTS_PYTHON_INTERPRETER}}")")" |
| 92 | + fi |
| 93 | +
|
| 94 | + if [ -z "$symlink_target" ]; then |
| 95 | + echo "No suitable Python interpreter found, giving up." |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +
|
| 99 | + eval "${{opts_state}}" |
| 100 | + """ |
| 101 | + ), |
| 102 | + *postfix, |
| 103 | + ] |
| 104 | + |
| 105 | + |
| 106 | +def get_python_shebang_rewrite_commands( |
| 107 | + expected_shebang: str, install_dir: str |
| 108 | +) -> list[str]: |
| 109 | + find_cmd = f'find "{install_dir}" -type f -executable -print0' |
| 110 | + xargs_cmd = "xargs --no-run-if-empty -0" |
| 111 | + sed_cmd = ( |
| 112 | + f'sed -i "1 s|^#\\!${{PARTS_PYTHON_VENV_INTERP_PATH}}.*$|{expected_shebang}|"' |
| 113 | + ) |
| 114 | + return [ |
| 115 | + dedent( |
| 116 | + f"""\ |
| 117 | + {find_cmd} | {xargs_cmd} \\ |
| 118 | + {sed_cmd} |
| 119 | + """ |
| 120 | + ) |
| 121 | + ] |
| 122 | + |
| 123 | + |
| 124 | +def test_get_build_packages(plugin) -> None: |
| 125 | + assert plugin.get_build_packages() == {"findutils", "python3-venv", "python3-dev"} |
| 126 | + |
| 127 | + |
| 128 | +def test_get_build_environment(plugin, new_dir) -> None: |
| 129 | + assert plugin.get_build_environment() == { |
| 130 | + "PATH": f"{new_dir}/parts/p1/install/bin:${{PATH}}", |
| 131 | + "PARTS_PYTHON_INTERPRETER": "python3", |
| 132 | + "PARTS_PYTHON_VENV_ARGS": "", |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | +def test_get_build_commands(plugin, new_dir) -> None: |
| 137 | + venv_path = new_dir / "parts" / "p1" / "install" |
| 138 | + assert plugin.get_build_commands() == [ |
| 139 | + f'"${{PARTS_PYTHON_INTERPRETER}}" -m venv ${{PARTS_PYTHON_VENV_ARGS}} "{venv_path}"', |
| 140 | + f'PARTS_PYTHON_VENV_INTERP_PATH="{venv_path}/bin/${{PARTS_PYTHON_INTERPRETER}}"', |
| 141 | + '"${PARTS_PYTHON_INTERPRETER}" -m fake_pip --install', |
| 142 | + *get_python_shebang_rewrite_commands( |
| 143 | + "#!/usr/bin/env ${PARTS_PYTHON_INTERPRETER}", |
| 144 | + str(plugin._part_info.part_install_dir), |
| 145 | + ), |
| 146 | + *get_python_build_commands(new_dir, should_remove_symlinks=False), |
| 147 | + ] |
| 148 | + |
| 149 | + |
| 150 | +def test_call_should_remove_symlinks(plugin, new_dir, monkeypatch): |
| 151 | + monkeypatch.setattr(plugin, "_should_remove_symlinks", lambda: True) |
| 152 | + |
| 153 | + venv_path = new_dir / "parts" / "p1" / "install" |
| 154 | + assert plugin.get_build_commands() == [ |
| 155 | + f'"${{PARTS_PYTHON_INTERPRETER}}" -m venv ${{PARTS_PYTHON_VENV_ARGS}} "{venv_path}"', |
| 156 | + f'PARTS_PYTHON_VENV_INTERP_PATH="{venv_path}/bin/${{PARTS_PYTHON_INTERPRETER}}"', |
| 157 | + '"${PARTS_PYTHON_INTERPRETER}" -m fake_pip --install', |
| 158 | + *get_python_shebang_rewrite_commands( |
| 159 | + "#!/usr/bin/env ${PARTS_PYTHON_INTERPRETER}", |
| 160 | + str(plugin._part_info.part_install_dir), |
| 161 | + ), |
| 162 | + *get_python_build_commands(new_dir, should_remove_symlinks=True), |
| 163 | + ] |
| 164 | + |
| 165 | + |
| 166 | +def test_script_interpreter(plugin): |
| 167 | + assert plugin._get_script_interpreter() == ( |
| 168 | + "#!/usr/bin/env ${PARTS_PYTHON_INTERPRETER}" |
| 169 | + ) |
| 170 | + |
| 171 | + |
| 172 | +def test_get_system_python_interpreter(plugin): |
| 173 | + assert plugin._get_system_python_interpreter() == ( |
| 174 | + '$(readlink -f "$(which "${PARTS_PYTHON_INTERPRETER}")")' |
| 175 | + ) |
0 commit comments