Skip to content

Commit

Permalink
Fixed formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kvg committed Jul 27, 2023
1 parent 61a6d71 commit 7c27e80
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
14 changes: 11 additions & 3 deletions src/cromshell/submit/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import csv
import json
import logging
import tempfile
from datetime import datetime
from pathlib import Path, PurePath
import tempfile

import click
import requests
Expand Down Expand Up @@ -65,15 +65,23 @@ class WorkflowStatusError(Exception):
help=".",
)
@click.pass_obj
def main(config, wdl, wdl_json, options_json, dependencies_zip, no_validation, do_not_flatten_wdls):
def main(
config,
wdl,
wdl_json,
options_json,
dependencies_zip,
no_validation,
do_not_flatten_wdls,
):
"""Submit a workflow and arguments to the Cromwell Server"""

LOGGER.info("submit")

http_utils.assert_can_communicate_with_server(config=config)

if not do_not_flatten_wdls and io_utils.has_nested_dependencies(wdl):
tempdir = tempfile.TemporaryDirectory(prefix='cromshell_')
tempdir = tempfile.TemporaryDirectory(prefix="cromshell_")

LOGGER.info(f"Flattening WDL structure to {tempdir.name}.")

Expand Down
64 changes: 41 additions & 23 deletions src/cromshell/utilities/io_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import json
import logging
import re
#import os

# import os
import shutil
import tempfile
from contextlib import nullcontext
from io import BytesIO
from pathlib import Path
import tempfile
from typing import BinaryIO, List, Union, Tuple
from typing import BinaryIO, List, Union
from zipfile import ZIP_DEFLATED, ZipFile

from pygments import formatters, highlight, lexers
Expand Down Expand Up @@ -106,30 +107,41 @@ def assert_path_is_not_empty(path: Union[str, Path], description: str) -> None:
def has_nested_dependencies(wdl_path: str or Path) -> bool:
"""Determine if a WDL has any nested imports."""

with open(wdl_path, 'r') as rf:
for l in rf:
if l.startswith('import'):
m = re.match(r'import "(.+)"', l)
with open(wdl_path, "r") as rf:
for line in rf:
if line.startswith("import"):
m = re.match(r'import "(.+)"', line)

if "../" in m.group(1):
imported_wdl_name = m.group(1)
if "../" in imported_wdl_name:
return True

return False


def get_flattened_filename(tempdir: tempfile.TemporaryDirectory, wdl_path: str or Path) -> Path:
""" Generate hyphen-separated path to use for flattened WDL file path.
For example:
def get_flattened_filename(
tempdir: tempfile.TemporaryDirectory, wdl_path: str or Path
) -> Path:
"""Generate hyphen-separated path to use for flattened WDL file path.
For example:
tempdir: /path/2/tempdir/ and wdl_path: /dir/path/2/wdl.wdl
returns: /path/2/tempdir/dir-path-2-wdl.wdl
"""

p = Path(wdl_path)

return Path(tempdir.name + "/" + re.sub("^-", "", re.sub("/", "-", str(p.parent))) + '-' + str(p.name))
return Path(
tempdir.name
+ "/"
+ re.sub("^-", "", re.sub("/", "-", str(p.parent)))
+ "-"
+ str(p.name)
)


def flatten_nested_dependencies(tempdir: tempfile.TemporaryDirectory, wdl_path: str or Path) -> Path:
def flatten_nested_dependencies(
tempdir: tempfile.TemporaryDirectory, wdl_path: str or Path
) -> Path:
"""Flatten a WDL directory structure and rewrite imports accordingly.
Return string representing the filesystem location of the rewritten WDL.
Expand All @@ -140,22 +152,28 @@ def flatten_nested_dependencies(tempdir: tempfile.TemporaryDirectory, wdl_path:

new_wdl_path = get_flattened_filename(tempdir, wdl_path)

with open(wdl_path, 'r') as rf, open(new_wdl_path, 'w') as wf:
for l in rf:
if l.startswith('import'):
m = re.match(r'import "(.+)"', l)
with open(wdl_path, "r") as rf, open(new_wdl_path, "w") as wf:
for line in rf:
if line.startswith("import"):
m = re.match(r'import "(.+)"', line)
imported_wdl_name = m.group(1)
imported_wdl_path = (Path(wdl_dir) / m.group(1)).absolute()
import_line = re.sub(m.group(1), Path(get_flattened_filename(tempdir, imported_wdl_path)).name, l)

if ' as ' in l:
imported_wdl_path = (Path(wdl_dir) / imported_wdl_name).absolute()
import_line = re.sub(
imported_wdl_name,
Path(get_flattened_filename(tempdir, imported_wdl_path)).name,
line,
)

if " as " in line:
wf.write(import_line)
else:
wf.write(f'{import_line.strip()} as {re.sub(".wdl", "", Path(imported_wdl_path).name)}\n')
wf.write(
f'{import_line.strip()} as {re.sub(".wdl", "", Path(imported_wdl_path).name)}\n'
)

flatten_nested_dependencies(tempdir, imported_wdl_path)
else:
wf.write(l)
wf.write(line)

return new_wdl_path

Expand Down

0 comments on commit 7c27e80

Please sign in to comment.