Skip to content

Commit

Permalink
feat: add overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
camilovelezr committed Feb 21, 2024
1 parent 4a0ccb7 commit f4467ab
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/wic/api/pythonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,13 @@ def _yml(self) -> dict:
}
return d

def _save_cwl(self, path: Path) -> None:
def _save_cwl(self, path: Path, overwrite: bool) -> None:
cwl_adapters = path.joinpath("cwl_adapters")
cwl_adapters.mkdir(exist_ok=True, parents=True)
if not overwrite:
if cwl_adapters.joinpath(f"{self.clt_name}.cwl").exists():
return
# does not exist:
with open(
cwl_adapters.joinpath(f"{self.clt_name}.cwl"),
"w",
Expand All @@ -354,6 +358,7 @@ def _save_cwl(self, path: Path) -> None:
class Workflow:
steps: list[Step]
name: str
path: Path = field(default=Path.cwd())
yml_path: Optional[Path] = field(default=None, init=False, repr=False) # pylint: disable=E3701

def __post_init__(self) -> None:
Expand All @@ -364,6 +369,7 @@ def __post_init__(self) -> None:
raise InvalidStepError(
f"{s.clt_name} is missing required inputs"
) from exc
self.path.joinpath(self.name).mkdir(parents=True, exist_ok=True)

def append(self, step_: Step) -> None:
"""Append step to Workflow."""
Expand All @@ -383,25 +389,36 @@ def _save_yaml(self) -> None:
with open(self.yml_path, "w", encoding="utf-8") as file:
file.write(yaml.dump(self.yaml))

def _save_all_cwl(self) -> None:
# copy to wf path, only informational, not used by WIC
with open(self.path.joinpath(self.name, f"{self.name}.yml"), "w", encoding="utf-8") as file:
file.write(yaml.dump(self.yaml))

def _save_all_cwl(self, overwrite: bool) -> None:
"""Save CWL files to cwl_adapters.
This is necessary for WIC to compile the workflow.
"""
_WIC_PATH.mkdir(parents=True, exist_ok=True)
for s in self.steps:
try:
s._save_cwl(_WIC_PATH) # pylint: disable=W0212
s._save_cwl(_WIC_PATH, overwrite) # pylint: disable=W0212
except BaseException as exc:
raise exc

# copy to wf path, only informational, not used by WIC
for s in self.steps:
try:
s._save_cwl(self.path.joinpath(self.name), overwrite) # pylint: disable=W0212
except BaseException as exc:
raise exc

def compile(self, run_local: bool = False) -> Path:
def compile(self, run_local: bool = False, overwrite: bool = False) -> Path:
"""Compile Workflow using WIC.
Returns path to compiled CWL Workflow.
"""

self._save_all_cwl()
self._save_all_cwl(overwrite)
self._save_yaml()
logger.info(f"Compiling {self.name}")
args = ["wic", "--yaml", f"{self.name}.yml"]
Expand All @@ -421,9 +438,12 @@ def compile(self, run_local: bool = False) -> Path:
logger.info(exc.stdout)
raise exc
logger.info(proc.stdout)
return _WIC_PATH.joinpath("autogenerated", f"{self.name}.cwl")
# copy files to wf path
compiled_cwl_path = _WIC_PATH.joinpath("autogenerated", f"{self.name}.cwl")
self.path.joinpath(self.name, f"{self.name}.cwl").symlink_to(compiled_cwl_path)
return compiled_cwl_path

def run(self) -> None:
def run(self, overwrite: bool = False) -> None:
"""Run compiled workflow."""
logger.info(f"Running {self.name}")
self.compile(run_local=True)
self.compile(run_local=True, overwrite=overwrite)

0 comments on commit f4467ab

Please sign in to comment.