Bug 1347582 - Use File object to obtain normalized file mode; r=dustin,gps
authorSwapnesh Kumar Sahoo <swapneshks@gmail.com>
Wed, 26 Jul 2017 11:51:37 +0530
changeset 420381 2c7389aef3bc6ac357b49af91f89485bfd2756ad
parent 420380 db29261fe4c7afebd6490ad21a2df6239d185c6c
child 420382 639e49a6841b3446a01c6e7cb9808f2574d2562f
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdustin, gps
bugs1347582
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1347582 - Use File object to obtain normalized file mode; r=dustin,gps MozReview-Commit-ID: HeDq4EwAfVS
python/mozbuild/mozpack/archive.py
--- a/python/mozbuild/mozpack/archive.py
+++ b/python/mozbuild/mozpack/archive.py
@@ -6,17 +6,17 @@ from __future__ import absolute_import
 
 import bz2
 import gzip
 import io
 import stat
 import tarfile
 
 from .files import (
-    BaseFile,
+    BaseFile, File,
 )
 
 # 2016-01-01T00:00:00+0000
 DEFAULT_MTIME = 1451606400
 
 
 def create_tar_from_files(fp, files):
     """Create a tar file deterministically.
@@ -28,22 +28,22 @@ def create_tar_from_files(fp, files):
     for writing.
 
     Only regular files can be written.
 
     FUTURE accept a filename argument (or create APIs to write files)
     """
     with tarfile.open(name='', mode='w', fileobj=fp, dereference=True) as tf:
         for archive_path, f in sorted(files.items()):
-            if isinstance(f, BaseFile):
-                ti = tarfile.TarInfo(archive_path)
-                ti.mode = f.mode or 0644
-                ti.type = tarfile.REGTYPE
-            else:
-                ti = tf.gettarinfo(f, archive_path)
+            if not isinstance(f, BaseFile):
+                f = File(f)
+
+            ti = tarfile.TarInfo(archive_path)
+            ti.mode = f.mode or 0644
+            ti.type = tarfile.REGTYPE
 
             if not ti.isreg():
                 raise ValueError('not a regular file: %s' % f)
 
             # Disallow setuid and setgid bits. This is an arbitrary restriction.
             # However, since we set uid/gid to root:root, setuid and setgid
             # would be a glaring security hole if the archive were
             # uncompressed as root.
@@ -55,24 +55,20 @@ def create_tar_from_files(fp, files):
             ti.uid = 0
             ti.gid = 0
             ti.uname = ''
             ti.gname = ''
 
             # Set mtime to a constant value.
             ti.mtime = DEFAULT_MTIME
 
-            if isinstance(f, BaseFile):
-                ti.size = f.size()
-                # tarfile wants to pass a size argument to read(). So just
-                # wrap/buffer in a proper file object interface.
-                tf.addfile(ti, f.open())
-            else:
-                with open(f, 'rb') as fh:
-                    tf.addfile(ti, fh)
+            ti.size = f.size()
+            # tarfile wants to pass a size argument to read(). So just
+            # wrap/buffer in a proper file object interface.
+            tf.addfile(ti, f.open())
 
 
 def create_tar_gz_from_files(fp, files, filename=None, compresslevel=9):
     """Create a tar.gz file deterministically from files.
 
     This is a glorified wrapper around ``create_tar_from_files`` that
     adds gzip compression.