URL: https://github.com/freeipa/freeipa/pull/264
Author: tiran
 Title: #264: Python3 pylint fixes
Action: synchronized

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/264/head:pr264
git checkout pr264
From 07a5c1a62f2a42728544a305480b7ff1ecfac8c1 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Wed, 23 Nov 2016 10:04:43 +0100
Subject: [PATCH 1/2] Python3 pylint fixes

Sprinkle 'pylint disable' comments over the code base to silence a bunch
of pylint warnings on Python 3. All silenced warnings are harmless and
not bugs.

https://fedorahosted.org/freeipa/ticket/4985

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 client/ipa-client-automount                 | 2 +-
 install/tools/ipa-adtrust-install           | 2 +-
 install/tools/ipa-compat-manage             | 2 +-
 install/tools/ipa-csreplica-manage          | 2 +-
 install/tools/ipa-dns-install               | 2 +-
 install/tools/ipa-managed-entries           | 2 +-
 install/tools/ipa-nis-manage                | 2 +-
 install/tools/ipa-replica-conncheck         | 2 ++
 ipaclient/ipa_certupdate.py                 | 2 ++
 ipalib/cli.py                               | 6 +++---
 ipalib/plugable.py                          | 2 +-
 ipalib/rpc.py                               | 2 ++
 ipalib/text.py                              | 4 +++-
 ipapython/admintool.py                      | 2 +-
 ipapython/config.py                         | 5 ++++-
 ipapython/install/cli.py                    | 2 +-
 ipapython/ipautil.py                        | 2 ++
 ipaserver/install/certs.py                  | 6 +++++-
 ipaserver/install/ipa_cacert_manage.py      | 2 +-
 ipaserver/install/ipa_replica_prepare.py    | 2 ++
 ipaserver/install/ipa_server_certinstall.py | 2 +-
 ipatests/i18n.py                            | 2 +-
 lite-server.py                              | 2 +-
 makeapi                                     | 2 +-
 24 files changed, 40 insertions(+), 21 deletions(-)

diff --git a/client/ipa-client-automount b/client/ipa-client-automount
index b4aa7e8..53c0537 100755
--- a/client/ipa-client-automount
+++ b/client/ipa-client-automount
@@ -39,7 +39,7 @@ import SSSDConfig
 from six.moves.urllib.parse import urlsplit
 # pylint: enable=import-error
 
-from optparse import OptionParser
+from optparse import OptionParser  # pylint: disable=deprecated-module
 from ipalib import api, errors
 from ipapython import sysrestore
 from ipapython import ipautil
diff --git a/install/tools/ipa-adtrust-install b/install/tools/ipa-adtrust-install
index 8eed519..c8d5894 100755
--- a/install/tools/ipa-adtrust-install
+++ b/install/tools/ipa-adtrust-install
@@ -29,7 +29,7 @@ import ldap
 
 import six
 
-from optparse import SUPPRESS_HELP
+from optparse import SUPPRESS_HELP  # pylint: disable=deprecated-module
 
 from ipaserver.install import adtrustinstance
 from ipaserver.install.installutils import (
diff --git a/install/tools/ipa-compat-manage b/install/tools/ipa-compat-manage
index 905495f..307af1f 100755
--- a/install/tools/ipa-compat-manage
+++ b/install/tools/ipa-compat-manage
@@ -24,7 +24,7 @@ from __future__ import print_function
 import sys
 from ipaplatform.paths import paths
 try:
-    from optparse import OptionParser
+    from optparse import OptionParser  # pylint: disable=deprecated-module
     from ipapython import ipautil, config
     from ipaserver.install import installutils
     from ipaserver.install.ldapupdate import LDAPUpdate
diff --git a/install/tools/ipa-csreplica-manage b/install/tools/ipa-csreplica-manage
index 532e353..a4d0a88 100755
--- a/install/tools/ipa-csreplica-manage
+++ b/install/tools/ipa-csreplica-manage
@@ -50,7 +50,7 @@ commands = {
 
 
 def parse_options():
-    from optparse import OptionParser
+    from optparse import OptionParser  # pylint: disable=deprecated-module
 
     parser = OptionParser(version=version.VERSION)
     parser.add_option("-H", "--host", dest="host", help="starting host")
diff --git a/install/tools/ipa-dns-install b/install/tools/ipa-dns-install
index 9c63e12..699749d 100755
--- a/install/tools/ipa-dns-install
+++ b/install/tools/ipa-dns-install
@@ -24,7 +24,7 @@ from __future__ import print_function
 import os
 import sys
 
-from optparse import SUPPRESS_HELP
+from optparse import SUPPRESS_HELP  # pylint: disable=deprecated-module
 
 from ipaserver.install import bindinstance
 from ipaserver.install import installutils
diff --git a/install/tools/ipa-managed-entries b/install/tools/ipa-managed-entries
index 5761020..59f14fc 100755
--- a/install/tools/ipa-managed-entries
+++ b/install/tools/ipa-managed-entries
@@ -22,7 +22,7 @@ from __future__ import print_function
 
 import re
 import sys
-from optparse import OptionParser
+from optparse import OptionParser  # pylint: disable=deprecated-module
 
 from ipapython import config
 from ipaserver.install import installutils
diff --git a/install/tools/ipa-nis-manage b/install/tools/ipa-nis-manage
index b72e273..fae458c 100755
--- a/install/tools/ipa-nis-manage
+++ b/install/tools/ipa-nis-manage
@@ -25,7 +25,7 @@ import sys
 import os
 from ipaplatform.paths import paths
 try:
-    from optparse import OptionParser
+    from optparse import OptionParser  # pylint: disable=deprecated-module
     from ipapython import ipautil, config
     from ipaserver.install import installutils
     from ipaserver.install.ldapupdate import LDAPUpdate
diff --git a/install/tools/ipa-replica-conncheck b/install/tools/ipa-replica-conncheck
index 4045e41..7ec1ef8 100755
--- a/install/tools/ipa-replica-conncheck
+++ b/install/tools/ipa-replica-conncheck
@@ -27,7 +27,9 @@ from ipapython import ipautil, certdb
 from ipalib import api, errors, x509
 from ipaserver.install import installutils
 import ipaclient.ipachangeconf
+# pylint: disable=deprecated-module
 from optparse import OptionGroup, OptionValueError
+# pylint: enable=deprecated-module
 from ipapython.ipa_log_manager import root_logger, standard_logging_setup
 import sys
 import os
diff --git a/ipaclient/ipa_certupdate.py b/ipaclient/ipa_certupdate.py
index 6bf9831..6e41de9 100644
--- a/ipaclient/ipa_certupdate.py
+++ b/ipaclient/ipa_certupdate.py
@@ -99,7 +99,9 @@ def run(self):
         if server_fstore.has_files():
             self.update_server(certs)
             try:
+                # pylint: disable=import-error
                 from ipaserver.install import cainstance
+                # pylint: enable=import-error
                 cainstance.add_lightweight_ca_tracking_requests(
                     self.log, lwcas)
             except Exception:
diff --git a/ipalib/cli.py b/ipalib/cli.py
index ad876ea..696be62 100644
--- a/ipalib/cli.py
+++ b/ipalib/cli.py
@@ -27,7 +27,7 @@
 import sys
 import getpass
 import code
-import optparse
+import optparse  # pylint: disable=deprecated-module
 import fcntl
 import termios
 import struct
@@ -41,8 +41,8 @@
     unicode = str
 
 if six.PY2:
-    reload(sys)                         # pylint: disable=reload-builtin
-    sys.setdefaultencoding('utf-8')     # pylint: disable=no-member
+    reload(sys)  # pylint: disable=reload-builtin, undefined-variable
+    sys.setdefaultencoding('utf-8')  # pylint: disable=no-member
 
 from ipalib import frontend
 from ipalib import backend
diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index 76fb9fd..31e2cdf 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -31,7 +31,7 @@
 import threading
 import os
 from os import path
-import optparse
+import optparse  # pylint: disable=deprecated-module
 import textwrap
 import collections
 import importlib
diff --git a/ipalib/rpc.py b/ipalib/rpc.py
index 7756eaf..bd13251 100644
--- a/ipalib/rpc.py
+++ b/ipalib/rpc.py
@@ -623,7 +623,9 @@ def single_request(self, host, handler, request_body, verbose=0):
 
             while True:
                 if six.PY2:
+                    # pylint: disable=no-value-for-parameter
                     self.send_request(h, handler, request_body)
+                    # pylint: enable=no-value-for-parameter
                     self.send_host(h, host)
                     self.send_user_agent(h)
                     self.send_content(h, request_body)
diff --git a/ipalib/text.py b/ipalib/text.py
index 34e5f1a..f4f810c 100644
--- a/ipalib/text.py
+++ b/ipalib/text.py
@@ -254,7 +254,7 @@ def as_unicode(self):
         else:
             t = create_translation(self.key)
         if six.PY2:
-            return t.ugettext(self.msg)
+            return t.ugettext(self.msg)  # pylint: disable=no-member
         else:
             return t.gettext(self.msg)
 
@@ -409,7 +409,9 @@ def __call__(self, count):
         else:
             t = create_translation(self.key)
         if six.PY2:
+            # pylint: disable=no-member
             return t.ungettext(self.singular, self.plural, count)
+            # pylint: enable=no-member
         else:
             return t.ngettext(self.singular, self.plural, count)
 
diff --git a/ipapython/admintool.py b/ipapython/admintool.py
index ec0e7e0..78bbcec 100644
--- a/ipapython/admintool.py
+++ b/ipapython/admintool.py
@@ -25,7 +25,7 @@
 import sys
 import os
 import traceback
-from optparse import OptionGroup
+from optparse import OptionGroup  # pylint: disable=deprecated-module
 
 from ipapython import version
 from ipapython import config
diff --git a/ipapython/config.py b/ipapython/config.py
index 8e5708e..f71c696 100644
--- a/ipapython/config.py
+++ b/ipapython/config.py
@@ -17,7 +17,10 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-from optparse import Option, Values, OptionParser, IndentedHelpFormatter, OptionValueError
+# pylint: disable=deprecated-module
+from optparse import (
+    Option, Values, OptionParser, IndentedHelpFormatter, OptionValueError)
+# pylint: enable=deprecated-module
 from copy import copy
 
 from dns import resolver, rdatatype
diff --git a/ipapython/install/cli.py b/ipapython/install/cli.py
index 7fbb725..b6f872e 100644
--- a/ipapython/install/cli.py
+++ b/ipapython/install/cli.py
@@ -9,7 +9,7 @@
 import collections
 import enum
 import functools
-import optparse
+import optparse  # pylint: disable=deprecated-module
 import signal
 
 import six
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index f7d7537..3a827d9 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -690,7 +690,9 @@ def __contains__(self, key):
 
     if six.PY2:
         def has_key(self, key):
+            # pylint: disable=no-member
             return super(CIDict, self).has_key(key.lower())
+            # pylint: enable=no-member
 
     def get(self, key, failobj=None):
         try:
diff --git a/ipaserver/install/certs.py b/ipaserver/install/certs.py
index 852bcec..75a0412 100644
--- a/ipaserver/install/certs.py
+++ b/ipaserver/install/certs.py
@@ -30,6 +30,7 @@
 import time
 import datetime
 
+import six
 from six.moves import configparser
 
 from ipapython.ipa_log_manager import root_logger
@@ -720,7 +721,10 @@ def _do(self, func, owner):
 
     def _read(self, fileobj):
         p = configparser.RawConfigParser()
-        p.readfp(fileobj)
+        if six.PY2:
+            p.readfp(fileobj)  # pylint: disable=deprecated-method
+        else:
+            p.read_file(fileobj)  # pylint: disable=no-member
 
         try:
             self._locked = p.getboolean('lock', 'locked')
diff --git a/ipaserver/install/ipa_cacert_manage.py b/ipaserver/install/ipa_cacert_manage.py
index 0dcb70f..9a09b2a 100644
--- a/ipaserver/install/ipa_cacert_manage.py
+++ b/ipaserver/install/ipa_cacert_manage.py
@@ -20,7 +20,7 @@
 from __future__ import print_function
 
 import os
-from optparse import OptionGroup
+from optparse import OptionGroup  # pylint: disable=deprecated-module
 from cryptography.hazmat.primitives import serialization
 import gssapi
 
diff --git a/ipaserver/install/ipa_replica_prepare.py b/ipaserver/install/ipa_replica_prepare.py
index 227d296..1b6a961 100644
--- a/ipaserver/install/ipa_replica_prepare.py
+++ b/ipaserver/install/ipa_replica_prepare.py
@@ -25,7 +25,9 @@
 import shutil
 import tempfile
 import time
+# pylint: disable=deprecated-module
 from optparse import OptionGroup, SUPPRESS_HELP
+# pylint: enable=deprecated-module
 
 import dns.resolver
 # pylint: disable=import-error
diff --git a/ipaserver/install/ipa_server_certinstall.py b/ipaserver/install/ipa_server_certinstall.py
index 3c30876..9cb7ed1 100644
--- a/ipaserver/install/ipa_server_certinstall.py
+++ b/ipaserver/install/ipa_server_certinstall.py
@@ -21,7 +21,7 @@
 import os
 import os.path
 import pwd
-import optparse
+import optparse  # pylint: disable=deprecated-module
 
 from ipaplatform.constants import constants
 from ipaplatform.paths import paths
diff --git a/ipatests/i18n.py b/ipatests/i18n.py
index 1f48e65..b86380c 100755
--- a/ipatests/i18n.py
+++ b/ipatests/i18n.py
@@ -23,7 +23,7 @@
 
 # WARNING: Do not import ipa modules, this is also used as a
 # stand-alone script (invoked from po Makefile).
-import optparse
+import optparse  # pylint: disable=deprecated-module
 import sys
 import gettext
 import re
diff --git a/lite-server.py b/lite-server.py
index 0e86791..cd4f09c 100755
--- a/lite-server.py
+++ b/lite-server.py
@@ -32,7 +32,7 @@
 """
 
 from os import path, getcwd
-import optparse
+import optparse  # pylint: disable=deprecated-module
 from paste import httpserver
 import paste.gzipper
 from paste.urlmap import URLMap
diff --git a/makeapi b/makeapi
index a02a491..d0a7295 100755
--- a/makeapi
+++ b/makeapi
@@ -86,7 +86,7 @@ OUTPUT_IGNORED_ATTRIBUTES = (
 )
 
 def parse_options():
-    from optparse import OptionParser
+    from optparse import OptionParser  # pylint: disable=deprecated-module
 
     parser = OptionParser()
     parser.add_option("--validate", dest="validate", action="store_true",

From 91cbd9e28e97cdb18bb00c05e52aea9478cdd236 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Wed, 23 Nov 2016 11:01:56 +0100
Subject: [PATCH 2/2] Fix Python 3 bugs discovered by pylint

In Python 3 exception instances no longer have a message attribute.
For most exceptions, str(e) or string formatting give the same result.

Fix some renamed modules, module members and functions.

https://fedorahosted.org/freeipa/ticket/4985

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipalib/krb_utils.py                           |  2 +-
 ipapython/ipaldap.py                          |  5 ++++-
 ipaserver/install/dns.py                      |  4 ++--
 ipaserver/plugins/aci.py                      |  2 +-
 ipaserver/plugins/dns.py                      |  2 +-
 ipatests/i18n.py                              | 12 ++++++++++--
 ipatests/test_integration/env_config.py       |  4 ++--
 ipatests/test_integration/tasks.py            |  3 ++-
 ipatests/test_integration/test_idviews.py     |  2 +-
 ipatests/test_xmlrpc/test_automount_plugin.py |  2 +-
 10 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/ipalib/krb_utils.py b/ipalib/krb_utils.py
index e6e277c..d005a87 100644
--- a/ipalib/krb_utils.py
+++ b/ipalib/krb_utils.py
@@ -161,7 +161,7 @@ def get_credentials(name=None, ccache_name=None):
         return gssapi.Credentials(usage='initiate', name=name, store=store)
     except gssapi.exceptions.GSSError as e:
         if e.min_code == KRB5_FCC_NOFILE:  # pylint: disable=no-member
-            raise ValueError('"%s", ccache="%s"' % (e.message, ccache_name))
+            raise ValueError('"%s", ccache="%s"' % (e, ccache_name))
         raise
 
 def get_principal(ccache_name=None):
diff --git a/ipapython/ipaldap.py b/ipapython/ipaldap.py
index bbfc6f6..94a5e1e 100644
--- a/ipapython/ipaldap.py
+++ b/ipapython/ipaldap.py
@@ -27,7 +27,10 @@
 import collections
 import os
 import pwd
-from urlparse import urlparse
+
+# pylint: disable=import-error
+from six.moves.urllib.parse import urlparse
+# pylint: enable=import-error
 
 import ldap
 import ldap.sasl
diff --git a/ipaserver/install/dns.py b/ipaserver/install/dns.py
index 5b40c03..ecf03ed 100644
--- a/ipaserver/install/dns.py
+++ b/ipaserver/install/dns.py
@@ -132,7 +132,7 @@ def install_check(standalone, api, replica, options, hostname):
             if options.force or options.allow_zone_overlap:
                 root_logger.warning("%s Please make sure that the domain is "
                                     "properly delegated to this IPA server.",
-                                    e.message)
+                                    e)
             else:
                 raise e
 
@@ -141,7 +141,7 @@ def install_check(standalone, api, replica, options, hostname):
             dnsutil.check_zone_overlap(reverse_zone)
         except ValueError as e:
             if options.force or options.allow_zone_overlap:
-                root_logger.warning(e.message)
+                root_logger.warning(six.text_type(e))
             else:
                 raise e
 
diff --git a/ipaserver/plugins/aci.py b/ipaserver/plugins/aci.py
index c353715..e09a0ab 100644
--- a/ipaserver/plugins/aci.py
+++ b/ipaserver/plugins/aci.py
@@ -354,7 +354,7 @@ def _aci_to_kw(ldap, a, test=False, pkey_only=False):
                 try:
                     targetdn = DN(target.replace('ldap:///',''))
                 except ValueError as e:
-                    raise errors.ValidationError(name='subtree', error=_("invalid DN (%s)") % e.message)
+                    raise errors.ValidationError(name='subtree', error=_("invalid DN (%s)") % e)
                 if targetdn.endswith(DN(api.env.container_group, api.env.basedn)):
                     kw['targetgroup'] = targetdn[0]['cn']
                 else:
diff --git a/ipaserver/plugins/dns.py b/ipaserver/plugins/dns.py
index e37050a..d8d9fd7 100644
--- a/ipaserver/plugins/dns.py
+++ b/ipaserver/plugins/dns.py
@@ -2149,7 +2149,7 @@ def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
             try:
                 check_zone_overlap(keys[-1], raise_on_error=False)
             except ValueError as e:
-                raise errors.InvocationError(e.message)
+                raise errors.InvocationError(six.text_type(e))
 
         return dn
 
diff --git a/ipatests/i18n.py b/ipatests/i18n.py
index b86380c..b21a907 100755
--- a/ipatests/i18n.py
+++ b/ipatests/i18n.py
@@ -32,6 +32,8 @@
 import polib
 from collections import namedtuple
 
+import six
+
 '''
 We test our translations by taking the original untranslated string
 (e.g. msgid) and prepend a prefix character and then append a suffix
@@ -607,8 +609,14 @@ def test_translations(po_file, lang, domain, locale_dir):
 
     t = gettext.translation(domain, locale_dir)
 
-    get_msgstr = t.ugettext
-    get_msgstr_plural = t.ungettext
+    if six.PY2:
+        # pylint: disable=no-member
+        get_msgstr = t.ugettext
+        get_msgstr_plural = t.ungettext
+        # pylint: enable=no-member
+    else:
+        get_msgstr = t.gettext
+        get_msgstr_plural = t.ngettext
 
     return po_file_iterate(po_file, get_msgstr, get_msgstr_plural)
 
diff --git a/ipatests/test_integration/env_config.py b/ipatests/test_integration/env_config.py
index 6e3770f..2a137c6 100644
--- a/ipatests/test_integration/env_config.py
+++ b/ipatests/test_integration/env_config.py
@@ -114,8 +114,8 @@ def config_from_env(env):
         try:
             import yaml
         except ImportError as e:
-            raise ImportError("%s, please install PyYAML package to fix it" %
-                              e.message)
+            raise ImportError(
+                "%s, please install PyYAML package to fix it" % e)
         with open(env['IPATEST_YAML_CONFIG']) as file:
             confdict = yaml.safe_load(file)
             return Config.from_dict(confdict)
diff --git a/ipatests/test_integration/tasks.py b/ipatests/test_integration/tasks.py
index c6aab32..58b3a65 100644
--- a/ipatests/test_integration/tasks.py
+++ b/ipatests/test_integration/tasks.py
@@ -1032,7 +1032,8 @@ def install_topo(topo, master, replicas, clients, domain_level=None,
 
 def install_clients(servers, clients):
     """Install IPA clients, distributing them among the given servers"""
-    for server, client in itertools.izip(itertools.cycle(servers), clients):
+    izip = getattr(itertools, 'izip', zip)
+    for server, client in izip(itertools.cycle(servers), clients):
         log.info('Installing client %s on %s' % (server, client))
         install_client(server, client)
 
diff --git a/ipatests/test_integration/test_idviews.py b/ipatests/test_integration/test_idviews.py
index 9df8c0a..8a117e0 100644
--- a/ipatests/test_integration/test_idviews.py
+++ b/ipatests/test_integration/test_idviews.py
@@ -68,7 +68,7 @@ def install(cls, mh):
         # Initialize NSS database
         tasks.run_certutil(master, ["-N", "-f", cls.pwname], cls.reqdir)
         # Now generate self-signed certs for a windows user
-        stdin_text = string.digits+string.letters[2:] + '\n'
+        stdin_text = string.digits+string.ascii_letters[2:] + '\n'
         tasks.run_certutil(master, ['-S', '-s',
                                     "cn=%s,dc=ad,dc=test" % cls.adcert1, '-n',
                                     cls.adcert1, '-x', '-t', 'CT,C,C', '-v',
diff --git a/ipatests/test_xmlrpc/test_automount_plugin.py b/ipatests/test_xmlrpc/test_automount_plugin.py
index e3ad8b4..aa39b60 100644
--- a/ipatests/test_xmlrpc/test_automount_plugin.py
+++ b/ipatests/test_xmlrpc/test_automount_plugin.py
@@ -106,7 +106,7 @@ def check_import_roundtrip(self):
                     skipped=(),
                     duplicatemaps=(),
                     duplicatekeys=(),
-                )), res)
+                )), res)  # pylint: disable=used-before-assignment
             self.check_tofiles()
         finally:
             res = api.Command['automountlocation_del'](self.locname)['result']
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to