From c0e6f1f37ed16e16e73ffb0090e6f56bd362253e Mon Sep 17 00:00:00 2001 From: "Michael R. Crusoe" Date: Fri, 1 Dec 2023 16:23:48 +0100 Subject: [PATCH] pathmapper: don't use temporary lists Should speed up items_exclude_children() a bit --- cwltool/pathmapper.py | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/cwltool/pathmapper.py b/cwltool/pathmapper.py index 660b5ddb1d..7b57f46e0a 100644 --- a/cwltool/pathmapper.py +++ b/cwltool/pathmapper.py @@ -4,8 +4,17 @@ import stat import urllib import uuid -from pathlib import Path -from typing import Dict, Iterator, List, Optional, Tuple, cast +from typing import ( + Dict, + ItemsView, + Iterable, + Iterator, + KeysView, + List, + Optional, + Tuple, + cast, +) from mypy_extensions import mypyc_attr from schema_salad.exceptions import ValidationException @@ -210,21 +219,30 @@ def mapper(self, src: str) -> MapperEnt: return MapperEnt(p.resolved, p.target + src[i:], p.type, p.staged) return self._pathmap[src] - def files(self) -> List[str]: - return list(self._pathmap.keys()) + def files(self) -> KeysView[str]: + """Return a dictionary keys view of locations.""" + return self._pathmap.keys() - def items(self) -> List[Tuple[str, MapperEnt]]: - return list(self._pathmap.items()) + def items(self) -> ItemsView[str, MapperEnt]: + """Return a dictionary items view.""" + return self._pathmap.items() - def items_exclude_children(self) -> List[Tuple[str, MapperEnt]]: + def items_exclude_children(self) -> ItemsView[str, MapperEnt]: + """Return a dictionary items view minus any entries which are childern of other entries.""" newitems = {} - keys = [key for key, entry in self.items()] + keys = self.files() + + def parents(path: str) -> Iterable[str]: + result = key + while result != "": + result = os.path.dirname(result) + yield result + for key, entry in self.items(): - parents = Path(key).parents - if any([Path(key_) in parents for key_ in keys]): + if not keys.isdisjoint(parents(key)): continue newitems[key] = entry - return list(newitems.items()) + return newitems.items() def reversemap( self,