nebelung 1.4.0__tar.gz

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,225 @@
1
+ Metadata-Version: 2.1
2
+ Name: nebelung
3
+ Version: 1.4.0
4
+ Summary: Firecloud API Wrapper
5
+ Home-page: https://github.com/broadinstitute/nebelung
6
+ Keywords: terra,firecloud
7
+ Author: Devin McCabe
8
+ Author-email: dmccabe@broadinstitute.org
9
+ Requires-Python: >=3.11
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Requires-Dist: firecloud (>=0.16)
15
+ Requires-Dist: pandas (>=1.5)
16
+ Requires-Dist: pandera[strategies] (>=0.20)
17
+ Requires-Dist: pydantic (>=2.8)
18
+ Requires-Dist: pygithub (>=2.3)
19
+ Project-URL: Repository, https://github.com/broadinstitute/nebelung
20
+ Description-Content-Type: text/markdown
21
+
22
+ Nebelung: Python wrapper for the Firecloud API
23
+ ---
24
+
25
+ ![](https://github.com/broadinstitute/nebelung/blob/main/nebelung.jpg?raw=true)
26
+
27
+ This package provides a wrapper around the [Firecloud](https://pypi.org/project/firecloud/) package and performs a similar, though cat-themed, function as [dalmation](https://github.com/getzlab/dalmatian).
28
+
29
+ # Installation
30
+
31
+ Nebelung requires Python 3.11 or later.
32
+
33
+ ```shell
34
+ poetry add nebelung # or pip install nebelung
35
+ ```
36
+
37
+ # Usage
38
+
39
+ The package has two classes, `TerraWorkspace` and `TerraWorkflow`, and a variety of utility functions that wrap a subset of Firecloud API functionality.
40
+
41
+ ## Workspaces
42
+
43
+ ```python
44
+ from nebelung.terra_workspace import TerraWorkspace
45
+
46
+ terra_workspace = TerraWorkspace(
47
+ workspace_namespace="terra_workspace_namespace",
48
+ workspace_name="terra_workspace_name",
49
+ owners=["user1@example.com", "group@firecloud.org"],
50
+ )
51
+ ```
52
+
53
+ ### Entities
54
+
55
+ ```python
56
+ # get a workspace data table as a Pandas data frame
57
+ df = terra_workspace.get_entities("sample")
58
+
59
+ # get a workspace data table as a Pandas data frame typed with Pandera
60
+ # (`YourPanderaSchema` should subclass `nebelung.types.CoercedDataFrame`)
61
+ df = terra_workspace.get_entities("sample", YourPanderaSchema)
62
+
63
+ # upsert a data frame to a workspace data table
64
+ terra_workspace.upload_entities(df) # first column of `df` should be, e.g., `entity:sample_id`
65
+
66
+ # create a sample set named, e.g., `sample_2024-08-21T17-24-19_call_cnvs"
67
+ sample_set_id = terra_workspace.create_entity_set(
68
+ entity_type="sample",
69
+ entity_ids=["sample_id1", "sample_id2"],
70
+ suffix="call_cnvs",
71
+ )
72
+ ```
73
+
74
+ ### Workflow outputs
75
+
76
+ ```python
77
+ # collect workflow outputs from successful jobs as a list of `nebelung.types.TaskResult` objects
78
+ outputs = terra_workspace.collect_workflow_outputs()
79
+
80
+ # collect workflow outputs from successful jobs submitted in the last week
81
+ import datetime
82
+ a_week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
83
+ outputs = terra_workspace.collect_workflow_outputs(since=a_week_ago)
84
+ ```
85
+
86
+ ## Workflow
87
+
88
+ Here, a "workflow" (standard data pipeline terminology) comprises a "method" and "method config" (Terra terminology).
89
+
90
+ The standard method for making a WDL-based workflow available in a Terra workspace is to configure the git repo to push to [Dockstore](https://dockstore.org/). Although this would be the recommended technique to make a workflow available publicly, there are several drawbacks:
91
+
92
+ - The git repo must be public (for GCP-backed Terra workspaces at least).
93
+ - Every change to the method (WDL) or method config (JSON) requires creating and pushing a git commit.
94
+ - The workflow isn't updated on Dockstore immediately, since it depends on continuous deployment (CD).
95
+ - The Dockstore UI doesn't provide great visibility into CD build failures and their causes.
96
+
97
+ An alternative to Dockstore is to push the WDL directly to Firecloud. However, [that API endpoint](https://api.firecloud.org/#/Method%20Repository/post_api_methods) doesn't support uploading a WDL script that imports other local WDL scripts, nor a zip file of cross-referenced WDL scripts (like Cromwell does). The endpoint will accept WDL that imports other scripts via URLs, but currently only from the `githubusercontent.com` domain.
98
+
99
+ ### Method persistence with GitHub gists
100
+
101
+ Thus, Nebelung (ab)uses [GitHub gists](https://gist.github.com/) to persist all the WDL scripts for a workflow as multiple files belonging to a single gist, then uploads the top-level WDL script's code to Firecloud. Any `import "./path/to/included/script.wdl" as other_script` statement is rewritten so that the imported script is persisted in the gist and thus imported from a `https://gist.githubusercontent.com` URL. This happens recursively, so local imports can have their own local imports.
102
+
103
+ ### Method config
104
+
105
+ To aid in automation and make it easier to submit jobs manually without filling out many fields in the job submission UI, a JSON-formatted method config is also required, e.g.:
106
+
107
+ ```json
108
+ {
109
+ "deleted": false,
110
+ "inputs": {
111
+ "call_cnvs.sample_id": "this.sample_id"
112
+ },
113
+ "methodConfigVersion": 1,
114
+ "methodRepoMethod": {
115
+ "methodNamespace": "omics_pipelines",
116
+ "methodName": "call_cnvs",
117
+ "methodVersion": 1
118
+ },
119
+ "namespace": "omics_pipelines",
120
+ "name": "call_cnvs",
121
+ "outputs": {
122
+ "call_cnvs.segs": "this.segments"
123
+ },
124
+ "rootEntityType": "sample"
125
+ }
126
+ ```
127
+
128
+ - Both methods and method configs have their own namespaces. To simplify things, the above example uses the same sets of values for both. This approach might not be ideal if your methods and their configs are not one-to-one.
129
+ - The `TerraWorkspace.update_workflow` method will replace the `methodVersion` with an auto-incrementing version number based on the latest method's "snapshot ID" each time the method is updated. The `methodConfigVersion` should be incremented manually if desired.
130
+
131
+ ### Versioning
132
+
133
+ Some information about a submitted job's method isn't easily recovered via the Firecloud API later on. Both `update_workflow` and `collect_workflow_outputs` are written to make it easier to connect workflow outputs to method versions for use in object (workflow output files and values) versioning. Include these workflow inputs in the WDL to enable this feature:
134
+
135
+ ```wdl
136
+ version 1.0
137
+
138
+ workflow call_cnvs {
139
+ input {
140
+ String workflow_version = "1.0" # internal version number for your use
141
+ String workflow_source_url # populated automatically with URL of this script
142
+ }
143
+ }
144
+ ```
145
+
146
+ The `update_workflow` method will automatically include these workflow inputs in the new method config's inputs, with `workflow_source_url` being set dynamically to the URL of the GitHub gist of that WDL script and `workflow_version` available for explicitly versioning the WDL.
147
+
148
+ Because GitHub gist has its own built-in versioning, a `workflow_source_url` stored in a job submission's inputs will always resolve to the exact WDL script that was used in the job, even if that method is updated later.
149
+
150
+ ### Validation
151
+
152
+ To avoid persisting potentially invalid WDL, `update_workflow` also validates all the WDL scripts with [WOMtool](https://cromwell.readthedocs.io/en/stable/WOMtool) first.
153
+
154
+ ### Example
155
+
156
+ See also the [example module](https://github.com/broadinstitute/nebelung/tree/main/example) module in this repo.
157
+
158
+ ```python
159
+ import os
160
+ from pathlib import Path
161
+ from nebelung.terra_workflow import TerraWorkflow
162
+
163
+ # download the latest WOMtool from https://github.com/broadinstitute/cromwell/releases
164
+ os.environ["WOMTOOL_JAR"] = "/path/to/womtool.jar"
165
+
166
+ # generate a Github personal access token (fine-grained) at
167
+ # https://github.com/settings/tokens?type=beta
168
+ # with the "Read and Write access to gists" permission
169
+ os.environ["GITHUB_PAT"] = "github_pat_..."
170
+
171
+ terra_workflow = TerraWorkflow(
172
+ method_namespace="omics_pipelines", # should match `methodRepoMethod.methodNamespace` from method config
173
+ method_name="call_cnvs", # should match `methodRepoMethod.name` from method config
174
+ method_config_namespace="omics_pipelines", # should match `namespace` from method config
175
+ method_config_name="call_cnvs", # should match `name` from method config
176
+ method_synopsis="This method calls CNVs.",
177
+ workflow_wdl_path=Path("/path/to/call_cnvs.wdl").resolve(),
178
+ method_config_json_path=Path("/path/to/call_cnvs.json").resolve(),
179
+ github_pat="github_pat_...", # (if not using the GITHUB_PAT ENV variable)
180
+ womtool_jar="/path/to/womtool.jar", # (if not using the WOMTOOL_JAR ENV variable)
181
+ )
182
+
183
+ # create or update a workflow (i.e. method and method config) directly in Firecloud
184
+ terra_workspace.update_workflow(terra_workflow, n_snapshots_to_keep=20)
185
+
186
+ # submit a job
187
+ terra_workspace.submit_workflow_run(
188
+ terra_workflow,
189
+ # any arguments below are passed to `firecloud_api.create_submission`
190
+ entity="sample_2024-08-21T17-24-19_call_cnvs", # from `create_entity_set`
191
+ etype="sample_set", # data type of the `entity` arg
192
+ expression="this.samples", # the root entity (i.e. the WDL expects a single sample)
193
+ use_callcache=True,
194
+ use_reference_disks=False,
195
+ memory_retry_multiplier=1.2,
196
+ )
197
+ ```
198
+
199
+ ## Call Firecloud API directly
200
+
201
+ All calls to the Firecloud API made internally by Nebelung are retried automatically (with a backoff function) in the case of a networking-related error. This function also detects other errors returned by the API and parses the JSON response if the call was successful.
202
+
203
+ To use this functionality in the cases where Nebelung doesn't provide an endpoint wrapper, import the Firecloud API and the `call_firecloud_api` function:
204
+
205
+ ```python
206
+ from firecloud import api as firecloud_api
207
+ from nebelung.utils import call_firecloud_api
208
+
209
+ # get a job submission
210
+ result = call_firecloud_api(
211
+ firecloud_api.get_submission,
212
+ namespace="terra_workspace_namespace",
213
+ workspace="terra_workspace_name",
214
+ max_retries=1,
215
+ # kwargs for `get_submission`
216
+ submission_id="<uuid>",
217
+ )
218
+ ```
219
+
220
+ # Development
221
+
222
+ Run `pre-commit run --all-files` to automatically format your code with [Ruff](https://docs.astral.sh/ruff/) and check static types with [Pyright](https://microsoft.github.io/pyright).
223
+
224
+ To update the [package on pipy.org](https://pypi.org/project/nebelung), update the `version` in `pyproject.toml` and run `poetry publish --build`.
225
+
@@ -0,0 +1,203 @@
1
+ Nebelung: Python wrapper for the Firecloud API
2
+ ---
3
+
4
+ ![](https://github.com/broadinstitute/nebelung/blob/main/nebelung.jpg?raw=true)
5
+
6
+ This package provides a wrapper around the [Firecloud](https://pypi.org/project/firecloud/) package and performs a similar, though cat-themed, function as [dalmation](https://github.com/getzlab/dalmatian).
7
+
8
+ # Installation
9
+
10
+ Nebelung requires Python 3.11 or later.
11
+
12
+ ```shell
13
+ poetry add nebelung # or pip install nebelung
14
+ ```
15
+
16
+ # Usage
17
+
18
+ The package has two classes, `TerraWorkspace` and `TerraWorkflow`, and a variety of utility functions that wrap a subset of Firecloud API functionality.
19
+
20
+ ## Workspaces
21
+
22
+ ```python
23
+ from nebelung.terra_workspace import TerraWorkspace
24
+
25
+ terra_workspace = TerraWorkspace(
26
+ workspace_namespace="terra_workspace_namespace",
27
+ workspace_name="terra_workspace_name",
28
+ owners=["user1@example.com", "group@firecloud.org"],
29
+ )
30
+ ```
31
+
32
+ ### Entities
33
+
34
+ ```python
35
+ # get a workspace data table as a Pandas data frame
36
+ df = terra_workspace.get_entities("sample")
37
+
38
+ # get a workspace data table as a Pandas data frame typed with Pandera
39
+ # (`YourPanderaSchema` should subclass `nebelung.types.CoercedDataFrame`)
40
+ df = terra_workspace.get_entities("sample", YourPanderaSchema)
41
+
42
+ # upsert a data frame to a workspace data table
43
+ terra_workspace.upload_entities(df) # first column of `df` should be, e.g., `entity:sample_id`
44
+
45
+ # create a sample set named, e.g., `sample_2024-08-21T17-24-19_call_cnvs"
46
+ sample_set_id = terra_workspace.create_entity_set(
47
+ entity_type="sample",
48
+ entity_ids=["sample_id1", "sample_id2"],
49
+ suffix="call_cnvs",
50
+ )
51
+ ```
52
+
53
+ ### Workflow outputs
54
+
55
+ ```python
56
+ # collect workflow outputs from successful jobs as a list of `nebelung.types.TaskResult` objects
57
+ outputs = terra_workspace.collect_workflow_outputs()
58
+
59
+ # collect workflow outputs from successful jobs submitted in the last week
60
+ import datetime
61
+ a_week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
62
+ outputs = terra_workspace.collect_workflow_outputs(since=a_week_ago)
63
+ ```
64
+
65
+ ## Workflow
66
+
67
+ Here, a "workflow" (standard data pipeline terminology) comprises a "method" and "method config" (Terra terminology).
68
+
69
+ The standard method for making a WDL-based workflow available in a Terra workspace is to configure the git repo to push to [Dockstore](https://dockstore.org/). Although this would be the recommended technique to make a workflow available publicly, there are several drawbacks:
70
+
71
+ - The git repo must be public (for GCP-backed Terra workspaces at least).
72
+ - Every change to the method (WDL) or method config (JSON) requires creating and pushing a git commit.
73
+ - The workflow isn't updated on Dockstore immediately, since it depends on continuous deployment (CD).
74
+ - The Dockstore UI doesn't provide great visibility into CD build failures and their causes.
75
+
76
+ An alternative to Dockstore is to push the WDL directly to Firecloud. However, [that API endpoint](https://api.firecloud.org/#/Method%20Repository/post_api_methods) doesn't support uploading a WDL script that imports other local WDL scripts, nor a zip file of cross-referenced WDL scripts (like Cromwell does). The endpoint will accept WDL that imports other scripts via URLs, but currently only from the `githubusercontent.com` domain.
77
+
78
+ ### Method persistence with GitHub gists
79
+
80
+ Thus, Nebelung (ab)uses [GitHub gists](https://gist.github.com/) to persist all the WDL scripts for a workflow as multiple files belonging to a single gist, then uploads the top-level WDL script's code to Firecloud. Any `import "./path/to/included/script.wdl" as other_script` statement is rewritten so that the imported script is persisted in the gist and thus imported from a `https://gist.githubusercontent.com` URL. This happens recursively, so local imports can have their own local imports.
81
+
82
+ ### Method config
83
+
84
+ To aid in automation and make it easier to submit jobs manually without filling out many fields in the job submission UI, a JSON-formatted method config is also required, e.g.:
85
+
86
+ ```json
87
+ {
88
+ "deleted": false,
89
+ "inputs": {
90
+ "call_cnvs.sample_id": "this.sample_id"
91
+ },
92
+ "methodConfigVersion": 1,
93
+ "methodRepoMethod": {
94
+ "methodNamespace": "omics_pipelines",
95
+ "methodName": "call_cnvs",
96
+ "methodVersion": 1
97
+ },
98
+ "namespace": "omics_pipelines",
99
+ "name": "call_cnvs",
100
+ "outputs": {
101
+ "call_cnvs.segs": "this.segments"
102
+ },
103
+ "rootEntityType": "sample"
104
+ }
105
+ ```
106
+
107
+ - Both methods and method configs have their own namespaces. To simplify things, the above example uses the same sets of values for both. This approach might not be ideal if your methods and their configs are not one-to-one.
108
+ - The `TerraWorkspace.update_workflow` method will replace the `methodVersion` with an auto-incrementing version number based on the latest method's "snapshot ID" each time the method is updated. The `methodConfigVersion` should be incremented manually if desired.
109
+
110
+ ### Versioning
111
+
112
+ Some information about a submitted job's method isn't easily recovered via the Firecloud API later on. Both `update_workflow` and `collect_workflow_outputs` are written to make it easier to connect workflow outputs to method versions for use in object (workflow output files and values) versioning. Include these workflow inputs in the WDL to enable this feature:
113
+
114
+ ```wdl
115
+ version 1.0
116
+
117
+ workflow call_cnvs {
118
+ input {
119
+ String workflow_version = "1.0" # internal version number for your use
120
+ String workflow_source_url # populated automatically with URL of this script
121
+ }
122
+ }
123
+ ```
124
+
125
+ The `update_workflow` method will automatically include these workflow inputs in the new method config's inputs, with `workflow_source_url` being set dynamically to the URL of the GitHub gist of that WDL script and `workflow_version` available for explicitly versioning the WDL.
126
+
127
+ Because GitHub gist has its own built-in versioning, a `workflow_source_url` stored in a job submission's inputs will always resolve to the exact WDL script that was used in the job, even if that method is updated later.
128
+
129
+ ### Validation
130
+
131
+ To avoid persisting potentially invalid WDL, `update_workflow` also validates all the WDL scripts with [WOMtool](https://cromwell.readthedocs.io/en/stable/WOMtool) first.
132
+
133
+ ### Example
134
+
135
+ See also the [example module](https://github.com/broadinstitute/nebelung/tree/main/example) module in this repo.
136
+
137
+ ```python
138
+ import os
139
+ from pathlib import Path
140
+ from nebelung.terra_workflow import TerraWorkflow
141
+
142
+ # download the latest WOMtool from https://github.com/broadinstitute/cromwell/releases
143
+ os.environ["WOMTOOL_JAR"] = "/path/to/womtool.jar"
144
+
145
+ # generate a Github personal access token (fine-grained) at
146
+ # https://github.com/settings/tokens?type=beta
147
+ # with the "Read and Write access to gists" permission
148
+ os.environ["GITHUB_PAT"] = "github_pat_..."
149
+
150
+ terra_workflow = TerraWorkflow(
151
+ method_namespace="omics_pipelines", # should match `methodRepoMethod.methodNamespace` from method config
152
+ method_name="call_cnvs", # should match `methodRepoMethod.name` from method config
153
+ method_config_namespace="omics_pipelines", # should match `namespace` from method config
154
+ method_config_name="call_cnvs", # should match `name` from method config
155
+ method_synopsis="This method calls CNVs.",
156
+ workflow_wdl_path=Path("/path/to/call_cnvs.wdl").resolve(),
157
+ method_config_json_path=Path("/path/to/call_cnvs.json").resolve(),
158
+ github_pat="github_pat_...", # (if not using the GITHUB_PAT ENV variable)
159
+ womtool_jar="/path/to/womtool.jar", # (if not using the WOMTOOL_JAR ENV variable)
160
+ )
161
+
162
+ # create or update a workflow (i.e. method and method config) directly in Firecloud
163
+ terra_workspace.update_workflow(terra_workflow, n_snapshots_to_keep=20)
164
+
165
+ # submit a job
166
+ terra_workspace.submit_workflow_run(
167
+ terra_workflow,
168
+ # any arguments below are passed to `firecloud_api.create_submission`
169
+ entity="sample_2024-08-21T17-24-19_call_cnvs", # from `create_entity_set`
170
+ etype="sample_set", # data type of the `entity` arg
171
+ expression="this.samples", # the root entity (i.e. the WDL expects a single sample)
172
+ use_callcache=True,
173
+ use_reference_disks=False,
174
+ memory_retry_multiplier=1.2,
175
+ )
176
+ ```
177
+
178
+ ## Call Firecloud API directly
179
+
180
+ All calls to the Firecloud API made internally by Nebelung are retried automatically (with a backoff function) in the case of a networking-related error. This function also detects other errors returned by the API and parses the JSON response if the call was successful.
181
+
182
+ To use this functionality in the cases where Nebelung doesn't provide an endpoint wrapper, import the Firecloud API and the `call_firecloud_api` function:
183
+
184
+ ```python
185
+ from firecloud import api as firecloud_api
186
+ from nebelung.utils import call_firecloud_api
187
+
188
+ # get a job submission
189
+ result = call_firecloud_api(
190
+ firecloud_api.get_submission,
191
+ namespace="terra_workspace_namespace",
192
+ workspace="terra_workspace_name",
193
+ max_retries=1,
194
+ # kwargs for `get_submission`
195
+ submission_id="<uuid>",
196
+ )
197
+ ```
198
+
199
+ # Development
200
+
201
+ Run `pre-commit run --all-files` to automatically format your code with [Ruff](https://docs.astral.sh/ruff/) and check static types with [Pyright](https://microsoft.github.io/pyright).
202
+
203
+ To update the [package on pipy.org](https://pypi.org/project/nebelung), update the `version` in `pyproject.toml` and run `poetry publish --build`.
@@ -0,0 +1,11 @@
1
+ from importlib import metadata as importlib_metadata
2
+
3
+
4
+ def get_version() -> str:
5
+ try:
6
+ return importlib_metadata.version(__name__)
7
+ except importlib_metadata.PackageNotFoundError:
8
+ return "unknown"
9
+
10
+
11
+ version: str = get_version()
@@ -0,0 +1,149 @@
1
+ import json
2
+ import logging
3
+ import os
4
+ import tempfile
5
+ from pathlib import Path
6
+
7
+ from firecloud import api as firecloud_api
8
+ from firecloud.api import __post as firecloud_post
9
+
10
+ from nebelung.types import PersistedWdl
11
+ from nebelung.utils import call_firecloud_api
12
+ from nebelung.wdl import GistedWdl
13
+
14
+
15
+ class TerraWorkflow:
16
+ def __init__(
17
+ self,
18
+ method_namespace: str,
19
+ method_name: str,
20
+ method_config_namespace: str,
21
+ method_config_name: str,
22
+ method_synopsis: str,
23
+ workflow_wdl_path: Path,
24
+ method_config_json_path: Path,
25
+ github_pat: str | None = None,
26
+ womtool_jar: str | None = None,
27
+ ) -> None:
28
+ self.method_namespace = method_namespace
29
+ self.method_name = method_name
30
+ self.method_config_namespace = method_config_namespace
31
+ self.method_config_name = method_config_name
32
+ self.method_synopsis = method_synopsis
33
+ self.workflow_wdl_path = workflow_wdl_path
34
+ self.method_config_json_path = method_config_json_path
35
+ self.github_pat = os.getenv("GITHUB_PAT", github_pat)
36
+ self.womtool_jar = os.getenv("WOMTOOL_JAR", womtool_jar)
37
+
38
+ self.method_config = json.load(open(self.method_config_json_path, "r"))
39
+ self.persisted_wdl_script: PersistedWdl | None = None
40
+
41
+ def persist_method_on_github(self) -> None:
42
+ """
43
+ Upload the method's WDL script to GitHub, rewriting import statements for
44
+ dependent WDL scripts as needed.
45
+ """
46
+
47
+ assert self.github_pat is not None, (
48
+ "A GitHub personal access token must be defined to persist this method on "
49
+ "GitHub. Set the GITHUB_PAT environment variable or the `github_pat` "
50
+ "argument when instantiating this `TerraWorkflow` instance."
51
+ )
52
+
53
+ assert self.womtool_jar is not None, (
54
+ "A path to a WOMTool .jar file is required to validate the WDL script. Set "
55
+ "the WOMTOOL_JAR environment variable or the `womtool_jar` argument when "
56
+ "instantiating this `TerraWorkflow` instance."
57
+ )
58
+
59
+ if self.persisted_wdl_script is None:
60
+ logging.info(f"Persisting {self.workflow_wdl_path} on GitHub")
61
+ gisted_wdl = GistedWdl(
62
+ method_name=self.method_name,
63
+ github_pat=self.github_pat,
64
+ womtool_jar=self.womtool_jar,
65
+ )
66
+ self.persisted_wdl_script = gisted_wdl.persist_wdl_script(
67
+ wdl_path=self.workflow_wdl_path
68
+ )
69
+
70
+ def update_method(self, owners: list[str]) -> dict:
71
+ """
72
+ Update a Firecloud method.
73
+
74
+ :param owners: a list of Firecloud users/groups to set as owners
75
+ :return: the latest method's snapshot
76
+ """
77
+
78
+ # get contents of WDL uploaded to GCS
79
+ self.persist_method_on_github()
80
+ assert self.persisted_wdl_script is not None
81
+
82
+ with tempfile.NamedTemporaryFile("w") as f:
83
+ f.write(self.persisted_wdl_script["wdl"])
84
+ f.flush()
85
+
86
+ logging.info("Updating method")
87
+ snapshot = call_firecloud_api(
88
+ firecloud_api.update_repository_method,
89
+ namespace=self.method_namespace,
90
+ method=self.method_name,
91
+ synopsis=self.method_synopsis,
92
+ wdl=f.name,
93
+ )
94
+
95
+ logging.info("Setting method ACL")
96
+ call_firecloud_api(
97
+ firecloud_api.update_repository_method_acl,
98
+ namespace=self.method_namespace,
99
+ method=self.method_name,
100
+ snapshot_id=snapshot["snapshotId"],
101
+ acl_updates=[{"user": x, "role": "OWNER"} for x in owners],
102
+ )
103
+
104
+ logging.info("Setting method repository config ACL")
105
+ # the firecloud package doesn't have a wrapper for this endpoint
106
+ call_firecloud_api(
107
+ firecloud_post,
108
+ methcall=f"configurations/{self.method_namespace}/permissions",
109
+ json=[{"user": x, "role": "OWNER"} for x in owners],
110
+ )
111
+
112
+ return snapshot
113
+
114
+ def get_method_snapshots(self) -> list[dict]:
115
+ """
116
+ Get all of the snapshots of the method.
117
+
118
+ :return: list of snapshot information, most recent first
119
+ """
120
+
121
+ logging.info(f"Getting {self.method_name} method snapshots")
122
+ snapshots = call_firecloud_api(
123
+ firecloud_api.list_repository_methods,
124
+ namespace=self.method_namespace,
125
+ name=self.method_name,
126
+ )
127
+
128
+ snapshots.sort(key=lambda x: x["snapshotId"], reverse=True)
129
+ return snapshots
130
+
131
+ def delete_old_method_snapshots(self, n_snapshots_to_keep: int) -> None:
132
+ """
133
+ Delete all but `n_snapshots_to_keep` of the most recent snapshots of the method.
134
+
135
+ :param n_snapshots_to_keep: the number of snapshots to keep
136
+ """
137
+
138
+ snapshots = self.get_method_snapshots()
139
+
140
+ to_delete = snapshots[n_snapshots_to_keep:]
141
+ logging.info(f"Deleting {len(to_delete)} old snapshot(s)")
142
+
143
+ for x in to_delete:
144
+ call_firecloud_api(
145
+ firecloud_api.delete_repository_method,
146
+ namespace=self.method_namespace,
147
+ name=self.method_name,
148
+ snapshot_id=x["snapshotId"],
149
+ )