Skip to content

Commit 5d64de3

Browse files
vjaganat90Vasu Jaganath
andauthored
Refactor some common transformations (PolusAI#285)
* add user args for workflows built with python api * remove redundant io in main & remove irrelevant warning/exit in compiler * Refactor some common transformations into post_compile --------- Co-authored-by: Vasu Jaganath <[email protected]>
1 parent edbe28e commit 5d64de3

File tree

5 files changed

+57
-66
lines changed

5 files changed

+57
-66
lines changed

src/sophios/api/http/restapi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from sophios.utils_graphs import get_graph_reps
1414
from sophios.utils_yaml import wic_loader
1515
from sophios import utils_cwl
16+
from sophios.post_compile import cwl_inline_runtag
1617
from sophios.cli import get_args
1718
from sophios.wic_types import CompilerInfo, Json, Tool, Tools, StepId, YamlTree, Cwl, NodeData
1819
from sophios.api.utils import converter
@@ -132,9 +133,8 @@ async def compile_wf(request: Request) -> Json:
132133
tools_cwl, True, relative_run_path=True, testing=False)
133134

134135
rose_tree = compiler_info.rose
135-
if args.inline_cwl_runtag:
136-
input_output.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
137-
rose_tree = plugins.cwl_update_inline_runtag_rosetree(rose_tree, Path('autogenerated/'), True)
136+
input_output.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
137+
cwl_inline_runtag(args, rose_tree)
138138
# ======== OUTPUT PROCESSING ================
139139
# ========= PROCESS COMPILED OBJECT =========
140140
sub_node_data: NodeData = rose_tree.data

src/sophios/api/pythonapi.py

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from cwl_utils.parser import load_document_by_uri, load_document_by_yaml
1313
from pydantic import BaseModel, ConfigDict, Field, PrivateAttr, field_validator
1414

15-
from sophios import compiler, input_output, plugins, utils_cwl
15+
from sophios import compiler, input_output, plugins, utils_cwl, post_compile
1616
from sophios import run_local as run_local_module
1717
from sophios.cli import get_args
1818
from sophios.utils_graphs import get_graph_reps
@@ -756,32 +756,11 @@ def run(self) -> None:
756756
args = get_args(self.process_name, self.user_args) # Use mock CLI args
757757
rose_tree: RoseTree = compiler_info.rose
758758

759-
# cwl-docker-extract recursively `docker pull`s all images in all subworkflows.
760-
# This is important because cwltool only uses `docker run` when executing
761-
# workflows, and if there is a local image available,
762-
# `docker run` will NOT query the remote repository for the latest image!
763-
# cwltool has a --force-docker-pull option, but this may cause multiple pulls in parallel.
764-
if args.container_engine == 'singularity':
765-
cmd = ['cwl-docker-extract', '-s', '--dir',
766-
f'{args.singularity_pull_dir}', f'autogenerated/{self.process_name}.cwl']
767-
else:
768-
cmd = ['cwl-docker-extract', '--force-download', f'autogenerated/{self.process_name}.cwl']
769-
sub.run(cmd, check=True)
770-
771-
# If you don't like it, you can programmatically overwrite anything in args
772-
# args.docker_remove_entrypoints = True
773-
if args.docker_remove_entrypoints:
774-
# Requires root, so guard behind CLI option
775-
if args.container_engine == 'docker':
776-
plugins.remove_entrypoints_docker()
777-
if args.container_engine == 'podman':
778-
plugins.remove_entrypoints_podman()
779-
780-
rose_tree = plugins.dockerPull_append_noentrypoint_rosetree(rose_tree)
781-
input_output.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
759+
post_compile.cwl_docker_extract(args, self.process_name)
760+
post_compile.remove_entrypoints(args, rose_tree)
782761

783762
# Do NOT capture stdout and/or stderr and pipe warnings and errors into a black hole.
784-
retval = run_local_module.run_local(args, rose_tree, args.cachedir, 'cwltool', True)
763+
retval = run_local_module.run_local(args, rose_tree, args.cachedir, args.cwl_runner, True)
785764

786765
# Finally, since there is an output file copying bug in cwltool,
787766
# we need to copy the output files manually. See comment above.

src/sophios/main.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from sophios.utils_yaml import wic_loader
1313
from . import input_output as io
14+
from . import post_compile as pc
1415
from . import ast, cli, compiler, inference, inlineing, plugins, run_local, utils # , utils_graphs
1516
from .schemas import wic_schema
1617
from .wic_types import GraphData, GraphReps, Json, StepId, Yaml, YamlTree
@@ -170,11 +171,8 @@ def main() -> None:
170171

171172
io.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
172173

173-
# this has to happen after at least one write
174-
# so we can copy from local cwl_dapters in autogenerated/
175-
if args.inline_cwl_runtag:
176-
rose_tree = plugins.cwl_update_inline_runtag_rosetree(rose_tree, Path('autogenerated/'), True)
177-
io.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
174+
pc.cwl_inline_runtag(args, rose_tree)
175+
io.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
178176

179177
if args.graphviz:
180178
if shutil.which('dot'):
@@ -195,27 +193,8 @@ def main() -> None:
195193
print("but not the graphviz system package.)")
196194

197195
if args.run_local or args.generate_run_script:
198-
# cwl-docker-extract recursively `docker pull`s all images in all subworkflows.
199-
# This is important because cwltool only uses `docker run` when executing
200-
# workflows, and if there is a local image available,
201-
# `docker run` will NOT query the remote repository for the latest image!
202-
# cwltool has a --force-docker-pull option, but this may cause multiple pulls in parallel.
203-
if args.container_engine == 'singularity':
204-
cmd = ['cwl-docker-extract', '-s', '--dir',
205-
f'{args.singularity_pull_dir}', f'autogenerated/{yaml_stem}.cwl']
206-
else:
207-
cmd = ['cwl-docker-extract', '--force-download', f'autogenerated/{yaml_stem}.cwl']
208-
sub.run(cmd, check=True)
209-
210-
if args.docker_remove_entrypoints:
211-
# Requires root, so guard behind CLI option
212-
if args.container_engine == 'docker':
213-
plugins.remove_entrypoints_docker()
214-
if args.container_engine == 'podman':
215-
plugins.remove_entrypoints_podman()
216-
217-
rose_tree = plugins.dockerPull_append_noentrypoint_rosetree(rose_tree)
218-
io.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
196+
pc.cwl_docker_extract(args, yaml_stem)
197+
pc.remove_entrypoints(args, rose_tree)
219198

220199
run_local.run_local(args, rose_tree, args.cachedir, args.cwl_runner, False)
221200

src/sophios/post_compile.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import argparse
2+
from pathlib import Path
3+
import subprocess as sub
4+
5+
from . import plugins
6+
from . import input_output as io
7+
from .wic_types import RoseTree
8+
9+
10+
def cwl_docker_extract(args: argparse.Namespace, file_name: str) -> None:
11+
"""Helper function to do the cwl_docker_extract"""
12+
# cwl-docker-extract recursively `docker pull`s all images in all subworkflows.
13+
# This is important because cwltool only uses `docker run` when executing
14+
# workflows, and if there is a local image available,
15+
# `docker run` will NOT query the remote repository for the latest image!
16+
# cwltool has a --force-docker-pull option, but this may cause multiple pulls in parallel.
17+
if args.container_engine == 'singularity':
18+
cmd = ['cwl-docker-extract', '-s', '--dir',
19+
f'{args.singularity_pull_dir}', f'autogenerated/{file_name}.cwl']
20+
else:
21+
cmd = ['cwl-docker-extract', '--force-download', f'autogenerated/{file_name}.cwl']
22+
sub.run(cmd, check=True)
23+
24+
25+
def cwl_inline_runtag(args: argparse.Namespace, rose_tree: RoseTree) -> None:
26+
"""Transform with cwl inline runtag"""
27+
# this has to happen after at least one write
28+
# so we can copy from local cwl_dapters in autogenerated/
29+
if args.inline_cwl_runtag:
30+
rose_tree = plugins.cwl_update_inline_runtag_rosetree(rose_tree, Path('autogenerated/'), True)
31+
32+
33+
def remove_entrypoints(args: argparse.Namespace, rose_tree: RoseTree) -> None:
34+
"""Remove entry points"""
35+
if args.docker_remove_entrypoints:
36+
# Requires root, so guard behind CLI option
37+
if args.container_engine == 'docker':
38+
plugins.remove_entrypoints_docker()
39+
if args.container_engine == 'podman':
40+
plugins.remove_entrypoints_podman()
41+
42+
rose_tree = plugins.dockerPull_append_noentrypoint_rosetree(rose_tree)
43+
io.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)

tests/test_examples.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from sophios import auto_gen_header
2323
from sophios.cli import get_args
2424
from sophios.utils_yaml import wic_loader
25+
from sophios.post_compile import cwl_docker_extract
2526
from sophios.wic_types import NodeData, StepId, Yaml, YamlTree, Json
2627
from sophios.utils_graphs import get_graph_reps
2728

@@ -212,18 +213,7 @@ def run_workflows(yml_path_str: str, yml_path: Path, cwl_runner: str, args: argp
212213
sophios.input_output.write_to_disk(rose_tree, Path('autogenerated/'), True, args.inputs_file)
213214

214215
if docker_pull_only:
215-
# cwl-docker-extract recursively `docker pull`s all images in all subworkflows.
216-
# This is important because cwltool only uses `docker run` when executing
217-
# workflows, and if there is a local image available,
218-
# `docker run` will NOT query the remote repository for the latest image!
219-
# cwltool has a --force-docker-pull option, but this may cause multiple pulls in parallel.
220-
if args.container_engine == 'singularity':
221-
cmd = ['cwl-docker-extract', '-s', '--dir',
222-
f'{args.singularity_pull_dir}', f'autogenerated/{Path(yml_path).stem}.cwl']
223-
else:
224-
cmd = ['cwl-docker-extract', '--force-download', f'autogenerated/{Path(yml_path).stem}.cwl']
225-
sub.run(cmd, check=True)
226-
216+
cwl_docker_extract(args, Path(yml_path).stem)
227217
return
228218

229219
if args.docker_remove_entrypoints:

0 commit comments

Comments
 (0)