From bb14c416634ff925d5f5609e9ad035d26d85c3b4 Mon Sep 17 00:00:00 2001
From: Christophe Monniez <moc@odoo.com>
Date: Mon, 9 Oct 2017 15:45:58 +0200
Subject: [PATCH] [REF] core: Remove psycogreen dependency

Purpose:
    The psycogreen module is unmaintained. Last source update was in
    2015 and last pypi package was in 2012. Odoo only use a small
    part of the psycogreen module that could be written directly in
    the odoo code. This commit reproduce the small function from
    psycogreen used in ODoo and remove all depencies from it.
    Also the copyright and license are honored and the documentation
    is updated accordingly.

    pypi pckage: https://pypi.python.org/pypi/psycogreen
    bitbucket repos: https://bitbucket.org/dvarrazzo/psycogreen/
---
 debian/copyright                   | 30 ++++++++++++++++++++++++++++++
 doc/setup/deploy.rst               |  4 ----
 odoo/__init__.py                   | 22 ++++++++++++++++++++--
 requirements.txt                   |  1 -
 setup.cfg                          |  1 -
 setup.py                           |  1 -
 setup/package.dffedora             |  1 -
 setup/win32/winpy_requirements.txt |  1 -
 8 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/debian/copyright b/debian/copyright
index 65cb05636ddf..8c495fad5c8a 100644
--- a/debian/copyright
+++ b/debian/copyright
@@ -223,6 +223,36 @@ Files: addons/web/static/lib/fontawesome/css/font-awesome.css
 Copyright: Font Awesome by Dave Gandy - http://fontawesome.io
 License: MIT
 
+Files: odoo/__init__.py
+Copyright: Copyright (C) 2010-2012 Daniele Varrazzo <daniele.varrazzo@gmail.com>
+License:
+  Copyright (c) 2010-2012, Daniele Varrazzo <daniele.varrazzo@gmail.com>
+  All rights reserved.
+  .
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions are met:
+  .
+  * Redistributions of source code must retain the above copyright notice, this
+    list of conditions and the following disclaimer.
+  * Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation
+    and/or other materials provided with the distribution.
+  * The name of Daniele Varrazzo may not be used to endorse or promote
+    products derived from this software without specific prior written
+    permission.
+  .
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+Comment: This license only applies to the function 'gevent_wait_callback'
+
 Files: addons/web/static/lib/fontawesome/fonts/*
 Copyright: Font Awesome by Dave Gandy - http://fontawesome.io
 License: OFL-1.1
diff --git a/doc/setup/deploy.rst b/doc/setup/deploy.rst
index 6e54815496ad..ed1825298e23 100644
--- a/doc/setup/deploy.rst
+++ b/doc/setup/deploy.rst
@@ -214,10 +214,6 @@ Instead you must have a proxy redirecting requests whose URL starts with
 ``/longpolling/`` to the longpolling port. Other request should be proxied to
 the :option:`normal HTTP port <odoo-bin --http-port>`
 
-.. warning:: The livechat worker requires the ``psycogreen`` Python module,
-             which is not always included with all installation packages.
-             It can be manually installed with ``pip install psycogreen``.
-
 Configuration sample
 --------------------
 
diff --git a/odoo/__init__.py b/odoo/__init__.py
index 2814706b57aa..c6fa6e3032a6 100644
--- a/odoo/__init__.py
+++ b/odoo/__init__.py
@@ -18,9 +18,27 @@ evented = False
 if len(sys.argv) > 1 and sys.argv[1] == 'gevent':
     sys.argv.remove('gevent')
     import gevent.monkey
+    import psycopg2
+    from gevent.socket import wait_read, wait_write
     gevent.monkey.patch_all()
-    import psycogreen.gevent
-    psycogreen.gevent.patch_psycopg()
+
+    def gevent_wait_callback(conn, timeout=None):
+        """A wait callback useful to allow gevent to work with Psycopg."""
+        # Copyright (C) 2010-2012 Daniele Varrazzo <daniele.varrazzo@gmail.com>
+        # This function is borrowed from psycogreen module which is licensed
+        # under the BSD license (see in odoo/debian/copyright)
+        while 1:
+            state = conn.poll()
+            if state == psycopg2.extensions.POLL_OK:
+                break
+            elif state == psycopg2.extensions.POLL_READ:
+                wait_read(conn.fileno(), timeout=timeout)
+            elif state == psycopg2.extensions.POLL_WRITE:
+                wait_write(conn.fileno(), timeout=timeout)
+            else:
+                raise psycopg2.OperationalError(
+                    "Bad result from poll: %r" % state)
+    psycopg2.extensions.set_wait_callback(gevent_wait_callback)
     evented = True
 
 # Is the server running in prefork mode (e.g. behind Gunicorn).
diff --git a/requirements.txt b/requirements.txt
index 626cbfb12eff..fda778e7d96c 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -16,7 +16,6 @@ ofxparse==0.16
 passlib==1.6.5
 Pillow==3.4.1
 psutil==4.3.1
-psycogreen==1.0
 psycopg2==2.7.1
 pydot==1.2.3
 pyldap==2.4.28
diff --git a/setup.cfg b/setup.cfg
index 7fc4abb611a7..f9127ec44c44 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -27,7 +27,6 @@ requires =
   python3-passlib
   python3-pillow
   python3-psutil
-  python3-psycogreen
   python3-pydot
   python3-pyldap
   python3-pyparsing
diff --git a/setup.py b/setup.py
index 004575e28d41..437b4b609750 100644
--- a/setup.py
+++ b/setup.py
@@ -149,7 +149,6 @@ setup(
         'passlib',
         'pillow',  # windows binary http://www.lfd.uci.edu/~gohlke/pythonlibs/
         'psutil',  # windows binary code.google.com/p/psutil/downloads/list
-        'psycogreen',
         'psycopg2 >= 2.2',
         'pydot',
         'pyldap',  # optional
diff --git a/setup/package.dffedora b/setup/package.dffedora
index 5aa1f3a761fa..683f34c2d8ff 100644
--- a/setup/package.dffedora
+++ b/setup/package.dffedora
@@ -27,7 +27,6 @@ RUN dnf update -d 0 -e 0 -y && \
 	  python3-passlib \
 	  python3-pillow \
 	  python3-psutil \
-	  python3-psycogreen \
 	  python3-pydot \
 	  python3-pyldap \
 	  python3-pyparsing \
diff --git a/setup/win32/winpy_requirements.txt b/setup/win32/winpy_requirements.txt
index 70f696459ade..458473815ece 100644
--- a/setup/win32/winpy_requirements.txt
+++ b/setup/win32/winpy_requirements.txt
@@ -16,7 +16,6 @@ ofxparse==0.16
 passlib==1.6.5
 Pillow>=3.4.1
 psutil>=4.3.1
-psycogreen==1.0
 psycopg2==2.7.1
 pydot==1.2.3
 pyldap>=2.4.28
-- 
GitLab