summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2009-08-07 22:39:42 +0300
committerLars Wirzenius <liw@liw.fi>2009-08-07 22:39:42 +0300
commit107801a5fd8adc50c11ac1f17d078358a2c9a311 (patch)
treee792af975a7dd211a4a45920c3830af2e458be8a
parent721028c921e56e3126cbf94b95a11d4f279c0fdb (diff)
parentfa7ad45397845730f6c623830f98196101d815f4 (diff)
downloadextrautils-107801a5fd8adc50c11ac1f17d078358a2c9a311.tar.gz
Merged implementation of minimify command.
-rwxr-xr-xminimify71
-rw-r--r--minimify.133
2 files changed, 104 insertions, 0 deletions
diff --git a/minimify b/minimify
new file mode 100755
index 0000000..c4985cf
--- /dev/null
+++ b/minimify
@@ -0,0 +1,71 @@
+#!/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)
+ 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()
+ sizes = sorted(pool.map(run_compressor, args))
+ for size, pathname in sizes[1:]:
+ os.remove(pathname)
+ return sizes[0]
+
+
+def main():
+ options, filenames = parse_args()
+ for filename in filenames:
+ size, name = compress(filename, options)
+ print size, name
+
+
+if __name__ == "__main__":
+ main()
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 <http://www.gnu.org/licenses/>.
+.\"
+.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 .