From 367fc208bbe6ef6934207f6c36cc86ab5b81d0de Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Thu, 21 Jan 2016 13:18:36 +0100
Subject: [PATCH] Package pki client library for Python 3

Dogtag's Python code has been compatible with Python 3 for a while. A
new package pki-base-python3 provides the pki package for clients.
As of now pki.server is not packages for Python 3.

The pki-base package also provides pki-base-python2.

https://fedorahosted.org/pki/ticket/1739
---
 CMakeLists.txt                    | 26 +++++++++++++++++++++-
 base/common/python/CMakeLists.txt | 13 ++++++++++-
 base/server/CMakeLists.txt        |  4 ++--
 specs/pki-core.spec               | 47 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e4fd15e3917b06863a5d8c44c1064eb207c1918a..c74605633f09958532f386e709aaa8dd17a6e701 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -98,10 +98,34 @@ execute_process(
         ${PYTHON_EXECUTABLE} -c
         "from distutils.sysconfig import get_python_lib; print get_python_lib()"
     OUTPUT_VARIABLE
-        PYTHON_SITE_PACKAGES
+        PYTHON2_SITE_PACKAGES
     OUTPUT_STRIP_TRAILING_WHITESPACE
 )
 
+# CMake doesn't support multiple PythonInterp
+execute_process(
+    COMMAND
+        python3 -c
+        "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
+    OUTPUT_VARIABLE
+        PYTHON3_SITE_PACKAGES
+    ERROR_VARIABLE
+        PYTHON3_ERROR
+    RESULT_VARIABLE
+        PYTHON3_RESULT
+    OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+
+if(PYTHON3_RESULT)
+    message(WARNING "python3 not found: ${PYTHON3_RESULT}")
+    if(PYTHON3_ERROR)
+        message(WARNING ${PYTHON3_ERROR})
+    endif()
+    unset(PYTHON3_SITE_PACKAGES)
+endif()
+unset(PYTHON3_RESULT)
+unset(PYTHON3_ERROR)
+
 # config.h checks
 include(ConfigureChecks.cmake)
 configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
diff --git a/base/common/python/CMakeLists.txt b/base/common/python/CMakeLists.txt
index f6e57b949815f61f8b38f6d2d9da8df501029376..1aa82e20e74d2427962a46634f8d8afa1640e613 100644
--- a/base/common/python/CMakeLists.txt
+++ b/base/common/python/CMakeLists.txt
@@ -43,11 +43,22 @@ install(
     DIRECTORY
         pki
     DESTINATION
-        ${PYTHON_SITE_PACKAGES}
+        ${PYTHON2_SITE_PACKAGES}
     FILES_MATCHING PATTERN
         *.py
 )
 
+if(PYTHON3_SITE_PACKAGES)
+    install(
+        DIRECTORY
+            pki
+        DESTINATION
+            ${PYTHON3_SITE_PACKAGES}
+        FILES_MATCHING PATTERN
+            *.py
+    )
+endif()
+
 install(
     DIRECTORY
         ${CMAKE_CURRENT_BINARY_DIR}/html
diff --git a/base/server/CMakeLists.txt b/base/server/CMakeLists.txt
index 9e986a0451d135d0476d2c01b7ab199bfc8f3ca2..5a6aea96a2317655fb454967f9f218020443bcb8 100644
--- a/base/server/CMakeLists.txt
+++ b/base/server/CMakeLists.txt
@@ -32,12 +32,12 @@ install(
         WORLD_READ
 )
 
-# install Python libraries
+# install Python libraries (Python 2 only)
 install(
     DIRECTORY
         python/pki/server
     DESTINATION
-        ${PYTHON_SITE_PACKAGES}/pki
+        ${PYTHON2_SITE_PACKAGES}/pki
     FILES_MATCHING PATTERN
         *.py
 )
diff --git a/specs/pki-core.spec b/specs/pki-core.spec
index c69800ce5245c87f9c33e2db134d37687e6fd001..7aa77f5f61be67825cc27fed191f51f9f63ab76e 100644
--- a/specs/pki-core.spec
+++ b/specs/pki-core.spec
@@ -2,6 +2,11 @@
 %{!?__python2: %global __python2 /usr/bin/python2}
 %{!?python2_sitelib: %global python2_sitelib %(%{__python2} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}
 
+%if 0%{?rhel}
+%global with_python3 0
+%else
+%global with_python3 1
+%endif
 
 # Tomcat
 %if 0%{?fedora} >= 23
@@ -129,6 +134,13 @@ BuildRequires:    tomcatjss >= 7.1.2
 %endif
 %endif
 
+%if 0%{?with_python3}
+BuildRequires:  python3-devel
+BuildRequires:  python3-nss
+BuildRequires:  python3-requests
+BuildRequires:  python3-six
+%endif  # with_python3
+BuildRequires:  python-devel
 
 # additional build requirements needed to build native 'tpsclient'
 # REMINDER:  Revisit these once 'tpsclient' is rewritten as a Java app
@@ -182,6 +194,8 @@ PKI Core contains ALL top-level java-based Tomcat PKI components:      \
                                                                        \
   * pki-symkey                                                         \
   * pki-base                                                           \
+  * pki-base-python2 (alias for pki-base)                              \
+  * pki-base-python3                                                   \
   * pki-base-java                                                      \
   * pki-tools                                                          \
   * pki-server                                                         \
@@ -265,6 +279,7 @@ BuildArch:        noarch
 
 Provides:         pki-common = %{version}-%{release}
 Provides:         pki-util = %{version}-%{release}
+Provides:         pki-base-python2 = %{version}-%{release}
 
 Obsoletes:        pki-common < %{version}-%{release}
 Obsoletes:        pki-util < %{version}-%{release}
@@ -338,6 +353,28 @@ This package is a part of the PKI Core used by the Certificate System.
 
 %{overview}
 
+%if 0%{?with_python3}
+
+%package -n       pki-base-python3
+Summary:          Certificate System - PKI Framework
+Group:            System Environment/Base
+
+BuildArch:        noarch
+
+Requires:         pki-base = %{version}-%{release}
+
+Requires:         python3-nss
+Requires:         python3-requests
+Requires:         python3-six
+
+%description -n   pki-base-python3
+This package contains PKI client library for Python 3.
+
+This package is a part of the PKI Core used by the Certificate System.
+
+%{overview}
+
+%endif  # with_python3 for python3-pki
 
 %package -n       pki-tools
 Summary:          Certificate System - PKI Tools
@@ -392,6 +429,9 @@ Obsoletes:        pki-silent < %{version}-%{release}
 Requires:         java-headless >= 1:1.7.0
 Requires:         net-tools
 
+Requires:         python-ldap
+Requires:         python-lxml
+
 %if 0%{?rhel}
 Requires:    nuxwdog-client-java >= 1.0.1-11
 %else
@@ -890,6 +930,13 @@ systemctl daemon-reload
 %{_javadir}/pki/pki-nsutil.jar
 %{_javadir}/pki/pki-certsrv.jar
 
+%if %{with_python3}
+%files -n pki-base-python3
+%defattr(-,root,root,-)
+%doc base/common/LICENSE
+%{python3_sitelib}/pki
+%endif # with_python3
+
 %files -n pki-tools
 %defattr(-,root,root,-)
 %doc base/native-tools/LICENSE base/native-tools/doc/README
-- 
2.5.0

