Package: Update zip file creation

This commit is contained in:
UK992 2018-10-16 23:16:00 +02:00
parent f4d3d8ee1e
commit c4b5c944c0

View file

@ -21,6 +21,7 @@ import subprocess
from subprocess import PIPE from subprocess import PIPE
import sys import sys
import tarfile import tarfile
import zipfile
from xml.etree.ElementTree import XML from xml.etree.ElementTree import XML
from servo.util import download_file from servo.util import download_file
import urllib2 import urllib2
@ -85,10 +86,14 @@ def archive_deterministically(dir_to_archive, dest_archive, prepend_path=None):
dest_archive = os.path.abspath(dest_archive) dest_archive = os.path.abspath(dest_archive)
with cd(dir_to_archive): with cd(dir_to_archive):
current_dir = "." current_dir = "."
file_list = [current_dir] file_list = []
for root, dirs, files in os.walk(current_dir): for root, dirs, files in os.walk(current_dir):
for name in itertools.chain(dirs, files): if dest_archive.endswith(".zip"):
file_list.append(os.path.join(root, name)) for f in files:
file_list.append(os.path.join(root, f))
else:
for name in itertools.chain(dirs, files):
file_list.append(os.path.join(root, name))
# Sort file entries with the fixed locale # Sort file entries with the fixed locale
with setlocale('C'): with setlocale('C'):
@ -99,13 +104,21 @@ def archive_deterministically(dir_to_archive, dest_archive, prepend_path=None):
# TODO do this in a temporary folder after #11983 is fixed # TODO do this in a temporary folder after #11983 is fixed
temp_file = '{}.temp~'.format(dest_archive) temp_file = '{}.temp~'.format(dest_archive)
with os.fdopen(os.open(temp_file, os.O_WRONLY | os.O_CREAT, 0644), 'w') as out_file: with os.fdopen(os.open(temp_file, os.O_WRONLY | os.O_CREAT, 0644), 'w') as out_file:
with gzip.GzipFile('wb', fileobj=out_file, mtime=0) as gzip_file: if dest_archive.endswith('.zip'):
with tarfile.open(fileobj=gzip_file, mode='w:') as tar_file: with zipfile.ZipFile(temp_file, 'w', zipfile.ZIP_DEFLATED) as zip_file:
for entry in file_list: for entry in file_list:
arcname = entry arcname = entry
if prepend_path is not None: if prepend_path is not None:
arcname = os.path.normpath(os.path.join(prepend_path, arcname)) arcname = os.path.normpath(os.path.join(prepend_path, arcname))
tar_file.add(entry, filter=reset, recursive=False, arcname=arcname) zip_file.write(entry, arcname=arcname)
else:
with gzip.GzipFile('wb', fileobj=out_file, mtime=0) as gzip_file:
with tarfile.open(fileobj=gzip_file, mode='w:') as tar_file:
for entry in file_list:
arcname = entry
if prepend_path is not None:
arcname = os.path.normpath(os.path.join(prepend_path, arcname))
tar_file.add(entry, filter=reset, recursive=False, arcname=arcname)
os.rename(temp_file, dest_archive) os.rename(temp_file, dest_archive)