Bug 1348229 - Add an unzip py_action script based on the mozjar code. r?mshal draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 17 Mar 2017 14:24:35 +0900
changeset 500494 90860909bd1c84618b6c2650bbe2f82605f11084
parent 500493 67c3d82f8caece2779cd28e17e47dbb629ccf288
child 500495 6a1e8dccc27df226f725bec35eedf2112c245a4d
push id49734
push userbmo:mh+mozilla@glandium.org
push dateFri, 17 Mar 2017 08:40:19 +0000
reviewersmshal
bugs1348229
milestone55.0a1
Bug 1348229 - Add an unzip py_action script based on the mozjar code. r?mshal In the long term, this can be used to replace uses of the system unzip program.
python/mozbuild/mozbuild/action/unzip.py
new file mode 100644
--- /dev/null
+++ b/python/mozbuild/mozbuild/action/unzip.py
@@ -0,0 +1,44 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# This script creates a zip file, but will also strip any binaries
+# it finds before adding them to the zip.
+
+from __future__ import absolute_import
+
+import argparse
+import mozpack.path as mozpath
+import sys
+from mozbuild.util import ensureParentDir
+from mozpack.mozjar import JarReader
+from mozpack.files import DeflatedFile
+
+
+def main(args):
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-C", metavar='DIR', default=".",
+                        help="Change to given directory before extracting")
+    parser.add_argument("-l", action='store_true',
+                        help="List files")
+    parser.add_argument("zip", help="Path to zip file")
+    parser.add_argument("files", nargs="*",
+                        help="Path to files to extract from zip")
+    args = parser.parse_args(args)
+
+    jar = JarReader(file=args.zip)
+
+    for entry in jar.entries:
+        if not args.files or any(mozpath.match(entry, f)
+                                 for f in args.files):
+            if args.l:
+                print entry
+            else:
+                print 'Extracting {}'.format(entry)
+                dest = mozpath.join(args.C, entry)
+                ensureParentDir(dest)
+                DeflatedFile(jar[entry]).copy(dest)
+
+
+if __name__ == '__main__':
+    main(sys.argv[1:])