2.14. 実行時にファイルを作成する#
コマンドラインパラメーターではなく、ファイルから入力設定を読み込むことを期待するツールや、小さなラッパーシェルスクリプトが必要な場合など、入力パラメーターからその場でファイルを作成する必要があることがあります。
InitialWorkDirRequirement
を使ってそのうようなファイルを生成します。
createfile.cwl
##!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: ["sh", "example.sh"]
requirements:
InitialWorkDirRequirement:
listing:
- entryname: example.sh
entry: |-
PREFIX='Message is:'
MSG="\${PREFIX} $(inputs.message)"
echo \${MSG}
inputs:
message: string
outputs:
example_out:
type: stdout
stdout: output.txt
$(inputs.message)
のような Expressionsは、ファイルを作成する前にCWL実行エンジンによって展開されます。ここでは、入力パラメータmessage
に値を挿入します。
Tip
CWL expressions は、コマンドラインツール起動時に使用される_shell variables_ とは無関係です。つまり、$
という文字が本当に必要な場合は、\
でエスケープする必要があります。例えば,上記の\${PREFIX}
は,CWLエンジンの代わりにシェルスクリプトで評価されるように,生成されたファイルでは${PREFIX}
に展開されます。
上記のCWLツールをテストするために、このジョブを使用して、入力値message
を与えます:
echo-job.yml
#message: Hello world!
これを実行する前に、各ステップをもう少し詳しく見てみましょう。ベースコマンドbaseCommand: ["sh", "example.sh"]
は、sh example.sh
というコマンドを実行します。これで、作成したファイルがシェルで実行されます。
InitialWorkDirRequirement
はlisting
を必要とします。listing
は YAML 配列なので、配列の各要素の行の最初に-
が必要です。この場合、要素は 1 つだけです。entryname:
は任意の値を指定できますが、baseCommand
で指定したものと一致していなければなりません。最後の部分はentry:
で、この後に|-
が続きます。これは YAML の引用構文で、複数行の文字列を使用していることを意味します (これがなければ、スクリプト全体を 1 行で記述する必要があります).
注釈
フォーマットについては、YAMLガイドを参照してください。
ここで、コマンドラインにツール定義と入力オブジェクトを指定して、cwltool
を起動します:
$ cwltool createfile.cwl echo-job.yml
INFO /opt/hostedtoolcache/Python/3.9.19/x64/bin/cwltool 3.1.20240508115724
INFO Resolved 'createfile.cwl' to 'file:///home/runner/work/user_guide/user_guide/src/_includes/cwl/creating-files-at-runtime/createfile.cwl'
INFO [job createfile.cwl] /tmp/9ukqd8ut$ sh \
example.sh > /tmp/9ukqd8ut/output.txt
INFO [job createfile.cwl] completed success
{
"example_out": {
"location": "file:///home/runner/work/user_guide/user_guide/src/_includes/cwl/creating-files-at-runtime/output.txt",
"basename": "output.txt",
"class": "File",
"checksum": "sha1$9045abe4bd04dd8ccfe50c6ff61820b784b64aa7",
"size": 25,
"path": "/home/runner/work/user_guide/user_guide/src/_includes/cwl/creating-files-at-runtime/output.txt"
}
}INFO Final process status is success
$ cat output.txt
Message is: Hello world!