Skip to content

Commit 0b9086e

Browse files
authored
Strip trailing slash (#368)
* Strip trailing slashes on path component of location URIs.
1 parent bc9cc74 commit 0b9086e

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

cwltool/pathmapper.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,25 @@ def addLocation(d):
6969
"Anonymous directory object must have 'listing' and 'basename' fields.")
7070
d["location"] = "_:" + Text(uuid.uuid4())
7171
if "basename" not in d:
72-
d["basename"] = Text(uuid.uuid4())
72+
d["basename"] = d["location"][2:]
73+
74+
parse = urllib.parse.urlparse(d["location"])
75+
path = parse.path
76+
# strip trailing slash
77+
if path.endswith("/"):
78+
if d["class"] != "Directory":
79+
raise validate.ValidationException(
80+
"location '%s' ends with '/' but is not a Directory" % d["location"])
81+
path = path.rstrip("/")
82+
d["location"] = urllib.parse.urlunparse((parse.scheme, parse.netloc, path, parse.params, parse.query, parse.fragment))
7383

7484
if "basename" not in d:
75-
parse = urllib.parse.urlparse(d["location"])
76-
d["basename"] = os.path.basename(urllib.request.url2pathname(parse.path))
85+
d["basename"] = os.path.basename(urllib.request.url2pathname(path))
7786

7887
adjustFileObjs(job, addLocation)
7988
adjustDirObjs(job, addLocation)
8089

8190

82-
83-
8491
def dedup(listing): # type: (List[Any]) -> List[Any]
8592
marksub = set()
8693

tests/test_pathmapper.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from cwltool.pathmapper import PathMapper
3+
from cwltool.pathmapper import PathMapper, normalizeFilesDirs
44

55

66
class TestPathMapper(unittest.TestCase):
@@ -12,3 +12,17 @@ def __init__(self, referenced_files, basedir, stagedir, new):
1212

1313
a = SubPathMapper([], '', '', "new")
1414
self.assertTrue(a.new, "new")
15+
16+
def test_strip_trailing(self):
17+
d = {
18+
"class": "Directory",
19+
"location": "/foo/bar/"
20+
}
21+
normalizeFilesDirs(d)
22+
self.assertEqual(
23+
{
24+
"class": "Directory",
25+
"location": "/foo/bar",
26+
"basename": "bar"
27+
},
28+
d)

0 commit comments

Comments
 (0)