snakemake-software-deployment-plugin-container 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+ __author__ = "ben carrillo"
2
+ __copyright__ = "Copyright 2025, ben carrillo"
3
+ __email__ = "ben.uzh@pm.me"
4
+ __license__ = "MIT"
5
+ import os.path
6
+ import shlex
7
+
8
+ from dataclasses import dataclass, field
9
+ from os import getcwd
10
+ from shutil import which
11
+ from typing import Iterable
12
+
13
+ from snakemake_interface_software_deployment_plugins.settings import (
14
+ SoftwareDeploymentSettingsBase,
15
+ )
16
+ from snakemake_interface_software_deployment_plugins import (
17
+ EnvBase,
18
+ EnvSpecBase,
19
+ SoftwareReport,
20
+ )
21
+
22
+ # Raise errors that will not be handled within this plugin but thrown upwards to
23
+ # Snakemake and the user as WorkflowError.
24
+ from snakemake_interface_common.exceptions import WorkflowError # noqa: F401
25
+ from snakemake_interface_common.settings import SettingsEnumBase
26
+
27
+
28
+ # The mountpoint for the Snakemake working directory inside the container.
29
+ SNAKEMAKE_MOUNTPOINT = "/mnt/snakemake"
30
+
31
+
32
+ # ContainerType is an enum that defines the different container types we support.
33
+ # If adding new ones, make sure the choice is the same as the command name.
34
+ class Runtime(SettingsEnumBase):
35
+ UDOCKER = 0
36
+ PODMAN = 1
37
+
38
+
39
+ @dataclass
40
+ class Settings(SoftwareDeploymentSettingsBase):
41
+ runtime: Runtime = field(
42
+ default=Runtime.UDOCKER,
43
+ metadata={
44
+ "help": "Container kind (udocker by default)",
45
+ "env_var": False,
46
+ "required": False,
47
+ },
48
+ )
49
+
50
+
51
+ @dataclass
52
+ class EnvSpec(EnvSpecBase):
53
+ # TODO: when integrating this plugin, image_uri should be populated from the container keyword (via whatever mechanism is exposing)
54
+ # the plugin to the software deployment registry.
55
+ image_uri: str
56
+
57
+ @classmethod
58
+ def identity_attributes(cls) -> Iterable[str]:
59
+ return ["image_uri"]
60
+
61
+ @classmethod
62
+ def source_path_attributes(cls) -> Iterable[str]:
63
+ return ()
64
+
65
+ def __str__(self) -> str:
66
+ """Return a string representation of the environment spec."""
67
+ return self.image_uri
68
+
69
+
70
+ # All errors should be wrapped with snakemake-interface-common.errors.WorkflowError
71
+ class Env(EnvBase):
72
+ # image_repo is the de-referenced repository from where the image was obtained
73
+ image_repo: str
74
+ settings: Settings # type: ignore
75
+ spec: EnvSpec # type: ignore
76
+
77
+ # image_hash is a hash of the image that can be used to identify it
78
+ # TODO: populate it *after* fetching/execution
79
+ image_hash: str
80
+
81
+ def __post_init__(self) -> None:
82
+ self.check()
83
+
84
+ # The decorator ensures that the decorated method is only called once
85
+ # in case multiple environments of the same kind are created.
86
+ @EnvBase.once
87
+ def check(self) -> None:
88
+ self._check_service()
89
+
90
+ def _check_service(self) -> None:
91
+ if self.spec.image_uri == "":
92
+ raise WorkflowError("Image URI is empty")
93
+
94
+ # TODO: if we don't get the tag, we should assume :latest
95
+
96
+ if self.settings.runtime not in Runtime.all():
97
+ raise WorkflowError("Invalid container kind")
98
+
99
+ # this assumes that the choices are the same as the command names. If
100
+ # this is not the case, we need to add a mapping.
101
+ self._check_executable()
102
+
103
+ def _check_executable(self) -> None:
104
+ cmd = self.settings.runtime.item_to_choice()
105
+ if which(cmd) is None:
106
+ raise WorkflowError(f"{cmd} is not available in PATH")
107
+
108
+ def decorate_shellcmd(self, cmd: str) -> str:
109
+ # TODO pass more options here (extra mount volumes, user etc)
110
+ containercache = os.path.join(
111
+ SNAKEMAKE_MOUNTPOINT, ".cache/snakemake/source-cache"
112
+ )
113
+
114
+ decorated_cmd = (
115
+ f"{self.settings.runtime} run"
116
+ " --rm" # Remove container after execution
117
+ f" -e HOME={SNAKEMAKE_MOUNTPOINT!r}" # Set HOME to working directory
118
+ f" -w {SNAKEMAKE_MOUNTPOINT!r}" # Working directory inside container
119
+ f" -v {getcwd()!r}:{SNAKEMAKE_MOUNTPOINT!r}" # Mount host directory to container
120
+ f" -v {str(self.source_cache)!r}:{containercache!r}" # Mount host cache to container
121
+ f" {self.spec.image_uri}" # Container image
122
+ " /bin/sh" # Shell executable
123
+ f" -c {shlex.quote(cmd)}" # The command to execute
124
+ )
125
+
126
+ return decorated_cmd
127
+
128
+ def record_hash(self, hash_object) -> None:
129
+ # Update given hash such that it changes whenever the environment
130
+ # could potentially contain a different set of software (in terms of versions or
131
+ # packages). Use self.spec (containing the corresponding EnvSpec object)
132
+ # to determine the hash.
133
+ hash_object.update(...)
134
+
135
+ def report_software(self) -> Iterable[SoftwareReport]:
136
+ # Report the software contained in the environment. This should be a list of
137
+ # snakemake_interface_software_deployment_plugins.SoftwareReport data class.
138
+ # Use SoftwareReport.is_secondary = True if the software is just some
139
+ # less important technical dependency. This allows Snakemake's report to
140
+ # hide those for clarity. In case of containers, it is also valid to
141
+ # return the container URI as a "software".
142
+ # Return an empty tuple () if no software can be reported.
143
+ # TODO: implement.
144
+ # Get container URI + hash (assuming we've already executd and fetched the image,
145
+ # so that we can get the hash for the image plus the tag)
146
+ return ()
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.4
2
+ Name: snakemake-software-deployment-plugin-container
3
+ Version: 0.1.0
4
+ Project-URL: repository, https://github.com/snakemake/snakemake-software-deployment-plugin-container
5
+ Project-URL: documentation, https://snakemake.github.io/snakemake-plugin-catalog/plugins/software-deployment/container.html
6
+ Author-email: Ben Carrillo <ben.uzh@pm.me>, Johannes Köster <johannes.koester@uni-due.de>
7
+ License-File: LICENSE
8
+ Requires-Python: <4.0,>=3.11
9
+ Requires-Dist: snakemake-interface-common<2.0.0,>=1.17.4
10
+ Requires-Dist: snakemake-interface-software-deployment-plugins<1.0,>=0.9.1
11
+ Requires-Dist: udocker<2.0.0,>=1.3.17
12
+ Description-Content-Type: text/markdown
13
+
14
+ # snakemake-software-deployment-plugin-container
15
+
16
+ A generic container plugin implementing snakemake's software-deployment interface.
17
+
18
+ * It executes the given commands within a rootless container.
19
+ * It currently supports both `udocker` and `podman`.
20
+
21
+ ## Usage
22
+
23
+ The `kind` parameter specifies valid values for the container runtime to use.
24
+
25
+ The `container` directive (should be) used to specify the container image to use
26
+ for the software deployment. This string should be a valid container image name,
27
+ including the tag; e.g., `biocontainers/example:1.0`.
28
+
29
+ By default, the plugin will attemtp to use 'latest' tag if no tag is provided.
30
+ However, be aware that some registries may not support this, for [good
31
+ reasons](https://github.com/BioContainers/containers/issues/297#issuecomment-439055879)
32
+ having to do with reproducibility. So, it's a good idea to specify a tag.
33
+
34
+ ## Status
35
+
36
+ - Work in progress. The plugin conforms to the current software-deployment interface specification, and it has passing tests.
37
+ - Will revisit when further work is done in snakemake core to allow a more reusable integration of deployment plugins.
38
+
39
+ ## TODO
40
+
41
+ - [ ] Add support for apptainer
42
+ - [ ] Implement repot_sofware method with container URI *and* the resolved hash of the image.
@@ -0,0 +1,5 @@
1
+ snakemake_software_deployment_plugin_container/__init__.py,sha256=PA7_ScZwH3kjlB6ebA3g5_FFs86TwzfNXrDNOGMEdAA,5394
2
+ snakemake_software_deployment_plugin_container-0.1.0.dist-info/METADATA,sha256=YY0VeImLyFkBAn0jb8yFsSdBeAQZIhYEr5st2mtrLkg,1930
3
+ snakemake_software_deployment_plugin_container-0.1.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
4
+ snakemake_software_deployment_plugin_container-0.1.0.dist-info/licenses/LICENSE,sha256=pj54hC8EVqRRIr6bzQlgqnB-cyNUsCIDXLmCZ4xhA7E,1064
5
+ snakemake_software_deployment_plugin_container-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 btraven
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.