From 3a2d9907f08dfc4acd70403af0a2058a34313b9f Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 28 Feb 2014 12:42:22 -0500
Subject: [PATCH] Replaced CLI wrapper with Python.

The existing CLI wrapper script was written in Perl to call Java CLI.
It has been replaced with a Python script that can call either the
existing Java CLI or a not-yet-implemented Python CLI by specifying
a --client-type parameter. This will allow testing the Python client
library via CLI in the future.
---
 base/java-tools/CMakeLists.txt |   6 +-
 base/java-tools/bin/pki        | 156 +++++++++++++++++++++++++++++++++++++++++
 base/java-tools/pki            | 139 ------------------------------------
 3 files changed, 159 insertions(+), 142 deletions(-)
 create mode 100644 base/java-tools/bin/pki
 delete mode 100755 base/java-tools/pki

diff --git a/base/java-tools/CMakeLists.txt b/base/java-tools/CMakeLists.txt
index d34290690469117321195ff7ec97231dd4f65a4e..5541659d78e62274444d085f58bfad47f1be6366 100644
--- a/base/java-tools/CMakeLists.txt
+++ b/base/java-tools/CMakeLists.txt
@@ -12,11 +12,11 @@ install(
 )
 
 install(
-    FILES
-        pki
+    DIRECTORY
+        bin/
     DESTINATION
         ${BIN_INSTALL_DIR}
-    PERMISSIONS
+    FILE_PERMISSIONS
         OWNER_EXECUTE OWNER_WRITE OWNER_READ
         GROUP_EXECUTE GROUP_READ
         WORLD_EXECUTE WORLD_READ
diff --git a/base/java-tools/bin/pki b/base/java-tools/bin/pki
new file mode 100644
index 0000000000000000000000000000000000000000..64438b58a2c108d6c8dc16fc4c767e00ac9ee910
--- /dev/null
+++ b/base/java-tools/bin/pki
@@ -0,0 +1,156 @@
+#!/usr/bin/python
+# Authors:
+#     Endi S. Dewata <edewata@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) 2014 Red Hat, Inc.
+# All rights reserved.
+#
+
+import os
+import shlex
+import subprocess
+import sys
+
+def run_java_cli(args):
+
+    # construct libpath based on the architecture
+    arch = subprocess.check_output('arch')
+    arch = arch.strip()
+
+    if arch == 'i686':
+        libpath = [
+            '/usr/lib/jss',
+            '/usr/lib',
+            '/lib'
+        ]
+
+    elif arch == 'x86_64':
+        libpath = [
+            '/usr/lib64/jss',
+            '/usr/lib64',
+            '/lib64',
+            '/usr/lib/jss',
+            '/usr/lib',
+            '/lib'
+        ]
+
+    else:
+        raise Exception('Unsupported architecture: ' + arch)
+
+    # read RESTEasy library path
+    value = subprocess.check_output(
+        '. /etc/pki/pki.conf && echo $RESTEASY_LIB',
+        shell=True)
+    resteasy_lib = str(value).strip()
+
+    # construct classpath
+    classpath = [
+        '/usr/share/java/commons-cli.jar',
+        '/usr/share/java/commons-codec.jar',
+        '/usr/share/java/commons-httpclient.jar',
+        '/usr/share/java/commons-io.jar',
+        '/usr/share/java/commons-lang.jar',
+        '/usr/share/java/commons-logging.jar',
+        '/usr/share/java/httpcomponents/httpclient.jar',
+        '/usr/share/java/httpcomponents/httpcore.jar',
+        '/usr/share/java/jackson-annotations.jar',
+        '/usr/share/java/jackson-core.jar',
+        '/usr/share/java/jackson-databind.jar',
+        '/usr/share/java/jackson-module-jaxb-annotations.jar',
+        '/usr/share/java/jackson/jackson-core-asl.jar',
+        '/usr/share/java/jackson/jackson-jaxrs.jar',
+        '/usr/share/java/jackson/jackson-mapper-asl.jar',
+        '/usr/share/java/jackson/jackson-mrbean.jar',
+        '/usr/share/java/jackson/jackson-smile.jar',
+        '/usr/share/java/jackson/jackson-xc.jar',
+        '/usr/share/java/jackson-jaxrs-providers/jackson-jaxrs-base.jar',
+        '/usr/share/java/jackson-jaxrs-providers/jackson-jaxrs-json-provider.jar',
+        '/usr/share/java/jaxb-api.jar',
+        '/usr/share/java/servlet.jar',
+        resteasy_lib + '/jaxrs-api.jar',
+        resteasy_lib + '/resteasy-atom-provider.jar',
+        resteasy_lib + '/resteasy-client.jar',
+        resteasy_lib + '/resteasy-jaxb-provider.jar',
+        resteasy_lib + '/resteasy-jaxrs.jar',
+        resteasy_lib + '/resteasy-jaxrs-jandex.jar',
+        resteasy_lib + '/resteasy-jackson-provider.jar',
+        '/usr/share/java/pki/pki-nsutil.jar',
+        '/usr/share/java/pki/pki-cmsutil.jar',
+        '/usr/share/java/pki/pki-certsrv.jar',
+        '/usr/share/java/pki/pki-tools.jar',
+    ]
+
+    if arch == 'i686':
+        classpath.append('/usr/lib/java/jss4.jar')
+
+    elif arch == 'x86_64':
+        classpath.append('/usr/lib64/java/jss4.jar')
+        classpath.append('/usr/lib/java/jss4.jar')
+
+    command = [
+        'java',
+        '-Djava.library.path=' + (':'.join(libpath)),
+        '-cp',
+        ':'.join(classpath),
+        'com.netscape.cmstools.cli.MainCLI'
+    ]
+
+    command.extend(args)
+
+    subprocess.call(command)
+
+
+def run_python_cli(args):
+
+    raise Exception('Not implemented')
+
+
+def main(argv):
+
+    # read global options
+    value = subprocess.check_output(
+        '. /etc/pki/pki.conf && echo $PKI_CLI_OPTIONS',
+        shell=True)
+    args = shlex.split(value.strip())
+    args.extend(argv[1:])
+
+    client_type = 'java'
+
+    new_args = []
+
+    # read --client-type parameter and remove it from the argument list
+    i = 0
+    while i < len(args):
+        if args[i] == '--client-type':
+            client_type = args[i + 1]
+            i = i + 1
+
+        else:
+            new_args.append(args[i])
+
+        i = i + 1
+
+    if client_type == 'java':
+        run_java_cli(new_args)
+
+    elif client_type == 'python':
+        run_python_cli(new_args)
+
+    else:
+        raise Exception('Unsupported client type: ' + client_type)
+
+if __name__ == '__main__':
+    main(sys.argv)
diff --git a/base/java-tools/pki b/base/java-tools/pki
deleted file mode 100755
index 9dfd7ec6e033dc2aec37c370bbf03708a309cc33..0000000000000000000000000000000000000000
--- a/base/java-tools/pki
+++ /dev/null
@@ -1,139 +0,0 @@
-#!/usr/bin/perl
-# --- BEGIN COPYRIGHT BLOCK ---
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-#
-# This library 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
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor,
-# Boston, MA  02110-1301  USA
-#
-# Copyright (C) 2007 Red Hat, Inc.
-# All rights reserved.
-# --- END COPYRIGHT BLOCK ---
-
-###############################################################################
-##  (1) Specify variables used by this script.                               ##
-###############################################################################
-
-my $PRODUCT="pki";
-my $libpath="";
-
-
-###############################################################################
-##  (2) Define helper functions.                                             ##
-###############################################################################
-
-sub invalid_architecture()
-{
-    print "\n";
-    print "ERROR:  pki does not execute on this architecture\n";
-    print "ERROR:  check to make sure pki-tools package is installed!\n";
-    print "\n";
-}
-
-
-###############################################################################
-##  (3) Set the LD_LIBRARY_PATH environment variable                         ##
-##      (as well as the ${libpath} java property) to determine the           ##
-##      search order this command uses to find shared libraries.             ##
-###############################################################################
-
-my $ARCHITECTURE=`arch`;
-chop( $ARCHITECTURE );
-
-if( $ARCHITECTURE eq "i686" ) {
-    $libpath="/usr/lib";
-
-    $ENV{LD_LIBRARY_PATH} = "/usr/lib/jss:"
-                          . "/usr/lib:/lib";
-} elsif($ARCHITECTURE eq "x86_64") {
-    $libpath="/usr/lib64";
-
-    $ENV{LD_LIBRARY_PATH} = "/usr/lib64/jss:"
-                          . "/usr/lib64:/lib64:"
-                          . "/usr/lib/jss:"
-                          . "/usr/lib:/lib";
-} else {
-    invalid_architecture();
-    exit(255);
-}
-
-
-###############################################################################
-##  (4) Set the CP environment variable to determine the search              ##
-##      order this command wrapper uses to find jar files.                   ##
-###############################################################################
-
-my $RESTEASY_LIB = `. /etc/pki/pki.conf && echo \$RESTEASY_LIB`;
-chomp($RESTEASY_LIB);
-
-$ENV{CLASSPATH} = "/usr/share/java/${PRODUCT}/pki-certsrv.jar:"
-                . "/usr/share/java/${PRODUCT}/pki-nsutil.jar:"
-                . "/usr/share/java/${PRODUCT}/pki-cmsutil.jar:"
-                . "/usr/share/java/${PRODUCT}/pki-tools.jar:"
-                . "/usr/share/java/apache-commons-cli.jar:"
-                . "/usr/share/java/apache-commons-codec.jar:"
-                . "/usr/share/java/apache-commons-io.jar:"
-                . "/usr/share/java/apache-commons-lang.jar:"
-                . "/usr/share/java/apache-commons-logging.jar:"
-                . "/usr/share/java/commons-httpclient.jar:"
-                . "/usr/share/java/commons-io.jar:"
-                . "/usr/share/java/httpcomponents/httpclient.jar:"
-                . "/usr/share/java/httpcomponents/httpcore.jar:"
-                . "/usr/share/java/jackson-annotations.jar:"
-                . "/usr/share/java/jackson/jackson-core-asl.jar:"
-                . "/usr/share/java/jackson-core.jar:"
-                . "/usr/share/java/jackson-databind.jar:"
-                . "/usr/share/java/jackson-jaxrs-providers/jackson-jaxrs-base.jar:"
-                . "/usr/share/java/jackson/jackson-jaxrs.jar:"
-                . "/usr/share/java/jackson-jaxrs-providers/jackson-jaxrs-json-provider.jar:"
-                . "/usr/share/java/jackson/jackson-mapper-asl.jar:"
-                . "/usr/share/java/jackson-module-jaxb-annotations.jar:"
-                . "/usr/share/java/jackson/jackson-mrbean.jar:"
-                . "/usr/share/java/jackson/jackson-smile.jar:"
-                . "/usr/share/java/jackson/jackson-xc.jar:"
-                . "/usr/share/java/jaxb-api.jar:"
-                . "${RESTEASY_LIB}/jaxrs-api.jar:"
-                . "${RESTEASY_LIB}/resteasy-atom-provider.jar:"
-                . "${RESTEASY_LIB}/resteasy-client.jar:"
-                . "${RESTEASY_LIB}/resteasy-jaxb-provider.jar:"
-                . "${RESTEASY_LIB}/resteasy-jaxrs.jar:"
-                . "${RESTEASY_LIB}/resteasy-jaxrs-jandex.jar:"
-                . "${RESTEASY_LIB}/resteasy-jackson-provider.jar:"
-                . "/usr/share/java/servlet.jar:";
-
-if( $ARCHITECTURE eq "x86_64" ) {
-    $ENV{CLASSPATH} = $ENV{CLASSPATH}
-                    . "/usr/lib64/java/jss4.jar:"
-                    . "/usr/lib/java/jss4.jar:";
-} else {
-    $ENV{CLASSPATH} = $ENV{CLASSPATH}
-                    . "/usr/lib/java/jss4.jar:";
-}
-
-
-###############################################################################
-##  (5) Execute the java command specified by this java command wrapper      ##
-##      based upon the preset LD_LIBRARY_PATH and CP environment variables.  ##
-###############################################################################
-
-my $PKI_CLI_OPTIONS = `. /etc/pki/pki.conf && echo \$PKI_CLI_OPTIONS`;
-chomp($PKI_CLI_OPTIONS);
-
-my @args = ();
-foreach (@ARGV) {
-    push(@args, quotemeta($_));
-}
-
-my $command = "java -cp $ENV{CLASSPATH} com.netscape.cmstools.cli.MainCLI $PKI_CLI_OPTIONS @args";
-
-system($command) and do {
-    exit $? >> 8;
-}
-- 
1.8.4.2

