From ee67dd1a50142d9aff2b66917c076a03536b8042 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Mon, 20 Jul 2015 09:56:09 +0200
Subject: [PATCH 13/18] Rewrite pylint-build-scan as improved Python script

The upgrade scripts don't have a .py file extension. For this reason
they are not picked up by pylint in tox.ini. Tox doesn't support shell
scripting. In order to check all files I rewrote the
pylint-build-scan.sh script as Python script.
---
 pylint-build-scan.py      | 130 ++++++++++++++++++++++++++++++++++++++++++++++
 pylint-build-scan.sh      |  34 ------------
 scripts/compose_functions |   2 +-
 specs/pki-core.spec       |   2 +-
 tox.ini                   |  29 +++++------
 5 files changed, 146 insertions(+), 51 deletions(-)
 create mode 100755 pylint-build-scan.py
 delete mode 100755 pylint-build-scan.sh

diff --git a/pylint-build-scan.py b/pylint-build-scan.py
new file mode 100755
index 0000000000000000000000000000000000000000..9cd8a98c42a6cfc869edbcb6c6cb97fb5615fb7e
--- /dev/null
+++ b/pylint-build-scan.py
@@ -0,0 +1,130 @@
+#!/usr/bin/python
+
+# 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.
+
+from __future__ import print_function, unicode_literals
+
+import argparse
+import os
+import fnmatch
+import pprint
+import subprocess
+import sys
+
+from distutils.sysconfig import get_python_lib  # pylint: disable=F0401
+
+SCRIPTPATH = os.path.dirname(os.path.abspath(__file__))
+FILENAMES = [
+    '{scriptpath}/pylint-build-scan.py',
+    '{sitepackages}/pki',
+    '{bin}/pki',
+    '{sbin}/pkispawn',
+    '{sbin}/pkidestroy',
+    '{sbin}/pki-upgrade',
+    '{sbin}/pki-server',
+    '{sbin}/pki-server-upgrade',
+]
+
+
+def tox_env(args):
+    """Paths for tox environment"""
+    prefix = args.prefix
+    env = {
+        'scriptpath': SCRIPTPATH,
+        'bin': os.path.join(prefix, 'bin'),
+        'sbin': os.path.join(prefix, 'bin'),
+        'sharepki': os.path.join(prefix, 'share', 'pki'),
+        'sitepackages': get_python_lib()
+    }
+    return env
+
+
+def rpm_env(args):
+    """Paths for RPM build environment"""
+    prefix = args.prefix
+    relative = get_python_lib().lstrip(os.sep)
+    env = {
+        'scriptpath': SCRIPTPATH,
+        'bin': os.path.join(prefix, 'usr', 'bin'),
+        'sbin': os.path.join(prefix, 'usr', 'sbin'),
+        'sharepki': os.path.join(prefix, 'usr', 'share', 'pki'),
+        'sitepackages': os.path.join(prefix, relative),
+    }
+    return env
+
+
+def find_upgrades(root):
+    """Find upgrade scripts"""
+    for dirpath, _, filenames in os.walk(root):
+        for filename in filenames:
+            if fnmatch.fnmatch(filename, '[0-9][0-9]-*'):
+                yield os.path.join(dirpath, filename)
+
+
+def main():
+    """Dogtag pylint script"""
+    parser = argparse.ArgumentParser(
+        description=main.__doc__,
+        epilog="Additional arguments can be passed to pylint with: "
+               "'-- --arg1 --arg2 ...'",
+    )
+    parser.add_argument('--verbose', action='store_true')
+    subparsers = parser.add_subparsers(dest='command')
+    subparsers.required = True
+
+    toxparser = subparsers.add_parser('tox', help='tox in-tree tests')
+    toxparser.add_argument('--prefix', dest='prefix', default=sys.prefix)
+    toxparser.add_argument('pylint_args', nargs=argparse.REMAINDER)
+    toxparser.set_defaults(get_env=tox_env)
+
+    rpmparser = subparsers.add_parser('rpm', help='RPM source tree tests')
+    rpmparser.add_argument('--prefix', dest='prefix', required=True)
+    rpmparser.add_argument('pylint_args', nargs=argparse.REMAINDER)
+    rpmparser.set_defaults(get_env=rpm_env)
+
+    args = parser.parse_args()
+    env = args.get_env(args)
+    if args.verbose:
+        pprint.pprint(env)
+    # sanity check
+    for key, path in env.items():
+        if not os.path.exists(path):
+            raise RuntimeError('{} ({}) does not exist'.format(key, path))
+
+    if args.pylint_args and args.pylint_args[0] == '--':
+        extra_args = args.pylint_args[1:]
+    else:
+        extra_args = args.pylint_args
+
+    pylint = [
+        'pylint',
+        '--rcfile={scriptpath}/dogtag.pylintrc'.format(**env)
+    ]
+    pylint.extend(extra_args)
+    pylint.extend(filename.format(**env) for filename in FILENAMES)
+    pylint.extend(find_upgrades('{sharepki}/upgrade'.format(**env)))
+    pylint.extend(find_upgrades('{sharepki}/server/upgrade'.format(**env)))
+    if args.verbose:
+        pprint.pprint(pylint)
+
+    return subprocess.call(pylint, cwd=env['sitepackages'])
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/pylint-build-scan.sh b/pylint-build-scan.sh
deleted file mode 100755
index 55c58d3954dfde4b17d45679b7805db537607df0..0000000000000000000000000000000000000000
--- a/pylint-build-scan.sh
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/bin/sh -x
-
-### To be used only while building pki-core. ###
-
-if [ -z "$1" ]
-then
-  echo "PKI codebase home not specified. Could not scan the python scripts. Returning 0 - for SUCCESS"
-  echo 0
-  exit 0
-fi
-
-HOME_DIR=$1
-
-SCRIPTPATH="$( cd $(dirname $0) ; pwd -P )"
-
-PYLINT_RC_FILE_PATH="$SCRIPTPATH/dogtag.pylintrc"
-
-PYTHON_PACKAGE_DIR="$HOME_DIR`python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"`"
-
-cd $PYTHON_PACKAGE_DIR
-
-FILES="pki/"
-FILES="$FILES $HOME_DIR/usr/bin/pki"
-FILES="$FILES $HOME_DIR/usr/sbin/pkispawn"
-FILES="$FILES $HOME_DIR/usr/sbin/pkidestroy"
-FILES="$FILES $HOME_DIR/usr/sbin/pki-upgrade"
-FILES="$FILES $HOME_DIR/usr/sbin/pki-server"
-FILES="$FILES $HOME_DIR/usr/sbin/pki-server-upgrade"
-FILES="$FILES $(find $HOME_DIR/usr/share/pki/upgrade -type f)"
-FILES="$FILES $(find $HOME_DIR/usr/share/pki/server/upgrade -type f)"
-
-pylint --rcfile=$PYLINT_RC_FILE_PATH $FILES
-
-exit $?
diff --git a/scripts/compose_functions b/scripts/compose_functions
index 14dd96572779d8ffbfbd58397e7d5876f1438f4a..c9b0de36a73e1276697278daf515994d2f8cfda3 100644
--- a/scripts/compose_functions
+++ b/scripts/compose_functions
@@ -42,7 +42,7 @@ export PKI_DOGTAG_DIR
 PKI_REDHAT_DIR="${PKI_DIR}/redhat"
 export PKI_REDHAT_DIR
 
-PKI_FILE_LIST="CMakeLists.txt COPYING CPackConfig.cmake ConfigureChecks.cmake DefineOptions.cmake README cmake_uninstall.cmake.in config.h.cmake pylint-build-scan.sh dogtag.pylintrc"
+PKI_FILE_LIST="CMakeLists.txt COPYING CPackConfig.cmake ConfigureChecks.cmake DefineOptions.cmake README cmake_uninstall.cmake.in config.h.cmake pylint-build-scan.py dogtag.pylintrc"
 export PKI_FILE_LIST
 
 PKI_CMAKE_DIR="cmake"
diff --git a/specs/pki-core.spec b/specs/pki-core.spec
index 49edc185b0ea5cabba49394d316ca09ecbd201b4..bf68937dd1c36ab7ead7c156876483329a95dae3 100644
--- a/specs/pki-core.spec
+++ b/specs/pki-core.spec
@@ -693,7 +693,7 @@ done
 
 %if ! 0%{?rhel}
 # Scanning the python code with pylint.
-sh ../pylint-build-scan.sh %{buildroot} `pwd`
+python2 ../pylint-build-scan.py rpm --prefix %{buildroot}
 if [ $? -ne 0 ]; then
     echo "pylint failed. RC: $?"
     exit 1
diff --git a/tox.ini b/tox.ini
index 54ff3b8734c1f267716222c03882ab3943cc4070..aacbddb2add194f7e68dc5c7e0974bd62d745f46 100644
--- a/tox.ini
+++ b/tox.ini
@@ -31,11 +31,11 @@ deps =
 [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
+    {envpython} {envbindir}/pkidestroy --help
+    {envpython} {envbindir}/pkispawn --help
+    {envpython} {envbindir}/pki-server --help
+    {envpython} {envbindir}/pki-server-upgrade --help
+    {envpython} {envbindir}/pki-upgrade --help
     py.test --capture=no --strict {posargs}
 
 [testenv:lint]
@@ -44,16 +44,15 @@ sitepackages = True
 deps =
     pylint
 commands =
-    pylint \
-        {envsitepackagesdir}/pki \
-        {envbindir}/pkidestroy \
-        {envbindir}/pkispawn \
-        {envbindir}/pki-server \
-        {envbindir}/pki-server-upgrade \
-        {envbindir}/pki-upgrade \
-        {envbindir}/pki \
-        --rcfile={toxinidir}/dogtag.pylintrc \
-        {posargs}
+    {envpython} {toxinidir}/pylint-build-scan.py tox
+
+[testenv:lint3k]
+basepython = python2.7
+sitepackages = True
+deps =
+    pylint
+commands =
+    {envpython} {toxinidir}/pylint-build-scan.py tox -- --py3k
 
 [testenv:pep8]
 basepython = python2.7
-- 
2.4.3

