-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Snapcraft-specific Poetry plugin
The plugin has the same behavior and restrictions as the Python plugin with regards to base-dependent behavior, symlink handling, etc. Therefore, this common behavior is extracted into a new "python_common" module, used by both plugins. This approach is also taken for the reference docs - the requirement of staging a Python interpreter (or not) is the same for both plugins. Enabled for core22 and core24. Fixes #5025
- Loading branch information
Showing
19 changed files
with
232 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
Dependencies | ||
------------ | ||
|
||
Whether the Python interpreter needs to be included in the snap depends on its | ||
``confinement``. Specifically: | ||
|
||
- Projects with ``strict`` or ``devmode`` confinement can safely use the base | ||
snap's interpreter, so they typically do **not** need to include Python. | ||
- Projects with ``classic`` confinement **cannot** use the base snap's | ||
interpreter and thus must always bundle it (typically via ``stage-packages``). | ||
- In both cases, a specific/custom Python installation can always be included | ||
in the snap. This can be useful, for example, when using a different Python | ||
version or building an interpreter with custom flags. | ||
|
||
Snapcraft will prefer an included interpreter over the base's, even for projects | ||
with ``strict`` and ``devmode`` confinement. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.. include:: /common/craft-parts/reference/plugins/poetry_plugin.rst | ||
:end-before: .. _poetry-details-begin: | ||
|
||
.. include:: _python_common.rst | ||
|
||
.. include:: /common/craft-parts/reference/plugins/poetry_plugin.rst | ||
:start-after: .. _poetry-details-end: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""The Snapcraft Poetry plugin.""" | ||
|
||
from craft_parts.plugins import poetry_plugin | ||
from overrides import override | ||
|
||
from snapcraft.parts.plugins import python_common | ||
|
||
|
||
class PoetryPlugin(poetry_plugin.PoetryPlugin): | ||
"""A Poetry plugin for Snapcraft.""" | ||
|
||
@override | ||
def _get_system_python_interpreter(self) -> str | None: | ||
return python_common.get_system_interpreter(self._part_info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright 2023-2024 Canonical Ltd. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Common functionality for Python-based plugins. | ||
This plugin extends Craft-parts' vanilla Python plugin to properly | ||
set the Python interpreter according to the Snapcraft base and | ||
confinement parameters. | ||
""" | ||
|
||
import logging | ||
from pathlib import Path | ||
|
||
from craft_parts import PartInfo, StepInfo, errors | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
_CONFINED_PYTHON_PATH = { | ||
"core22": "/usr/bin/python3.10", | ||
"core24": "/usr/bin/python3.12", | ||
} | ||
|
||
|
||
def get_system_interpreter(part_info: PartInfo) -> str | None: | ||
"""Obtain the path to the system-provided python interpreter. | ||
:param part_info: The info of the part that is being built. | ||
""" | ||
base = part_info.project_base | ||
confinement = part_info.confinement | ||
|
||
if confinement == "classic" or base == "bare": | ||
# classic snaps, and snaps without bases, must always provision Python | ||
interpreter = None | ||
else: | ||
# otherwise, we should always know which Python is present on the | ||
# base. If this fails on a new base, update _CONFINED_PYTHON_PATH | ||
interpreter = _CONFINED_PYTHON_PATH.get(base) | ||
if interpreter is None: | ||
brief = f"Don't know which interpreter to use for base {base}." | ||
resolution = "Please contact the Snapcraft team." | ||
raise errors.PartsError(brief=brief, resolution=resolution) | ||
|
||
logger.debug( | ||
"Using python interpreter '%s' for base '%s', confinement '%s'", | ||
interpreter, | ||
base, | ||
confinement, | ||
) | ||
return interpreter | ||
|
||
|
||
def post_prime(step_info: StepInfo) -> None: | ||
"""Perform Python-specific actions right before packing.""" | ||
base = step_info.project_base | ||
|
||
if base in ("core20", "core22"): | ||
# Only fix pyvenv.cfg on core24+ snaps | ||
return | ||
|
||
root_path: Path = step_info.prime_dir | ||
|
||
pyvenv = root_path / "pyvenv.cfg" | ||
if not pyvenv.is_file(): | ||
return | ||
|
||
snap_path = Path(f"/snap/{step_info.project_name}/current") | ||
new_home = f"home = {snap_path}" | ||
|
||
candidates = ( | ||
step_info.part_install_dir, | ||
step_info.stage_dir, | ||
) | ||
|
||
old_contents = contents = pyvenv.read_text() | ||
for candidate in candidates: | ||
old_home = f"home = {candidate}" | ||
contents = contents.replace(old_home, new_home) | ||
|
||
if old_contents != contents: | ||
logger.debug("Updating pyvenv.cfg to:\n%s", contents) | ||
pyvenv.write_text(contents) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/spread/core24/python-hello/poetry/snap/snapcraft.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: python-hello-poetry | ||
version: "1.0" | ||
summary: simple python application | ||
description: build a python application using core24 | ||
base: core24 | ||
confinement: strict | ||
|
||
apps: | ||
python-hello-poetry: | ||
command: bin/hello | ||
parts: | ||
hello: | ||
plugin: poetry | ||
source: src |
18 changes: 18 additions & 0 deletions
18
tests/spread/core24/python-hello/poetry/src/pyproject.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[tool.poetry] | ||
name = "hello" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Your Name <[email protected]>"] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.10" | ||
black = "^24.8.0" | ||
|
||
[tool.poetry.dev-dependencies] | ||
|
||
[build-system] | ||
requires = ["poetry-core>=1.0.0"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry.scripts] | ||
hello = "hello:main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/spread/plugins/craft-parts/build-and-run-hello/poetry-hello/snap/snapcraft.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: poetry-hello | ||
version: "1.0" | ||
summary: simple python application | ||
description: build a python application using core22 | ||
base: core22 | ||
confinement: strict | ||
|
||
apps: | ||
poetry-hello: | ||
command: bin/hello | ||
|
||
parts: | ||
hello: | ||
plugin: poetry | ||
source: src |
2 changes: 2 additions & 0 deletions
2
tests/spread/plugins/craft-parts/build-and-run-hello/poetry-hello/src/hello/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def main(): | ||
print("hello world") |
Oops, something went wrong.