26 def normalize_stream(s, stream):
28 sortedfiles = list(stream.keys())
35 if b[arvados.LOCATOR] not in blocks:
36 stream_tokens.append(b[arvados.LOCATOR])
37 blocks[b[arvados.LOCATOR]] = streamoffset
38 streamoffset += b[arvados.BLOCKSIZE]
42 fout = f.replace(' ', '\\040')
43 for segment in stream[f]:
44 segmentoffset = blocks[segment[arvados.LOCATOR]] + segment[arvados.OFFSET]
45 if current_span == None:
46 current_span = [segmentoffset, segmentoffset + segment[arvados.SEGMENTSIZE]]
48 if segmentoffset == current_span[1]:
49 current_span[1] += segment[arvados.SEGMENTSIZE]
51 stream_tokens.append("{0}:{1}:{2}".format(current_span[0], current_span[1] - current_span[0], fout))
52 current_span = [segmentoffset, segmentoffset + segment[arvados.SEGMENTSIZE]]
54 if current_span != None:
55 stream_tokens.append("{0}:{1}:{2}".format(current_span[0], current_span[1] - current_span[0], fout))
57 if len(stream[f]) == 0:
58 stream_tokens.append("0:0:{0}".format(fout))
62 def normalize(collection):
64 for s in collection.all_streams():
65 for f in s.all_files():
66 filestream = s.name() + "/" + f.name()
67 r = filestream.rindex("/")
68 streamname = filestream[:r]
69 filename = filestream[r+1:]
70 if streamname not in streams:
71 streams[streamname] = {}
72 if filename not in streams[streamname]:
73 streams[streamname][filename] = []
75 streams[streamname][filename].extend(s.locators_and_ranges(r[0], r[1]))
77 normalized_streams = []
78 sortedstreams = list(streams.keys())
80 for s in sortedstreams:
81 normalized_streams.append(normalize_stream(s, streams[s]))
82 return normalized_streams
85 class CollectionReader(object):
86 def __init__(self, manifest_locator_or_text):
87 if re.search(r'^[a-f0-9]{32}\+\d+(\+\S)*$', manifest_locator_or_text):
88 self._manifest_locator = manifest_locator_or_text
89 self._manifest_text = None
91 self._manifest_text = manifest_locator_or_text
92 self._manifest_locator = None
102 if self._streams != None:
104 if not self._manifest_text:
106 c = arvados.api('v1').collections().get(
107 uuid=self._manifest_locator).execute()
108 self._manifest_text = c['manifest_text']
109 except Exception as e:
110 logging.warning("API lookup failed for collection %s (%s: %s)" %
111 (self._manifest_locator, type(e), str(e)))
112 self._manifest_text = Keep.get(self._manifest_locator)
114 for stream_line in self._manifest_text.split("\n"):
115 if stream_line != '':
116 stream_tokens = stream_line.split()
117 self._streams += [stream_tokens]
118 self._streams = normalize(self)
120 # now regenerate the manifest text based on the normalized stream
122 #print "normalizing", self._manifest_text
123 self._manifest_text = ''.join([StreamReader(stream).manifest_text() for stream in self._streams])
124 #print "result", self._manifest_text
127 def all_streams(self):
130 for s in self._streams:
131 resp.append(StreamReader(s))
135 for s in self.all_streams():
136 for f in s.all_files():
139 def manifest_text(self):
141 return self._manifest_text
143 class CollectionWriter(object):
144 KEEP_BLOCK_SIZE = 2**26
147 self._data_buffer = []
148 self._data_buffer_len = 0
149 self._current_stream_files = []
150 self._current_stream_length = 0
151 self._current_stream_locators = []
152 self._current_stream_name = '.'
153 self._current_file_name = None
154 self._current_file_pos = 0
155 self._finished_streams = []
163 def write_directory_tree(self,
164 path, stream_name='.', max_manifest_depth=-1):
165 self.start_new_stream(stream_name)
167 if max_manifest_depth == 0:
168 dirents = sorted(util.listdir_recursive(path))
170 dirents = sorted(os.listdir(path))
171 for dirent in dirents:
172 target = os.path.join(path, dirent)
173 if os.path.isdir(target):
175 os.path.join(stream_name, dirent),
176 max_manifest_depth-1]]
178 self.start_new_file(dirent)
179 with open(target, 'rb') as f:
185 self.finish_current_stream()
186 map(lambda x: self.write_directory_tree(*x), todo)
188 def write(self, newdata):
189 if hasattr(newdata, '__iter__'):
193 self._data_buffer += [newdata]
194 self._data_buffer_len += len(newdata)
195 self._current_stream_length += len(newdata)
196 while self._data_buffer_len >= self.KEEP_BLOCK_SIZE:
199 def flush_data(self):
200 data_buffer = ''.join(self._data_buffer)
201 if data_buffer != '':
202 self._current_stream_locators += [Keep.put(data_buffer[0:self.KEEP_BLOCK_SIZE])]
203 self._data_buffer = [data_buffer[self.KEEP_BLOCK_SIZE:]]
204 self._data_buffer_len = len(self._data_buffer[0])
206 def start_new_file(self, newfilename=None):
207 self.finish_current_file()
208 self.set_current_file_name(newfilename)
210 def set_current_file_name(self, newfilename):
211 if re.search(r'[\t\n]', newfilename):
212 raise errors.AssertionError(
213 "Manifest filenames cannot contain whitespace: %s" %
215 self._current_file_name = newfilename
217 def current_file_name(self):
218 return self._current_file_name
220 def finish_current_file(self):
221 if self._current_file_name == None:
222 if self._current_file_pos == self._current_stream_length:
224 raise errors.AssertionError(
225 "Cannot finish an unnamed file " +
226 "(%d bytes at offset %d in '%s' stream)" %
227 (self._current_stream_length - self._current_file_pos,
228 self._current_file_pos,
229 self._current_stream_name))
230 self._current_stream_files += [[self._current_file_pos,
231 self._current_stream_length - self._current_file_pos,
232 self._current_file_name]]
233 self._current_file_pos = self._current_stream_length
235 def start_new_stream(self, newstreamname='.'):
236 self.finish_current_stream()
237 self.set_current_stream_name(newstreamname)
239 def set_current_stream_name(self, newstreamname):
240 if re.search(r'[\t\n]', newstreamname):
241 raise errors.AssertionError(
242 "Manifest stream names cannot contain whitespace")
243 self._current_stream_name = '.' if newstreamname=='' else newstreamname
245 def current_stream_name(self):
246 return self._current_stream_name
248 def finish_current_stream(self):
249 self.finish_current_file()
251 if len(self._current_stream_files) == 0:
253 elif self._current_stream_name == None:
254 raise errors.AssertionError(
255 "Cannot finish an unnamed stream (%d bytes in %d files)" %
256 (self._current_stream_length, len(self._current_stream_files)))
258 if len(self._current_stream_locators) == 0:
259 self._current_stream_locators += [config.EMPTY_BLOCK_LOCATOR]
260 self._finished_streams += [[self._current_stream_name,
261 self._current_stream_locators,
262 self._current_stream_files]]
263 self._current_stream_files = []
264 self._current_stream_length = 0
265 self._current_stream_locators = []
266 self._current_stream_name = None
267 self._current_file_pos = 0
268 self._current_file_name = None
271 return Keep.put(self.manifest_text())
273 def manifest_text(self):
274 self.finish_current_stream()
277 for stream in self._finished_streams:
278 if not re.search(r'^\.(/.*)?$', stream[0]):
280 manifest += stream[0].replace(' ', '\\040')
281 manifest += ' ' + ' '.join(stream[1])
282 manifest += ' ' + ' '.join("%d:%d:%s" % (sfile[0], sfile[1], sfile[2].replace(' ', '\\040')) for sfile in stream[2])
285 #print 'writer',manifest
286 #print 'after reader',CollectionReader(manifest).manifest_text()
288 return CollectionReader(manifest).manifest_text()
290 def data_locators(self):
292 for name, locators, files in self._finished_streams: