From e948a42f8bf7823b18ad4551a8fe8a5db991e966 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Mon, 8 Aug 2016 13:08:17 +0200
Subject: [PATCH] Improve setup.py for standalone Dogtag client releases

PyPI requires a different spelling of LGPLv3+ classifier.

The correct name for installation requirements is 'install_requires',
not 'requirements'.

Add a new version_info command that rewrites setup.py in place to
include the current version. This fixes a problem with source
distributions of the client package.
---
 base/common/python/setup.cfg |  2 +-
 base/common/python/setup.py  | 83 +++++++++++++++++++++++++++++++++-----------
 2 files changed, 63 insertions(+), 22 deletions(-)

diff --git a/base/common/python/setup.cfg b/base/common/python/setup.cfg
index ad4348612f3bfd1f5c8589f1ba1ef8019f560a1b..32f2126416f706bea04eb1c0fd65d205766ca122 100644
--- a/base/common/python/setup.cfg
+++ b/base/common/python/setup.cfg
@@ -2,5 +2,5 @@
 universal = 1
 
 [aliases]
-packages = clean --all egg_info bdist_wheel sdist --format=zip
+packages = clean --all version_info egg_info bdist_wheel sdist --format=zip
 release = packages register upload
diff --git a/base/common/python/setup.py b/base/common/python/setup.py
index 86e0704cd05e1fe81a3008f69411ee982c8f73ad..e0920c1e6d73280d748759429a8a11bf8c460f64 100644
--- a/base/common/python/setup.py
+++ b/base/common/python/setup.py
@@ -43,28 +43,67 @@ try:
 except ImportError:
     from distutils.core import setup
 
+from distutils.cmd import Command
+
+
+class VersionInfo(Command):
+    user_options = []
 
-def get_version(specfile='../../../specs/pki-core.spec'):
     version_re = re.compile('^Version:\s*(\d+\.\d+\.\d+)')
     release_re = re.compile('^Release:.*?([\d\.]+)')
-    version = release = None
-    with open(specfile) as f:
-        for line in f:
-            if version is None:
-                match = version_re.match(line)
-                if match is not None:
-                    version = match.group(1)
-            if release is None:
-                match = release_re.match(line)
-                if match is not None:
-                    release = match.group(1)
-            if version is not None and release is not None:
-                break
-    if version is None or release is None:
-        raise ValueError(version, release)
-    return "%s.%s" % (version, release)
-
-VERSION = get_version()
+    specfile = '../../../specs/pki-core.spec'
+
+    def initialize_options(self):
+        self.rpm_version = None
+
+    def finalize_options(self):
+        try:
+            version, release = self.get_version()
+        except IOError:
+            pass
+        else:
+            self.rpm_version = "%s.%s" % (version, release)
+
+    def run(self):
+        if self.rpm_version is not None:
+            self.distribution.metadata.version = self.rpm_version
+            self.rewrite_setup_py()
+        else:
+            raise ValueError(
+                'Cannot load version from {}'.format(self.specfile)
+            )
+
+    def get_version(self):
+        version = release = None
+        with open(self.specfile) as f:
+            for line in f:
+                if version is None:
+                    match = self.version_re.match(line)
+                    if match is not None:
+                        version = match.group(1)
+                if release is None:
+                    match = self.release_re.match(line)
+                    if match is not None:
+                        release = match.group(1)
+                if version is not None and release is not None:
+                    break
+        if version is None or release is None:
+            raise ValueError(version, release)
+        return version, release
+
+    def rewrite_setup_py(self):
+        with open(__file__) as f:
+            lines = list(f)
+        for i, line in enumerate(lines):
+            if line.startswith('VERSION ='):
+                lines[i] = "VERSION = '{}'\n".format(self.rpm_version)
+        with open(__file__, 'w') as f:
+            f.write(''.join(lines))
+
+
+# auto-generated by version_info
+VERSION = None
+
 
 setup(
     author='Dogtag Certificate System Team',
@@ -85,7 +124,8 @@ and set up in less than an hour.""",
     keywords='pki x509 cert certificate',
     url='http://pki.fedoraproject.org/',
     packages=['pki', 'pki.cli'],
-    requirements=['python-nss', 'requests', 'six'],
+    install_requires=['python-nss', 'requests', 'six'],
+    cmdclass={'version_info': VersionInfo},
     classifiers=[
         'Development Status :: 5 - Production/Stable',
         'Environment :: Web Environment',
@@ -93,7 +133,8 @@ and set up in less than an hour.""",
         'Operating System :: OS Independent',
         'Programming Language :: Python :: 2.7',
         'Programming Language :: Python :: 3.4',
-        'License :: OSI Approved :: GNU Lesser General Public License v3+ (LGPLv3+)',
+        'License :: OSI Approved :: GNU Lesser General Public License ' +
+            'v3 or later (LGPLv3+)',
         'Topic :: Security :: Cryptography',
     ],
 )
-- 
2.7.4

