7 import arvados.collection
9 class CollectionFsAccess(cwltool.process.StdFsAccess):
10 """Implement the cwltool FsAccess interface for Arvados Collections."""
12 def __init__(self, basedir):
13 super(CollectionFsAccess, self).__init__(basedir)
16 def get_collection(self, path):
18 if p[0].startswith("keep:") and arvados.util.keep_locator_pattern.match(p[0][5:]):
20 if pdh not in self.collections:
21 self.collections[pdh] = arvados.collection.CollectionReader(pdh)
22 return (self.collections[pdh], "/".join(p[1:]))
26 def _match(self, collection, patternsegments, parent):
27 if not patternsegments:
30 if not isinstance(collection, arvados.collection.RichCollectionBase):
34 # iterate over the files and subcollections in 'collection'
35 for filename in collection:
36 if patternsegments[0] == '.':
37 # Pattern contains something like "./foo" so just shift
39 ret.extend(self._match(collection, patternsegments[1:], parent))
40 elif fnmatch.fnmatch(filename, patternsegments[0]):
41 cur = os.path.join(parent, filename)
42 if len(patternsegments) == 1:
45 ret.extend(self._match(collection[filename], patternsegments[1:], cur))
48 def glob(self, pattern):
49 collection, rest = self.get_collection(pattern)
50 patternsegments = rest.split("/")
51 return self._match(collection, patternsegments, "keep:" + collection.manifest_locator())
53 def open(self, fn, mode):
54 collection, rest = self.get_collection(fn)
56 return collection.open(rest, mode)
58 return open(self._abs(fn), mode)
61 collection, rest = self.get_collection(fn)
63 return collection.exists(rest)
65 return os.path.exists(self._abs(fn))