From c6eb6eafd157e65e879a0174af9af4dd86382deb Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Wed, 1 Jul 2015 13:15:30 +0200
Subject: [PATCH] In-tree tests and linting with tox

Before the patch it wasn't possible to run pylint outside a RPM build.
The Python sources were split into common and server files in two
separate trees. With setup.py and tox the pki package can now be
installed and tested in a virtual env.

Tox enables developers to automate installation and testing in Python
virtual environment. The new tox.ini performs several tasks with one
command:

 * It creates and installs a source distribution of pki packages and its
   command line scripts
 * It verifies that all CLI scripts can be execute (using its --help
   argument).
 * It runs pylint on all Python files and CLI scripts.
 * It can run flake8 on all Python and CLI files (disabled for now).
 * Finally it builds Sphinx autodocs.

I had to delay the root check in pkispawn and pkidestroy and modify two
files to get rid of Sphinx warnings.

https://fedorahosted.org/pki/ticket/696
http://tox.readthedocs.org
---
 .gitignore                            |  3 ++
 base/common/python/_static/.gitignore |  4 ++
 base/common/python/pki/cert.py        | 39 +++++++++--------
 base/server/sbin/pkidestroy           |  8 ++--
 base/server/sbin/pkispawn             |  8 ++--
 setup.py                              | 79 +++++++++++++++++++++++++++++++++++
 tox.ini                               | 79 +++++++++++++++++++++++++++++++++++
 7 files changed, 192 insertions(+), 28 deletions(-)
 create mode 100644 base/common/python/_static/.gitignore
 create mode 100644 setup.py
 create mode 100644 tox.ini

diff --git a/.gitignore b/.gitignore
index d3b7a61be334444b5d2d0aa1385106cc1f47c27e..d2ecc2f899555d7bdcf98bf616a3e7a0150cf6d5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
 .metadata/
 build/
 tests/dogtag/dev_java_tests/bin/
+.tox
+dist
+MANIFEST
diff --git a/base/common/python/_static/.gitignore b/base/common/python/_static/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..5e7d2734cfc60289debf74293817c0a8f572ff32
--- /dev/null
+++ b/base/common/python/_static/.gitignore
@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore
diff --git a/base/common/python/pki/cert.py b/base/common/python/pki/cert.py
index 1fe323f24058a73bd205c8868a2194e8b4cde02e..01d2a616b8f6063acb6a38ef0587e49966493100 100644
--- a/base/common/python/pki/cert.py
+++ b/base/common/python/pki/cert.py
@@ -1,26 +1,25 @@
 #!/usr/bin/python
-"""
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; version 2 of the License.
 
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2014 Red Hat, Inc.
+# All rights reserved.
+#
+# Authors:
+#     Abhishek Koneru <akoneru@redhat.com>
+#     Ade Lee <alee@redhat.com>
 
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
- Copyright (C) 2014 Red Hat, Inc.
- All rights reserved.
-
-Authors:
-    Abhishek Koneru <akoneru@redhat.com>
-    Ade Lee <alee@redhat.com>
-
-"""
 import copy
 import json
 import types
diff --git a/base/server/sbin/pkidestroy b/base/server/sbin/pkidestroy
index c2eb91a5aa897191f0fadda83970ec3e63bda674..abc11dc8d1fb9f407398b0cb61213f40ade41ce8 100755
--- a/base/server/sbin/pkidestroy
+++ b/base/server/sbin/pkidestroy
@@ -63,10 +63,6 @@ def main(argv):
 
     config.pki_deployment_executable = os.path.basename(argv[0])
 
-    # Only run this program as "root".
-    if not os.geteuid() == 0:
-        sys.exit("'%s' must be run as root!" % argv[0])
-
     # Set the umask
     os.umask(config.PKI_DEPLOYMENT_DEFAULT_UMASK)
 
@@ -130,6 +126,10 @@ def main(argv):
 
     interactive = False
 
+    # Only run this program as "root".
+    if not os.geteuid() == 0:
+        sys.exit("'%s' must be run as root!" % argv[0])
+
     while True:
 
         # -s <subsystem>
diff --git a/base/server/sbin/pkispawn b/base/server/sbin/pkispawn
index bebbf0b771a8a5b24347ffe5f97e0ce713a53e98..fb5a61a8f159d79e6456191df5e266427c4e3d0f 100755
--- a/base/server/sbin/pkispawn
+++ b/base/server/sbin/pkispawn
@@ -66,10 +66,6 @@ def main(argv):
 
     config.pki_deployment_executable = os.path.basename(argv[0])
 
-    # Only run this program as "root".
-    if not os.geteuid() == 0:
-        sys.exit("'%s' must be run as root!" % argv[0])
-
     # Set the umask
     os.umask(config.PKI_DEPLOYMENT_DEFAULT_UMASK)
 
@@ -130,6 +126,10 @@ def main(argv):
         parser.indent = 0
         print log.PKISPAWN_INTERACTIVE_INSTALLATION
 
+    # Only run this program as "root".
+    if not os.geteuid() == 0:
+        sys.exit("'%s' must be run as root!" % argv[0])
+
     while True:
         # -s <subsystem>
         if args.pki_subsystem is None:
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000000000000000000000000000000000000..dfb9efd9e54db6c26e63451ab5e5a717c424daa7
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,79 @@
+# Authors:
+#     Christian Heimes <cheimes@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2015 Red Hat, Inc.
+# All rights reserved.
+#
+
+import os
+from fnmatch import fnmatch
+from distutils.core import setup
+
+
+def find_upgrade(origroot, destroot):
+    upgrades = {}
+    for dirpath, dirnames, filenames in os.walk(origroot):
+        for filename in filenames:
+            if fnmatch(filename, '[0-9][0-9]-*'):
+                version = os.path.basename(dirpath)
+                orig = os.path.join(dirpath, filename)
+                dest = os.path.join(destroot, version)
+                upgrades.setdefault(dest, []).append(orig)
+    return upgrades
+
+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',
+        'pki.server',
+        'pki.server.cli',
+        'pki.server.deployment',
+        'pki.server.deployment.scriptlets',
+    ],
+    scripts=[
+        'base/common/sbin/pki-upgrade',
+        'base/server/sbin/pkidestroy',
+        'base/server/sbin/pki-server',
+        'base/server/sbin/pki-server-upgrade',
+        'base/server/sbin/pkispawn',
+    ],
+    data_files=upgrades.items(),
+    classifiers=[
+        'Development Status :: 5 - Production/Stable',
+        'Environment :: Web Environment',
+        'Intended Audience :: System Administrators',
+        'Operating System :: OS Independent',
+        'Programming Language :: Python :: 2.7',
+        'License :: OSI Approved :: GNU General Public License v2 (GPLv2)',
+    ],
+)
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000000000000000000000000000000000000..8e41a725ac7183794275b506a2710a7b9faf5e2d
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,79 @@
+# Authors:
+#     Christian Heimes <cheimes@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2015 Red Hat, Inc.
+# All rights reserved.
+#
+
+[tox]
+envlist = py27,lint,docs
+
+[testenv]
+# 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}
+
+[testenv:py27]
+sitepackages = True
+commands =
+    python2.7 {envbindir}/pkidestroy --help
+    python2.7 {envbindir}/pkispawn --help
+    python2.7 {envbindir}/pki-server --help
+    python2.7 {envbindir}/pki-server-upgrade --help
+    python2.7 {envbindir}/pki-upgrade --help
+
+[testenv:lint]
+basepython = python2.7
+sitepackages = True
+deps =
+    pylint
+commands =
+    pylint \
+        {envsitepackagesdir}/pki \
+        {envbindir}/pkidestroy \
+        {envbindir}/pkispawn \
+        {envbindir}/pki-server \
+        {envbindir}/pki-server-upgrade \
+        {envbindir}/pki-upgrade \
+        --rcfile={toxinidir}/dogtag.pylintrc \
+        {posargs}
+
+[testenv:pep8]
+basepython = python2.7
+sitepackages = False
+deps =
+    flake8
+    # flake8-import-order
+    pep8-naming
+commands =
+    flake8 {posargs}
+
+[testenv:docs]
+basepython = python2.7
+sitepackages = True
+changedir = base/common/python
+deps =
+    sphinx < 1.3.0
+commands =
+    sphinx-build -v -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html
+
+[flake8]
+exclude = .tox,*.egg,dist,build,conf.py,tests/*
+include = *.py,pki-upgrade,pkidestroy,pki-server,pki-server-upgrade,pkispawn
+show-source = true
+max-line-length = 99
+application-import-names = pki
+
-- 
2.4.3

