6 import cwltool.stdfsaccess
7 from cwltool.pathmapper import abspath
10 import arvados.collection
11 import arvados.arvfile
13 from schema_salad.ref_resolver import DefaultFetcher
15 class CollectionFsAccess(cwltool.stdfsaccess.StdFsAccess):
16 """Implement the cwltool FsAccess interface for Arvados Collections."""
18 def __init__(self, basedir, api_client=None, keep_client=None):
19 super(CollectionFsAccess, self).__init__(basedir)
20 self.api_client = api_client
21 self.keep_client = keep_client
24 def get_collection(self, path):
26 if p[0].startswith("keep:") and arvados.util.keep_locator_pattern.match(p[0][5:]):
28 if pdh not in self.collections:
29 self.collections[pdh] = arvados.collection.CollectionReader(pdh, api_client=self.api_client,
30 keep_client=self.keep_client)
31 return (self.collections[pdh], "/".join(p[1:]))
35 def _match(self, collection, patternsegments, parent):
36 if not patternsegments:
39 if not isinstance(collection, arvados.collection.RichCollectionBase):
43 # iterate over the files and subcollections in 'collection'
44 for filename in collection:
45 if patternsegments[0] == '.':
46 # Pattern contains something like "./foo" so just shift
48 ret.extend(self._match(collection, patternsegments[1:], parent))
49 elif fnmatch.fnmatch(filename, patternsegments[0]):
50 cur = os.path.join(parent, filename)
51 if len(patternsegments) == 1:
54 ret.extend(self._match(collection[filename], patternsegments[1:], cur))
57 def glob(self, pattern):
58 collection, rest = self.get_collection(pattern)
59 if collection and not rest:
61 patternsegments = rest.split("/")
62 return self._match(collection, patternsegments, "keep:" + collection.manifest_locator())
64 def open(self, fn, mode):
65 collection, rest = self.get_collection(fn)
67 return collection.open(rest, mode)
69 return super(CollectionFsAccess, self).open(self._abs(fn), mode)
72 collection, rest = self.get_collection(fn)
74 return collection.exists(rest)
76 return super(CollectionFsAccess, self).exists(fn)
78 def isfile(self, fn): # type: (unicode) -> bool
79 collection, rest = self.get_collection(fn)
82 return isinstance(collection.find(rest), arvados.arvfile.ArvadosFile)
86 return super(CollectionFsAccess, self).isfile(fn)
88 def isdir(self, fn): # type: (unicode) -> bool
89 collection, rest = self.get_collection(fn)
92 return isinstance(collection.find(rest), arvados.collection.RichCollectionBase)
96 return super(CollectionFsAccess, self).isdir(fn)
98 def listdir(self, fn): # type: (unicode) -> List[unicode]
99 collection, rest = self.get_collection(fn)
102 dir = collection.find(rest)
106 raise IOError(errno.ENOENT, "Directory '%s' in '%s' not found" % (rest, collection.portable_data_hash()))
107 if not isinstance(dir, arvados.collection.RichCollectionBase):
108 raise IOError(errno.ENOENT, "Path '%s' in '%s' is not a Directory" % (rest, collection.portable_data_hash()))
109 return [abspath(l, fn) for l in dir.keys()]
111 return super(CollectionFsAccess, self).listdir(fn)
113 def join(self, path, *paths): # type: (unicode, *unicode) -> unicode
114 if paths and paths[-1].startswith("keep:") and arvados.util.keep_locator_pattern.match(paths[-1][5:]):
116 return os.path.join(path, *paths)
118 def realpath(self, path):
119 if path.startswith("$(task.tmpdir)") or path.startswith("$(task.outdir)"):
121 collection, rest = self.get_collection(path)
125 return os.path.realpath(path)
127 class CollectionFetcher(DefaultFetcher):
128 def __init__(self, cache, session, api_client=None, keep_client=None):
129 super(CollectionFetcher, self).__init__(cache, session)
130 self.fsaccess = CollectionFsAccess("", api_client=api_client, keep_client=keep_client)
132 def fetch_text(self, url):
133 if url.startswith("keep:"):
134 with self.fsaccess.open(url) as f:
136 return super(CollectionFetcher, self).fetch_text(url)
138 def check_exists(self, url):
139 if url.startswith("keep:"):
140 return self.fsaccess.exists(url)
141 return super(CollectionFetcher, self).check_exists(url)
143 def urljoin(self, base_url, url):
147 urlsp = urlparse.urlsplit(url)
151 basesp = urlparse.urlsplit(base_url)
152 if basesp.scheme == "keep":
154 raise IOError(errno.EINVAL, "Invalid Keep locator", base_url)
156 baseparts = basesp.path.split("/")
157 urlparts = urlsp.path.split("/")
159 pdh = baseparts.pop(0)
161 if not arvados.util.keep_locator_pattern.match(pdh):
162 raise IOError(errno.EINVAL, "Invalid Keep locator", base_url)
164 if urlsp.path.startswith("/"):
167 if baseparts and urlparts:
170 path = "/".join([pdh] + baseparts + urlparts)
171 return urlparse.urlunsplit(("keep", "", path, "", urlsp.fragment))
173 return super(CollectionFetcher, self).urljoin(base_url, url)