Skip to content

Commit

Permalink
Merge branch 'main' into work/CRAFT-3373/security-scan
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau authored Sep 27, 2024
2 parents c3db980 + b6e370a commit 75074cb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
9 changes: 7 additions & 2 deletions craft_parts/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,16 @@ def _get_rewrite_shebangs_commands(self) -> list[str]:
shebangs in the final environment should be handled.
"""
script_interpreter = self._get_script_interpreter()
find_cmd = (
f'find "{self._part_info.part_install_dir}" -type f -executable -print0'
)
xargs_cmd = "xargs --no-run-if-empty -0"
sed_cmd = f'sed -i "1 s|^#\\!${{PARTS_PYTHON_VENV_INTERP_PATH}}.*$|{script_interpreter}|"'
return [
textwrap.dedent(
f"""\
find "{self._part_info.part_install_dir}" -type f -executable -print0 | xargs -0 \\
sed -i "1 s|^#\\!${{PARTS_PYTHON_VENV_INTERP_PATH}}.*$|{script_interpreter}|"
{find_cmd} | {xargs_cmd} \\
{sed_cmd}
"""
)
]
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ X.Y.Z (2024-MM-DD)

- Replace the dependency on requests-unixsocket with requests-unixsocket2

Bug Fixes:

- Fixed an issue where the ``python`` plugin would fail to build if the part
had no Python scripts.

2.1.1 (2024-09-13)
------------------

Expand Down
39 changes: 39 additions & 0 deletions tests/integration/plugins/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,42 @@ def test_find_payload_python_good_version(new_dir, partitions):
"""
)
assert expected_text in output


def test_no_shebangs(new_dir, partitions):
"""Test that building a Python part with no scripts works."""

class ScriptlessPlugin(craft_parts.plugins.plugins.PythonPlugin):
@override
def _get_package_install_commands(self) -> list[str]:
return [
*super()._get_package_install_commands(),
f"rm {self._part_info.part_install_dir}/bin/pip*",
f"rm {self._part_info.part_install_dir}/bin/mytest",
]

plugins.register({"python": ScriptlessPlugin})

source_location = Path(__file__).parent / "test_python"

parts_yaml = textwrap.dedent(
f"""\
parts:
foo:
plugin: python
source: {source_location}
python-packages: [] # to remove default wheel, setuptools, etc
"""
)
parts = yaml.safe_load(parts_yaml)

lf = LifecycleManager(
parts, application_name="test_python", cache_dir=new_dir, partitions=partitions
)
actions = lf.plan(Step.PRIME)

with lf.action_executor() as ctx:
ctx.execute(actions)

primed_script = lf.project_info.prime_dir / "bin/mytest"
assert not primed_script.exists()
4 changes: 2 additions & 2 deletions tests/unit/plugins/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_python_get_rewrite_shebangs_commands(new_dir, python_plugin: FooPythonP
assert python_plugin._get_rewrite_shebangs_commands() == [
textwrap.dedent(
f"""\
find "{new_dir}/parts/p1/install" -type f -executable -print0 | xargs -0 \\
find "{new_dir}/parts/p1/install" -type f -executable -print0 | xargs --no-run-if-empty -0 \\
sed -i "1 s|^#\\!${{PARTS_PYTHON_VENV_INTERP_PATH}}.*$|#!/usr/bin/env ${{PARTS_PYTHON_INTERPRETER}}|"
"""
)
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_python_get_build_commands(new_dir, python_plugin: FooPythonPlugin):
"echo 'This is where I put my install commands... if I had any!'",
textwrap.dedent(
f"""\
find "{new_dir}/parts/p1/install" -type f -executable -print0 | xargs -0 \\
find "{new_dir}/parts/p1/install" -type f -executable -print0 | xargs --no-run-if-empty -0 \\
sed -i "1 s|^#\\!${{PARTS_PYTHON_VENV_INTERP_PATH}}.*$|#!/usr/bin/env ${{PARTS_PYTHON_INTERPRETER}}|"
"""
),
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/test_poetry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def get_build_commands(
return [
dedent(
f"""\
find "{new_dir}/parts/p1/install" -type f -executable -print0 | xargs -0 \\
find "{new_dir}/parts/p1/install" -type f -executable -print0 | xargs --no-run-if-empty -0 \\
sed -i "1 s|^#\\!${{PARTS_PYTHON_VENV_INTERP_PATH}}.*$|#!/usr/bin/env ${{PARTS_PYTHON_INTERPRETER}}|"
"""
),
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/test_python_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_build_commands(
return [
dedent(
f"""\
find "{new_dir}/parts/p1/install" -type f -executable -print0 | xargs -0 \\
find "{new_dir}/parts/p1/install" -type f -executable -print0 | xargs --no-run-if-empty -0 \\
sed -i "1 s|^#\\!${{PARTS_PYTHON_VENV_INTERP_PATH}}.*$|#!/usr/bin/env ${{PARTS_PYTHON_INTERPRETER}}|"
"""
),
Expand Down

0 comments on commit 75074cb

Please sign in to comment.