common-workflow-language / cwltool

Common Workflow Language reference implementation
https://cwltool.readthedocs.io/
Apache License 2.0
335 stars 231 forks source link

Unable to rename file inside directory by altering the basename #1254

Open maitai opened 4 years ago

maitai commented 4 years ago

Description

As suggested in https://www.biostars.org/p/384082/ files can be renamed inside a CWL ExpressionTool by altering their basename property. This seems to work well for a simple File input but appears to not be working (anymore) for the contents of the listing of a simple Directory input.

Environment

How to reproduce

Prerequisites

Move to an empty directory of your choice and execute:

mkdir myDir output
touch myDir/oldname.dat

cat << "EOF" > renameFile.cwl
cwlVersion: v1.0
class: ExpressionTool
requirements:
- class: InlineJavascriptRequirement
inputs:
  oldFile: File
outputs:
  newFile: File
expression: |
  ${
    inputs.oldFile.basename = "newname.dat";
    return { "newFile": inputs.oldFile };
  }
EOF

cat << "EOF" > renameFirstFileInDir.cwl
cwlVersion: v1.0
class: ExpressionTool
requirements:
- class: InlineJavascriptRequirement
inputs:
  oldDir: Directory
outputs:
  newDir: Directory
expression: |
  ${
    inputs.oldDir.listing[0].basename = "newname.dat";
    return { "newDir": inputs.oldDir };
  }
EOF

cd output

The directory layout should now look like this:

..
├── myOldDir
│   └── oldname.dat
├── output                    <- current working directory
├── renameFile.cwl
└── renameFirstFileInDir.cwl

Rename plain File

cwltool ../renameFile.cwl --oldFile ../myDir/oldname.dat

results in the expected outcome:

../output/
└── newname.dat

Works. Great!

Cleanup:

rm newname.dat

Rename File inside a Directory

cwltool --debug ../renameFirstFileInDir.cwl --oldDir ../myDir/

results in the unexpected outcome:

../output/
└── myDir
    └── oldname.dat       <-- should now be named "newname.dat"

Does not work. File has not been renamed. Therefore execution also throws an error:

ERROR Unhandled error:
  [Errno 2] No such file or directory: '/[REDACTED]/output/myDir/newname.dat'
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/cwltool/main.py", line 1099, in main
    tool, initialized_job_order_object, runtimeContext, logger=_logger
  File "/usr/local/lib/python3.6/dist-packages/cwltool/executors.py", line 43, in __call__
    return self.execute(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/cwltool/executors.py", line 132, in execute
    path_mapper=runtime_context.path_mapper,
  File "/usr/local/lib/python3.6/dist-packages/cwltool/process.py", line 411, in relocateOutputs
    outputObj, ("File",), functools.partial(compute_checksums, fs_access)
  File "/usr/local/lib/python3.6/dist-packages/cwltool/utils.py", line 231, in visit_class
    visit_class(rec[d], cls, op)
  File "/usr/local/lib/python3.6/dist-packages/cwltool/utils.py", line 231, in visit_class
    visit_class(rec[d], cls, op)
  File "/usr/local/lib/python3.6/dist-packages/cwltool/utils.py", line 234, in visit_class
    visit_class(d, cls, op)
  File "/usr/local/lib/python3.6/dist-packages/cwltool/utils.py", line 229, in visit_class
    op(rec)
  File "/usr/local/lib/python3.6/dist-packages/cwltool/process.py", line 1207, in compute_checksums
    with fs_access.open(fileobj["location"], "rb") as f:
  File "/usr/local/lib/python3.6/dist-packages/cwltool/stdfsaccess.py", line 41, in open
    return open(self._abs(fn), mode)
FileNotFoundError: [Errno 2] No such file or directory: '/[REDACTED]/output/myDir/newname.dat'
maitai commented 4 years ago

The issue above is also reproducible using the docker image commonworkflowlanguage/cwltool:latest:

docker run --rm -v "$PWD":"$PWD" -w="$PWD" -v /var/run/docker.sock:/var/run/docker.sock -ti commonworkflowlanguage/cwltool:latest --outdir output/ renameFirstFileInDir.cwl --oldDir myDir

INFO /usr/local/bin/cwltool 2.0.20200107113851
INFO Resolved 'renameFirstFileInDir.cwl' to 'file://[REDACTED]/renameFirstFileInDir.cwl'
ERROR Unhandled error, try again with --debug for more information:
  [Errno 2] No such file or directory: '[REDACTED]/output/myDir/newname.dat'

I tried almost all of the Docker image tags in that repo but even old versions trigger the error which is why I wonder how I have been able to use this in the first place and if @mr-c's convert-to-v3-layout.cwl is now broken too.

kunal12298 commented 4 years ago

Hey, looking forward to contribute for this project for GSOC, can you let me know how to get started?