From 9be97617ca40f65a19f27a9ccd95249a4de9b713 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Wed, 6 Oct 2021 12:33:01 +0200
Subject: [PATCH] use python3-psycopg2 in the virtualenv (#55980)

---
 roles/base/tasks/main.yml              | 49 ++++++++++++++++++++++----
 roles/base/templates/PSYCOPG2-METADATA | 38 ++++++++++++++++++++
 roles/base/templates/getpsycopg2.j2    |  7 ++++
 3 files changed, 88 insertions(+), 6 deletions(-)
 create mode 100644 roles/base/templates/PSYCOPG2-METADATA
 create mode 100644 roles/base/templates/getpsycopg2.j2

diff --git a/roles/base/tasks/main.yml b/roles/base/tasks/main.yml
index 981672b..ca1c935 100644
--- a/roles/base/tasks/main.yml
+++ b/roles/base/tasks/main.yml
@@ -62,12 +62,6 @@
     name: "django-debug-toolbar<3"
     virtualenv: "{{venv_py3}}"
 
-- name: install psycopg2-binary
-  pip:
-    name: psycopg2-binary
-    version: 2.8.6
-    virtualenv: "{{venv_py3}}"
-
 - name: install pytest
   pip:
     name: pytest
@@ -112,6 +106,49 @@
 - name: execute getlasso
   command: "{{venv_py3_bin}}/getlasso.sh"
 
+- name: clean psycopg2 pip installation
+  ansible.builtin.pip:
+    name:
+      - psycopg2-binary
+      - psycopg2
+    state: absent
+    virtualenv: "{{venv_py3}}"
+
+- name: guess venv site-packages path
+  ansible.builtin.command: "{{venv_py3_python}} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())'"
+  register: site_packages
+
+- name: set venv_py3_site_packages
+  ansible.builtin.set_fact:
+    venv_py3_site_packages: "{{site_packages.stdout}}"
+
+- name: create getpsycopg2 scripts
+  ansible.builtin.template:
+    src: getpsycopg2.j2
+    dest: "{{venv_py3_bin}}/getpsycopg2.sh"
+    mode: "u=rwx,g=rx,o=rx"
+
+- name: execute getpsycopg2
+  ansible.builtin.command: "{{venv_py3_bin}}/getpsycopg2.sh"
+
+- name: Gather the package facts
+  ansible.builtin.package_facts:
+    manager: auto
+
+- name: set python3_psycopg2_version
+  ansible.builtin.set_fact:
+    python3_psycopg2_version: "{{ansible_facts.packages['python3-psycopg2'][0]['version']}}"
+
+- name: create psycopg .dist-info
+  ansible.builtin.file:
+    path: "{{venv_py3_site_packages}}/psycopg2-{{python3_psycopg2_version}}.dist-info"
+    state: directory
+
+- name: copy psycopg2 metadata
+  ansible.builtin.template:
+    src: PSYCOPG2-METADATA
+    dest: "{{venv_py3_site_packages}}/psycopg2-{{python3_psycopg2_version}}.dist-info/METADATA"
+
 - name: create get-uwsgidecorators scripts
   template:
     src: get-uwsgidecorators.j2
diff --git a/roles/base/templates/PSYCOPG2-METADATA b/roles/base/templates/PSYCOPG2-METADATA
new file mode 100644
index 0000000..ab8c467
--- /dev/null
+++ b/roles/base/templates/PSYCOPG2-METADATA
@@ -0,0 +1,38 @@
+Metadata-Version: 2.1
+Name: psycopg2
+Version: {{python3_psycopg2_version}}
+Summary: psycopg2 - Python-PostgreSQL Database Adapter
+Home-page: https://psycopg.org/
+Author: Federico Di Gregorio
+Author-email: fog@initd.org
+Maintainer: Daniele Varrazzo
+Maintainer-email: daniele.varrazzo@gmail.org
+License: LGPL with exceptions
+Project-URL: Homepage, https://psycopg.org/
+Project-URL: Documentation, https://www.psycopg.org/docs/
+Project-URL: Code, https://github.com/psycopg/psycopg2
+Project-URL: Issue Tracker, https://github.com/psycopg/psycopg2/issues
+Project-URL: Download, https://pypi.org/project/psycopg2/
+Platform: any
+Classifier: Development Status :: 5 - Production/Stable
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
+Classifier: Programming Language :: Python
+Classifier: Programming Language :: Python :: 2
+Classifier: Programming Language :: Python :: 2.7
+Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python :: 3.4
+Classifier: Programming Language :: Python :: 3.5
+Classifier: Programming Language :: Python :: 3.6
+Classifier: Programming Language :: Python :: 3.7
+Classifier: Programming Language :: Python :: 3.8
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: C
+Classifier: Programming Language :: SQL
+Classifier: Topic :: Database
+Classifier: Topic :: Database :: Front-Ends
+Classifier: Topic :: Software Development
+Classifier: Topic :: Software Development :: Libraries :: Python Modules
+Classifier: Operating System :: Microsoft :: Windows
+Classifier: Operating System :: Unix
+Requires-Python: >=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*
diff --git a/roles/base/templates/getpsycopg2.j2 b/roles/base/templates/getpsycopg2.j2
new file mode 100644
index 0000000..545bd3f
--- /dev/null
+++ b/roles/base/templates/getpsycopg2.j2
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+# Clean up
+rm -f {{venv_py3_site_packages}}/psycopg2
+
+# symlink
+ln -sv /usr/lib/python3/dist-packages/psycopg2 {{venv_py3_site_packages}}/
-- 
2.30.2

