5 from cwltool.pathmapper import abspath
8 import arvados.collection
11 class CollectionFsAccess(cwltool.process.StdFsAccess):
12 """Implement the cwltool FsAccess interface for Arvados Collections."""
14 def __init__(self, basedir, api_client=None):
15 super(CollectionFsAccess, self).__init__(basedir)
16 self.api_client = api_client
19 def get_collection(self, path):
21 if p[0].startswith("keep:") and arvados.util.keep_locator_pattern.match(p[0][5:]):
23 if pdh not in self.collections:
24 self.collections[pdh] = arvados.collection.CollectionReader(pdh, api_client=self.api_client)
25 return (self.collections[pdh], "/".join(p[1:]))
29 def _match(self, collection, patternsegments, parent):
30 if not patternsegments:
33 if not isinstance(collection, arvados.collection.RichCollectionBase):
37 # iterate over the files and subcollections in 'collection'
38 for filename in collection:
39 if patternsegments[0] == '.':
40 # Pattern contains something like "./foo" so just shift
42 ret.extend(self._match(collection, patternsegments[1:], parent))
43 elif fnmatch.fnmatch(filename, patternsegments[0]):
44 cur = os.path.join(parent, filename)
45 if len(patternsegments) == 1:
48 ret.extend(self._match(collection[filename], patternsegments[1:], cur))
51 def glob(self, pattern):
52 collection, rest = self.get_collection(pattern)
53 if collection and not rest:
55 patternsegments = rest.split("/")
56 return self._match(collection, patternsegments, "keep:" + collection.manifest_locator())
58 def open(self, fn, mode):
59 collection, rest = self.get_collection(fn)
61 return collection.open(rest, mode)
63 return super(CollectionFsAccess, self).open(self._abs(fn), mode)
66 collection, rest = self.get_collection(fn)
68 return collection.exists(rest)
70 return super(CollectionFsAccess, self).exists(fn)
72 def isfile(self, fn): # type: (unicode) -> bool
73 collection, rest = self.get_collection(fn)
76 return isinstance(collection.find(rest), arvados.arvfile.ArvadosFile)
80 return super(CollectionFsAccess, self).isfile(fn)
82 def isdir(self, fn): # type: (unicode) -> bool
83 collection, rest = self.get_collection(fn)
86 return isinstance(collection.find(rest), arvados.collection.Collection)
90 return super(CollectionFsAccess, self).isdir(fn)
92 def listdir(self, fn): # type: (unicode) -> List[unicode]
93 collection, rest = self.get_collection(fn)
95 dir = collection.find(rest)
99 return [abspath(l, fn) for l in dir.keys()]
101 return super(CollectionFsAccess, self).listdir(fn)
103 def join(self, path, *paths): # type: (unicode, *unicode) -> unicode
104 if paths and paths[-1].startswith("keep:") and arvados.util.keep_locator_pattern.match(paths[-1][5:]):
106 return os.path.join(path, *paths)