1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import absolute_import
11 return re.sub(r'[\\:\000-\040]', lambda m: "\\%03o" % ord(m.group(0)), path)
13 def normalize_stream(stream_name, stream):
14 """Take manifest stream and return a list of tokens in normalized format.
17 The name of the stream.
20 A dict mapping each filename to a list of `_range.LocatorAndRange` objects.
24 stream_name = escape(stream_name)
25 stream_tokens = [stream_name]
26 sortedfiles = list(stream.keys())
31 # Go through each file and add each referenced block exactly once.
32 for streamfile in sortedfiles:
33 for segment in stream[streamfile]:
34 if segment.locator not in blocks:
35 stream_tokens.append(segment.locator)
36 blocks[segment.locator] = streamoffset
37 streamoffset += segment.block_size
39 # Add the empty block if the stream is otherwise empty.
40 if len(stream_tokens) == 1:
41 stream_tokens.append(config.EMPTY_BLOCK_LOCATOR)
43 for streamfile in sortedfiles:
44 # Add in file segments
46 fout = escape(streamfile)
47 for segment in stream[streamfile]:
48 # Collapse adjacent segments
49 streamoffset = blocks[segment.locator] + segment.segment_offset
50 if current_span is None:
51 current_span = [streamoffset, streamoffset + segment.segment_size]
53 if streamoffset == current_span[1]:
54 current_span[1] += segment.segment_size
56 stream_tokens.append(u"{0}:{1}:{2}".format(current_span[0], current_span[1] - current_span[0], fout))
57 current_span = [streamoffset, streamoffset + segment.segment_size]
59 if current_span is not None:
60 stream_tokens.append(u"{0}:{1}:{2}".format(current_span[0], current_span[1] - current_span[0], fout))
62 if not stream[streamfile]:
63 stream_tokens.append(u"0:0:{0}".format(fout))