[IMP] Allow custom ordering of files contained in zip dumps

This will allow to speed up our migration platform: as funzip can pipe
the output of the first file of the archive, even if the file is not
fully unzipped yet. This is useful to plays with dump.sql without waiting
for the full archive decompression.
This commit is contained in:
Dharmraj Jhala 2015-03-09 10:48:20 +05:30 committed by Richard Mathot
parent d93148240b
commit 01f2c5c9d2
2 changed files with 9 additions and 3 deletions

View File

@ -195,10 +195,10 @@ def dump_db(db_name, stream, backup_format='zip'):
cmd.insert(-1, '--file=' + os.path.join(dump_dir, 'dump.sql'))
openerp.tools.exec_pg_command(*cmd)
if stream:
openerp.tools.osutil.zip_dir(dump_dir, stream, include_dir=False)
openerp.tools.osutil.zip_dir(dump_dir, stream, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
else:
t=tempfile.TemporaryFile()
openerp.tools.osutil.zip_dir(dump_dir, t, include_dir=False)
openerp.tools.osutil.zip_dir(dump_dir, t, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
t.seek(0)
return t
else:

View File

@ -73,7 +73,12 @@ def tempdir():
finally:
shutil.rmtree(tmpdir)
def zip_dir(path, stream, include_dir=True): # TODO add ignore list
def zip_dir(path, stream, include_dir=True, fnct_sort=None): # TODO add ignore list
"""
: param fnct_sort : Function to be passed to "key" parameter of built-in
python sorted() to provide flexibility of sorting files
inside ZIP archive according to specific requirements.
"""
path = os.path.normpath(path)
len_prefix = len(os.path.dirname(path)) if include_dir else len(path)
if len_prefix:
@ -81,6 +86,7 @@ def zip_dir(path, stream, include_dir=True): # TODO add ignore list
with zipfile.ZipFile(stream, 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=True) as zipf:
for dirpath, dirnames, filenames in os.walk(path):
filenames = sorted(filenames, key=fnct_sort)
for fname in filenames:
bname, ext = os.path.splitext(fname)
ext = ext or bname