Bug 1347582 - Add option to force file mode during tar creation; r=dustin

MozReview-Commit-ID: HeDq4EwAfVS

--HG--
extra : rebase_source : 027c1b9067f9d28921d18c2a2653cea4eabac46d
This commit is contained in:
Swapnesh Kumar Sahoo 2017-07-12 22:59:26 +05:30
Родитель 2a645b1ab7
Коммит b73d585ecd
2 изменённых файлов: 13 добавлений и 4 удалений

Просмотреть файл

@ -18,7 +18,7 @@ from .files import (
DEFAULT_MTIME = 1451606400
def create_tar_from_files(fp, files):
def create_tar_from_files(fp, files, forcemode=None):
"""Create a tar file deterministically.
Receives a dict mapping names of files in the archive to local filesystem
@ -29,6 +29,8 @@ def create_tar_from_files(fp, files):
Only regular files can be written.
Mode (permissions) of files is forced if a value for forcemode is passed.
FUTURE accept a filename argument (or create APIs to write files)
"""
with tarfile.open(name='', mode='w', fileobj=fp, dereference=True) as tf:
@ -51,6 +53,10 @@ def create_tar_from_files(fp, files):
raise ValueError('cannot add file with setuid or setgid set: '
'%s' % f)
# Force file mode (permissions) if 'forcemode' is passed
if forcemode is not None:
ti.mode = forcemode
# Set uid, gid, username, and group as deterministic values.
ti.uid = 0
ti.gid = 0
@ -70,7 +76,7 @@ def create_tar_from_files(fp, files):
tf.addfile(ti, fh)
def create_tar_gz_from_files(fp, files, filename=None, compresslevel=9):
def create_tar_gz_from_files(fp, files, filename=None, compresslevel=9, forcemode=None):
"""Create a tar.gz file deterministically from files.
This is a glorified wrapper around ``create_tar_from_files`` that
@ -78,13 +84,16 @@ def create_tar_gz_from_files(fp, files, filename=None, compresslevel=9):
The passed file handle should be opened for writing in binary mode.
When the function returns, all data has been written to the handle.
Mode (permissions) of files is forced if a value for forcemode is
passed.
"""
# Offset 3-7 in the gzip header contains an mtime. Pin it to a known
# value so output is deterministic.
gf = gzip.GzipFile(filename=filename or '', mode='wb', fileobj=fp,
compresslevel=compresslevel, mtime=DEFAULT_MTIME)
with gf:
create_tar_from_files(gf, files)
create_tar_from_files(gf, files, forcemode)
class _BZ2Proxy(object):

Просмотреть файл

@ -119,7 +119,7 @@ def create_context_tar(topsrcdir, context_dir, out_path, prefix):
archive_files[archive_path] = fs_path
with open(out_path, 'wb') as fh:
create_tar_gz_from_files(fh, archive_files, '%s.tar.gz' % prefix)
create_tar_gz_from_files(fh, archive_files, '%s.tar.gz' % prefix, forcemode=0644)
h = hashlib.sha256()
with open(out_path, 'rb') as fh: