2013-10-17 22:25:19 +00:00
|
|
|
import marisa_trie
|
|
|
|
import os
|
2013-10-18 18:31:14 +00:00
|
|
|
import tarfile
|
2013-10-17 22:25:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
AUFS_METADATA = u'.wh..wh.'
|
|
|
|
|
|
|
|
AUFS_WHITEOUT = u'.wh.'
|
|
|
|
AUFS_WHITEOUT_PREFIX_LENGTH = len(AUFS_WHITEOUT)
|
|
|
|
|
2013-10-19 00:19:05 +00:00
|
|
|
ALLOWED_TYPES = {tarfile.REGTYPE, tarfile.AREGTYPE}
|
|
|
|
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2013-10-18 18:31:14 +00:00
|
|
|
def files_and_dirs_from_tar(source_stream, removed_prefix_collector):
|
2014-01-31 16:29:55 +00:00
|
|
|
try:
|
|
|
|
tar_stream = tarfile.open(mode='r|*', fileobj=source_stream)
|
|
|
|
except tarfile.ReadError:
|
|
|
|
# Empty tar file
|
|
|
|
return
|
2013-10-18 18:31:14 +00:00
|
|
|
|
|
|
|
for tar_info in tar_stream:
|
2013-10-19 00:19:05 +00:00
|
|
|
absolute = os.path.relpath(tar_info.name.decode('utf-8'), './')
|
|
|
|
dirname = os.path.dirname(absolute)
|
2013-10-18 18:31:14 +00:00
|
|
|
filename = os.path.basename(absolute)
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2013-10-19 00:19:05 +00:00
|
|
|
# Skip directories and metadata
|
2013-10-18 18:31:14 +00:00
|
|
|
if (filename.startswith(AUFS_METADATA) or
|
|
|
|
absolute.startswith(AUFS_METADATA)):
|
|
|
|
# Skip
|
|
|
|
continue
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2013-10-18 18:31:14 +00:00
|
|
|
elif filename.startswith(AUFS_WHITEOUT):
|
2013-10-19 00:19:05 +00:00
|
|
|
removed_filename = filename[AUFS_WHITEOUT_PREFIX_LENGTH:]
|
|
|
|
removed_prefix = os.path.join('/', dirname, removed_filename)
|
|
|
|
removed_prefix_collector.add(removed_prefix)
|
2013-10-18 18:31:14 +00:00
|
|
|
continue
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2013-10-19 00:19:05 +00:00
|
|
|
elif tar_info.type in ALLOWED_TYPES:
|
|
|
|
yield '/' + absolute
|
2013-10-17 22:25:19 +00:00
|
|
|
|
|
|
|
|
2013-10-18 18:31:14 +00:00
|
|
|
def __compute_removed(base_trie, removed_prefixes):
|
2013-10-17 22:25:19 +00:00
|
|
|
for prefix in removed_prefixes:
|
|
|
|
for filename in base_trie.keys(prefix):
|
|
|
|
yield filename
|
|
|
|
|
|
|
|
|
2013-10-18 18:31:14 +00:00
|
|
|
def __compute_added_changed(base_trie, delta_trie):
|
2013-10-17 22:25:19 +00:00
|
|
|
added = set()
|
|
|
|
changed = set()
|
|
|
|
|
|
|
|
for filename in delta_trie.keys():
|
|
|
|
if filename not in base_trie:
|
|
|
|
added.add(filename)
|
|
|
|
else:
|
|
|
|
changed.add(filename)
|
|
|
|
|
|
|
|
return added, changed
|
|
|
|
|
|
|
|
|
2013-10-18 18:31:14 +00:00
|
|
|
def __new_fs(base_trie, added, removed):
|
2013-10-17 22:25:19 +00:00
|
|
|
for filename in base_trie.keys():
|
|
|
|
if filename not in removed:
|
|
|
|
yield filename
|
|
|
|
|
|
|
|
for filename in added:
|
2013-10-18 18:31:14 +00:00
|
|
|
yield filename
|
|
|
|
|
|
|
|
|
|
|
|
def empty_fs():
|
|
|
|
return marisa_trie.Trie()
|
|
|
|
|
|
|
|
|
|
|
|
def compute_new_diffs_and_fs(base_trie, filename_source,
|
|
|
|
removed_prefix_collector):
|
|
|
|
new_trie = marisa_trie.Trie(filename_source)
|
|
|
|
(new_added, new_changed) = __compute_added_changed(base_trie, new_trie)
|
|
|
|
|
|
|
|
new_removed = marisa_trie.Trie(__compute_removed(base_trie,
|
|
|
|
removed_prefix_collector))
|
|
|
|
|
|
|
|
new_fs = marisa_trie.Trie(__new_fs(base_trie, new_added, new_removed))
|
|
|
|
|
|
|
|
return (new_fs, new_added, new_changed, new_removed.keys())
|