From 25a34e1a16d0e8de5cdbc73bd5f0e57e9c1732bc Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:12:22 +0300 Subject: Wrote manual page for minimify. --- minimify.1 | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 minimify.1 diff --git a/minimify.1 b/minimify.1 new file mode 100644 index 0000000..0e9a4b3 --- /dev/null +++ b/minimify.1 @@ -0,0 +1,33 @@ +.\" minimify.1 - manual page for the minimify command +.\" 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 . +.\" +.TH MINIMIFY 1 +.SH NAME +minimify \- compress file to minimal size +.SH SYNOPSIS +.BR minimify +.IR file ... +.SH DESCRIPTION +.B minimify +compresses a file using different compression programs and picks the +smallest output. +Note that this can be extremely slow. +.PP +The following compression programs are supported: +.BR gzip , +.BR bzip2 , +and +.BR lzma . -- cgit v1.2.1 From 099a60a02d7c3f78282c74e2c09c567bbab06979 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:32:55 +0300 Subject: Use multiprocessing to make use of all available processors. --- minimify | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 minimify 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 . + + +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() -- cgit v1.2.1 From 9085df7df610c9ecd7a6661d28a9551fd9c13131 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:37:08 +0300 Subject: Keep only the smallest output file. --- minimify | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/minimify b/minimify index 1c05b47..e0a1f9c 100755 --- a/minimify +++ b/minimify @@ -50,17 +50,22 @@ def run_compressor(t): def compress(filename, options): + args = [(compressor, filename, suffix, options) + for compressor, suffix in COMPRESSORS] pool = multiprocessing.Pool() - pool.map(run_compressor, - [(compressor, filename, suffix, options) - for compressor, suffix in COMPRESSORS]) - + pool.map(run_compressor, args) + sizes = [(os.path.getsize(filename + suffix), filename + suffix) + for compressor, filename, suffix, options in args] + sizes.sort() + for size, pathname in sizes[1:]: + os.remove(pathname) + return sizes[0] def main(): options, filenames = parse_args() for filename in filenames: - compress(filename, options) - + size, name = compress(filename, options) + print size, name if __name__ == "__main__": -- cgit v1.2.1 From d3aad85411aa2835730719d4d70262218b787c02 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:38:22 +0300 Subject: Simplify a bit. --- minimify | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/minimify b/minimify index e0a1f9c..e6830b9 100755 --- a/minimify +++ b/minimify @@ -47,16 +47,14 @@ def run_compressor(t): if p.returncode: raise Exception('Compression program %s failed' % p.returncode) os.rename(name, filename + suffix) + return os.path.getsize(filename + suffix), filename + suffix def compress(filename, options): args = [(compressor, filename, suffix, options) for compressor, suffix in COMPRESSORS] pool = multiprocessing.Pool() - pool.map(run_compressor, args) - sizes = [(os.path.getsize(filename + suffix), filename + suffix) - for compressor, filename, suffix, options in args] - sizes.sort() + sizes = sorted(pool.map(run_compressor, args)) for size, pathname in sizes[1:]: os.remove(pathname) return sizes[0] -- cgit v1.2.1 From fa7ad45397845730f6c623830f98196101d815f4 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:39:27 +0300 Subject: Added missing empty line for formatting. --- minimify | 1 + 1 file changed, 1 insertion(+) diff --git a/minimify b/minimify index e6830b9..c4985cf 100755 --- a/minimify +++ b/minimify @@ -59,6 +59,7 @@ def compress(filename, options): os.remove(pathname) return sizes[0] + def main(): options, filenames = parse_args() for filename in filenames: -- cgit v1.2.1