← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/lpcraft:add-interpolated-run-command into lpcraft:main

 

Jürgen Gmach has proposed merging ~jugmac00/lpcraft:add-interpolated-run-command into lpcraft:main.

Commit message:
Add documentation for run command interpolation

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/lpcraft/+git/lpcraft/+merge/423654
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/lpcraft:add-interpolated-run-command into lpcraft:main.
diff --git a/NEWS.rst b/NEWS.rst
index 3624a23..7a51838 100644
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -11,6 +11,8 @@ Version history
 
 - Add support for pydantic configuration on plugin classes.
 
+- Allow interpolation of the  ``run`` commands.
+
 0.0.14 (2022-05-18)
 ===================
 
diff --git a/docs/plugins.rst b/docs/plugins.rst
index a8adda5..ef7234b 100644
--- a/docs/plugins.rst
+++ b/docs/plugins.rst
@@ -83,6 +83,32 @@ following:
                 pip install --upgrade pytest
                 python -m build .
 
+Interpolation of run commands
+*****************************
+
+By default a ``run`` command in the configuration file overrides the command
+defined by the plugin, if any, unless the plugin class sets
+``INTERPOLATES_RUN_COMMANDS`` to ``True``, in which case the plugin can
+interpolate the command, like in the following example:
+
+.. code-block:: python
+
+    class MiniCondaPlugin(BasePlugin):
+        INTERPOLATES_RUN_COMMAND = True
+
+        @hookimpl
+        def lpcraft_execute_run(self) -> str:
+            run = self.config.run or ""
+            return textwrap.dedent(
+                f"""\
+                export PATH=$HOME/miniconda3/bin:$PATH
+                source activate $CONDA_ENV
+                {run}"""
+            )
+
+This applies to the ``run-before``, the ``run``,  and the ``run-after``
+commands.
+
 
 Builtin plugins
 ---------------
diff --git a/lpcraft/commands/run.py b/lpcraft/commands/run.py
index cc4bf41..4a0ab82 100644
--- a/lpcraft/commands/run.py
+++ b/lpcraft/commands/run.py
@@ -21,6 +21,7 @@ from lpcraft import env
 from lpcraft.config import Config, Job, Output
 from lpcraft.errors import CommandError
 from lpcraft.plugin.manager import get_plugin_manager
+from lpcraft.plugins import PLUGINS
 from lpcraft.providers import Provider, get_provider
 from lpcraft.utils import get_host_architecture
 
@@ -197,11 +198,18 @@ def _copy_output_properties(
 def _resolve_runtime_value(
     pm: PluginManager, job: Job, hook_name: str, job_property: str
 ) -> Optional[str]:
-    command_from_config: Optional[str] = getattr(job, job_property, None)
-    if command_from_config is not None:
-        return command_from_config
-    rv: List[str] = getattr(pm.hook, hook_name)()
-    return next(iter(rv), None)
+    command_value: Optional[str] = None
+    command_from_config = getattr(job, job_property)
+    plugin: Optional[str] = getattr(job, "plugin", None)
+    interpolated_run_command = False
+    if plugin is not None and plugin in PLUGINS:
+        interpolated_run_command = PLUGINS[plugin].INTERPOLATES_RUN_COMMAND
+    if command_from_config is not None and not interpolated_run_command:
+        command_value = command_from_config
+    else:
+        rv = getattr(pm.hook, hook_name)()
+        command_value = next(iter(rv), None)
+    return command_value
 
 
 def _install_apt_packages(
diff --git a/lpcraft/plugin/lib.py b/lpcraft/plugin/lib.py
index ced3cdd..2b22680 100644
--- a/lpcraft/plugin/lib.py
+++ b/lpcraft/plugin/lib.py
@@ -15,6 +15,9 @@ from lpcraft.plugin import hookimpl
 
 
 class InternalPlugins:
+
+    INTERPOLATES_RUN_COMMAND: bool = False
+
     def __init__(self, config: Job) -> None:
         self.config = config
 
diff --git a/lpcraft/plugins/plugins.py b/lpcraft/plugins/plugins.py
index fc136ff..9117da8 100644
--- a/lpcraft/plugins/plugins.py
+++ b/lpcraft/plugins/plugins.py
@@ -29,6 +29,9 @@ class BaseConfig(
 
 
 class BasePlugin:
+
+    INTERPOLATES_RUN_COMMAND: bool = False
+
     class Config(BaseConfig):
         pass