From d282bcf8e902026b07cb474e262e8d696478b56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1lm=C3=A1n=20Tarnay?= Date: Tue, 20 Apr 2010 23:29:24 +0200 Subject: [PATCH 1/2] py2exe support Usage: setup.py py2exe You must be able to start trimage.py before even attempting to use py2exe. It generates a standalone executable. That standalone exacutable starts up only if the following files can be found on the path: jpegoptim.exe optipng.exe advpng.exe (they must return 0) Compression does not work yet and the Windows versions of these tools are a lot slower than on Linux. The binaries I found were pretty old. They should most probably be recompiled to make use of new cpu features. Or we should find other tools. trimage.hurry.filename -> trimage.filename because py2exe couldn't deal with it --- setup.py | 21 +++++- src/trimage/filesize/README.txt | 1 + src/trimage/filesize/__init__.py | 2 + src/trimage/filesize/filesize.py | 110 +++++++++++++++++++++++++++++++ src/trimage/trimage.py | 14 ++-- 5 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 src/trimage/filesize/README.txt create mode 100644 src/trimage/filesize/__init__.py create mode 100644 src/trimage/filesize/filesize.py mode change 100755 => 100644 src/trimage/trimage.py diff --git a/setup.py b/setup.py index fcef83a..bf318aa 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,11 @@ #!/usr/bin/env python +import sys +win=(sys.platform == "win32") +if win: + import py2exe + sys.path.append("src/trimage") + from distutils.core import setup setup(name = "trimage", @@ -11,8 +17,7 @@ setup(name = "trimage", license = "MIT license", package_dir = {'trimage' : 'src/trimage'}, packages = ["trimage", - "trimage.hurry", - "trimage.hurry.filesize", + "trimage.filesize", "trimage.ThreadPool",], package_data = {"trimage" : ["pixmaps/*.*"] }, data_files=[('share/icons/hicolor/scalable/apps', ['desktop/trimage.svg']), @@ -20,4 +25,16 @@ setup(name = "trimage", scripts = ["trimage"], long_description = """Trimage is a cross-platform GUI and command-line interface to optimize image files via optipng, advpng and jpegoptim, depending on the filetype (currently, PNG and JPG files are supported). It was inspired by imageoptim. All image files are losslessy compressed on the highest available compression levels. Trimage gives you various input functions to fit your own workflow: A regular file dialog, dragging and dropping and various command line options.""", requires = ["PyQt4 (>=4.4)"], + + #for py2exe + windows=[r'src\trimage\trimage.py'], + zipfile=None, + options={"py2exe":{ + "optimize":2, + "compressed":1, + "bundle_files":1, + "includes":["sip",], + "excludes":['email'], + }, + }, ) diff --git a/src/trimage/filesize/README.txt b/src/trimage/filesize/README.txt new file mode 100644 index 0000000..f91201d --- /dev/null +++ b/src/trimage/filesize/README.txt @@ -0,0 +1 @@ +http://pypi.python.org/pypi/hurry.filesize \ No newline at end of file diff --git a/src/trimage/filesize/__init__.py b/src/trimage/filesize/__init__.py new file mode 100644 index 0000000..68d9fc2 --- /dev/null +++ b/src/trimage/filesize/__init__.py @@ -0,0 +1,2 @@ +from filesize import size +from filesize import traditional, alternative, verbose, iec, si diff --git a/src/trimage/filesize/filesize.py b/src/trimage/filesize/filesize.py new file mode 100644 index 0000000..32065fe --- /dev/null +++ b/src/trimage/filesize/filesize.py @@ -0,0 +1,110 @@ + +traditional = [ + (1024 ** 5, 'P'), + (1024 ** 4, 'T'), + (1024 ** 3, 'G'), + (1024 ** 2, 'M'), + (1024 ** 1, 'K'), + (1024 ** 0, 'B'), + ] + +alternative = [ + (1024 ** 5, ' PB'), + (1024 ** 4, ' TB'), + (1024 ** 3, ' GB'), + (1024 ** 2, ' MB'), + (1024 ** 1, ' KB'), + (1024 ** 0, (' byte', ' bytes')), + ] + +verbose = [ + (1024 ** 5, (' petabyte', ' petabytes')), + (1024 ** 4, (' terabyte', ' terabytes')), + (1024 ** 3, (' gigabyte', ' gigabytes')), + (1024 ** 2, (' megabyte', ' megabytes')), + (1024 ** 1, (' kilobyte', ' kilobytes')), + (1024 ** 0, (' byte', ' bytes')), + ] + +iec = [ + (1024 ** 5, 'Pi'), + (1024 ** 4, 'Ti'), + (1024 ** 3, 'Gi'), + (1024 ** 2, 'Mi'), + (1024 ** 1, 'Ki'), + (1024 ** 0, ''), + ] + +si = [ + (1000 ** 5, 'P'), + (1000 ** 4, 'T'), + (1000 ** 3, 'G'), + (1000 ** 2, 'M'), + (1000 ** 1, 'K'), + (1000 ** 0, 'B'), + ] + + + +def size(bytes, system=traditional): + """Human-readable file size. + + Using the traditional system, where a factor of 1024 is used:: + + >>> size(10) + '10B' + >>> size(100) + '100B' + >>> size(1000) + '1000B' + >>> size(2000) + '1K' + >>> size(10000) + '9K' + >>> size(20000) + '19K' + >>> size(100000) + '97K' + >>> size(200000) + '195K' + >>> size(1000000) + '976K' + >>> size(2000000) + '1M' + + Using the SI system, with a factor 1000:: + + >>> size(10, system=si) + '10B' + >>> size(100, system=si) + '100B' + >>> size(1000, system=si) + '1K' + >>> size(2000, system=si) + '2K' + >>> size(10000, system=si) + '10K' + >>> size(20000, system=si) + '20K' + >>> size(100000, system=si) + '100K' + >>> size(200000, system=si) + '200K' + >>> size(1000000, system=si) + '1M' + >>> size(2000000, system=si) + '2M' + + """ + for factor, suffix in system: + if bytes >= factor: + break + amount = int(bytes/factor) + if isinstance(suffix, tuple): + singular, multiple = suffix + if amount == 1: + suffix = singular + else: + suffix = multiple + return str(amount) + suffix + diff --git a/src/trimage/trimage.py b/src/trimage/trimage.py old mode 100755 new mode 100644 index 0a8b4cd..8ba1452 --- a/src/trimage/trimage.py +++ b/src/trimage/trimage.py @@ -9,7 +9,7 @@ from optparse import OptionParser from PyQt4.QtCore import * from PyQt4.QtGui import * -from hurry.filesize import * +from filesize import * from imghdr import what as determinetype from Queue import Queue @@ -209,18 +209,19 @@ class StartQT4(QMainWindow): def checkapps(self): """Check if the required command line apps exist.""" + exe=".exe" if (sys.platform=="win32") else "" status = False - retcode = self.safe_call("jpegoptim --version") + retcode = self.safe_call("jpegoptim"+exe+" --version") if retcode != 0: status = True sys.stderr.write("[error] please install jpegoptim") - retcode = self.safe_call("optipng -v") + retcode = self.safe_call("optipng"+exe+" -v") if retcode != 0: status = True sys.stderr.write("[error] please install optipng") - retcode = self.safe_call("advpng --version") + retcode = self.safe_call("advpng"+exe+" --version") if retcode != 0: status = True sys.stderr.write("[error] please install advancecomp") @@ -343,9 +344,10 @@ class Image: raise "Tried to compress invalid image (unsupported format or not file)" self.reset() self.compressing=True + exe=".exe" if (sys.platform=="win32") else "" runString = { - "jpeg": u"jpegoptim -f --strip-all '%(file)s'", - "png" : u"optipng -force -o7 '%(file)s'&&advpng -z4 '%(file)s'"} + "jpeg": u"jpegoptim"+exe+" -f --strip-all '%(file)s'", + "png" : u"optipng"+exe+" -force -o7 '%(file)s'&&advpng"+exe+" -z4 '%(file)s'"} try: retcode = call(runString[self.filetype] % {"file": self.fullpath}, shell = True, stdout=PIPE) From a936903b00f19420d7fb40c82a4b3ba03274892a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1lm=C3=A1n=20Tarnay?= Date: Tue, 20 Apr 2010 23:42:00 +0200 Subject: [PATCH 2/2] delete now unused hurry.filesize (I forgot it in the previous commit) --- src/trimage/hurry/__init__.py | 7 -- src/trimage/hurry/filesize/README.txt | 47 ----------- src/trimage/hurry/filesize/__init__.py | 4 - src/trimage/hurry/filesize/filesize.py | 110 ------------------------- src/trimage/hurry/filesize/tests.py | 7 -- 5 files changed, 175 deletions(-) delete mode 100644 src/trimage/hurry/__init__.py delete mode 100644 src/trimage/hurry/filesize/README.txt delete mode 100644 src/trimage/hurry/filesize/__init__.py delete mode 100644 src/trimage/hurry/filesize/filesize.py delete mode 100644 src/trimage/hurry/filesize/tests.py diff --git a/src/trimage/hurry/__init__.py b/src/trimage/hurry/__init__.py deleted file mode 100644 index 2e2033b..0000000 --- a/src/trimage/hurry/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -# this is a namespace package -try: - import pkg_resources - pkg_resources.declare_namespace(__name__) -except ImportError: - import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/src/trimage/hurry/filesize/README.txt b/src/trimage/hurry/filesize/README.txt deleted file mode 100644 index f5aa749..0000000 --- a/src/trimage/hurry/filesize/README.txt +++ /dev/null @@ -1,47 +0,0 @@ -hurry.filesize -============== - -hurry.filesize a simple Python library that can take a number of bytes and -returns a human-readable string with the size in it, in kilobytes (K), -megabytes (M), etc. - -The default system it uses is "traditional", where multipliers of 1024 -increase the unit size:: - - >>> from hurry.filesize import size - >>> size(1024) - '1K' - -An alternative, slightly more verbose system:: - - >>> from hurry.filesize import alternative - >>> size(1, system=alternative) - '1 byte' - >>> size(10, system=alternative) - '10 bytes' - >>> size(1024, system=alternative) - '1 KB' - -A verbose system:: - - >>> from hurry.filesize import verbose - >>> size(10, system=verbose) - '10 bytes' - >>> size(1024, system=verbose) - '1 kilobyte' - >>> size(2000, system=verbose) - '1 kilobyte' - >>> size(3000, system=verbose) - '2 kilobytes' - >>> size(1024 * 1024, system=verbose) - '1 megabyte' - >>> size(1024 * 1024 * 3, system=verbose) - '3 megabytes' - -You can also use the SI system, where multipliers of 1000 increase the unit -size:: - - >>> from hurry.filesize import si - >>> size(1000, system=si) - '1K' - diff --git a/src/trimage/hurry/filesize/__init__.py b/src/trimage/hurry/filesize/__init__.py deleted file mode 100644 index d130dac..0000000 --- a/src/trimage/hurry/filesize/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from hurry.filesize.filesize import size -from hurry.filesize.filesize import traditional, alternative, verbose, iec, si - - diff --git a/src/trimage/hurry/filesize/filesize.py b/src/trimage/hurry/filesize/filesize.py deleted file mode 100644 index 32065fe..0000000 --- a/src/trimage/hurry/filesize/filesize.py +++ /dev/null @@ -1,110 +0,0 @@ - -traditional = [ - (1024 ** 5, 'P'), - (1024 ** 4, 'T'), - (1024 ** 3, 'G'), - (1024 ** 2, 'M'), - (1024 ** 1, 'K'), - (1024 ** 0, 'B'), - ] - -alternative = [ - (1024 ** 5, ' PB'), - (1024 ** 4, ' TB'), - (1024 ** 3, ' GB'), - (1024 ** 2, ' MB'), - (1024 ** 1, ' KB'), - (1024 ** 0, (' byte', ' bytes')), - ] - -verbose = [ - (1024 ** 5, (' petabyte', ' petabytes')), - (1024 ** 4, (' terabyte', ' terabytes')), - (1024 ** 3, (' gigabyte', ' gigabytes')), - (1024 ** 2, (' megabyte', ' megabytes')), - (1024 ** 1, (' kilobyte', ' kilobytes')), - (1024 ** 0, (' byte', ' bytes')), - ] - -iec = [ - (1024 ** 5, 'Pi'), - (1024 ** 4, 'Ti'), - (1024 ** 3, 'Gi'), - (1024 ** 2, 'Mi'), - (1024 ** 1, 'Ki'), - (1024 ** 0, ''), - ] - -si = [ - (1000 ** 5, 'P'), - (1000 ** 4, 'T'), - (1000 ** 3, 'G'), - (1000 ** 2, 'M'), - (1000 ** 1, 'K'), - (1000 ** 0, 'B'), - ] - - - -def size(bytes, system=traditional): - """Human-readable file size. - - Using the traditional system, where a factor of 1024 is used:: - - >>> size(10) - '10B' - >>> size(100) - '100B' - >>> size(1000) - '1000B' - >>> size(2000) - '1K' - >>> size(10000) - '9K' - >>> size(20000) - '19K' - >>> size(100000) - '97K' - >>> size(200000) - '195K' - >>> size(1000000) - '976K' - >>> size(2000000) - '1M' - - Using the SI system, with a factor 1000:: - - >>> size(10, system=si) - '10B' - >>> size(100, system=si) - '100B' - >>> size(1000, system=si) - '1K' - >>> size(2000, system=si) - '2K' - >>> size(10000, system=si) - '10K' - >>> size(20000, system=si) - '20K' - >>> size(100000, system=si) - '100K' - >>> size(200000, system=si) - '200K' - >>> size(1000000, system=si) - '1M' - >>> size(2000000, system=si) - '2M' - - """ - for factor, suffix in system: - if bytes >= factor: - break - amount = int(bytes/factor) - if isinstance(suffix, tuple): - singular, multiple = suffix - if amount == 1: - suffix = singular - else: - suffix = multiple - return str(amount) + suffix - diff --git a/src/trimage/hurry/filesize/tests.py b/src/trimage/hurry/filesize/tests.py deleted file mode 100644 index 1e89a32..0000000 --- a/src/trimage/hurry/filesize/tests.py +++ /dev/null @@ -1,7 +0,0 @@ -import unittest, doctest - -def test_suite(): - return unittest.TestSuite(( - doctest.DocFileSuite('README.txt'), - doctest.DocTestSuite('hurry.filesize.filesize'), - ))