Skip to content

Commit

Permalink
CLI directory input
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin committed Sep 4, 2020
1 parent 2e4ee08 commit d2e4327
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
16 changes: 15 additions & 1 deletion WDL/CLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,20 @@ def runner_input_value(s_value, ty, file_found, root):
elif not (file_found and file_found(fn)): # maybe URI
raise Error.InputError("File not found: " + fn)
return Value.File(fn)
if isinstance(ty, Type.Directory):
dn = os.path.expanduser(s_value)
if os.path.isdir(dn):
dn = os.path.abspath(dn)
if not path_really_within(dn, root):
raise Error.InputError(
f"all input paths must be located within the configured `file_io.root' directory `{root}' "
f"unlike `{dn}'"
)
# TODO: courtesy check for symlinks that have absolute paths or relatively point
# outside the directory
else: # TODO: relax for URIs
raise Error.InputError("Directory not found: " + dn)
return Value.Directory(dn)
if isinstance(ty, Type.Boolean):
if s_value == "true":
return Value.Boolean(True)
Expand Down Expand Up @@ -1241,7 +1255,7 @@ def localize(
doc = load(wdlfile, path or [], check_quant=check_quant, read_source=read_source)

def file_found(fn):
return runtime.download.able(cfg, fn) or os.path.isfile(fn)
return runtime.download.able(cfg, fn) or os.path.exists(fn)

try:
target, input_env, input_json = runner_input(
Expand Down
6 changes: 1 addition & 5 deletions WDL/Value.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ class Directory(String):

def coerce(self, desired_type: Optional[Type.Base] = None) -> Base:
""
if self.value is None:
if isinstance(desired_type, Type.Directory) and desired_type.optional:
return Null(self.expr)
else:
raise FileNotFoundError()
# TODO: similar coercion logic for Directory? outputs when we support those
return super().coerce(desired_type)


Expand Down
37 changes: 36 additions & 1 deletion tests/runner.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ source tests/bash-tap/bash-tap-bootstrap
export PYTHONPATH="$SOURCE_DIR:$PYTHONPATH"
miniwdl="python3 -m WDL"

plan tests 53
plan tests 55

$miniwdl run_self_test
is "$?" "0" "run_self_test"
Expand Down Expand Up @@ -246,6 +246,41 @@ $miniwdl run --copy-input-files mv_input_file.wdl file=quick
is "$?" "0" "copy input files"
is "$(basename `jq -r '.["mv_input_file.xxx"]' _LAST/outputs.json`)" "xxx" "updated _LAST"

cat << 'EOF' > dir_io.wdl
version development
workflow w {
input {
Directory d
}
call t {
input:
d = d
}
output {
Int dsz = round(size(t.files))
}
}
task t {
input {
Directory d
}
command <<<
mkdir outdir
find ~{d} -type f | xargs -i{} cp {} outdir/
>>>
output {
Array[File] files = glob("outdir/*")
}
}
EOF

mkdir -p indir/subdir
echo alice > indir/alice.txt
echo bob > indir/subdir/bob.txt
miniwdl run dir_io.wdl d=indir
is "$?" "0" "directory input"
is `jq -r '.["w.dsz"]' _LAST/outputs.json` "10" "use of directory input"

cat << 'EOF' > uri_inputs.json
{"my_workflow.files": ["https://google.com/robots.txt", "https://raw.githubusercontent.com/chanzuckerberg/miniwdl/main/tests/alyssa_ben.txt"]}
EOF
Expand Down
6 changes: 6 additions & 0 deletions tests/test_7runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def _run(self, wdl:str, inputs = None, expected_exception: Exception = None, cfg
return WDL.values_to_json(outputs)

class TestDirectoryIO(RunnerTestCase):
def test_coercion(self):
assert WDL.Type.Directory().coerces(WDL.Type.String())
d = WDL.Value.String("foo").coerce(WDL.Type.Directory())
assert isinstance(d, WDL.Value.Directory)
assert d.value == "foo"

def test_basic_directory(self):
wdl = R"""
version development
Expand Down

0 comments on commit d2e4327

Please sign in to comment.