Bug 1260241 - Pre: Add compress option to Jarrer.add. r=glandium
authorNick Alexander <nalexander@mozilla.com>
Mon, 28 Mar 2016 19:02:05 -0700
changeset 291314 bc439c65ecdcabcf039b9a4c6074380088d8530a
parent 291313 230a0a44d0871347fb8215cd65d2d126cecfa93c
child 291315 99f8eb253c8769c6752a98be0306249ed05d7731
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs1260241
milestone48.0a1
Bug 1260241 - Pre: Add compress option to Jarrer.add. r=glandium MozReview-Commit-ID: FWXeBOi9FTY
python/mozbuild/mozpack/copier.py
python/mozbuild/mozpack/test/test_copier.py
--- a/python/mozbuild/mozpack/copier.py
+++ b/python/mozbuild/mozpack/copier.py
@@ -479,18 +479,24 @@ class Jarrer(FileRegistry, BaseFile):
     def __init__(self, compress=True, optimize=True):
         '''
         Create a Jarrer instance. See mozpack.mozjar.JarWriter documentation
         for details on the compress and optimize arguments.
         '''
         self.compress = compress
         self.optimize = optimize
         self._preload = []
+        self._compress_options = {}  # Map path to compress boolean option.
         FileRegistry.__init__(self)
 
+    def add(self, path, content, compress=None):
+        FileRegistry.add(self, path, content)
+        if compress is not None:
+            self._compress_options[path] = compress
+
     def copy(self, dest, skip_if_older=True):
         '''
         Pack all registered files in the given destination jar. The given
         destination jar may be a path to jar file, or a Dest instance for
         a jar file.
         If the destination jar file exists, its (compressed) contents are used
         instead of the registered BaseFile instances when appropriate.
         '''
@@ -535,22 +541,24 @@ class Jarrer(FileRegistry, BaseFile):
         except Exception:
             old_jar = []
 
         old_contents = dict([(f.filename, f) for f in old_jar])
 
         with JarWriter(fileobj=dest, compress=self.compress,
                        optimize=self.optimize) as jar:
             for path, file in self:
+                compress = self._compress_options.get(path, self.compress)
+
                 if path in old_contents:
-                    deflater = DeflaterDest(old_contents[path], self.compress)
+                    deflater = DeflaterDest(old_contents[path], compress)
                 else:
-                    deflater = DeflaterDest(compress=self.compress)
+                    deflater = DeflaterDest(compress=compress)
                 file.copy(deflater, skip_if_older)
-                jar.add(path, deflater.deflater, mode=file.mode)
+                jar.add(path, deflater.deflater, mode=file.mode, compress=compress)
             if self._preload:
                 jar.preload(self._preload)
 
     def open(self):
         raise RuntimeError('unsupported')
 
     def preload(self, paths):
         '''
--- a/python/mozbuild/mozpack/test/test_copier.py
+++ b/python/mozbuild/mozpack/test/test_copier.py
@@ -504,10 +504,26 @@ class TestJarrer(unittest.TestCase):
         copier.copy(dest)
 
         dest.seek(0)
         jar = JarReader(fileobj=dest)
         self.assertEqual([f.filename for f in jar], preloaded +
                          [p for p in copier.paths() if not p in preloaded])
         self.assertEqual(jar.last_preloaded, preloaded[-1])
 
+
+    def test_jarrer_compress(self):
+        copier = Jarrer()
+        copier.add('foo/bar', GeneratedFile('ffffff'))
+        copier.add('foo/qux', GeneratedFile('ffffff'), compress=False)
+
+        dest = MockDest()
+        copier.copy(dest)
+        self.check_jar(dest, copier)
+
+        dest.seek(0)
+        jar = JarReader(fileobj=dest)
+        self.assertTrue(jar['foo/bar'].compressed)
+        self.assertFalse(jar['foo/qux'].compressed)
+
+
 if __name__ == '__main__':
     mozunit.main()