Filippo Giunchedi has submitted this change and it was merged.

Change subject: Upgrade to plugins 0.1.7
......................................................................


Upgrade to plugins 0.1.7

Change-Id: I272e3b62d5a6a4cd9c92f3bd0ac4950037224ce7
---
M debian/changelog
M debian/control
A debian/source/options
M setup.py
M wikimedia_thumbor/engine/__init__.py
M wikimedia_thumbor/engine/ghostscript/ghostscript.py
M wikimedia_thumbor/engine/vips/vips.py
7 files changed, 125 insertions(+), 54 deletions(-)

Approvals:
  Filippo Giunchedi: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/debian/changelog b/debian/changelog
index 3010ca9..5d84348 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,8 +1,8 @@
-python-thumbor-wikimedia (1.0.5-1) unstable; urgency=low
+python-thumbor-wikimedia (0.1.7-1) unstable; urgency=low
 
-  * New upstream release 
+  * New upstream release
 
- -- Gilles Dubuc <gil...@wikimedia.org>  Tue, 26 Jul 2016 06:34:37 +0000
+ -- Gilles Dubuc <gil...@wikimedia.org>  Thu, 28 Jul 2016 10:40:32 +0000
 
 python-thumbor-wikimedia (0.1.3-1) unstable; urgency=low
 
diff --git a/debian/control b/debian/control
index 51a5b25..95c8dd2 100644
--- a/debian/control
+++ b/debian/control
@@ -10,12 +10,12 @@
                ffmpeg,
                ghostscript,
                gifsicle,
-               gir1.2-vips-8.0,
                libimage-exiftool-perl,
+               libvips-tools,
                python-all,
                python-bs4,
                python-cairosvg,
-               python-djvulibre,
+               python-djvu,
                python-gi,
                python-libthumbor (>= 1.3.2),
                python-lxml,
@@ -30,7 +30,7 @@
 
 Package: python-thumbor-wikimedia
 Architecture: all
-Depends: ${misc:Depends}, ${python:Depends}, cgroup-tools, djvulibre-bin, 
ffmpeg, ghostscript, gir1.2-vips-8.0, libimage-exiftool-perl, python-bs4, 
python-cairosvg, python-djvulibre, python-gi, python-libthumbor (>= 1.3.2), 
python-lxml, python-swiftclient, python-thumbor-community-core, python-wand, 
thumbor (>= 6.0.1), xcftools
+Depends: ${misc:Depends}, ${python:Depends}, cgroup-tools, djvulibre-bin, 
ffmpeg, ghostscript, gifsicle, libimage-exiftool-perl, libvips-tools, 
python-bs4, python-cairosvg, python-djvu, python-gi, python-libthumbor (>= 
1.3.2), python-lxml, python-swiftclient, python-thumbor-community-core, 
python-wand, thumbor (>= 6.0.1), xcftools
 Provides: ${python:Provides}
 Description: Thumbor wikimedia extensions
  This provides all the custom Thumbor extensions needed at Wikimedia
diff --git a/debian/source/options b/debian/source/options
new file mode 100644
index 0000000..80c51ea
--- /dev/null
+++ b/debian/source/options
@@ -0,0 +1 @@
+extend-diff-ignore = "^.gitreview$"
diff --git a/setup.py b/setup.py
index 43377c2..68195cf 100644
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@
 
 setup(
     name='wikimedia_thumbor',
-    version='0.1.5',
+    version='0.1.7',
     url='https://phabricator.wikimedia.org/diffusion/THMBREXT/',
     license='MIT',
     author='Gilles Dubuc, Wikimedia Foundation',
diff --git a/wikimedia_thumbor/engine/__init__.py 
b/wikimedia_thumbor/engine/__init__.py
index 98d9512..31ed840 100644
--- a/wikimedia_thumbor/engine/__init__.py
+++ b/wikimedia_thumbor/engine/__init__.py
@@ -21,6 +21,10 @@
 from wikimedia_thumbor.engine.imagemagick import Engine as IMEngine
 
 
+class CommandError(Exception):
+    pass
+
+
 class BaseWikimediaEngine(IMEngine):
     # Put temp files and fifos into their own temp folder to avoid
     # exploits where converters might access other files in the same folder
@@ -80,3 +84,23 @@
 
     def cleanup(self):
         shutil.rmtree(self.temp_dir, True)
+
+    def command(self, command, env=None):
+        returncode, stderr, stdout = ShellRunner.command(
+            command,
+            self.context,
+            env=env
+        )
+
+        if returncode != 0:
+            self.cleanup_source()
+            raise CommandError(
+                command,
+                stdout,
+                stderr,
+                returncode
+            )
+
+        self.cleanup_source()
+
+        return stdout
diff --git a/wikimedia_thumbor/engine/ghostscript/ghostscript.py 
b/wikimedia_thumbor/engine/ghostscript/ghostscript.py
index e051a57..8e80790 100644
--- a/wikimedia_thumbor/engine/ghostscript/ghostscript.py
+++ b/wikimedia_thumbor/engine/ghostscript/ghostscript.py
@@ -12,7 +12,6 @@
 # Ghostscript engine
 
 from wikimedia_thumbor.engine import BaseWikimediaEngine
-from wikimedia_thumbor.shell_runner import ShellRunner
 
 
 BaseWikimediaEngine.add_format(
@@ -54,24 +53,3 @@
         png = self.command(command)
 
         return super(Engine, self).create_image(png)
-
-    def command(self, command, env=None):
-        returncode, stderr, stdout = ShellRunner.command(
-            command,
-            self.context,
-            env=env
-        )
-
-        if returncode != 0:
-            self.cleanup_source()
-            raise Exception(
-                'CommandError',
-                command,
-                stdout,
-                stderr,
-                returncode
-            )
-
-        self.cleanup_source()
-
-        return stdout
diff --git a/wikimedia_thumbor/engine/vips/vips.py 
b/wikimedia_thumbor/engine/vips/vips.py
index 9befdcd..829eac1 100644
--- a/wikimedia_thumbor/engine/vips/vips.py
+++ b/wikimedia_thumbor/engine/vips/vips.py
@@ -11,24 +11,43 @@
 
 # VIPS engine
 
-import gi
 import logging
 import math
+import os
 
-if hasattr(gi, 'require_version'):
-    pgi = gi
-else:
-    import pgi
-
-pgi.require_version('Vips', '8.0')
-
-# VIPS is very chatty in the debug logs
-logging.disable(logging.DEBUG)
-from pgi.repository import Vips
-logging.disable(logging.NOTSET)
+from thumbor.utils import logger
 
 from wikimedia_thumbor.engine import BaseWikimediaEngine
+from wikimedia_thumbor.engine import CommandError
+from wikimedia_thumbor.shell_runner import ShellRunner  # noqa
 
+
+use_command_line = True
+
+try:
+    import gi
+    if hasattr(gi, 'require_version'):
+        logger.debug('[VIPS] gi found')
+        try:
+            gi.require_version('Vips', '8.0')
+            logging.disable(logging.DEBUG)
+            from gi.repository import Vips
+            logging.disable(logging.NOTSET)
+            logger.debug('[VIPS] VIPS found in gi repository')
+            use_command_line = False
+        except ImportError:
+            logger.debug('[VIPS] VIPS not found in gi repository')
+        except ValueError:
+            logger.debug('[VIPS] VIPS 8.0+ not found in gi repository')
+    else:
+        logger.debug('[VIPS] Wrong gi found (not PyGObject)')
+except ImportError:
+    logger.debug('[VIPS] gi not found')
+
+if use_command_line:
+    logger.debug('[VIPS] Will use command line')
+else:
+    logger.debug('[VIPS] Will use bindings')
 
 BaseWikimediaEngine.add_format(
     'image/tiff',
@@ -82,26 +101,75 @@
 
         self.original_buffer = buffer
 
-        # VIPS is very chatty in the debug logs
-        logging.disable(logging.DEBUG)
-
-        try:
-            page = self.context.request.page - 1
-            source = Vips.Image.new_from_buffer(buffer, 'page=%d' % page)
-        except AttributeError, gi.overrides.Vips.Error:
-            source = Vips.Image.new_from_buffer(buffer, '')
-
         shrink_factor = int(math.floor(
             float(self.context.vips['width'])
             /
             float(self.context.request.width)
         ))
 
-        source = source.shrink(shrink_factor, shrink_factor)
-        result = source.write_to_buffer('.png')
-
-        logging.disable(logging.NOTSET)
+        if use_command_line:
+            result = self.shrink_with_command(buffer, shrink_factor)
+        else:
+            result = self.shrink_with_bindings(buffer, shrink_factor)
 
         self.extension = original_ext
 
         return super(Engine, self).create_image(result)
+
+    def shrink_with_bindings(self, buffer, shrink_factor):
+        logger.debug('[VIPS] Shrinking with bindings')
+        logging.disable(logging.DEBUG)
+
+        if gi and hasattr(gi, 'overrides'):
+            exceptions = (AttributeError, gi.overrides.Vips.Error)
+        else:
+            exceptions = AttributeError
+
+        try:
+            page = self.context.request.page - 1
+            source = Vips.Image.new_from_buffer(buffer, 'page=%d' % page)
+        except exceptions:
+            source = Vips.Image.new_from_buffer(buffer, '')
+
+        source = source.shrink(shrink_factor, shrink_factor)
+        result = source.write_to_buffer('.png')
+
+        logging.disable(logging.NOTSET)
+        return result
+
+    def shrink_with_command(self, buffer, shrink_factor):
+        logger.debug('[VIPS] Shrinking with command')
+        self.prepare_source(buffer)
+
+        try:
+            source = "%s[page=%d]" % (
+                self.source,
+                self.context.request.page - 1
+            )
+        except AttributeError:
+            source = self.source
+
+        destination = os.path.join(self.temp_dir, 'vips_result.png')
+
+        command = [
+            self.context.config.VIPS_PATH,
+            'shrink',
+            source,
+            destination,
+            "%d" % shrink_factor,
+            "%d" % shrink_factor
+        ]
+
+        try:
+            self.command(command)
+        except CommandError as e:
+            ShellRunner.rm_f(destination)
+            raise e
+
+        with open(destination, 'rb') as f:
+            result = f.read()
+
+        self.cleanup_source()
+        ShellRunner.rm_f(destination)
+
+        return result

-- 
To view, visit https://gerrit.wikimedia.org/r/301573
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I272e3b62d5a6a4cd9c92f3bd0ac4950037224ce7
Gerrit-PatchSet: 4
Gerrit-Project: operations/debs/python-thumbor-wikimedia
Gerrit-Branch: master
Gerrit-Owner: Gilles <gdu...@wikimedia.org>
Gerrit-Reviewer: Filippo Giunchedi <fgiunch...@wikimedia.org>
Gerrit-Reviewer: Gilles <gdu...@wikimedia.org>
Gerrit-Reviewer: Hashar <has...@free.fr>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to