[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Pki-devel] [PATCH 013-018] spring-cleaning for Python 3



Hello,

I had some outstanding patches for Python 3 in my queue. Today I split
them up in digestible portions. The attached patches all hand-written.
I'm planning to submit the auto-generated patches by python-modernize at
a later point.

https://fedorahosted.org/pki/ticket/1480

Christian
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

From 8008e389b432295330170da54e5cd7421c13eefd Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes redhat com>
Date: Mon, 20 Jul 2015 10:17:00 +0200
Subject: [PATCH 14/18] Use dict.iteritems() instead of dict.items()

iteritems() is the preferred way to iterate over key, value pairs.
Python-modernize can convert iteritems() to efficient code on Python 2
and 3.
---
 base/common/python/pki/cert.py    | 42 +++++++++++++++++++--------------------
 base/common/python/pki/encoder.py |  4 ++--
 base/common/python/pki/key.py     |  6 +++---
 base/common/python/pki/profile.py | 14 ++++++-------
 4 files changed, 32 insertions(+), 34 deletions(-)

diff --git a/base/common/python/pki/cert.py b/base/common/python/pki/cert.py
index 01d2a616b8f6063acb6a38ef0587e49966493100..61b04377eeef119b6a256410d5f4530d563a6547 100644
--- a/base/common/python/pki/cert.py
+++ b/base/common/python/pki/cert.py
@@ -74,7 +74,7 @@ class CertData(object):
         """ Return CertData object from JSON dict """
         cert_data = cls()
 
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['Link']:
                 if k in CertData.json_attribute_names:
                     setattr(cert_data, CertData.json_attribute_names[k], v)
@@ -130,7 +130,7 @@ class CertDataInfo(object):
     def from_json(cls, attr_list):
         """ Return CertDataInfo object from JSON dict """
         cert_data_info = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['Link']:
                 if k in CertDataInfo.json_attribute_names:
                     setattr(cert_data_info,
@@ -220,7 +220,7 @@ class CertRequestInfo(object):
     def from_json(cls, attr_list):
         cert_request_info = cls()
 
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['Link']:
                 if k in CertRequestInfo.json_attribute_names:
                     setattr(cert_request_info,
@@ -497,7 +497,7 @@ class CertEnrollmentRequest(object):
 
         enroll_request = cls()
 
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['Input', 'Output']:
                 if k in CertEnrollmentRequest.json_attribute_names:
                     setattr(enroll_request,
@@ -531,23 +531,21 @@ class CertReviewResponse(CertEnrollmentRequest):
     reviewing a certificate enrollment request.
     It contains a nonce required to perform action on the request.
     """
-
-    json_attribute_names = dict(
-        CertEnrollmentRequest.json_attribute_names.items() + {
-            'requestId': 'request_id', 'requestType': 'request_type',
-            'requestStatus': 'request_status', 'requestOwner': 'request_owner',
-            'requestCreationTime': 'request_creation_time',
-            'requestNotes': 'request_notes',
-            'requestModificationTime': 'request_modification_time',
-            'profileApprovedBy': 'profile_approved_by',
-            'profileSetId': 'profile_set_id', 'profileName': 'profile_name',
-            'profileIsVisible': 'profile_is_visible',
-            'profileDescription': 'profile_description',
-            'profileRemoteHost': 'profile_remote_host',
-            'profileRemoteAddr': 'profile_remote_address',
-            'ProfilePolicySet': 'policy_sets'
-        }.items()
-    )
+    json_attribute_names = CertEnrollmentRequest.json_attribute_names.copy()
+    json_attribute_names.update({
+        'requestId': 'request_id', 'requestType': 'request_type',
+        'requestStatus': 'request_status', 'requestOwner': 'request_owner',
+        'requestCreationTime': 'request_creation_time',
+        'requestNotes': 'request_notes',
+        'requestModificationTime': 'request_modification_time',
+        'profileApprovedBy': 'profile_approved_by',
+        'profileSetId': 'profile_set_id', 'profileName': 'profile_name',
+        'profileIsVisible': 'profile_is_visible',
+        'profileDescription': 'profile_description',
+        'profileRemoteHost': 'profile_remote_host',
+        'profileRemoteAddr': 'profile_remote_address',
+        'ProfilePolicySet': 'policy_sets'
+    })
 
     def __init__(self, profile_id=None, renewal=False, serial_number=None,
                  remote_host=None, remote_address=None, inputs=None,
@@ -591,7 +589,7 @@ class CertReviewResponse(CertEnrollmentRequest):
         #First read the values for attributes defined in CertEnrollmentRequest
         review_response = super(CertReviewResponse, cls).from_json(attr_list)
 
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['ProfilePolicySet'] and k not in \
                     CertEnrollmentRequest.json_attribute_names:
                 if k in CertReviewResponse.json_attribute_names:
diff --git a/base/common/python/pki/encoder.py b/base/common/python/pki/encoder.py
index 1af0feaadb7af51c4f31230b5038850866bc335a..84bf325065883dbe8442a35471a68853ff0b7ccc 100644
--- a/base/common/python/pki/encoder.py
+++ b/base/common/python/pki/encoder.py
@@ -33,7 +33,7 @@ class CustomTypeEncoder(json.JSONEncoder):
     """
     # pylint: disable=E0202
     def default(self, obj):
-        for k, v in TYPES.items():
+        for k, v in TYPES.iteritems():
             if isinstance(obj, v):
                 return {k: obj.__dict__}
         for t in NOTYPES.itervalues():
@@ -48,7 +48,7 @@ class CustomTypeEncoder(json.JSONEncoder):
         reverse_dict = {v: k for k, v in
                         object_class.json_attribute_names.iteritems()}
         new_dict = dict()
-        for k, v in attr_dict.items():
+        for k, v in attr_dict.iteritems():
             if k in reverse_dict:
                 new_dict[reverse_dict[k]] = v
             else:
diff --git a/base/common/python/pki/key.py b/base/common/python/pki/key.py
index 3ff6e3dba628181bbb72d5decf8403c8dd747881..5d6a79c245126c8ca7ae43fdf7f4ae00754f0a5d 100644
--- a/base/common/python/pki/key.py
+++ b/base/common/python/pki/key.py
@@ -69,7 +69,7 @@ class KeyData(object):
     def from_json(cls, attr_list):
         """ Return a KeyData object from a JSON dict """
         key_data = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k in KeyData.json_attribute_names:
                 setattr(key_data, KeyData.json_attribute_names[k], v)
             else:
@@ -123,7 +123,7 @@ class KeyInfo(object):
     def from_json(cls, attr_list):
         """ Return KeyInfo from JSON dict """
         key_info = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k in KeyInfo.json_attribute_names:
                 setattr(key_info, KeyInfo.json_attribute_names[k], v)
             else:
@@ -188,7 +188,7 @@ class KeyRequestInfo(object):
     def from_json(cls, attr_list):
         """ Return a KeyRequestInfo object from a JSON dict. """
         key_request_info = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k in KeyRequestInfo.json_attribute_names:
                 setattr(key_request_info,
                         KeyRequestInfo.json_attribute_names[k], v)
diff --git a/base/common/python/pki/profile.py b/base/common/python/pki/profile.py
index 050f854a4eeb9176e424ae38d8c473de0c26280a..57ff831b4276db14e38d2983044d09d9c662f1e3 100644
--- a/base/common/python/pki/profile.py
+++ b/base/common/python/pki/profile.py
@@ -59,7 +59,7 @@ class ProfileDataInfo(object):
             return None
 
         profile_data_info = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k in ProfileDataInfo.json_attribute_names:
                 setattr(profile_data_info,
                         ProfileDataInfo.json_attribute_names[k], v)
@@ -129,7 +129,7 @@ class Descriptor(object):
             return None
 
         descriptor = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k in Descriptor.json_attribute_names:
                 setattr(descriptor,
                         Descriptor.json_attribute_names[k], v)
@@ -260,7 +260,7 @@ class ProfileInput(object):
             return None
         profile_input = cls()
 
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['Attribute', 'ConfigAttribute']:
                 if k in ProfileInput.json_attribute_names:
                     setattr(profile_input,
@@ -345,7 +345,7 @@ class ProfileOutput(object):
             return None
 
         profile_output = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['attributes']:
                 if k in ProfileOutput.json_attribute_names:
                     setattr(profile_output,
@@ -467,7 +467,7 @@ class PolicyDefault(object):
             return None
 
         policy_def = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['policyAttribute', 'params']:
                 if k in PolicyDefault.json_attribute_names:
                     setattr(policy_def,
@@ -585,7 +585,7 @@ class PolicyConstraint(object):
             return None
 
         policy_constraint = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['constraint']:
                 if k in PolicyConstraint.json_attribute_names:
                     setattr(policy_constraint,
@@ -917,7 +917,7 @@ class Profile(object):
     @classmethod
     def from_json(cls, attr_list):
         profile_data = cls()
-        for k, v in attr_list.items():
+        for k, v in attr_list.iteritems():
             if k not in ['Input', 'Output', 'PolicySets']:
                 if k in Profile.json_attribute_names:
                     setattr(profile_data,
-- 
2.4.3

From 8f8c6320e436b631d8ed21dcc8026f9ae131b630 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes redhat com>
Date: Mon, 20 Jul 2015 10:22:50 +0200
Subject: [PATCH 15/18] Don't use the types module for builtin types

Since Python 2.2 most types in the types module refer to builtin type
objects, e.g. types.ListType is list.
---
 base/common/python/pki/cert.py    | 15 +++++++--------
 base/common/python/pki/key.py     |  5 ++---
 base/common/python/pki/profile.py | 27 +++++++++++++--------------
 3 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/base/common/python/pki/cert.py b/base/common/python/pki/cert.py
index 61b04377eeef119b6a256410d5f4530d563a6547..7df9acfd93f26b291a4aa21716c9c84135fe2d5c 100644
--- a/base/common/python/pki/cert.py
+++ b/base/common/python/pki/cert.py
@@ -22,7 +22,6 @@
 
 import copy
 import json
-import types
 
 import pki
 import pki.client as client
@@ -164,7 +163,7 @@ class CertDataInfoCollection(object):
         """ Populate object from JSON input """
         ret = cls()
         cert_infos = json_value['entries']
-        if not isinstance(cert_infos, types.ListType):
+        if not isinstance(cert_infos, list):
             ret.cert_data_info_list.append(CertDataInfo.from_json(cert_infos))
         else:
             for cert_info in cert_infos:
@@ -172,7 +171,7 @@ class CertDataInfoCollection(object):
                     CertDataInfo.from_json(cert_info))
 
         links = json_value['Link']
-        if not isinstance(links, types.ListType):
+        if not isinstance(links, list):
             ret.links.append(pki.Link.from_json(links))
         else:
             for link in links:
@@ -281,7 +280,7 @@ class CertRequestInfoCollection(object):
         """ Populate object from JSON input """
         ret = cls()
         cert_req_infos = json_value['entries']
-        if not isinstance(cert_req_infos, types.ListType):
+        if not isinstance(cert_req_infos, list):
             ret.cert_request_info_list.append(
                 CertRequestInfo.from_json(cert_req_infos))
         else:
@@ -290,7 +289,7 @@ class CertRequestInfoCollection(object):
                     CertRequestInfo.from_json(cert_info))
 
         links = json_value['Link']
-        if not isinstance(links, types.ListType):
+        if not isinstance(links, list):
             ret.links.append(pki.Link.from_json(links))
         else:
             for link in links:
@@ -506,7 +505,7 @@ class CertEnrollmentRequest(object):
                     setattr(enroll_request, k, v)
 
         inputs = attr_list['Input']
-        if not isinstance(inputs, types.ListType):
+        if not isinstance(inputs, list):
             enroll_request.inputs.append(profile.ProfileInput.from_json(inputs))
         else:
             for profile_input in inputs:
@@ -514,7 +513,7 @@ class CertEnrollmentRequest(object):
                     profile.ProfileInput.from_json(profile_input))
 
         outputs = attr_list['Output']
-        if not isinstance(outputs, types.ListType):
+        if not isinstance(outputs, list):
             enroll_request.outputs.append(
                 profile.ProfileOutput.from_json(outputs))
         else:
@@ -599,7 +598,7 @@ class CertReviewResponse(CertEnrollmentRequest):
                     setattr(review_response, k, v)
 
         profile_policy_sets = attr_list['ProfilePolicySet']
-        if not isinstance(profile_policy_sets, types.ListType):
+        if not isinstance(profile_policy_sets, list):
             review_response.policy_sets.append(
                 profile.ProfilePolicySet.from_json(profile_policy_sets))
         else:
diff --git a/base/common/python/pki/key.py b/base/common/python/pki/key.py
index 5d6a79c245126c8ca7ae43fdf7f4ae00754f0a5d..f428481f09e2a9346919c2730c248f72c722af88 100644
--- a/base/common/python/pki/key.py
+++ b/base/common/python/pki/key.py
@@ -26,7 +26,6 @@ KeyRequestClient REST API on a DRM
 """
 import base64
 import json
-import types
 import urllib
 
 import pki
@@ -157,7 +156,7 @@ class KeyInfoCollection(object):
         """ Return a KeyInfoCollection object from its JSON representation """
         ret = cls()
         infos = json_value['entries']
-        if not isinstance(infos, types.ListType):
+        if not isinstance(infos, list):
             ret.key_infos.append(KeyInfo.from_json(infos))
         else:
             for info in infos:
@@ -232,7 +231,7 @@ class KeyRequestInfoCollection(object):
         """
         ret = cls()
         infos = json_value['entries']
-        if not isinstance(infos, types.ListType):
+        if not isinstance(infos, list):
             ret.key_requests.append(KeyRequestInfo.from_json(infos))
         else:
             for info in infos:
diff --git a/base/common/python/pki/profile.py b/base/common/python/pki/profile.py
index 57ff831b4276db14e38d2983044d09d9c662f1e3..81e2a6c900dc5456815263116092961694f06efb 100644
--- a/base/common/python/pki/profile.py
+++ b/base/common/python/pki/profile.py
@@ -19,7 +19,6 @@
 
 import json
 import os
-import types
 
 
 import pki
@@ -86,7 +85,7 @@ class ProfileDataInfoCollection(object):
     def from_json(cls, attr_list):
         ret = cls()
         profile_data_infos = attr_list['entries']
-        if not isinstance(profile_data_infos, types.ListType):
+        if not isinstance(profile_data_infos, list):
             ret.profile_data_list.append(
                 ProfileDataInfo.from_json(profile_data_infos))
         else:
@@ -95,7 +94,7 @@ class ProfileDataInfoCollection(object):
                     ProfileDataInfo.from_json(profile_info))
 
         links = attr_list['Link']
-        if not isinstance(links, types.ListType):
+        if not isinstance(links, list):
             ret.links.append(pki.Link.from_json(links))
         else:
             for link in links:
@@ -269,7 +268,7 @@ class ProfileInput(object):
                     setattr(profile_input, k, v)
 
         attributes = attr_list['Attribute']
-        if not isinstance(attributes, types.ListType):
+        if not isinstance(attributes, list):
             profile_input.attributes.append(
                 ProfileAttribute.from_json(attributes))
         else:
@@ -278,7 +277,7 @@ class ProfileInput(object):
                     ProfileAttribute.from_json(profile_info))
 
         config_attributes = attr_list['ConfigAttribute']
-        if not isinstance(config_attributes, types.ListType):
+        if not isinstance(config_attributes, list):
             profile_input.config_attributes.append(
                 ProfileAttribute.from_json(config_attributes))
         else:
@@ -354,7 +353,7 @@ class ProfileOutput(object):
                     setattr(profile_output, k, v)
 
         attributes = attr_list['attributes']
-        if not isinstance(attributes, types.ListType):
+        if not isinstance(attributes, list):
             profile_output.attributes.append(
                 ProfileAttribute.from_json(attributes))
         else:
@@ -477,7 +476,7 @@ class PolicyDefault(object):
 
         if 'policyAttribute' in attr_list:
             attributes = attr_list['policyAttribute']
-            if not isinstance(attributes, types.ListType):
+            if not isinstance(attributes, list):
                 policy_def.policy_attributes.append(
                     ProfileAttribute.from_json(attributes))
             else:
@@ -487,7 +486,7 @@ class PolicyDefault(object):
 
         if 'params' in attr_list:
             params = attr_list['params']
-            if not isinstance(params, types.ListType):
+            if not isinstance(params, list):
                 policy_def.policy_params.append(
                     ProfileParameter.from_json(params))
             else:
@@ -595,7 +594,7 @@ class PolicyConstraint(object):
 
         if 'constraint' in attr_list:
             constraints = attr_list['constraint']
-            if not isinstance(constraints, types.ListType):
+            if not isinstance(constraints, list):
                 policy_constraint.add_constraint_value(
                     PolicyConstraintValue.from_json(constraints))
             else:
@@ -657,7 +656,7 @@ class ProfilePolicySet(object):
         policy_set = cls()
 
         policies = attr_list['policies']
-        if not isinstance(policies, types.ListType):
+        if not isinstance(policies, list):
             policy_set.policies.append(ProfilePolicy.from_json(policies))
         else:
             for policy in policies:
@@ -718,7 +717,7 @@ class PolicySet(object):
 
         policy_set.name = attr_list['id']
         policies = attr_list['value']
-        if not isinstance(policies, types.ListType):
+        if not isinstance(policies, list):
             policy_set.policy_list.append(ProfilePolicy.from_json(policies))
         else:
             for policy in policies:
@@ -783,7 +782,7 @@ class PolicySetList(object):
 
         policy_set_list = cls()
         policy_sets = attr_list['PolicySet']
-        if not isinstance(policy_sets, types.ListType):
+        if not isinstance(policy_sets, list):
             policy_set_list.policy_sets.append(PolicySet.from_json(policy_sets))
         else:
             for policy_set in policy_sets:
@@ -926,7 +925,7 @@ class Profile(object):
                     setattr(profile_data, k, v)
 
         profile_inputs = attr_list['Input']
-        if not isinstance(profile_inputs, types.ListType):
+        if not isinstance(profile_inputs, list):
             profile_data.inputs.append(ProfileInput.from_json(profile_inputs))
         else:
             for profile_input in profile_inputs:
@@ -934,7 +933,7 @@ class Profile(object):
                     ProfileInput.from_json(profile_input))
 
         profile_outputs = attr_list['Output']
-        if not isinstance(profile_outputs, types.ListType):
+        if not isinstance(profile_outputs, list):
             profile_data.outputs.append(
                 ProfileOutput.from_json(profile_outputs))
         else:
-- 
2.4.3

From da746f2827cc8bd62315fb091d464477d526bb57 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes redhat com>
Date: Mon, 20 Jul 2015 10:26:41 +0200
Subject: [PATCH 16/18] Remove import of exceptions module

The exceptions module is obsolete. All builtin exception classes are
globals.
---
 base/common/python/pki/crypto.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/base/common/python/pki/crypto.py b/base/common/python/pki/crypto.py
index 147e65447b7bc78782d471f7c7f3d9ed2868224a..6a99c13feaea44936ea2b09894f12d0a0ac556c6 100644
--- a/base/common/python/pki/crypto.py
+++ b/base/common/python/pki/crypto.py
@@ -22,7 +22,6 @@
 Module containing crypto classes.
 """
 import abc
-import exceptions
 import nss.nss as nss
 import os
 import shutil
@@ -106,7 +105,7 @@ class NSSCryptoProvider(CryptoProvider):
         """ Create an NSS database """
         if os.path.exists(db_dir):
             if not over_write:
-                raise exceptions.IOError("Directory already exists.")
+                raise IOError("Directory already exists.")
             if os.path.isdir(db_dir):
                 shutil.rmtree(db_dir)
             else:
-- 
2.4.3

From a3d13828cdc39da95cad6ca192799ef5a49c4625 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes redhat com>
Date: Mon, 20 Jul 2015 10:42:31 +0200
Subject: [PATCH 17/18] Simplify exception handling in pkihelper

Several methods except OSError before they except shutil.Error. In
Python 3 the second except clause will be ignored because in Python 3
shutil.Error is a subclass of OSError.

The body of the except clauses only differs in the logging message. A
single except clause with an isinstance() check has the same effect.
---
 .../python/pki/server/deployment/pkihelper.py      | 56 ++++++++++------------
 1 file changed, 24 insertions(+), 32 deletions(-)

diff --git a/base/server/python/pki/server/deployment/pkihelper.py b/base/server/python/pki/server/deployment/pkihelper.py
index 5bc4ffab814891aadf0508d0ae20bb8cc315bc90..bdda989cb0f46890f5222357a5b7cb88e2d696a3 100644
--- a/base/server/python/pki/server/deployment/pkihelper.py
+++ b/base/server/python/pki/server/deployment/pkihelper.py
@@ -1462,14 +1462,12 @@ class Directory:
                               dir_perms, file_perms, symlink_perms,
                               dir_acls, file_acls, symlink_acls,
                               recursive_flag, critical_failure)
-        except OSError as exc:
-            config.pki_log.error(log.PKI_OSERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
-            if critical_failure:
-                raise
-        except shutil.Error as exc:
-            config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
+        except (shutil.Error, OSError) as exc:
+            if isinstance(exc, shutil.Error):
+                msg = log.PKI_SHUTIL_ERROR_1
+            else:
+                msg = log.PKI_OSERROR_1
+            config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure:
                 raise
         return
@@ -1667,14 +1665,12 @@ class File:
                 record.permissions = perms
                 record.acls = acls
                 self.manifest_db.append(record)
-        except OSError as exc:
-            config.pki_log.error(log.PKI_OSERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
-            if critical_failure:
-                raise
-        except shutil.Error as exc:
-            config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
+        except (shutil.Error, OSError) as exc:
+            if isinstance(exc, shutil.Error):
+                msg = log.PKI_SHUTIL_ERROR_1
+            else:
+                msg = log.PKI_OSERROR_1
+            config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure:
                 raise
         return
@@ -1727,14 +1723,12 @@ class File:
             record.permissions = perms
             record.acls = acls
             self.manifest_db.append(record)
-        except OSError as exc:
-            config.pki_log.error(log.PKI_OSERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
-            if critical_failure:
-                raise
-        except shutil.Error as exc:
-            config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
+        except (shutil.Error, OSError) as exc:
+            if isinstance(exc, shutil.Error):
+                msg = log.PKI_SHUTIL_ERROR_1
+            else:
+                msg = log.PKI_OSERROR_1
+            config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure:
                 raise
         return
@@ -1799,14 +1793,12 @@ class File:
                 record.permissions = perms
                 record.acls = acls
                 self.manifest_db.append(record)
-        except OSError as exc:
-            config.pki_log.error(log.PKI_OSERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
-            if critical_failure:
-                raise
-        except shutil.Error as exc:
-            config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
-                                 extra=config.PKI_INDENTATION_LEVEL_2)
+        except (shutil.Error, OSError) as exc:
+            if isinstance(exc, shutil.Error):
+                msg = log.PKI_SHUTIL_ERROR_1
+            else:
+                msg = log.PKI_OSERROR_1
+            config.pki_log.error(msg, exc, extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure:
                 raise
         return
-- 
2.4.3

From a4ec73dacfdab25068559e4e48d89ca7c7ff8bf3 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes redhat com>
Date: Mon, 20 Jul 2015 11:11:35 +0200
Subject: [PATCH 18/18] Replace Exception.message with str(exc)

Python 3 has deprecated and remove Exception.message. Instead we should
simply use string formatting to print the message of an Exception.

>>> import pki
>>> pki.PKIException('msg')
PKIException('msg',)
>>> pki.PKIException('msg').message
'msg'
>>> str(pki.PKIException('msg'))
'msg'
>>> '%s' % pki.PKIException('msg')
'msg'
---
 base/common/python/pki/upgrade.py        | 10 +++++-----
 base/server/python/pki/server/upgrade.py |  9 ++++-----
 2 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/base/common/python/pki/upgrade.py b/base/common/python/pki/upgrade.py
index 5534069c1d61158f62b36923854296fe51878145..30911613aab42c045999943798a0ab096108005e 100644
--- a/base/common/python/pki/upgrade.py
+++ b/base/common/python/pki/upgrade.py
@@ -307,7 +307,7 @@ class PKIUpgradeScriptlet(object):
             if verbose:
                 traceback.print_exc()
             else:
-                print 'ERROR: ' + e.message
+                print 'ERROR: %s' % e
 
             message = 'Failed upgrading system.'
             if self.upgrader.silent:
@@ -320,7 +320,7 @@ class PKIUpgradeScriptlet(object):
                 if result == 'y':
                     return
 
-            raise pki.PKIException('Upgrade failed: ' + e.message, e)
+            raise pki.PKIException('Upgrade failed: %s' % e, e)
 
     def revert(self):
 
@@ -631,7 +631,7 @@ class PKIUpgrader(object):
                 if verbose:
                     traceback.print_exc()
                 else:
-                    print e.message
+                    print e
 
                 print
 
@@ -693,12 +693,12 @@ class PKIUpgrader(object):
 
                 print
 
-                message = 'Revert failed: ' + e.message
+                message = 'Revert failed: %s' % e
 
                 if verbose:
                     traceback.print_exc()
                 else:
-                    print e.message
+                    print e
 
                 print
 
diff --git a/base/server/python/pki/server/upgrade.py b/base/server/python/pki/server/upgrade.py
index c9426a07ce81cdad734072fedb0d60da4a988a83..1db945e12a08fd480b00b668afaef3b7fb37c363 100644
--- a/base/server/python/pki/server/upgrade.py
+++ b/base/server/python/pki/server/upgrade.py
@@ -95,7 +95,7 @@ class PKIServerUpgradeScriptlet(pki.upgrade.PKIUpgradeScriptlet):
                 if verbose:
                     traceback.print_exc()
                 else:
-                    print 'ERROR: ' + e.message
+                    print 'ERROR: %s' % e
 
                 message = 'Failed upgrading ' + str(instance) + ' instance.'
                 if self.upgrader.silent:
@@ -109,8 +109,7 @@ class PKIServerUpgradeScriptlet(pki.upgrade.PKIUpgradeScriptlet):
                         continue
 
                 raise pki.server.PKIServerException(
-                    'Upgrade failed in ' + str(instance) + ': ' + e.message,
-                    e, instance)
+                    'Upgrade failed in %s: %s' % (instance, e), e, instance)
 
     def upgrade_subsystems(self, instance):
 
@@ -132,7 +131,7 @@ class PKIServerUpgradeScriptlet(pki.upgrade.PKIUpgradeScriptlet):
                 if verbose:
                     traceback.print_exc()
                 else:
-                    print 'ERROR: ' + e.message
+                    print 'ERROR: %s' % e
 
                 message = 'Failed upgrading ' + str(subsystem) + ' subsystem.'
                 if self.upgrader.silent:
@@ -146,7 +145,7 @@ class PKIServerUpgradeScriptlet(pki.upgrade.PKIUpgradeScriptlet):
                         continue
 
                 raise pki.server.PKIServerException(
-                    'Upgrade failed in ' + str(subsystem) + ': ' + e.message,
+                    'Upgrade failed in %s: %s' % (subsystem, e),
                     e, instance, subsystem)
 
     def upgrade_subsystem(self, instance, subsystem):
-- 
2.4.3

Attachment: signature.asc
Description: OpenPGP digital signature


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]