summaryrefslogtreecommitdiff
path: root/minimify
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2009-08-07 22:32:55 +0300
committerLars Wirzenius <liw@liw.fi>2009-08-07 22:32:55 +0300
commit099a60a02d7c3f78282c74e2c09c567bbab06979 (patch)
treece38248abb32bed9e846f3f24fd8019f93067d28 /minimify
parent25a34e1a16d0e8de5cdbc73bd5f0e57e9c1732bc (diff)
downloadextrautils-099a60a02d7c3f78282c74e2c09c567bbab06979.tar.gz
Use multiprocessing to make use of all available processors.
Diffstat (limited to 'minimify')
-rwxr-xr-xminimify67
1 files changed, 67 insertions, 0 deletions
diff --git a/minimify b/minimify
new file mode 100755
index 0000000..1c05b47
--- /dev/null
+++ b/minimify
@@ -0,0 +1,67 @@
+#!/usr/bin/python
+#
+# minimify -- compress file to smallest size
+# Copyright (C) 2009 Lars Wirzenius
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import multiprocessing
+import optparse
+import os
+import subprocess
+import tempfile
+
+
+COMPRESSORS = (
+ ('gzip', '.gz'),
+ ('bzip2', '.bz2'),
+ ('lzma', '.lzma'),
+)
+
+
+def parse_args():
+ parser = optparse.OptionParser()
+ options, filenames = parser.parse_args()
+ return options, filenames
+
+
+def run_compressor(t):
+ compressor, filename, suffix, options = t
+ input_f = file(filename)
+ fd, name = tempfile.mkstemp(dir=os.path.dirname(filename))
+ p = subprocess.Popen([compressor], stdin=input_f, stdout=fd)
+ p.communicate('')
+ os.close(fd)
+ if p.returncode:
+ raise Exception('Compression program %s failed' % p.returncode)
+ os.rename(name, filename + suffix)
+
+
+def compress(filename, options):
+ pool = multiprocessing.Pool()
+ pool.map(run_compressor,
+ [(compressor, filename, suffix, options)
+ for compressor, suffix in COMPRESSORS])
+
+
+def main():
+ options, filenames = parse_args()
+ for filename in filenames:
+ compress(filename, options)
+
+
+
+if __name__ == "__main__":
+ main()