Skip to content

Commit 08a9cc2

Browse files
committed
fix compute_checksums for literal files
1 parent d2059c7 commit 08a9cc2

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

cwltool/command_line_tool.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ def calc_checksum(location: str) -> Optional[str]:
866866
and "checksum" in e
867867
and e["checksum"] != "sha1$hash"
868868
):
869-
return cast(Optional[str], e["checksum"])
869+
return cast(str, e["checksum"])
870870
return None
871871

872872
def remove_prefix(s: str, prefix: str) -> str:

cwltool/process.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,15 @@ def compute_checksums(fs_access: StdFsAccess, fileobj: CWLObjectType) -> None:
13421342
if "checksum" not in fileobj:
13431343
checksum = hashlib.sha1() # nosec
13441344
location = cast(str, fileobj["location"])
1345-
with fs_access.open(location, "rb") as f:
1346-
contents = f.read(1024 * 1024)
1347-
while contents != b"":
1348-
checksum.update(contents)
1345+
if "contents" in fileobj:
1346+
contents = cast(str, fileobj["contents"]).encode("utf-8")
1347+
checksum.update(contents)
1348+
fileobj["size"] = len(contents)
1349+
else:
1350+
with fs_access.open(location, "rb") as f:
13491351
contents = f.read(1024 * 1024)
1352+
while contents != b"":
1353+
checksum.update(contents)
1354+
contents = f.read(1024 * 1024)
1355+
fileobj["size"] = fs_access.size(location)
13501356
fileobj["checksum"] = "sha1$%s" % checksum.hexdigest()
1351-
fileobj["size"] = fs_access.size(location)

tests/test_examples.py

+22
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,28 @@ def test_cache_relative_paths(tmp_path: Path, factor: str) -> None:
13251325
assert (tmp_path / "cwltool_cache" / "27903451fc1ee10c148a0bdeb845b2cf").exists()
13261326

13271327

1328+
@pytest.mark.parametrize("factor", test_factors)
1329+
def test_cache_default_literal_file(tmp_path: Path, factor: str) -> None:
1330+
"""Confirm that running a CLT with a default literal file with caching succeeds."""
1331+
test_file = "tests/wf/extract_region_specs.cwl"
1332+
cache_dir = str(tmp_path / "cwltool_cache")
1333+
commands = factor.split()
1334+
commands.extend(
1335+
[
1336+
"--out",
1337+
str(tmp_path / "out"),
1338+
"--cachedir",
1339+
cache_dir,
1340+
get_data(test_file),
1341+
]
1342+
)
1343+
error_code, _, stderr = get_main_output(commands)
1344+
1345+
stderr = re.sub(r"\s\s+", " ", stderr)
1346+
assert "completed success" in stderr
1347+
assert error_code == 0
1348+
1349+
13281350
def test_write_summary(tmp_path: Path) -> None:
13291351
"""Test --write-summary."""
13301352
commands = [

tests/wf/extract_region_specs.cwl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"cwlVersion": "v1.0",
3+
"class": "CommandLineTool",
4+
"inputs": [
5+
{
6+
"type": "File",
7+
"default": {
8+
"class": "File",
9+
"basename": "extract_regions.py",
10+
"contents": "#!/usr/bin/env python3\n\nfrom __future__ import print_function, division\nimport sys\n\ninput_filename = sys.argv[1]\nif len(sys.argv) == 3:\n fuzz = int(sys.argv[2])\nelse:\n fuzz = 0\ninput_file = open(input_filename)\n\ncount = 0\nfor line in input_file:\n if not line.startswith(\">\"):\n continue\n count += 1\n contig_regions_file = open(\"contig_regions{}.txt\".format(count), \"w\")\n proteins_list_file = open(\"proteins{}.txt\".format(count), \"w\")\n fields = line.split(\"|\")\n protein_id = fields[0][1:]\n contig_id = fields[1]\n r_start = int(fields[6])\n if r_start > fuzz:\n r_start = r_start - fuzz\n r_end = int(fields[7]) + fuzz\n print(\"{}:{}-{}\".format(contig_id, r_start, r_end), file=contig_regions_file)\n print(protein_id, file=proteins_list_file)\n contig_regions_file.close()\n proteins_list_file.close()\n"
11+
},
12+
"inputBinding": {
13+
"position": 1
14+
},
15+
"id": "scripts"
16+
}
17+
],
18+
"outputs": [
19+
],
20+
"baseCommand": "cat"
21+
}

0 commit comments

Comments
 (0)