Skip to content

Commit

Permalink
feat: support for the uv plugin (#5196)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Schvezov <[email protected]>
Co-authored-by: Michael DuBelko <[email protected]>
Co-authored-by: Sergio Schvezov <[email protected]>
  • Loading branch information
3 people authored Jan 21, 2025
1 parent 3aacad7 commit 800823a
Show file tree
Hide file tree
Showing 24 changed files with 451 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ __pycache__
*.comp
*.snap
snap/.snapcraft/
.spread_multipass
.spread-reuse.*
/stage
*.swp
Expand Down
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023-2024 Canonical Ltd.
# Copyright 2023-2025 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -79,6 +79,7 @@
"common/craft-parts/reference/plugins/poetry_plugin.rst",
"common/craft-parts/reference/plugins/python_plugin.rst",
"common/craft-parts/reference/plugins/maven_plugin.rst",
"common/craft-parts/reference/plugins/uv_plugin.rst",
# Extra non-craft-parts exclusions can be added after this comment
]

Expand Down
9 changes: 9 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ Core

* Improved error messaging when unable to pack.

Plugins
#######

uv
""

* Add a new plugin for projects using `uv <https://docs.astral.sh/uv/>`_.
For more information, see :doc:`uv plugin reference
</reference/plugins/uv_plugin>`.

8.5.1 (2024-Dec-17)
-------------------
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Snapcraft.
/common/craft-parts/reference/plugins/cmake_plugin
/common/craft-parts/reference/plugins/dump_plugin
/common/craft-parts/reference/plugins/go_plugin
/common/craft-parts/reference/plugins/go_use_plugin
/common/craft-parts/reference/plugins/make_plugin
plugins/maven_plugin
/common/craft-parts/reference/plugins/meson_plugin
Expand All @@ -26,3 +27,4 @@ Snapcraft.
/common/craft-parts/reference/plugins/qmake_plugin
/common/craft-parts/reference/plugins/rust_plugin
/common/craft-parts/reference/plugins/scons_plugin
plugins/uv_plugin
7 changes: 7 additions & 0 deletions docs/reference/plugins/uv_plugin.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. include:: /common/craft-parts/reference/plugins/uv_plugin.rst
:end-before: .. _uv-details-begin:

.. include:: _python_common.rst

.. include:: /common/craft-parts/reference/plugins/uv_plugin.rst
:start-after: .. _uv-details-end:
2 changes: 1 addition & 1 deletion requirements-devel.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ craft-grammar==2.0.1
# via
# craft-application
# snapcraft (setup.py)
craft-parts==2.1.2
craft-parts==2.3.0
# via
# craft-application
# snapcraft (setup.py)
Expand Down
2 changes: 1 addition & 1 deletion requirements-docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ craft-grammar==2.0.1
# via
# craft-application
# snapcraft (setup.py)
craft-parts==2.1.2
craft-parts==2.3.0
# via
# craft-application
# snapcraft (setup.py)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ craft-grammar==2.0.1
# via
# craft-application
# snapcraft (setup.py)
craft-parts==2.1.2
craft-parts==2.3.0
# via
# craft-application
# snapcraft (setup.py)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def recursive_data_files(directory, install_directory):
"craft-archives~=2.0",
"craft-cli~=2.9",
"craft-grammar>=2.0.1,<3.0.0",
"craft-parts>=2.1.2,<3.0.0",
"craft-parts==2.3.0",
"craft-platforms~=0.4",
"craft-providers>=2.0.4,<3.0.0",
"craft-store>=3.0.2,<4.0.0",
Expand Down
7 changes: 5 additions & 2 deletions snapcraft/parts/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022 Canonical Ltd.
# Copyright 2022, 2025 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
Expand All @@ -17,14 +17,16 @@
"""Snapcraft specific plugins."""


from .register import get_plugins, register

from .colcon_plugin import ColconPlugin
from .conda_plugin import CondaPlugin
from .flutter_plugin import FlutterPlugin
from .kernel_plugin import KernelPlugin
from .matter_sdk_plugin import MatterSdkPlugin
from .poetry_plugin import PoetryPlugin
from .python_plugin import PythonPlugin
from .register import get_plugins, register
from .uv_plugin import UvPlugin

__all__ = [
"ColconPlugin",
Expand All @@ -36,4 +38,5 @@
"PythonPlugin",
"get_plugins",
"register",
"UvPlugin",
]
15 changes: 14 additions & 1 deletion snapcraft/parts/plugins/python_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
import logging
from pathlib import Path

from craft_parts import PartInfo, StepInfo, errors
from craft_parts import PartInfo, StepInfo, errors, plugins

from .poetry_plugin import PoetryPlugin
from .python_plugin import PythonPlugin
from .uv_plugin import UvPlugin

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -94,3 +98,12 @@ def post_prime(step_info: StepInfo) -> None:
if old_contents != contents:
logger.debug("Updating pyvenv.cfg to:\n%s", contents)
pyvenv.write_text(contents)


def get_python_plugins() -> dict[str, plugins.plugins.PluginType]:
"""Get a list of currently supported Python-based plugins."""
return {
"poetry": PoetryPlugin,
"python": PythonPlugin,
"uv": UvPlugin,
}
10 changes: 4 additions & 6 deletions snapcraft/parts/plugins/register.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022 Canonical Ltd.
# Copyright 2022, 2025 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
Expand All @@ -19,13 +19,13 @@
import craft_parts
from craft_parts.plugins.plugins import PluginType

from snapcraft.parts.plugins import python_common

from .colcon_plugin import ColconPlugin
from .conda_plugin import CondaPlugin
from .flutter_plugin import FlutterPlugin
from .kernel_plugin import KernelPlugin
from .matter_sdk_plugin import MatterSdkPlugin
from .poetry_plugin import PoetryPlugin
from .python_plugin import PythonPlugin


def get_plugins(core22: bool) -> dict[str, PluginType]:
Expand All @@ -37,10 +37,8 @@ def get_plugins(core22: bool) -> dict[str, PluginType]:
"colcon": ColconPlugin,
"conda": CondaPlugin,
"flutter": FlutterPlugin,
"python": PythonPlugin,
"matter-sdk": MatterSdkPlugin,
"poetry": PoetryPlugin,
}
} | python_common.get_python_plugins()

if core22:
plugins["kernel"] = KernelPlugin
Expand Down
32 changes: 32 additions & 0 deletions snapcraft/parts/plugins/uv_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2025 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 uv plugin."""

from typing import Optional

from craft_parts.plugins import uv_plugin
from overrides import override

from snapcraft.parts.plugins import python_common


class UvPlugin(uv_plugin.UvPlugin):
"""A uv plugin for Snapcraft."""

@override
def _get_system_python_interpreter(self) -> Optional[str]:
return python_common.get_system_interpreter(self._part_info)
2 changes: 1 addition & 1 deletion snapcraft/services/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def post_prime(self, step_info: StepInfo) -> bool:
plugin_name = project.parts[part_name]["plugin"]

# Handle plugin-specific prime fixes
if plugin_name in ("python", "poetry"):
if plugin_name in python_common.get_python_plugins().keys():
python_common.post_prime(step_info)

# Handle patch-elf
Expand Down
1 change: 1 addition & 0 deletions tests/spread/core24/python-hello/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
PARAM/strict: ""
PARAM/classic: "--classic"
PARAM/poetry: ""
PARAM/uv: ""

restore: |
cd ./"${SPREAD_VARIANT}"
Expand Down
16 changes: 16 additions & 0 deletions tests/spread/core24/python-hello/uv/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: python-hello-uv
version: "1.0"
summary: simple python application
description: build a python application using core24
base: core24
confinement: strict

apps:
python-hello-uv:
command: bin/python $SNAP/bin/hello
parts:
hello:
plugin: uv
source: src
build-snaps:
- astral-uv
5 changes: 5 additions & 0 deletions tests/spread/core24/python-hello/uv/src/hello/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import black


def main():
print(f"hello world! black version: {black.__version__}")
16 changes: 16 additions & 0 deletions tests/spread/core24/python-hello/uv/src/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[project]
name = "hello"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"black==24.8.0",
]

[build-system]
requires = ["setuptools>61"]
build-backend = "setuptools.build_meta"

[project.scripts]
hello = "hello:main"
Loading

0 comments on commit 800823a

Please sign in to comment.