Skip to content

Commit 0250406

Browse files
committed
pathmapper: don't use temporary lists
Should speed up items_exclude_children() a bit
1 parent d9dff8c commit 0250406

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

cwltool/pathmapper.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
import stat
55
import urllib
66
import uuid
7-
from pathlib import Path
8-
from typing import Dict, Iterator, List, Optional, Tuple, cast
7+
from typing import (
8+
Dict,
9+
ItemsView,
10+
Iterable,
11+
Iterator,
12+
KeysView,
13+
List,
14+
Optional,
15+
Tuple,
16+
cast,
17+
)
918

1019
from mypy_extensions import mypyc_attr
1120
from schema_salad.exceptions import ValidationException
@@ -210,21 +219,29 @@ def mapper(self, src: str) -> MapperEnt:
210219
return MapperEnt(p.resolved, p.target + src[i:], p.type, p.staged)
211220
return self._pathmap[src]
212221

213-
def files(self) -> List[str]:
214-
return list(self._pathmap.keys())
222+
def files(self) -> KeysView[str]:
223+
"""Return a dictionary keys view of locations."""
224+
return self._pathmap.keys()
215225

216-
def items(self) -> List[Tuple[str, MapperEnt]]:
217-
return list(self._pathmap.items())
226+
def items(self) -> ItemsView[str, MapperEnt]:
227+
"""Return a dictionary items view."""
228+
return self._pathmap.items()
218229

219-
def items_exclude_children(self) -> List[Tuple[str, MapperEnt]]:
230+
def items_exclude_children(self) -> ItemsView[str, MapperEnt]:
231+
"""Return a dictionary items view minus any entries which are children of other entries."""
220232
newitems = {}
221-
keys = [key for key, entry in self.items()]
233+
234+
def parents(path: str) -> Iterable[str]:
235+
result = path
236+
while len(result) > 1:
237+
result = os.path.dirname(result)
238+
yield result
239+
222240
for key, entry in self.items():
223-
parents = Path(key).parents
224-
if any([Path(key_) in parents for key_ in keys]):
241+
if not self.files().isdisjoint(parents(key)):
225242
continue
226243
newitems[key] = entry
227-
return list(newitems.items())
244+
return newitems.items()
228245

229246
def reversemap(
230247
self,

cwltool/process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def stage_files(
242242
"""
243243
items = pathmapper.items() if not symlink else pathmapper.items_exclude_children()
244244
targets: Dict[str, MapperEnt] = {}
245-
for key, entry in items:
245+
for key, entry in list(items):
246246
if "File" not in entry.type:
247247
continue
248248
if entry.target not in targets:
@@ -265,7 +265,7 @@ def stage_files(
265265
)
266266
# refresh the items, since we may have updated the pathmapper due to file name clashes
267267
items = pathmapper.items() if not symlink else pathmapper.items_exclude_children()
268-
for key, entry in items:
268+
for key, entry in list(items):
269269
if not entry.staged:
270270
continue
271271
if not os.path.exists(os.path.dirname(entry.target)):

0 commit comments

Comments
 (0)