← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/lpcraft:add-pyproject-build-plugin into lpcraft:main

 

Jürgen Gmach has proposed merging ~jugmac00/lpcraft:add-pyproject-build-plugin into lpcraft:main.

Commit message:
Create `pyproject-build` plugin

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/lpcraft/+git/lpcraft/+merge/414393
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/lpcraft:add-pyproject-build-plugin into lpcraft:main.
diff --git a/.gitignore b/.gitignore
index 903613f..abb3ac0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,4 +6,5 @@ __pycache__
 .coverage
 htmlcov
 build/
+dist/
 docs/_build/
diff --git a/.launchpad.yaml b/.launchpad.yaml
index f9993cd..290429c 100644
--- a/.launchpad.yaml
+++ b/.launchpad.yaml
@@ -1,5 +1,6 @@
 pipeline:
     - test
+    - build
 
 jobs:
     test:
@@ -7,3 +8,7 @@ jobs:
         architectures: amd64
         packages: [git]
         plugin: tox
+    build:
+        series: focal
+        architectures: amd64
+        plugin: pyproject-build
diff --git a/docs/conf.py b/docs/conf.py
index 0b5ba94..678f3f9 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -19,7 +19,7 @@ sys.path.insert(0, os.path.abspath(".."))
 # -- Project information -----------------------------------------------------
 
 project = "lpcraft"
-copyright = "2021, Canonical Ltd"
+copyright = "2021-2022, Canonical Ltd"
 
 
 # -- General configuration ---------------------------------------------------
diff --git a/lpcraft/plugin/tests/test_plugins.py b/lpcraft/plugin/tests/test_plugins.py
index 276e963..6e89d1e 100644
--- a/lpcraft/plugin/tests/test_plugins.py
+++ b/lpcraft/plugin/tests/test_plugins.py
@@ -40,9 +40,7 @@ class TestPlugins(CommandBaseTestCase):
 
     @patch("lpcraft.commands.run.get_provider")
     @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
-    def test_builtin_plugin(
-        self, mock_get_host_architecture, mock_get_provider
-    ):
+    def test_tox_plugin(self, mock_get_host_architecture, mock_get_provider):
         launcher = Mock(spec=launch)
         provider = self.makeLXDProvider(lxd_launcher=launcher)
         mock_get_provider.return_value = provider
@@ -183,3 +181,61 @@ class TestPlugins(CommandBaseTestCase):
             ],
             execute_run.call_args_list,
         )
+
+    @patch("lpcraft.commands.run.get_provider")
+    @patch("lpcraft.commands.run.get_host_architecture", return_value="amd64")
+    def test_pyproject_build_plugin(
+        self, mock_get_host_architecture, mock_get_provider
+    ):
+        launcher = Mock(spec=launch)
+        provider = self.makeLXDProvider(lxd_launcher=launcher)
+        mock_get_provider.return_value = provider
+        execute_run = launcher.return_value.execute_run
+        execute_run.return_value = subprocess.CompletedProcess([], 0)
+        config = dedent(
+            """
+            pipeline:
+                - build
+
+            jobs:
+                build:
+                    series: focal
+                    architectures: amd64
+                    plugin: pyproject-build
+            """
+        )
+        Path(".launchpad.yaml").write_text(config)
+
+        self.run_command("run")
+
+        self.assertEqual(
+            [
+                call(
+                    [
+                        "apt",
+                        "install",
+                        "-y",
+                        "python3-pip",
+                        "python3-venv",
+                    ],
+                    cwd=PosixPath("/root/project"),
+                    env={},
+                    stdout=ANY,
+                    stderr=ANY,
+                ),
+                call(
+                    [
+                        "bash",
+                        "--noprofile",
+                        "--norc",
+                        "-ec",
+                        "python3 -m pip install build==0.7.0; python3 -m build",  # noqa: E501
+                    ],
+                    cwd=PosixPath("/root/project"),
+                    env={},
+                    stdout=ANY,
+                    stderr=ANY,
+                ),
+            ],
+            execute_run.call_args_list,
+        )
diff --git a/lpcraft/plugins/plugins.py b/lpcraft/plugins/plugins.py
index cf5862b..7708e35 100644
--- a/lpcraft/plugins/plugins.py
+++ b/lpcraft/plugins/plugins.py
@@ -3,7 +3,7 @@
 
 from __future__ import annotations
 
-__all__ = ["ToxPlugin"]
+__all__ = ["ToxPlugin", "PyProjectBuildPlugin"]
 
 from lpcraft.config import Job
 from lpcraft.plugin import hookimpl
@@ -38,3 +38,33 @@ class ToxPlugin:
         # necessary. Let's remove this once we have a plugin which actually
         # needs to set environment variables.
         return {"PLUGIN": "tox"}
+
+
+@register(name="pyproject-build")
+class PyProjectBuildPlugin:
+    """Installs `build` and builds a Python package according to PEP 517.
+
+    Usage:
+        In `.launchpad.yaml` create a key/value pair with `plugin` and
+        `pyproject-build` within the job definition.
+    """
+
+    def __init__(self, config: Job) -> None:
+        self.config = config
+
+    @hookimpl  # type: ignore
+    def lpcraft_install_packages(self) -> list[str]:
+        # Ubuntu 20.04 does not provide a packaged version of build,
+        # so we need pip to install it
+        #
+        # `build` needs `python3-venv` to create an isolated build
+        # environment
+        return [
+            "python3-pip",
+            "python3-venv",
+        ]
+
+    @hookimpl  # type: ignore
+    def lpcraft_execute_run(self) -> str:
+        # XXX jugmac00 2022-01-20: we should consider using a PPA
+        return "python3 -m pip install build==0.7.0; python3 -m build"

Follow ups