>From 675a038cf6369efde2a32f3cb0d70cf38e6c1e67 Mon Sep 17 00:00:00 2001 From: Lukas Slebodnik Date: Thu, 29 Jan 2015 09:46:27 +0100 Subject: [PATCH 1/5] SSSDConfig: Port missing parts to python3 * fix incompatible imports * fix translation.[u]?gettext * fix dict method has_key * fix catching multiple exception classes * fix octal literals PEP 3127 Resolves: https://fedorahosted.org/sssd/ticket/2017 --- src/config/SSSDConfig/__init__.py.in | 45 ++++++++++++++++++++---------------- src/config/SSSDConfigTest.py | 24 +++++++++---------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/config/SSSDConfig/__init__.py.in b/src/config/SSSDConfig/__init__.py.in index ae00a2b7f9130725a6a766a4cbbba0a53f86dd7a..3f1597dbf5baa86bc3835c7f28edbf526f0cb4c8 100644 --- a/src/config/SSSDConfig/__init__.py.in +++ b/src/config/SSSDConfig/__init__.py.in @@ -6,9 +6,11 @@ Created on Sep 18, 2009 import os import gettext -import exceptions +import sys +if sys.version_info[0] == 2: + import exceptions import re -from ipachangeconf import SSSDChangeConf +from .ipachangeconf import SSSDChangeConf # Exceptions class SSSDConfigException(Exception): pass @@ -32,7 +34,10 @@ PACKAGE = 'sss_daemon' LOCALEDIR = '/usr/share/locale' translation = gettext.translation(PACKAGE, LOCALEDIR, fallback=True) -_ = translation.ugettext +if sys.version_info[0] > 2: + _ = translation.gettext +else: + _ = translation.ugettext # TODO: This needs to be made external option_strings = { @@ -481,7 +486,7 @@ class SSSDConfigSchema(SSSDChangeConf): subtype = self.type_lookup[split_option[SUBTYPE]] mandatory = self.bool_lookup[split_option[MANDATORY]] - if option_strings.has_key(option['name']): + if option['name'] in option_strings: desc = option_strings[option['name']] else: desc = None @@ -527,7 +532,7 @@ class SSSDConfigSchema(SSSDChangeConf): mandatory, desc, [subtype(split_option[DEFAULT])]) - except ValueError, KeyError: + except (ValueError, KeyError): raise ParsingError else: try: @@ -546,7 +551,7 @@ class SSSDConfigSchema(SSSDChangeConf): mandatory, desc, primarytype(split_option[DEFAULT])) - except ValueError, KeyError: + except (ValueError, KeyError): raise ParsingError elif optionlen > 4: @@ -561,7 +566,7 @@ class SSSDConfigSchema(SSSDChangeConf): else: newvalue = subtype(x) fixed_options.extend([newvalue]) - except ValueError, KeyError: + except (ValueError, KeyError): raise ParsingError else: fixed_options.extend([x]) @@ -610,7 +615,7 @@ class SSSDConfigSchema(SSSDChangeConf): splitsection = section['name'].split('/') if (splitsection[0] == 'provider'): if(len(splitsection) == 3): - if not providers.has_key(splitsection[1]): + if splitsection[1] not in providers: providers[splitsection[1]] = [] providers[splitsection[1]].extend([splitsection[2]]) for key in providers.keys(): @@ -674,7 +679,7 @@ class SSSDConfigObject(object): === Errors === No errors """ - if self.options.has_key(optionname): + if optionname in self.options: del self.options[optionname] class SSSDService(SSSDConfigObject): @@ -1309,12 +1314,12 @@ class SSSDDomain(SSSDConfigObject): # We should now have a list of options used only by this # provider. So we remove them. for option in options: - if self.options.has_key(option): + if option in self.options: del self.options[option] # Remove this provider from the option list option = '%s_provider' % provider_type - if self.options.has_key(option): + if option in self.options: del self.options[option] self.providers.remove((provider, provider_type)) @@ -1452,7 +1457,7 @@ class SSSDConfig(SSSDChangeConf): outputfile = self.configfile # open() will raise IOError if it fails - old_umask = os.umask(0177) + old_umask = os.umask(0o177) of = open(outputfile, "wb") output = self.dump(self.opts) of.write(output) @@ -1477,7 +1482,7 @@ class SSSDConfig(SSSDChangeConf): if (self.has_option('sssd', 'services')): active_services = striplist(self.get('sssd', 'services').split(',')) service_dict = dict.fromkeys(active_services) - if service_dict.has_key(''): + if '' in service_dict: del service_dict[''] # Remove any entries in this list that don't @@ -1633,7 +1638,7 @@ class SSSDConfig(SSSDChangeConf): # This guarantees uniqueness and makes it easy # to add a new value service_dict = dict.fromkeys(striplist(item['value'].split(','))) - if service_dict.has_key(''): + if '' in service_dict: del service_dict[''] # Add a new key for the service being activated @@ -1674,11 +1679,11 @@ class SSSDConfig(SSSDChangeConf): # This guarantees uniqueness and makes it easy # to remove the one unwanted value. service_dict = dict.fromkeys(striplist(item['value'].split(','))) - if service_dict.has_key(''): + if '' in service_dict: del service_dict[''] # Remove the unwanted service from the lest - if service_dict.has_key(name): + if name in service_dict: del service_dict[name] # Write out the joined keys @@ -1760,7 +1765,7 @@ class SSSDConfig(SSSDChangeConf): if (self.has_option('sssd', 'domains')): active_domains = striplist(self.get('sssd', 'domains').split(',')) domain_dict = dict.fromkeys(active_domains) - if domain_dict.has_key(''): + if '' in domain_dict: del domain_dict[''] # Remove any entries in this list that don't @@ -1955,7 +1960,7 @@ class SSSDConfig(SSSDChangeConf): # This guarantees uniqueness and makes it easy # to add a new value domain_dict = dict.fromkeys(striplist(item['value'].split(','))) - if domain_dict.has_key(''): + if '' in domain_dict: del domain_dict[''] # Add a new key for the domain being activated @@ -1996,11 +2001,11 @@ class SSSDConfig(SSSDChangeConf): # This guarantees uniqueness and makes it easy # to remove the one unwanted value. domain_dict = dict.fromkeys(striplist(item['value'].split(','))) - if domain_dict.has_key(''): + if '' in domain_dict: del domain_dict[''] # Remove the unwanted domain from the lest - if domain_dict.has_key(name): + if name in domain_dict: del domain_dict[name] # Write out the joined keys diff --git a/src/config/SSSDConfigTest.py b/src/config/SSSDConfigTest.py index 5d6662a9ad5d27280bb3f48e94cc0fb071665fd6..4c4af18d35232362d664480912a9cece01ec8a63 100755 --- a/src/config/SSSDConfigTest.py +++ b/src/config/SSSDConfigTest.py @@ -748,12 +748,12 @@ class SSSDConfigTestSSSDDomain(unittest.TestCase): # Ensure that all of the expected defaults are there for provider in control_provider_dict.keys(): for ptype in control_provider_dict[provider]: - self.assertTrue(providers.has_key(provider)) + self.assertTrue(provider in providers) self.assertTrue(ptype in providers[provider]) for provider in providers.keys(): for ptype in providers[provider]: - self.assertTrue(control_provider_dict.has_key(provider)) + self.assertTrue(provider in control_provider_dict) self.assertTrue(ptype in control_provider_dict[provider]) def testListProviderOptions(self): @@ -1003,7 +1003,7 @@ class SSSDConfigTestSSSDDomain(unittest.TestCase): # Remove the local ID provider and add an LDAP one # LDAP ID providers can also use the krb5_realm domain.remove_provider('id') - self.assertFalse(domain.options.has_key('id_provider')) + self.assertFalse('id_provider' in domain.options) domain.add_provider('ldap', 'id') @@ -1020,7 +1020,7 @@ class SSSDConfigTestSSSDDomain(unittest.TestCase): domain.remove_provider('id') self.assertEquals(domain.get_option('krb5_realm'), 'EXAMPLE.COM') - self.assertFalse(domain.options.has_key('ldap_uri')) + self.assertFalse('ldap_uri' in domain.options) # Put the LOCAL provider back domain.add_provider('local', 'id') @@ -1028,7 +1028,7 @@ class SSSDConfigTestSSSDDomain(unittest.TestCase): # Remove the auth domain and verify that the options # revert to the backup_list domain.remove_provider('auth') - self.assertFalse(domain.options.has_key('auth_provider')) + self.assertFalse('auth_provider' in domain.options) options = domain.list_options() self.assertTrue(type(options) == dict, @@ -1047,21 +1047,21 @@ class SSSDConfigTestSSSDDomain(unittest.TestCase): option) # Ensure that the krb5_realm option is now gone - self.assertFalse(domain.options.has_key('krb5_realm')) + self.assertFalse('krb5_realm' in domain.options) # Test removing nonexistent provider - Real domain.remove_provider('id') - self.assertFalse(domain.options.has_key('id_provider')) + self.assertFalse('id_provider' in domain.options) # Test removing nonexistent provider - Bad backend type # Should pass without complaint domain.remove_provider('id') - self.assertFalse(domain.options.has_key('id_provider')) + self.assertFalse('id_provider' in domain.options) # Test removing nonexistent provider - Bad provider type # Should pass without complaint domain.remove_provider('nosuchprovider') - self.assertFalse(domain.options.has_key('nosuchprovider_provider')) + self.assertFalse('nosuchprovider_provider' in domain.options) def testGetOption(self): domain = SSSDConfig.SSSDDomain('sssd', self.schema) @@ -1367,7 +1367,7 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase): # Positive test - Service with invalid option loads # but ignores the invalid option service = sssdconfig.get_service('pam') - self.assertFalse(service.options.has_key('nosuchoption')) + self.assertFalse('nosuchoption' in service.options) def testNewService(self): sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf", @@ -1598,13 +1598,13 @@ class SSSDConfigTestSSSDConfig(unittest.TestCase): # Expected result: Domain is imported, but does not contain the # unknown provider entry domain = sssdconfig.get_domain('INVALIDPROVIDER') - self.assertFalse(domain.options.has_key('chpass_provider')) + self.assertFalse('chpass_provider' in domain.options) # Positive Test - Domain with unknown option # Expected result: Domain is imported, but does not contain the # unknown option entry domain = sssdconfig.get_domain('INVALIDOPTION') - self.assertFalse(domain.options.has_key('nosuchoption')) + self.assertFalse('nosuchoption' in domain.options) def testNewDomain(self): sssdconfig = SSSDConfig.SSSDConfig(srcdir + "/etc/sssd.api.conf", -- 2.1.0