Skip to content

Commit 65a7c63

Browse files
author
Chris Patterson
authored
plugins v2: quote python packages argument for pip (#3246)
If using a python package with conditionals, e.g.: python-packages: - ibm-db-sa; platform_machine == 'x86_64' - ibm-db-sa; platform_machine == 'ppc64le' - ibm-db-sa; platform_machine == 's390x' Then snapcraft would pass the python packages unquoted to pip, resulting in build errors. Ensure the packages are quoted safely using shlex.quote() when building the command string. Update existing python-package spread test and python-package unit test to cover this case. LP: #1884429 Signed-off-by: Chris Patterson <chris.patterson@canonical.com>
1 parent 409b8e8 commit 65a7c63

3 files changed

Lines changed: 7 additions & 4 deletions

File tree

snapcraft/plugins/v2/python.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
inclusion of the python interpreter.
6464
"""
6565

66+
import shlex
6667
from textwrap import dedent
6768
from typing import Any, Dict, List, Set
6869

@@ -125,7 +126,9 @@ def get_build_commands(self) -> List[str]:
125126
constraints = ""
126127

127128
if self.options.python_packages:
128-
python_packages = " ".join(self.options.python_packages)
129+
python_packages = " ".join(
130+
[shlex.quote(pkg) for pkg in self.options.python_packages]
131+
)
129132
python_packages_cmd = f"pip install {constraints} -U {python_packages}"
130133
build_commands.append(python_packages_cmd)
131134

tests/spread/plugins/v2/snaps/python-hello-with-python-package-dep/snap/snapcraft.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ parts:
1818
plugin: python
1919
python-packages:
2020
- pip==20.0.2
21-
- appdirs
21+
- appdirs; sys_platform == 'linux'

tests/unit/plugins/v2/test_python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def test_get_build_commands_with_all_properties():
120120
class Options:
121121
constraints = ["constraints.txt"]
122122
requirements = ["requirements.txt"]
123-
python_packages = ["pip"]
123+
python_packages = ["pip", "some-pkg; sys_platform != 'win32'"]
124124

125125
plugin = PythonPlugin(part_name="my-part", options=Options())
126126

@@ -129,7 +129,7 @@ class Options:
129129
== [
130130
'"${SNAPCRAFT_PYTHON_INTERPRETER}" -m venv ${SNAPCRAFT_PYTHON_VENV_ARGS} '
131131
'"${SNAPCRAFT_PART_INSTALL}"',
132-
"pip install -c 'constraints.txt' -U pip",
132+
"pip install -c 'constraints.txt' -U pip 'some-pkg; sys_platform != '\"'\"'win32'\"'\"''",
133133
"pip install -c 'constraints.txt' -U -r 'requirements.txt'",
134134
"[ -f setup.py ] && pip install -c 'constraints.txt' -U .",
135135
]

0 commit comments

Comments
 (0)