-
Notifications
You must be signed in to change notification settings - Fork 448
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
11 changed files
with
127 additions
and
6 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
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,55 @@ | ||
# -*- 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 functions for Python-based Snapcraft plugins.""" | ||
import logging | ||
from pathlib import Path | ||
|
||
from craft_parts import StepInfo | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
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
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-strict: | ||
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 <you@example.com>"] | ||
|
||
[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: python-hello | ||
version: "1.0" | ||
summary: simple python application | ||
description: build a python application using core22 | ||
base: core22 | ||
confinement: strict | ||
|
||
apps: | ||
python-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") |
18 changes: 18 additions & 0 deletions
18
tests/spread/plugins/craft-parts/build-and-run-hello/poetry-hello/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 <you@example.com>"] | ||
|
||
[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
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