fixed tar bug :)

This commit is contained in:
Kenneth Reitz
2014-04-02 11:19:35 -04:00
parent cfee5a31c5
commit 88e18ff8cc
+39 -1
View File
@@ -45,8 +45,46 @@ def pipe(a, b, indent=True):
def archive_tree(dir, archive):
"""Creates a tar.gz archive from a given directory."""
abspath = os.path.abspath(dir)
base_root = None
transposed_base = None
is_top_level = False
with tarfile.open(archive, 'w:gz') as tar:
tar.add(dir, arcname=os.path.basename(dir))
for root, _, files in os.walk(abspath):
# Mark the first pass as the top-level directory.
if is_top_level is None:
is_top_level = True
if not base_root:
base_root = root
# No path at all for the top-level directory.
if not is_top_level:
transposed_base = root[len(base_root)+1:]
else:
transposed_base = ''
for file in files:
standard_path = os.path.join(root, file)
if not transposed_base:
transposed_path = file
else:
transposed_path = os.path.join(transposed_base, file)
# Add the file to the archive, with the proper transposed path.
tar.add(standard_path, arcname=transposed_path)
# Close out the top-level directory marker.
is_top_level = False
def extract_tree(archive, dir):
"""Extract tar.gz archive to a given directory."""