Skip to content

Commit

Permalink
Fixes #241 - reorder tar adds so dirs are last
Browse files Browse the repository at this point in the history
This doesn't address mtimes, but then neither did the original
version.

Thanks to Richard Maw for identifying the root cause, explaining
how to fix, and reviewing the code.
  • Loading branch information
devcurmudgeon committed Sep 6, 2016
1 parent 3e0ec8c commit 766b244
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions ybd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from fs.osfs import OSFS
from fs.multifs import MultiFS
import calendar

import app

# The magic number for timestamps: 2011-11-11 11:11:11
Expand Down Expand Up @@ -343,22 +342,28 @@ def add_directory_to_tarfile(f_tar, dir_name, dir_arcname):
add_directory_to_tarfile(f_tar, root_dir, '.')


def make_deterministic_tar_archive(base_name, root_dir):
def make_deterministic_tar_archive(base_name, root):
'''Make a tar archive of contents of 'root_dir'.
This function uses monkeypatching to make shutil.make_archive() create
a deterministic tarfile.
This function takes extra steps to make the output more deterministic,
compared to shutil.make_archive() - it sorts the results to ensure
the ordering of the files in the archive is always the same.
https://bugs.python.org/issue24465 will make this function redundant.
Also this puts the directory last, to workaround a bug in docker/overlayfs
runners - see https://gitlab.com/baserock/ybd/issues/241
'''
real_listdir = os.listdir
FIXME: make this do timestamps
def stable_listdir(path):
return sorted(real_listdir(path))
'''

with monkeypatch(os, 'listdir', stable_listdir):
shutil.make_archive(base_name, 'tar', root_dir)
with app.chdir(root), open(base_name + '.tar', 'wb') as f:
with tarfile.TarFile(mode='w', fileobj=f) as f_tar:
directories = [d[0] for d in os.walk('.')]
for d in sorted(directories):
files = [os.path.join(d, f) for f in os.listdir(d)]
for path in sorted(files):
f_tar.add(name=path, recursive=False)
f_tar.add(name=d, recursive=False)


def _find_extensions(paths):
Expand Down

0 comments on commit 766b244

Please sign in to comment.