From a5a7f3cf74d74a40b5aa407bf968ad0c91635243 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Tue, 29 Sep 2015 13:34:59 +0200
Subject: [PATCH] Python packaging of PKI client library

Now setup.py can be used to create a client-side only Python package and
to upload it on PyPI. For tox testing an env var is used to enforce a
full build and full installation of all Python files.
---
 setup.py | 94 +++++++++++++++++++++++++++++++++++++++++++---------------------
 tox.ini  |  2 ++
 2 files changed, 65 insertions(+), 31 deletions(-)

diff --git a/setup.py b/setup.py
index 8b606fb815ca74a4f35a48dbf22323c4a7b020c5..7297d305bc8a063f02215f57dc24f6f3a6d73b51 100644
--- a/setup.py
+++ b/setup.py
@@ -18,67 +18,99 @@
 # All rights reserved.
 #
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 import os
 import re
+import sys
+# don't use setuptools here! it breaks data_files installation
 from distutils.core import setup
 
 
-UPGRADE_SCRIPT = re.compile('^[0-9]+-.*')
-
-
 def find_upgrade(origroot, destroot):
+    upgrade_re = re.compile('^[0-9]+-.*')
     upgrades = {}
-    for dirpath, dirnames, filenames in os.walk(origroot):
+    for dirpath, _, filenames in os.walk(origroot):
         for filename in filenames:
-            if UPGRADE_SCRIPT.match(filename):
+            if upgrade_re.match(filename):
                 version = os.path.basename(dirpath)
                 orig = os.path.join(dirpath, filename)
                 dest = os.path.join(destroot, version)
                 upgrades.setdefault(dest, []).append(orig)
-    return upgrades
+    return list(upgrades.items())
 
-upgrades = {}
-upgrades.update(find_upgrade('base/common/upgrade',
-                             'share/pki/upgrade'))
-upgrades.update(find_upgrade('base/server/upgrade',
-                             'share/pki/server/upgrade'))
 
-setup(
-    author='Dogtag Certificate System Team',
-    author_email='pki-devel@redhat.com',
-    name='Dogtag PKI',
-    version='10',
-    description='Dogtag Certificate System',
-    license='GPL',
-    keywords='pki',
-    url='http://pki.fedoraproject.org/',
-    package_dir={
-        'pki': 'base/common/python/pki',
-        'pki.server': 'base/server/python/pki/server'
-    },
-    packages=[
-        'pki',
+package_dir = {'pki': 'base/common/python/pki'}
+packages = ['pki']
+scripts = []
+data_files = []
+requirements = ['python-nss', 'six']
+
+# only include server files for internal server builds
+if "PKI_PACKAGE" in os.environ:
+    print("'PKI_PACKAGE' env %s" % os.environ["PKI_PACKAGE"])
+    include_server = os.environ["PKI_PACKAGE"] == "SERVER"
+elif any(arg.endswith('.tox/dist') for arg in sys.argv):
+    # tox cannot set ENV vars for sdist command
+    print('detecting tox build')
+    include_server = True
+else:
+    print('skipping pki.server')
+    include_server = False
+
+if include_server:
+    print('including pki.server')
+    package_dir['pki.server'] = 'base/server/python/pki/server'
+    packages.extend([
         'pki.server',
         'pki.server.cli',
         'pki.server.deployment',
         'pki.server.deployment.scriptlets',
-    ],
-    scripts=[
+    ])
+    scripts.extend([
         'base/common/sbin/pki-upgrade',
         'base/server/sbin/pkidestroy',
         'base/server/sbin/pki-server',
         'base/server/sbin/pki-server-upgrade',
         'base/server/sbin/pkispawn',
         'base/java-tools/bin/pki',
-    ],
-    data_files=list(upgrades.items()),
+    ])
+    data_files.extend(
+        find_upgrade('base/common/upgrade', 'share/pki/upgrade')
+    )
+    data_files.extend(
+        find_upgrade('base/server/upgrade', 'share/pki/server/upgrade')
+    )
+
+
+setup(
+    author='Dogtag Certificate System Team',
+    author_email='pki-devel@redhat.com',
+    name='dogtag-pki',
+    version='10.2.6',
+    description='Dogtag Certificate System',
+    long_description="""\
+The Dogtag Certificate System is an enterprise-class open source
+Certificate Authority (CA). It is a full-featured system, and has been
+hardened by real-world deployments. It supports all aspects of certificate
+lifecycle management, including key archival, OCSP and smartcard management,
+and much more. The Dogtag Certificate System can be downloaded for free
+and set up in less than an hour.""",
+    license='GPL',
+    keywords='pki x509 cert certificate',
+    url='http://pki.fedoraproject.org/',
+    package_dir=package_dir,
+    packages=packages,
+    scripts=scripts,
+    data_files=data_files,
+    requirements=requirements,
     classifiers=[
         'Development Status :: 5 - Production/Stable',
         'Environment :: Web Environment',
         'Intended Audience :: System Administrators',
         'Operating System :: OS Independent',
         'Programming Language :: Python :: 2.7',
+        'Programming Language :: Python :: 3.4',
         'License :: OSI Approved :: GNU General Public License v2 (GPLv2)',
+        'Topic :: Security :: Cryptography',
     ],
 )
diff --git a/tox.ini b/tox.ini
index 748e3d0dbb5da7268aeb4ef385f349e1765d95c5..79852b28bf88512efea3a56c8217144d2b4ae34f 100644
--- a/tox.ini
+++ b/tox.ini
@@ -22,6 +22,8 @@
 envlist = py27,pep8,pep8py3,lint,lint3k,docs
 
 [testenv]
+setenv =
+    PKI_PACKAGE=SERVER
 # force installation of sphinx and lint in virtual env, otherwise
 # the command pick up the `pki` package from the system's site packages.
 install_command = pip install {opts} --force-reinstall --upgrade {packages}
-- 
2.4.3

