From a4f8f3967d42dd47de2604d590d79d6e3ca60e45 Mon Sep 17 00:00:00 2001 From: root <3c2b2ff5@inbox.ru> Date: Tue, 29 Oct 2019 00:26:02 +0100 Subject: [PATCH] first step to make code python3 compatible --- nss_cache/app.py | 10 +- nss_cache/app_test.py | 20 ++-- nss_cache/caches/cache_factory.py | 3 - nss_cache/caches/cache_factory_test.py | 6 +- nss_cache/caches/caches.py | 6 +- nss_cache/caches/caches_test.py | 5 +- nss_cache/caches/files.py | 15 +-- nss_cache/caches/files_test.py | 23 +++-- nss_cache/caches/nssdb.py | 9 +- nss_cache/caches/nssdb_test.py | 125 ++++++++++++----------- nss_cache/command.py | 30 +++--- nss_cache/command_test.py | 19 ++-- nss_cache/config.py | 7 +- nss_cache/lock.py | 8 +- nss_cache/lock_test.py | 5 +- nss_cache/maps/automount_test.py | 30 +++--- nss_cache/maps/group_test.py | 34 +++--- nss_cache/maps/maps.py | 10 +- nss_cache/maps/maps_test.py | 4 +- nss_cache/maps/netgroup_test.py | 26 ++--- nss_cache/maps/passwd_test.py | 60 +++++------ nss_cache/maps/shadow_test.py | 54 +++++----- nss_cache/nss.py | 2 +- nss_cache/nss_test.py | 5 +- nss_cache/sources/consulsource.py | 2 +- nss_cache/sources/consulsource_test.py | 84 +++++++-------- nss_cache/sources/httpsource.py | 16 ++- nss_cache/sources/httpsource_test.py | 45 ++++---- nss_cache/sources/ldapsource.py | 11 +- nss_cache/sources/ldapsource_test.py | 45 ++++---- nss_cache/sources/s3source_test.py | 94 ++++++++--------- nss_cache/sources/source_factory.py | 2 +- nss_cache/sources/source_factory_test.py | 4 +- nss_cache/update/files_updater.py | 2 +- nss_cache/update/files_updater_test.py | 7 +- nss_cache/update/map_updater_test.py | 5 +- nss_cache/update/updater.py | 6 +- nss_cache/update/updater_test.py | 5 +- nss_cache/util/curl.py | 11 +- nss_cache/util/timestamps.py | 4 +- nss_cache/util/timestamps_test.py | 5 +- 41 files changed, 463 insertions(+), 401 deletions(-) diff --git a/nss_cache/app.py b/nss_cache/app.py index 2189f52a..ee1ac230 100644 --- a/nss_cache/app.py +++ b/nss_cache/app.py @@ -92,7 +92,7 @@ def __init__(self): handler = logging.handlers.SysLogHandler(address='/dev/log', facility=facility) except socket.error: - print '/dev/log could not be opened; falling back on stderr.' + print('/dev/log could not be opened; falling back on stderr.') # Omitting an argument to StreamHandler results in sys.stderr being # used. handler = logging.StreamHandler() @@ -188,7 +188,7 @@ def Run(self, args, env): # Parse the commandline. try: (options, args) = self.parser.parse_args(args) - except SystemExit, e: + except SystemExit as e: # OptionParser objects raise SystemExit (error() calls exit() # calls sys.exit()) upon a parser error. # This can be handled better by overriding error or monkeypatching @@ -213,7 +213,7 @@ def Run(self, args, env): # Identify the command to dispatch. if not args: - print 'No command given' + print('No command given') self.parser.print_help() return os.EX_USAGE # print global help if command is 'help' with no argument @@ -232,13 +232,13 @@ def Run(self, args, env): command_callable = getattr(command, command_name.capitalize()) except AttributeError: self.log.warn('%s is not implemented', command_name) - print 'command %r is not implemented' % command_name + print('command %r is not implemented' % command_name) self.parser.print_help() return os.EX_SOFTWARE try: retval = command_callable().Run(conf=conf, args=args) - except error.SourceUnavailable, e: + except error.SourceUnavailable as e: self.log.error('Problem with configured data source: %s', e) return os.EX_TEMPFAIL diff --git a/nss_cache/app_test.py b/nss_cache/app_test.py index ffd77dfb..42c42dad 100644 --- a/nss_cache/app_test.py +++ b/nss_cache/app_test.py @@ -20,7 +20,7 @@ import logging import os -import StringIO +from io import BytesIO as StringIO import sys import unittest @@ -31,7 +31,7 @@ class TestNssCacheApp(unittest.TestCase): """Unit tests for NssCacheApp class.""" def setUp(self): - dev_null = StringIO.StringIO() + dev_null = StringIO() self.stdout = sys.stdout sys.stdout = dev_null @@ -85,14 +85,14 @@ def testParseCommandLineConfigFile(self): def testBadOptionsCauseNoExit(self): a = app.NssCacheApp() - stderr_buffer = StringIO.StringIO() + stderr_buffer = StringIO() old_stderr = sys.stderr sys.stderr = stderr_buffer self.assertEquals(2, a.Run(['--invalid'], {})) sys.stderr = old_stderr def testHelpOptionPrintsGlobalHelp(self): - stdout_buffer = StringIO.StringIO() + stdout_buffer = StringIO() a = app.NssCacheApp() old_stdout = sys.stdout sys.stdout = stdout_buffer @@ -109,7 +109,7 @@ def testHelpOptionPrintsGlobalHelp(self): def testHelpCommandOutput(self): # trap stdout into a StringIO - stdout_buffer = StringIO.StringIO() + stdout_buffer = StringIO() a = app.NssCacheApp() old_stdout = sys.stdout sys.stdout = stdout_buffer @@ -127,7 +127,7 @@ def testHelpCommandOutput(self): # # This will fail when run under 'nosetests -s' because nose will # # also intercept sys.stdout :( (Recommend refactoring NssCacheApp # # to take in an output stream for help and usage? -# output = cStringIO.StringIO() +# output = StringIO() # stdout = sys.stdout # sys.stdout = output @@ -151,16 +151,16 @@ def testHelpCommandOutput(self): # self.levels = [] # def emit(self, record): - # print record + # print(record) # self.levels.append(record.levelno) - # print self.levels + # print(self.levels) # handler = test_handler() # a = app.NssCacheApp() - # print "log:", a.log + # print("log:", a.log) # a.log.addHandler(handler) # a.log.debug2('logged at level debug2') - # print handler.levels + # print(handler.levels) # self.failUnless(5 in handler.levels) # def testVerboseLoggingLevel(self): diff --git a/nss_cache/caches/cache_factory.py b/nss_cache/caches/cache_factory.py index a1d44447..c0c480f5 100644 --- a/nss_cache/caches/cache_factory.py +++ b/nss_cache/caches/cache_factory.py @@ -83,6 +83,3 @@ def Create(conf, map_name, automount_mountpoint=None): files.RegisterAllImplementations(RegisterImplementation) nssdb.RegisterAllImplementations(RegisterImplementation) - - - diff --git a/nss_cache/caches/cache_factory_test.py b/nss_cache/caches/cache_factory_test.py index bec6967b..aa80ef76 100644 --- a/nss_cache/caches/cache_factory_test.py +++ b/nss_cache/caches/cache_factory_test.py @@ -33,9 +33,9 @@ class DummyCache(caches.Cache): old_cache_implementations = cache_factory._cache_implementations cache_factory._cache_implementations = {} cache_factory.RegisterImplementation('dummy', 'dummy', DummyCache) - self.failUnlessEqual(1, len(cache_factory._cache_implementations)) - self.failUnlessEqual(1, len(cache_factory._cache_implementations['dummy'])) - self.failUnlessEqual(DummyCache, + self.assertEqual(1, len(cache_factory._cache_implementations)) + self.assertEqual(1, len(cache_factory._cache_implementations['dummy'])) + self.assertEqual(DummyCache, cache_factory._cache_implementations['dummy']['dummy']) cache_factory._cache_implementations = old_cache_implementations diff --git a/nss_cache/caches/caches.py b/nss_cache/caches/caches.py index fa2f74fb..6b3f2a58 100644 --- a/nss_cache/caches/caches.py +++ b/nss_cache/caches/caches.py @@ -101,7 +101,7 @@ def _Begin(self): self.temp_cache_file = os.fdopen(fd, 'w+b') self.log.debug('opened temporary cache filename %r', self.temp_cache_filename) - except OSError, e: + except OSError as e: if e.errno == errno.EACCES: self.log.info('Got OSError (%s) when trying to create temporary file', e) @@ -116,7 +116,7 @@ def _Rollback(self): # Safe file remove (ignore "no such file or directory" errors): try: os.remove(self.temp_cache_filename) - except OSError, e: + except OSError as e: if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory raise # re-raise exception if a different error occured @@ -150,7 +150,7 @@ def _Commit(self): uid = stat_info.st_uid gid = stat_info.st_gid os.chown(self.temp_cache_filename, uid, gid) - except OSError, e: + except OSError as e: if e.errno == errno.ENOENT: if self.map_name == "sshkey": os.chmod(self.temp_cache_filename, diff --git a/nss_cache/caches/caches_test.py b/nss_cache/caches/caches_test.py index 74aec860..d3b456c4 100644 --- a/nss_cache/caches/caches_test.py +++ b/nss_cache/caches/caches_test.py @@ -24,7 +24,10 @@ import tempfile import unittest -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache import config from nss_cache.caches import caches diff --git a/nss_cache/caches/files.py b/nss_cache/caches/files.py index 220d4d9b..ae59c291 100644 --- a/nss_cache/caches/files.py +++ b/nss_cache/caches/files.py @@ -31,20 +31,23 @@ import shutil import stat import sys -import ConfigParser +try: + from ConfigParser import SafeConfigParser as ConfigParser +except ImportError: + from configparser import ConfigParser from nss_cache import config from nss_cache import error from nss_cache.caches import caches from nss_cache.util import file_formats -if sys.version >= (2, 5): +if sys.version_info[0:2] >= (2, 5): def LongestLength(l): return len(max(l, key=len)) else: # Python < 2.4, 50% slower def LongestLength(l): return max([len(x) for x in l]) # Load suffix config variables -parser = ConfigParser.ConfigParser() +parser = ConfigParser() for i in sys.argv: if ('nsscache.conf') in i: # Remove '--config-file=' from the string @@ -237,8 +240,8 @@ def WriteIndex(self): self.log.debug('Writing index %s', tmp_index_filename) index = self._indices[index_name] - key_length = LongestLength(index.keys()) - pos_length = LongestLength(index.values()) + key_length = LongestLength(list(index.keys())) + pos_length = LongestLength(list(index.values())) max_length = key_length + pos_length # Open for write/truncate index_file = open(tmp_index_filename, 'w') @@ -249,7 +252,7 @@ def WriteIndex(self): uid = stat_info.st_uid gid = stat_info.st_gid os.chown(tmp_index_filename, uid, gid) - except OSError, e: + except OSError as e: if e.errno == errno.ENOENT: os.chmod(tmp_index_filename, stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IROTH) diff --git a/nss_cache/caches/files_test.py b/nss_cache/caches/files_test.py index 5f0dd1da..0462f455 100644 --- a/nss_cache/caches/files_test.py +++ b/nss_cache/caches/files_test.py @@ -25,7 +25,10 @@ import unittest import sys -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache import config from nss_cache.maps import automount @@ -49,7 +52,7 @@ def tearDown(self): def testInstantiation(self): cache = files.FilesCache(self.config, config.MAP_PASSWORD) - self.failIfEqual(None, cache) + self.assertNotEqual(None, cache) def testWrite(self): cache = files.FilesPasswdMapHandler(self.config) @@ -76,7 +79,7 @@ def testCacheFilenameSuffixOption(self): cache._Commit() expected_cache_filename = os.path.join(self.workdir, 'test.blarg') - self.failUnless(os.path.exists(expected_cache_filename)) + self.assertTrue(os.path.exists(expected_cache_filename)) def testWritePasswdEntry(self): """We correctly write a typical entry in /etc/passwd format.""" @@ -173,14 +176,14 @@ def testAutomountSetsFilename(self): # also tests GetMapLocation() because it uses it :) conf = {'dir': self.workdir, 'cache_filename_suffix': ''} cache = files.FilesAutomountMapHandler(conf) - self.assertEquals(cache.GetMapLocation(), '%s/auto.master' % self.workdir) + self.assertEqual(cache.GetMapLocation(), '%s/auto.master' % self.workdir) cache = files.FilesAutomountMapHandler(conf, automount_mountpoint='/home') - self.assertEquals(cache.GetMapLocation(), '%s/auto.home' % self.workdir) + self.assertEqual(cache.GetMapLocation(), '%s/auto.home' % self.workdir) cache = files.FilesAutomountMapHandler(conf, automount_mountpoint='/usr/meh') - self.assertEquals(cache.GetMapLocation(), '%s/auto.usr_meh' % self.workdir) + self.assertEqual(cache.GetMapLocation(), '%s/auto.usr_meh' % self.workdir) def testCacheFileDoesNotExist(self): """Make sure we just get an empty map rather than exception.""" @@ -201,7 +204,7 @@ def testIndexCreation(self): cache.WriteIndex() index_filename = cache.GetCacheFilename() + '.ixname' - self.failUnless(os.path.exists(index_filename), + self.assertTrue(os.path.exists(index_filename), 'Index not created %s' % index_filename) f = open(index_filename) self.assertEqual('bar\x0015\x00\x00\n', f.readline()) @@ -209,7 +212,7 @@ def testIndexCreation(self): self.assertEqual('quux\x0030\x00\n', f.readline()) index_filename = cache.GetCacheFilename() + '.ixuid' - self.failUnless(os.path.exists(index_filename), + self.assertTrue(os.path.exists(index_filename), 'Index not created %s' % index_filename) f = open(index_filename) self.assertEqual('10\x000\x00\x00\n', f.readline()) @@ -228,10 +231,10 @@ def testWriteCacheAndIndex(self): self.assertTrue('foo' in written) self.assertTrue('bar' in written) index_filename = cache.GetCacheFilename() + '.ixname' - self.failUnless(os.path.exists(index_filename), + self.assertTrue(os.path.exists(index_filename), 'Index not created %s' % index_filename) index_filename = cache.GetCacheFilename() + '.ixuid' - self.failUnless(os.path.exists(index_filename), + self.assertTrue(os.path.exists(index_filename), 'Index not created %s' % index_filename) entries = [passwd.PasswdMapEntry(dict(name='foo', uid=10, gid=10)), diff --git a/nss_cache/caches/nssdb.py b/nss_cache/caches/nssdb.py index 31baa676..aa580fec 100644 --- a/nss_cache/caches/nssdb.py +++ b/nss_cache/caches/nssdb.py @@ -18,7 +18,10 @@ __author__ = 'jaq@google.com (Jamie Wilkinson)' -import bsddb +try: + from bsddb import btopen +except ImportError: + from bsddb3 import btopen import fcntl import os import select @@ -115,7 +118,7 @@ def _LoadBdbCacheFile(self, data): self.log.debug('cache file does not exist: %r', db_file) raise error.CacheNotFound('cache file does not exist: %r' % db_file) - db = bsddb.btopen(db_file, 'r') + db = btopen(db_file, 'r') for k in db: if self.IsMapPrimaryKey(k): password_entry = self.ConvertValueToMapEntry(db[k]) @@ -253,7 +256,7 @@ def Verify(self, written_keys): EmptyMap: The cache being verified is empty. """ self.log.debug('verification started %s', self.temp_cache_filename) - db = bsddb.btopen(self.temp_cache_filename, 'r') + db = btopen(self.temp_cache_filename, 'r') # cast keys to a set for fast __contains__ lookup in the loop # following cache_keys = set(db) diff --git a/nss_cache/caches/nssdb_test.py b/nss_cache/caches/nssdb_test.py index f26b602d..89dab2df 100644 --- a/nss_cache/caches/nssdb_test.py +++ b/nss_cache/caches/nssdb_test.py @@ -18,7 +18,6 @@ __author__ = 'jaq@google.com (Jamie Wilkinson)' -import bsddb import logging import os.path import select @@ -28,7 +27,15 @@ import time import unittest -import mox +try: + from bsddb import btopen +except ImportError: + from bsddb3 import btopen + +try: + import mox +except ImportError: + import mox3 from nss_cache import error @@ -80,9 +87,9 @@ def testConvertValueToMapEntry(self): def testIsMapPrimaryKey(self): updater = nssdb.NssDbPasswdHandler({}) - self.failUnless(updater.IsMapPrimaryKey('.foo')) - self.failIf(updater.IsMapPrimaryKey('=1000')) - self.failIf(updater.IsMapPrimaryKey('00')) + self.assertTrue(updater.IsMapPrimaryKey('.foo')) + self.assertFalse(updater.IsMapPrimaryKey('=1000')) + self.assertFalse(updater.IsMapPrimaryKey('00')) def testNssDbPasswdHandlerWriteData(self): entry_string = 'foo:x:1000:1000:foo:/:/bin/sh' @@ -101,7 +108,7 @@ def testNssDbPasswdHandlerWriteData(self): passwd_map_entry.dir = '/' passwd_map_entry.shell = '/bin/sh' passwd_map_entry.passwd = 'x' - self.failUnless(passwd_map.Add(passwd_map_entry)) + self.assertTrue(passwd_map.Add(passwd_map_entry)) writer = nssdb.NssDbPasswdHandler({'makedb': '/bin/false', 'dir': '/tmp'}) @@ -133,7 +140,7 @@ def testNssDbPasswdHandlerWrite(self): pw.shell = '/bin/sh' pw.passwd = 'x' pw.Verify() - self.failUnless(m.Add(pw)) + self.assertTrue(m.Add(pw)) self.mox.StubOutWithMock(select, 'select') select.select([makedb_stdout], (), (), 0).AndReturn(([37], [], [])) @@ -155,7 +162,7 @@ def SpawnMakeDb(): writer.Write(m) tmppasswd = os.path.join(self.workdir, 'passwd.db') - self.failIf(os.path.exists(tmppasswd)) + self.assertFalse(os.path.exists(tmppasswd)) # just clean it up, Write() doesn't Commit() writer._Rollback() @@ -167,18 +174,18 @@ def testVerify(self): e.name = 'foo' e.uid = 1000 e.gid = 2000 - self.failUnless(m.Add(e)) + self.assertTrue(m.Add(e)) updater = nssdb.NssDbPasswdHandler({'dir': self.workdir, 'makedb': '/usr/bin/makedb'}) written = updater.Write(m) - self.failUnless(os.path.exists(updater.temp_cache_filename), + self.assertTrue(os.path.exists(updater.temp_cache_filename), 'updater.Write() did not create a file') retval = updater.Verify(written) - self.failUnlessEqual(True, retval) + self.assertEqual(True, retval) os.unlink(updater.temp_cache_filename) @@ -199,25 +206,25 @@ def filter(self, record): e.name = 'foo' e.uid = 1000 e.gid = 2000 - self.failUnless(m.Add(e)) + self.assertTrue(m.Add(e)) updater = nssdb.NssDbPasswdHandler({'dir': self.workdir, 'makedb': '/usr/bin/makedb'}) written = updater.Write(m) - self.failUnless(os.path.exists(updater.temp_cache_filename), + self.assertTrue(os.path.exists(updater.temp_cache_filename), 'updater.Write() did not create a file') # change the cache - db = bsddb.btopen(updater.temp_cache_filename) + db = btopen(updater.temp_cache_filename) del db[db.first()[0]] db.sync() db.close() retval = updater.Verify(written) - self.failUnlessEqual(False, retval) - self.failIf(os.path.exists(os.path.join(updater.temp_cache_filename))) + self.assertEqual(False, retval) + self.assertFalse(os.path.exists(os.path.join(updater.temp_cache_filename))) # no longer hide this message logging.getLogger('NssDbPasswdHandler').removeFilter(fltr) @@ -228,7 +235,7 @@ def testVerifyEmptyMap(self): dir=self.workdir) updater.temp_cache_filename = temp_filename # make it empty - db = bsddb.btopen(temp_filename, 'w') + db = btopen(temp_filename, 'w') self.assertEqual(0, len(db)) db.close() # TODO(jaq): raising an exception is probably the wrong behaviour @@ -261,9 +268,9 @@ def testConvertValueToMapEntry(self): def testIsMapPrimaryKey(self): updater = nssdb.NssDbGroupHandler({}) - self.failUnless(updater.IsMapPrimaryKey('.foo')) - self.failIf(updater.IsMapPrimaryKey('=1000')) - self.failIf(updater.IsMapPrimaryKey('00')) + self.assertTrue(updater.IsMapPrimaryKey('.foo')) + self.assertFalse(updater.IsMapPrimaryKey('=1000')) + self.assertFalse(updater.IsMapPrimaryKey('00')) def testNssDbGroupHandlerWriteData(self): ent = 'foo:x:1000:bar' @@ -280,7 +287,7 @@ def testNssDbGroupHandlerWriteData(self): g.passwd = 'x' g.members = ['bar'] - self.failUnless(m.Add(g)) + self.assertTrue(m.Add(g)) writer = nssdb.NssDbGroupHandler({'makedb': '/bin/false', 'dir': '/tmp'}) @@ -309,7 +316,7 @@ def testNssDbGroupHandlerWrite(self): g.passwd = 'x' g.members = ['bar'] g.Verify() - self.failUnless(m.Add(g)) + self.assertTrue(m.Add(g)) self.mox.StubOutWithMock(select, 'select') select.select([makedb_stdout], (), (), 0).AndReturn(([37], [], [])) @@ -329,7 +336,7 @@ def SpawnMakeDb(): writer.Write(m) tmpgroup = os.path.join(self.workdir, 'group.db') - self.failIf(os.path.exists(tmpgroup)) + self.assertFalse(os.path.exists(tmpgroup)) # just clean it up, Write() doesn't Commit() writer._Rollback() @@ -340,18 +347,18 @@ def testVerify(self): e = group.GroupMapEntry() e.name = 'foo' e.gid = 2000 - self.failUnless(m.Add(e)) + self.assertTrue(m.Add(e)) updater = nssdb.NssDbGroupHandler({'dir': self.workdir, 'makedb': '/usr/bin/makedb'}) written = updater.Write(m) - self.failUnless(os.path.exists(updater.temp_cache_filename), + self.assertTrue(os.path.exists(updater.temp_cache_filename), 'updater.Write() did not create a file') retval = updater.Verify(written) - self.failUnlessEqual(True, retval) + self.assertEqual(True, retval) os.unlink(updater.temp_cache_filename) @unittest.skipIf(NoMakeDB(), 'no /usr/bin/makedb') @@ -370,25 +377,25 @@ def filter(self, record): e = group.GroupMapEntry() e.name = 'foo' e.gid = 2000 - self.failUnless(m.Add(e)) + self.assertTrue(m.Add(e)) updater = nssdb.NssDbGroupHandler({'dir': self.workdir, 'makedb': '/usr/bin/makedb'}) written = updater.Write(m) - self.failUnless(os.path.exists(updater.temp_cache_filename), + self.assertTrue(os.path.exists(updater.temp_cache_filename), 'updater.Write() did not create a file') # change the cache - db = bsddb.btopen(updater.temp_cache_filename) + db = btopen(updater.temp_cache_filename) del db[db.first()[0]] db.sync() db.close() retval = updater.Verify(written) - self.failUnlessEqual(False, retval) - self.failIf(os.path.exists(os.path.join(updater.temp_cache_filename))) + self.assertEqual(False, retval) + self.assertFalse(os.path.exists(os.path.join(updater.temp_cache_filename))) # no longer hide this message logging.getLogger('NssDbGroupHandler').removeFilter(fltr) @@ -418,8 +425,8 @@ def testConvertValueToMapEntry(self): def testIsMapPrimaryKey(self): updater = nssdb.NssDbShadowHandler({}) - self.failUnless(updater.IsMapPrimaryKey('.foo')) - self.failIf(updater.IsMapPrimaryKey('00')) + self.assertTrue(updater.IsMapPrimaryKey('.foo')) + self.assertFalse(updater.IsMapPrimaryKey('00')) def testNssDbShadowHandlerWriteData(self): ent = 'foo:!!:::::::0' @@ -432,7 +439,7 @@ def testNssDbShadowHandlerWriteData(self): s = shadow.ShadowMapEntry() s.name = 'foo' - self.failUnless(m.Add(s)) + self.assertTrue(m.Add(s)) writer = nssdb.NssDbShadowHandler({'makedb': '/bin/false', 'dir': '/tmp'}) @@ -458,7 +465,7 @@ def testNssDbShadowHandlerWrite(self): s.name = 'foo' s.passwd = '*' s.Verify() - self.failUnless(m.Add(s)) + self.assertTrue(m.Add(s)) self.mox.StubOutWithMock(select, 'select') select.select([makedb_stdout], (), (), 0).AndReturn(([37], [], [])) @@ -478,7 +485,7 @@ def SpawnMakeDb(): writer.Write(m) tmpshadow = os.path.join(self.workdir, 'shadow.db') - self.failIf(os.path.exists(tmpshadow)) + self.assertFalse(os.path.exists(tmpshadow)) # just clean it up, Write() doesn't Commit() writer._Rollback() @@ -487,18 +494,18 @@ def testVerify(self): m = shadow.ShadowMap() s = shadow.ShadowMapEntry() s.name = 'foo' - self.failUnless(m.Add(s)) + self.assertTrue(m.Add(s)) updater = nssdb.NssDbShadowHandler({'dir': self.workdir, 'makedb': '/usr/bin/makedb'}) written = updater.Write(m) - self.failUnless(os.path.exists(updater.temp_cache_filename), + self.assertTrue(os.path.exists(updater.temp_cache_filename), 'updater.Write() did not create a file') retval = updater.Verify(written) - self.failUnlessEqual(True, retval) + self.assertEqual(True, retval) os.unlink(updater.temp_cache_filename) @unittest.skipIf(NoMakeDB(), 'no /usr/bin/makedb') @@ -515,25 +522,25 @@ def filter(self, record): m = shadow.ShadowMap() s = shadow.ShadowMapEntry() s.name = 'foo' - self.failUnless(m.Add(s)) + self.assertTrue(m.Add(s)) updater = nssdb.NssDbShadowHandler({'dir': self.workdir, 'makedb': '/usr/bin/makedb'}) written = updater.Write(m) - self.failUnless(os.path.exists(updater.temp_cache_filename), + self.assertTrue(os.path.exists(updater.temp_cache_filename), 'updater.Write() did not create a file') # change the cache - db = bsddb.btopen(updater.temp_cache_filename) + db = btopen(updater.temp_cache_filename) del db[db.first()[0]] db.sync() db.close() retval = updater.Verify(written) - self.failUnlessEqual(False, retval) - self.failIf(os.path.exists(os.path.join(updater.temp_cache_filename))) + self.assertEqual(False, retval) + self.assertFalse(os.path.exists(os.path.join(updater.temp_cache_filename))) # no longer hide this message logging.getLogger('NssDbShadowHandler').removeFilter(fltr) @@ -559,7 +566,7 @@ def testWriteTestBdb(self): pw.gecos = 'doody' pw.dir = '/' pw.shell = '/bin/sh' - self.failUnless(data.Add(pw)) + self.assertTrue(data.Add(pw)) # instantiate object under test dummy_config = {'dir': self.workdir} @@ -570,12 +577,12 @@ def testWriteTestBdb(self): self.assertTrue('=1000' in written) # perform test - db = bsddb.btopen(cache.temp_cache_filename, 'r') + db = btopen(cache.temp_cache_filename, 'r') - self.assertEqual(3, len(db.keys())) - self.failUnless('.foo' in db.keys()) - self.failUnless('=1000' in db.keys()) - self.failUnless('00' in db.keys()) + self.assertEqual(3, len(list(db.keys()))) + self.assertTrue('.foo' in list(db.keys())) + self.assertTrue('=1000' in list(db.keys())) + self.assertTrue('00' in list(db.keys())) # convert data to pwent d = '%s:x:%s:%s:%s:%s:%s\x00' % (pw.name, pw.uid, pw.gid, pw.gecos, @@ -589,13 +596,13 @@ def testWriteTestBdb(self): def testLoadBdbCacheFile(self): pass_file = os.path.join(self.workdir, 'passwd.db') - db = bsddb.btopen(pass_file, 'c') + db = btopen(pass_file, 'c') ent = 'foo:x:1000:500:bar:/:/bin/sh' db['00'] = ent db['=1000'] = ent db['.foo'] = ent db.sync() - self.failUnless(os.path.exists(pass_file)) + self.assertTrue(os.path.exists(pass_file)) config = {'dir': self.workdir} cache = nssdb.NssDbPasswdHandler(config) @@ -612,7 +619,7 @@ def testLoadBdbCacheFile(self): def testGetMapRaisesCacheNotFound(self): bad_file = os.path.join(self.workdir, 'really_not_going_to_exist_okay') - self.failIf(os.path.exists(bad_file), 'what the hell, it exists!') + self.assertFalse(os.path.exists(bad_file), 'what the hell, it exists!') config = {} cache = nssdb.NssDbPasswdHandler(config) @@ -628,11 +635,11 @@ def testGetMapIsSizedObject(self): time.gmtime(timestamp))) update_ts_file.close() db_filename = os.path.join(self.workdir, 'passwd.db') - db = bsddb.btopen(db_filename) + db = btopen(db_filename) db.close() cache = nssdb.NssDbPasswdHandler({'dir': self.workdir}) cache_map = cache.GetMap() - self.assertEquals(0, len(cache_map)) + self.assertEqual(0, len(cache_map)) os.unlink(update_ts_filename) os.unlink(db_filename) @@ -645,11 +652,11 @@ def testGetMapHasMerge(self): time.gmtime(timestamp))) update_ts_file.close() db_filename = os.path.join(self.workdir, 'passwd.db') - db = bsddb.btopen(db_filename) + db = btopen(db_filename) db.close() cache = nssdb.NssDbPasswdHandler({'dir': self.workdir}) cache_map = cache.GetMap() - self.assertEquals(False, cache_map.Merge(passwd.PasswdMap())) + self.assertEqual(False, cache_map.Merge(passwd.PasswdMap())) os.unlink(update_ts_filename) os.unlink(db_filename) @@ -662,11 +669,11 @@ def testGetMapIsIterable(self): time.gmtime(timestamp))) update_ts_file.close() db_filename = os.path.join(self.workdir, 'passwd.db') - db = bsddb.btopen(db_filename) + db = btopen(db_filename) db.close() cache = nssdb.NssDbPasswdHandler({'dir': self.workdir}) cache_map = cache.GetMap() - self.assertEquals([], list(cache_map)) + self.assertEqual([], list(cache_map)) os.unlink(update_ts_filename) os.unlink(db_filename) diff --git a/nss_cache/command.py b/nss_cache/command.py index 215c12f1..44afbdab 100644 --- a/nss_cache/command.py +++ b/nss_cache/command.py @@ -23,7 +23,7 @@ import optparse import os import shutil -import StringIO +from io import BytesIO as StringIO import tempfile import time @@ -155,7 +155,7 @@ def Help(self, short=False): else: # lose the short summary first line help_text = '\n'.join(help_text.split('\n')[2:]) - help_buffer = StringIO.StringIO() + help_buffer = StringIO() self.parser.print_help(file=help_buffer) # lose the first line, which is the usage line help_text += '\n'.join(help_buffer.getvalue().split('\n')[1:]) @@ -208,7 +208,7 @@ def Run(self, conf, args): """ try: (options, args) = self.parser.parse_args(args) - except SystemExit, e: + except SystemExit as e: return e.code if options.maps: @@ -305,10 +305,10 @@ def UpdateMaps(self, conf, incremental, force_write=False, force_lock=False): self.log.error('Permission denied: could not update map %r. Aborting', map_name) retval += 1 - except (error.EmptyMap, error.InvalidMap), e: + except (error.EmptyMap, error.InvalidMap) as e: self.log.error(e) retval += 1 - except error.InvalidMerge, e: + except error.InvalidMerge as e: self.log.warn('Could not merge map %r: %s. Skipping.', map_name, e) finally: @@ -363,7 +363,7 @@ def Run(self, conf, args): """ try: (options, args) = self.parser.parse_args(args) - except SystemExit, e: + except SystemExit as e: return e.code if options.maps: @@ -480,7 +480,7 @@ def VerifySources(self, conf): source_options = conf.options[map_name].source try: source = source_factory.Create(source_options) - except error.SourceUnavailable, e: + except error.SourceUnavailable as e: self.log.debug('map %s dumps source error %s', map_name, e) self.log.error('Map %s is unvavailable!', map_name) retval +=1 @@ -518,17 +518,17 @@ def Run(self, conf, args): help_text = self.Help() else: help_command = args.pop() - print 'Usage: nsscache [global options] %s [options]' % help_command - print + print('Usage: nsscache [global options] %s [options]' % help_command) + print() try: callable_action = getattr(inspect.getmodule(self), help_command.capitalize()) help_text = callable_action().Help() except AttributeError: - print 'command %r is not implemented' % help_command + print('command %r is not implemented' % help_command) return 1 - print help_text + print(help_text) return 0 @@ -553,7 +553,7 @@ def Run(self, conf, args): """ try: (options, args) = self.parser.parse_args(args) - except SystemExit, e: + except SystemExit as e: return e.code if options.maps: @@ -623,7 +623,7 @@ def Run(self, conf, args): """ try: (options, args) = self.parser.parse_args(args) - except SystemExit, e: + except SystemExit as e: # See app.NssCacheApp.Run() return e.code @@ -639,13 +639,13 @@ def Run(self, conf, args): for value_dict in value_list: self.log.debug('Value dict: %r', value_dict) output = options.automount_template % value_dict - print output + print(output) else: for value_dict in self.GetSingleMapMetadata(map_name, conf, epoch=options.epoch): self.log.debug('Value dict: %r', value_dict) output = options.template % value_dict - print output + print(output) return os.EX_OK diff --git a/nss_cache/command_test.py b/nss_cache/command_test.py index 7e72c870..17e2c988 100644 --- a/nss_cache/command_test.py +++ b/nss_cache/command_test.py @@ -23,13 +23,16 @@ import os import pwd import shutil -import StringIO +from io import BytesIO as StringIO import sys import tempfile import time import unittest -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache import command from nss_cache import config @@ -166,7 +169,7 @@ def testRunWithBadParameters(self): c = command.Update() # Trap stderr so the unit test runs clean, # since unit test status is printed on stderr. - dev_null = StringIO.StringIO() + dev_null = StringIO() stderr = sys.stderr sys.stderr = dev_null self.assertEquals(2, c.Run(None, ['--invalid'])) @@ -446,7 +449,7 @@ def testRunWithBadParameters(self): c = command.Verify() # Trap stderr so the unit test runs clean, # since unit test status is printed on stderr. - dev_null = StringIO.StringIO() + dev_null = StringIO() stderr = sys.stderr sys.stderr = dev_null self.assertEquals(2, c.Run(None, ['--invalid'])) @@ -638,7 +641,7 @@ def testRunWithBadParameters(self): c = command.Repair() # Trap stderr so the unit test runs clean, # since unit test status is printed on stderr. - dev_null = StringIO.StringIO() + dev_null = StringIO() stderr = sys.stderr sys.stderr = dev_null self.assertEquals(2, c.Run(None, ['--invalid'])) @@ -663,7 +666,7 @@ class TestHelpCommand(unittest.TestCase): def setUp(self): self.stdout = sys.stdout - sys.stdout = StringIO.StringIO() + sys.stdout = StringIO() def tearDown(self): sys.stdout = self.stdout @@ -741,7 +744,7 @@ def testRunWithBadParameters(self): c = command.Status() # Trap stderr so the unit test runs clean, # since unit test status is printed on stderr. - dev_null = StringIO.StringIO() + dev_null = StringIO() stderr = sys.stderr sys.stderr = dev_null self.assertEquals(2, c.Run(None, ['--invalid'])) @@ -754,7 +757,7 @@ def testEpochFormatParameter(self): self.assertEqual([], args) def testObeysMapsFlag(self): - stdout_buffer = StringIO.StringIO() + stdout_buffer = StringIO() old_stdout = sys.stdout sys.stdout = stdout_buffer diff --git a/nss_cache/config.py b/nss_cache/config.py index 20a07b0c..b110e786 100644 --- a/nss_cache/config.py +++ b/nss_cache/config.py @@ -22,7 +22,10 @@ __author__ = 'vasilios@google.com (Vasilios Hoffman)' -import ConfigParser +try: + from configparser import ConfigParser +except ImportError: + from ConfigParser import SafeConfigParser as ConfigParser import logging import re @@ -140,7 +143,7 @@ def LoadConfig(configuration): Raises: error.NoConfigFound: no configuration file was found """ - parser = ConfigParser.ConfigParser() + parser = ConfigParser() # load config file configuration.log.debug('Attempting to parse configuration file: %s', diff --git a/nss_cache/lock.py b/nss_cache/lock.py index 7aae3bfc..6e9d6faa 100644 --- a/nss_cache/lock.py +++ b/nss_cache/lock.py @@ -122,7 +122,7 @@ def Lock(self, force=False): # Open the file and trap permission denied. try: self._Open() - except IOError, e: + except IOError as e: if e.errno == errno.EACCES: self.log.warning('Permission denied opening lock file: %s', self.filename) @@ -134,7 +134,7 @@ def Lock(self, force=False): try: fcntl.lockf(self._file, fcntl.LOCK_EX | fcntl.LOCK_NB) return_val = True - except IOError, e: + except IOError as e: if e.errno in [errno.EACCES, errno.EAGAIN]: # Catch the error raised when the file is locked. if not force: @@ -174,7 +174,7 @@ def SendTerm(self): pid_content = self._file.read() try: pid = int(pid_content.strip()) - except (AttributeError, ValueError), e: + except (AttributeError, ValueError) as e: self.log.warning('Not sending TERM, could not parse pid file content: %r', pid_content) return @@ -189,7 +189,7 @@ def SendTerm(self): proc_path = '%s/%i/cmdline' % (self.PROC_DIR, pid) try: proc_file = open(proc_path, 'r') - except IOError, e: + except IOError as e: if e.errno == errno.ENOENT: self.log.debug('process does not exist, skipping signal.') return diff --git a/nss_cache/lock_test.py b/nss_cache/lock_test.py index 34a0488c..0d48c0a0 100644 --- a/nss_cache/lock_test.py +++ b/nss_cache/lock_test.py @@ -31,7 +31,10 @@ import tempfile import unittest -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache import lock diff --git a/nss_cache/maps/automount_test.py b/nss_cache/maps/automount_test.py index 91916415..dc8ca0b7 100644 --- a/nss_cache/maps/automount_test.py +++ b/nss_cache/maps/automount_test.py @@ -42,10 +42,10 @@ def __init__(self, obj): def testInit(self): """Construct an empty or seeded AutomountMap.""" - self.assertEquals(automount.AutomountMap, type(automount.AutomountMap()), + self.assertEqual(automount.AutomountMap, type(automount.AutomountMap()), msg='failed to create an empty AutomountMap') amap = automount.AutomountMap([self._good_entry]) - self.assertEquals(self._good_entry, amap.PopItem(), + self.assertEqual(self._good_entry, amap.PopItem(), msg='failed to seed AutomountMap with list') self.assertRaises(TypeError, automount.AutomountMap, ['string']) @@ -53,12 +53,12 @@ def testAdd(self): """Add throws an error for objects it can't verify.""" amap = automount.AutomountMap() entry = self._good_entry - self.assert_(amap.Add(entry), msg='failed to append new entry.') + self.assertTrue(amap.Add(entry), msg='failed to append new entry.') - self.assertEquals(1, len(amap), msg='unexpected size for Map.') + self.assertEqual(1, len(amap), msg='unexpected size for Map.') ret_entry = amap.PopItem() - self.assertEquals(ret_entry, entry, msg='failed to pop correct entry.') + self.assertEqual(ret_entry, entry, msg='failed to pop correct entry.') pentry = passwd.PasswdMapEntry() pentry.name = 'foo' @@ -72,30 +72,30 @@ class TestAutomountMapEntry(unittest.TestCase): def testInit(self): """Construct an empty and seeded AutomountMapEntry.""" - self.assert_(automount.AutomountMapEntry(), + self.assertTrue(automount.AutomountMapEntry(), msg='Could not create empty AutomountMapEntry') seed = {'key': 'foo', 'location': '/dev/sda1'} entry = automount.AutomountMapEntry(seed) - self.assert_(entry.Verify(), + self.assertTrue(entry.Verify(), msg='Could not verify seeded AutomountMapEntry') - self.assertEquals(entry.key, 'foo', + self.assertEqual(entry.key, 'foo', msg='Entry returned wrong value for name') - self.assertEquals(entry.options, None, + self.assertEqual(entry.options, None, msg='Entry returned wrong value for options') - self.assertEquals(entry.location, '/dev/sda1', + self.assertEqual(entry.location, '/dev/sda1', msg='Entry returned wrong value for location') def testAttributes(self): """Test that we can get and set all expected attributes.""" entry = automount.AutomountMapEntry() entry.key = 'foo' - self.assertEquals(entry.key, 'foo', + self.assertEqual(entry.key, 'foo', msg='Could not set attribute: key') entry.options = 'noatime' - self.assertEquals(entry.options, 'noatime', + self.assertEqual(entry.options, 'noatime', msg='Could not set attribute: options') entry.location = '/dev/ipod' - self.assertEquals(entry.location, '/dev/ipod', + self.assertEqual(entry.location, '/dev/ipod', msg='Could not set attribute: location') def testVerify(self): @@ -103,13 +103,13 @@ def testVerify(self): entry = automount.AutomountMapEntry() # Empty object should bomb - self.failIf(entry.Verify()) + self.assertFalse(entry.Verify()) def testKey(self): """Key() should return the value of the 'key' attribute.""" entry = automount.AutomountMapEntry() entry.key = 'foo' - self.assertEquals(entry.Key(), entry.key) + self.assertEqual(entry.Key(), entry.key) if __name__ == '__main__': diff --git a/nss_cache/maps/group_test.py b/nss_cache/maps/group_test.py index ef668091..eff5e811 100644 --- a/nss_cache/maps/group_test.py +++ b/nss_cache/maps/group_test.py @@ -43,10 +43,10 @@ def __init__(self, obj): def testInit(self): """Construct an empty or seeded GroupMap.""" - self.assertEquals(group.GroupMap, type(group.GroupMap()), + self.assertEqual(group.GroupMap, type(group.GroupMap()), msg='failed to create an empty GroupMap') gmap = group.GroupMap([self._good_entry]) - self.assertEquals(self._good_entry, gmap.PopItem(), + self.assertEqual(self._good_entry, gmap.PopItem(), msg='failed to seed GroupMap with list') self.assertRaises(TypeError, group.GroupMap, ['string']) @@ -54,12 +54,12 @@ def testAdd(self): """Add throws an error for objects it can't verify.""" gmap = group.GroupMap() entry = self._good_entry - self.assert_(gmap.Add(entry), msg='failed to append new entry.') + self.assertTrue(gmap.Add(entry), msg='failed to append new entry.') - self.assertEquals(1, len(gmap), msg='unexpected size for Map.') + self.assertEqual(1, len(gmap), msg='unexpected size for Map.') ret_entry = gmap.PopItem() - self.assertEquals(ret_entry, entry, msg='failed to pop correct entry.') + self.assertEqual(ret_entry, entry, msg='failed to pop correct entry.') pentry = passwd.PasswdMapEntry() pentry.name = 'foo' @@ -73,36 +73,36 @@ class TestGroupMapEntry(unittest.TestCase): def testInit(self): """Construct an empty and seeded GroupMapEntry.""" - self.assert_(group.GroupMapEntry(), + self.assertTrue(group.GroupMapEntry(), msg='Could not create empty GroupMapEntry') seed = {'name': 'foo', 'gid': 10} entry = group.GroupMapEntry(seed) - self.assert_(entry.Verify(), + self.assertTrue(entry.Verify(), msg='Could not verify seeded PasswdMapEntry') - self.assertEquals(entry.name, 'foo', + self.assertEqual(entry.name, 'foo', msg='Entry returned wrong value for name') - self.assertEquals(entry.passwd, 'x', + self.assertEqual(entry.passwd, 'x', msg='Entry returned wrong value for passwd') - self.assertEquals(entry.gid, 10, + self.assertEqual(entry.gid, 10, msg='Entry returned wrong value for gid') - self.assertEquals(entry.members, [], + self.assertEqual(entry.members, [], msg='Entry returned wrong value for members') def testAttributes(self): """Test that we can get and set all expected attributes.""" entry = group.GroupMapEntry() entry.name = 'foo' - self.assertEquals(entry.name, 'foo', + self.assertEqual(entry.name, 'foo', msg='Could not set attribute: name') entry.passwd = 'x' - self.assertEquals(entry.passwd, 'x', + self.assertEqual(entry.passwd, 'x', msg='Could not set attribute: passwd') entry.gid = 10 - self.assertEquals(entry.gid, 10, + self.assertEqual(entry.gid, 10, msg='Could not set attribute: gid') members = ['foo', 'bar'] entry.members = members - self.assertEquals(entry.members, members, + self.assertEqual(entry.members, members, msg='Could not set attribute: members') def testVerify(self): @@ -110,13 +110,13 @@ def testVerify(self): entry = group.GroupMapEntry() # Empty object should bomb - self.failIf(entry.Verify()) + self.assertFalse(entry.Verify()) def testKey(self): """Key() should return the value of the 'name' attribute.""" entry = group.GroupMapEntry() entry.name = 'foo' - self.assertEquals(entry.Key(), entry.name) + self.assertEqual(entry.Key(), entry.name) if __name__ == '__main__': diff --git a/nss_cache/maps/maps.py b/nss_cache/maps/maps.py index 758af0dd..ca49ce00 100644 --- a/nss_cache/maps/maps.py +++ b/nss_cache/maps/maps.py @@ -133,7 +133,7 @@ def Add(self, entry): return False # Add to index if not already there. - if not self._data.has_key(entry.Key()): + if entry.Key() not in self._data: self._index.append(entry.Key()) else: self.log.warning('duplicate key detected when adding to map: %r, overwritten', entry.Key()) @@ -285,8 +285,12 @@ class MapEntry(object): # Using slots saves us over 2x memory on large maps. __slots__ = ('_KEY', '_ATTRS', 'log') # Overridden in the derived classes - _KEY = None - _ATTRS = None + def __init__(_KEY=None, _ATTRS=None): + _KEY + _ATTRS + super().__init__() + object.__setattr__(self, "_KEY", _KEY) + object.__setattr__(self, "_ATTRS", _ATTRS) def __init__(self, data=None): """This is an abstract class. diff --git a/nss_cache/maps/maps_test.py b/nss_cache/maps/maps_test.py index ab3a9b6a..b9022aea 100644 --- a/nss_cache/maps/maps_test.py +++ b/nss_cache/maps/maps_test.py @@ -44,7 +44,7 @@ class StubMap(maps.Map): foo = StubMap() now = int(time.time()) foo.SetModifyTimestamp(now) - self.assertEquals(now, + self.assertEqual(now, foo.GetModifyTimestamp()) self.assertRaises(TypeError, foo.SetModifyTimestamp, @@ -59,7 +59,7 @@ class StubMap(maps.Map): foo = StubMap() now = int(time.time()) foo.SetUpdateTimestamp(now) - self.assertEquals(now, + self.assertEqual(now, foo.GetUpdateTimestamp()) self.assertRaises(TypeError, foo.SetUpdateTimestamp, diff --git a/nss_cache/maps/netgroup_test.py b/nss_cache/maps/netgroup_test.py index b10faf88..486d6cb8 100644 --- a/nss_cache/maps/netgroup_test.py +++ b/nss_cache/maps/netgroup_test.py @@ -41,10 +41,10 @@ def __init__(self, obj): def testInit(self): """Construct an empty or seeded NetgroupMap.""" - self.assertEquals(netgroup.NetgroupMap, type(netgroup.NetgroupMap()), + self.assertEqual(netgroup.NetgroupMap, type(netgroup.NetgroupMap()), msg='failed to create an empty NetgroupMap') nmap = netgroup.NetgroupMap([self._good_entry]) - self.assertEquals(self._good_entry, nmap.PopItem(), + self.assertEqual(self._good_entry, nmap.PopItem(), msg='failed to seed NetgroupMap with list') self.assertRaises(TypeError, netgroup.NetgroupMap, ['string']) @@ -52,12 +52,12 @@ def testAdd(self): """Add throws an error for objects it can't verify.""" nmap = netgroup.NetgroupMap() entry = self._good_entry - self.assert_(nmap.Add(entry), msg='failed to append new entry.') + self.assertTrue(nmap.Add(entry), msg='failed to append new entry.') - self.assertEquals(1, len(nmap), msg='unexpected size for Map.') + self.assertEqual(1, len(nmap), msg='unexpected size for Map.') ret_entry = nmap.PopItem() - self.assertEquals(ret_entry, entry, msg='failed to pop correct entry.') + self.assertEqual(ret_entry, entry, msg='failed to pop correct entry.') pentry = passwd.PasswdMapEntry() pentry.name = 'foo' @@ -71,27 +71,27 @@ class TestNetgroupMapEntry(unittest.TestCase): def testInit(self): """Construct an empty and seeded NetgroupMapEntry.""" - self.assert_(netgroup.NetgroupMapEntry(), + self.assertTrue(netgroup.NetgroupMapEntry(), msg='Could not create empty NetgroupMapEntry') entries = ['bar', ('baz', '-', None)] seed = {'name': 'foo', 'entries': entries} entry = netgroup.NetgroupMapEntry(seed) - self.assert_(entry.Verify(), + self.assertTrue(entry.Verify(), msg='Could not verify seeded NetgroupMapEntry') - self.assertEquals(entry.name, 'foo', + self.assertEqual(entry.name, 'foo', msg='Entry returned wrong value for name') - self.assertEquals(entry.entries, entries, + self.assertEqual(entry.entries, entries, msg='Entry returned wrong value for entries') def testAttributes(self): """Test that we can get and set all expected attributes.""" entry = netgroup.NetgroupMapEntry() entry.name = 'foo' - self.assertEquals(entry.name, 'foo', + self.assertEqual(entry.name, 'foo', msg='Could not set attribute: name') entries = ['foo', '(-,bar,)'] entry.entries = entries - self.assertEquals(entry.entries, entries, + self.assertEqual(entry.entries, entries, msg='Could not set attribute: entries') def testVerify(self): @@ -99,13 +99,13 @@ def testVerify(self): entry = netgroup.NetgroupMapEntry() # Empty object should bomb - self.failIf(entry.Verify()) + self.assertFalse(entry.Verify()) def testKey(self): """Key() should return the value of the 'name' attribute.""" entry = netgroup.NetgroupMapEntry() entry.name = 'foo' - self.assertEquals(entry.Key(), entry.name) + self.assertEqual(entry.Key(), entry.name) if __name__ == '__main__': diff --git a/nss_cache/maps/passwd_test.py b/nss_cache/maps/passwd_test.py index f9f55331..61c33ab4 100644 --- a/nss_cache/maps/passwd_test.py +++ b/nss_cache/maps/passwd_test.py @@ -42,10 +42,10 @@ def setUp(self): def testInit(self): """Construct an empty or seeded PasswdMap.""" - self.assertEquals(passwd.PasswdMap, type(passwd.PasswdMap()), + self.assertEqual(passwd.PasswdMap, type(passwd.PasswdMap()), msg='failed to create emtpy PasswdMap') pmap = passwd.PasswdMap([self._good_entry]) - self.assertEquals(self._good_entry, pmap.PopItem(), + self.assertEqual(self._good_entry, pmap.PopItem(), msg='failed to seed PasswdMap with list') self.assertRaises(TypeError, passwd.PasswdMap, ['string']) @@ -53,14 +53,14 @@ def testAdd(self): """Add raises exceptions for objects it can't add or verify.""" pmap = passwd.PasswdMap() entry = self._good_entry - self.assert_(pmap.Add(entry), + self.assertTrue(pmap.Add(entry), msg='failed to add new entry.') - self.assertEquals(1, len(pmap), + self.assertEqual(1, len(pmap), msg='unexpected size for Map.') ret_entry = pmap.PopItem() - self.assertEquals(ret_entry, entry, + self.assertEqual(ret_entry, entry, msg='failed to pop existing entry.') gentry = group.GroupMapEntry() @@ -92,18 +92,18 @@ def testIterate(self): ret_entries = [] for entry in pmap: ret_entries.append(entry) - self.assertEquals(len(ret_entries), 1, + self.assertEqual(len(ret_entries), 1, msg='iterated over wrong count') - self.assertEquals(ret_entries[0], self._good_entry, + self.assertEqual(ret_entries[0], self._good_entry, msg='got the wrong entry back') def testLen(self): """Verify we have correctly overridden __len__ in MapEntry.""" pmap = passwd.PasswdMap() - self.assertEquals(len(pmap), 0, + self.assertEqual(len(pmap), 0, msg='expected len(pmap) to be 0') pmap.Add(self._good_entry) - self.assertEquals(len(pmap), 1, + self.assertEqual(len(pmap), 1, msg='expected len(pmap) to be 1') def testExists(self): @@ -161,7 +161,7 @@ def testMerge(self): def testPopItem(self): """Verify you can retrieve MapEntry with PopItem.""" pmap = passwd.PasswdMap([self._good_entry]) - self.assertEquals(pmap.PopItem(), self._good_entry) + self.assertEqual(pmap.PopItem(), self._good_entry) def testLastModificationTimestamp(self): """Test setting/getting of timestamps on maps.""" @@ -182,51 +182,51 @@ class TestPasswdMapEntry(unittest.TestCase): def testInit(self): """Construct empty and seeded PasswdMapEntry.""" entry = passwd.PasswdMapEntry() - self.assertEquals(type(entry), passwd.PasswdMapEntry, + self.assertEqual(type(entry), passwd.PasswdMapEntry, msg='Could not create empty PasswdMapEntry') seed = {'name': 'foo', 'passwd': 'x', 'uid': 10, 'gid': 10, 'gecos': '', 'dir': '', 'shell': ''} entry = passwd.PasswdMapEntry(seed) - self.assert_(entry.Verify(), + self.assertTrue(entry.Verify(), msg='Could not verify seeded PasswdMapEntry') - self.assertEquals(entry.name, 'foo', + self.assertEqual(entry.name, 'foo', msg='Entry returned wrong value for name') - self.assertEquals(entry.passwd, 'x', + self.assertEqual(entry.passwd, 'x', msg='Entry returned wrong value for passwd') - self.assertEquals(entry.uid, 10, + self.assertEqual(entry.uid, 10, msg='Entry returned wrong value for uid') - self.assertEquals(entry.gid, 10, + self.assertEqual(entry.gid, 10, msg='Entry returned wrong value for gid') - self.assertEquals(entry.gecos, '', + self.assertEqual(entry.gecos, '', msg='Entry returned wrong value for gecos') - self.assertEquals(entry.dir, '', + self.assertEqual(entry.dir, '', msg='Entry returned wrong value for dir') - self.assertEquals(entry.shell, '', + self.assertEqual(entry.shell, '', msg='Entry returned wrong value for shell') def testAttributes(self): """Test that we can get and set all expected attributes.""" entry = passwd.PasswdMapEntry() entry.name = 'foo' - self.assertEquals(entry.name, 'foo', + self.assertEqual(entry.name, 'foo', msg='Could not set attribute: name') entry.passwd = 'x' - self.assertEquals(entry.passwd, 'x', + self.assertEqual(entry.passwd, 'x', msg='Could not set attribute: passwd') entry.uid = 10 - self.assertEquals(entry.uid, 10, + self.assertEqual(entry.uid, 10, msg='Could not set attribute: uid') entry.gid = 10 - self.assertEquals(entry.gid, 10, + self.assertEqual(entry.gid, 10, msg='Could not set attribute: gid') entry.gecos = 'How Now Brown Cow' - self.assertEquals(entry.gecos, 'How Now Brown Cow', + self.assertEqual(entry.gecos, 'How Now Brown Cow', msg='Could not set attribute: gecos') entry.dir = '/home/foo' - self.assertEquals(entry.dir, '/home/foo', + self.assertEqual(entry.dir, '/home/foo', msg='Could not set attribute: dir') entry.shell = '/bin/bash' - self.assertEquals(entry.shell, '/bin/bash', + self.assertEqual(entry.shell, '/bin/bash', msg='Could not set attribute: shell') def testEq(self): @@ -242,9 +242,9 @@ def testEq(self): entry_bad = passwd.PasswdMapEntry() entry_bad.name = 'bar' - self.assertEquals(entry_good, entry_good, + self.assertEqual(entry_good, entry_good, msg='entry_good not equal to itself') - self.assertEquals(entry_good, entry_same_as_good, + self.assertEqual(entry_good, entry_same_as_good, msg='__eq__ not doing deep compare') self.assertNotEqual(entry_good, entry_like_good, msg='__eq__ not doing deep compare') @@ -256,13 +256,13 @@ def testVerify(self): entry = passwd.PasswdMapEntry() # by leaving _KEY unset, we should bomb. - self.failIf(entry.Verify()) + self.assertFalse(entry.Verify()) def testKey(self): """Key() should return the value of the 'name' attribute.""" entry = passwd.PasswdMapEntry() entry.name = 'foo' - self.assertEquals(entry.Key(), entry.name) + self.assertEqual(entry.Key(), entry.name) if __name__ == '__main__': diff --git a/nss_cache/maps/shadow_test.py b/nss_cache/maps/shadow_test.py index 48aefec5..42b3ccea 100644 --- a/nss_cache/maps/shadow_test.py +++ b/nss_cache/maps/shadow_test.py @@ -47,10 +47,10 @@ def __init__(self, obj): def testInit(self): """Construct an empty or seeded ShadowMap.""" - self.assertEquals(shadow.ShadowMap, type(shadow.ShadowMap()), + self.assertEqual(shadow.ShadowMap, type(shadow.ShadowMap()), msg='failed to create emtpy ShadowMap') smap = shadow.ShadowMap([self._good_entry]) - self.assertEquals(self._good_entry, smap.PopItem(), + self.assertEqual(self._good_entry, smap.PopItem(), msg='failed to seed ShadowMap with list') self.assertRaises(TypeError, shadow.ShadowMap, ['string']) @@ -58,12 +58,12 @@ def testAdd(self): """Add throws an error for objects it can't verify.""" smap = shadow.ShadowMap() entry = self._good_entry - self.assert_(smap.Add(entry), msg='failed to append new entry.') + self.assertTrue(smap.Add(entry), msg='failed to append new entry.') - self.assertEquals(1, len(smap), msg='unexpected size for Map.') + self.assertEqual(1, len(smap), msg='unexpected size for Map.') ret_entry = smap.PopItem() - self.assertEquals(ret_entry, entry, msg='failed to pop existing entry.') + self.assertEqual(ret_entry, entry, msg='failed to pop existing entry.') pentry = passwd.PasswdMapEntry() pentry.name = 'foo' @@ -77,60 +77,60 @@ class TestShadowMapEntry(unittest.TestCase): def testInit(self): """Construct empty and seeded ShadowMapEntry.""" - self.assert_(shadow.ShadowMapEntry(), + self.assertTrue(shadow.ShadowMapEntry(), msg='Could not create empty ShadowMapEntry') seed = {'name': 'foo'} entry = shadow.ShadowMapEntry(seed) - self.assert_(entry.Verify(), + self.assertTrue(entry.Verify(), msg='Could not verify seeded ShadowMapEntry') - self.assertEquals(entry.name, 'foo', + self.assertEqual(entry.name, 'foo', msg='Entry returned wrong value for name') - self.assertEquals(entry.passwd, '!!', + self.assertEqual(entry.passwd, '!!', msg='Entry returned wrong value for passwd') - self.assertEquals(entry.lstchg, None, + self.assertEqual(entry.lstchg, None, msg='Entry returned wrong value for lstchg') - self.assertEquals(entry.min, None, + self.assertEqual(entry.min, None, msg='Entry returned wrong value for min') - self.assertEquals(entry.max, None, + self.assertEqual(entry.max, None, msg='Entry returned wrong value for max') - self.assertEquals(entry.warn, None, + self.assertEqual(entry.warn, None, msg='Entry returned wrong value for warn') - self.assertEquals(entry.inact, None, + self.assertEqual(entry.inact, None, msg='Entry returned wrong value for inact') - self.assertEquals(entry.expire, None, + self.assertEqual(entry.expire, None, msg='Entry returned wrong value for expire') - self.assertEquals(entry.flag, None, + self.assertEqual(entry.flag, None, msg='Entry returned wrong value for flag') def testAttributes(self): """Test that we can get and set all expected attributes.""" entry = shadow.ShadowMapEntry() entry.name = 'foo' - self.assertEquals(entry.name, 'foo', + self.assertEqual(entry.name, 'foo', msg='Could not set attribute: name') entry.passwd = 'seekret' - self.assertEquals(entry.passwd, 'seekret', + self.assertEqual(entry.passwd, 'seekret', msg='Could not set attribute: passwd') entry.lstchg = 0 - self.assertEquals(entry.lstchg, 0, + self.assertEqual(entry.lstchg, 0, msg='Could not set attribute: lstchg') entry.min = 0 - self.assertEquals(entry.min, 0, + self.assertEqual(entry.min, 0, msg='Could not set attribute: min') entry.max = 0 - self.assertEquals(entry.max, 0, + self.assertEqual(entry.max, 0, msg='Could not set attribute: max') entry.warn = 0 - self.assertEquals(entry.warn, 0, + self.assertEqual(entry.warn, 0, msg='Could not set attribute: warn') entry.inact = 0 - self.assertEquals(entry.inact, 0, + self.assertEqual(entry.inact, 0, msg='Could not set attribute: inact') entry.expire = 0 - self.assertEquals(entry.expire, 0, + self.assertEqual(entry.expire, 0, msg='Could not set attribute: expire') entry.flag = 0 - self.assertEquals(entry.flag, 0, + self.assertEqual(entry.flag, 0, msg='Could not set attribute: flag') def testVerify(self): @@ -138,13 +138,13 @@ def testVerify(self): entry = shadow.ShadowMapEntry() # Emtpy object should bomb - self.failIf(entry.Verify()) + self.assertFalse(entry.Verify()) def testKey(self): """Key() should return the value of the 'name' attribute.""" entry = shadow.ShadowMapEntry() entry.name = 'foo' - self.assertEquals(entry.Key(), entry.name) + self.assertEqual(entry.Key(), entry.name) if __name__ == '__main__': diff --git a/nss_cache/nss.py b/nss_cache/nss.py index b543d9ad..c3631de2 100644 --- a/nss_cache/nss.py +++ b/nss_cache/nss.py @@ -18,9 +18,9 @@ __author__ = 'vasilios@google.com (Vasilios Hoffman)' +import pwd import grp import logging -import pwd import subprocess from nss_cache import config diff --git a/nss_cache/nss_test.py b/nss_cache/nss_test.py index e2f01bd9..5c0e824a 100644 --- a/nss_cache/nss_test.py +++ b/nss_cache/nss_test.py @@ -22,7 +22,10 @@ import pwd import unittest -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache import config from nss_cache import error diff --git a/nss_cache/sources/consulsource.py b/nss_cache/sources/consulsource.py index 64c61da4..014c3a99 100644 --- a/nss_cache/sources/consulsource.py +++ b/nss_cache/sources/consulsource.py @@ -140,7 +140,7 @@ def GetMap(self, cache_info, data): entry_piece = key[-1] entries[name][entry_piece] = value - for name, entry in entries.iteritems(): + for name, entry in entries.items(): map_entry = self._ReadEntry(name, entry) if map_entry is None: self.log.warn('Could not create entry from line %r in cache, skipping', diff --git a/nss_cache/sources/consulsource_test.py b/nss_cache/sources/consulsource_test.py index 79914d95..29b4cc91 100644 --- a/nss_cache/sources/consulsource_test.py +++ b/nss_cache/sources/consulsource_test.py @@ -2,7 +2,7 @@ __author__ = 'hexedpackets@gmail.com (William Huba)' -import StringIO +from io import BytesIO as StringIO import unittest from nss_cache.maps import group @@ -25,17 +25,17 @@ def setUp(self): def testDefaultConfiguration(self): source = consulsource.ConsulFilesSource({}) - self.assertEquals(source.conf['datacenter'], + self.assertEqual(source.conf['datacenter'], consulsource.ConsulFilesSource.DATACENTER) - self.assertEquals(source.conf['token'], + self.assertEqual(source.conf['token'], consulsource.ConsulFilesSource.TOKEN) def testOverrideDefaultConfiguration(self): source = consulsource.ConsulFilesSource(self.config) - self.assertEquals(source.conf['datacenter'], 'TEST_DATACENTER') - self.assertEquals(source.conf['token'], 'TEST_TOKEN') - self.assertEquals(source.conf['passwd_url'], 'PASSWD_URL?recurse&token=TEST_TOKEN&dc=TEST_DATACENTER') - self.assertEquals(source.conf['group_url'], 'GROUP_URL?recurse&token=TEST_TOKEN&dc=TEST_DATACENTER') + self.assertEqual(source.conf['datacenter'], 'TEST_DATACENTER') + self.assertEqual(source.conf['token'], 'TEST_TOKEN') + self.assertEqual(source.conf['passwd_url'], 'PASSWD_URL?recurse&token=TEST_TOKEN&dc=TEST_DATACENTER') + self.assertEqual(source.conf['group_url'], 'GROUP_URL?recurse&token=TEST_TOKEN&dc=TEST_DATACENTER') class TestPasswdMapParser(unittest.TestCase): @@ -53,34 +53,34 @@ def setUp(self): def testGetMap(self): passwd_map = passwd.PasswdMap() - cache_info = StringIO.StringIO('''[ - {"Key": "org/users/foo/uid", "Value": "MTA="}, - {"Key": "org/users/foo/gid", "Value": "MTA="}, - {"Key": "org/users/foo/home", "Value": "L2hvbWUvZm9v"}, - {"Key": "org/users/foo/shell", "Value": "L2Jpbi9iYXNo"}, - {"Key": "org/users/foo/comment", "Value": "SG93IE5vdyBCcm93biBDb3c="}, - {"Key": "org/users/foo/subkey/irrelevant_key", "Value": "YmFjb24="} - ]''') + cache_info = StringIO('''[ + {"Key": "org/users/foo/uid", "Value": "MTA="}, + {"Key": "org/users/foo/gid", "Value": "MTA="}, + {"Key": "org/users/foo/home", "Value": "L2hvbWUvZm9v"}, + {"Key": "org/users/foo/shell", "Value": "L2Jpbi9iYXNo"}, + {"Key": "org/users/foo/comment", "Value": "SG93IE5vdyBCcm93biBDb3c="}, + {"Key": "org/users/foo/subkey/irrelevant_key", "Value": "YmFjb24="} + ]''') self.parser.GetMap(cache_info, passwd_map) - self.assertEquals(self.good_entry, passwd_map.PopItem()) + self.assertEqual(self.good_entry, passwd_map.PopItem()) def testReadEntry(self): data = {'uid': '10', 'gid': '10', 'comment': 'How Now Brown Cow', 'shell': '/bin/bash', 'home': '/home/foo', 'passwd': 'x'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) def testDefaultEntryValues(self): data = {'uid': '10', 'gid': '10'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(entry.shell, '/bin/bash') - self.assertEquals(entry.dir, '/home/foo') - self.assertEquals(entry.gecos, '') - self.assertEquals(entry.passwd, 'x') + self.assertEqual(entry.shell, '/bin/bash') + self.assertEqual(entry.dir, '/home/foo') + self.assertEqual(entry.gecos, '') + self.assertEqual(entry.passwd, 'x') def testInvalidEntry(self): data = {'irrelevant_key': 'bacon'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(entry, None) + self.assertEqual(entry, None) class TestConsulGroupMapParser(unittest.TestCase): @@ -95,33 +95,33 @@ def setUp(self): def testGetMap(self): group_map = group.GroupMap() - cache_info = StringIO.StringIO('''[ - {"Key": "org/groups/foo/gid", "Value": "MTA="}, - {"Key": "org/groups/foo/members", "Value": "Zm9vCmJhcg=="}, - {"Key": "org/groups/foo/subkey/irrelevant_key", "Value": "YmFjb24="} - ]''') + cache_info = StringIO('''[ + {"Key": "org/groups/foo/gid", "Value": "MTA="}, + {"Key": "org/groups/foo/members", "Value": "Zm9vCmJhcg=="}, + {"Key": "org/groups/foo/subkey/irrelevant_key", "Value": "YmFjb24="} + ]''') self.parser.GetMap(cache_info, group_map) - self.assertEquals(self.good_entry, group_map.PopItem()) + self.assertEqual(self.good_entry, group_map.PopItem()) def testReadEntry(self): data = {'passwd': 'x', 'gid': '10', 'members': 'foo\nbar'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) def testDefaultPasswd(self): data = {'gid': '10', 'members': 'foo\nbar'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) def testNoMembers(self): data = {'gid': '10', 'members': ''} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(entry.members, ['']) + self.assertEqual(entry.members, ['']) def testInvalidEntry(self): data = {'irrelevant_key': 'bacon'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(entry, None) + self.assertEqual(entry, None) class TestConsulShadowMapParser(unittest.TestCase): @@ -138,25 +138,25 @@ def setUp(self): def testGetMap(self): shadow_map = shadow.ShadowMap() - cache_info = StringIO.StringIO('''[ - {"Key": "org/groups/foo/passwd", "Value": "Kg=="}, - {"Key": "org/groups/foo/lstchg", "Value": "MTcyNDY="}, - {"Key": "org/groups/foo/min", "Value": "MA=="}, - {"Key": "org/groups/foo/max", "Value": "OTk5OTk="}, - {"Key": "org/groups/foo/warn", "Value": "Nw=="} - ]''') + cache_info = StringIO('''[ + {"Key": "org/groups/foo/passwd", "Value": "Kg=="}, + {"Key": "org/groups/foo/lstchg", "Value": "MTcyNDY="}, + {"Key": "org/groups/foo/min", "Value": "MA=="}, + {"Key": "org/groups/foo/max", "Value": "OTk5OTk="}, + {"Key": "org/groups/foo/warn", "Value": "Nw=="} + ]''') self.parser.GetMap(cache_info, shadow_map) - self.assertEquals(self.good_entry, shadow_map.PopItem()) + self.assertEqual(self.good_entry, shadow_map.PopItem()) def testReadEntry(self): data = {'passwd': '*', 'lstchg': 17246, 'min': 0, 'max': 99999, 'warn': 7} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) def testDefaultPasswd(self): data = {'lstchg': 17246, 'min': 0, 'max': 99999, 'warn': 7} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) if __name__ == '__main__': diff --git a/nss_cache/sources/httpsource.py b/nss_cache/sources/httpsource.py index c9f9549c..01b29f17 100644 --- a/nss_cache/sources/httpsource.py +++ b/nss_cache/sources/httpsource.py @@ -20,12 +20,18 @@ import bz2 import calendar -import cStringIO import logging import os import pycurl import time -import urlparse +try: + from urlparse import urljoin +except ImportError: + from urllib.parse import urljoin +try: + from cStringIO import StringIO +except ImportError: + from io import BytesIO as StringIO from nss_cache import error from nss_cache.maps import automount @@ -177,7 +183,7 @@ def GetAutomountMap(self, since=None, location=None): if location is None: self.log.error('A location is required to retrieve an automount map!') raise error.EmptyMap - automount_url = urlparse.urljoin(self.conf['automount_base_url'], + automount_url = urljoin(self.conf['automount_base_url'], location) return AutomountUpdateGetter().GetUpdates(self, automount_url, since) @@ -304,10 +310,10 @@ def GetUpdates(self, source, url, since): # curl (on Ubuntu hardy at least) will handle gzip, but not bzip2 try: - response = cStringIO.StringIO(bz2.decompress(body)) + response = StringIO(bz2.decompress(body)) self.log.debug('bzip encoding found') except IOError: - response = cStringIO.StringIO(body) + response = StringIO(body) data_map = self.GetMap(cache_info=response) if http_ts_string: diff --git a/nss_cache/sources/httpsource_test.py b/nss_cache/sources/httpsource_test.py index e1f13764..d133c2b1 100644 --- a/nss_cache/sources/httpsource_test.py +++ b/nss_cache/sources/httpsource_test.py @@ -20,10 +20,13 @@ import time import unittest - -import mox import pycurl +try: + import mox +except ImportError: + import mox3 + from nss_cache import error from nss_cache.maps import automount @@ -54,32 +57,32 @@ def setUp(self): def testDefaultConfiguration(self): source = httpsource.HttpFilesSource({}) - self.assertEquals(source.conf['passwd_url'], + self.assertEqual(source.conf['passwd_url'], httpsource.HttpFilesSource.PASSWD_URL) - self.assertEquals(source.conf['shadow_url'], + self.assertEqual(source.conf['shadow_url'], httpsource.HttpFilesSource.SHADOW_URL) - self.assertEquals(source.conf['group_url'], + self.assertEqual(source.conf['group_url'], httpsource.HttpFilesSource.GROUP_URL) - self.assertEquals(source.conf['sshkey_url'], + self.assertEqual(source.conf['sshkey_url'], httpsource.HttpFilesSource.SSHKEY_URL) - self.assertEquals(source.conf['retry_max'], + self.assertEqual(source.conf['retry_max'], httpsource.HttpFilesSource.RETRY_MAX) - self.assertEquals(source.conf['retry_delay'], + self.assertEqual(source.conf['retry_delay'], httpsource.HttpFilesSource.RETRY_DELAY) - self.assertEquals(source.conf['tls_cacertfile'], + self.assertEqual(source.conf['tls_cacertfile'], httpsource.HttpFilesSource.TLS_CACERTFILE) - self.assertEquals(source.conf['http_proxy'], None) + self.assertEqual(source.conf['http_proxy'], None) def testOverrideDefaultConfiguration(self): source = httpsource.HttpFilesSource(self.config) - self.assertEquals(source.conf['passwd_url'], 'PASSWD_URL') - self.assertEquals(source.conf['group_url'], 'GROUP_URL') - self.assertEquals(source.conf['shadow_url'], 'SHADOW_URL') - self.assertEquals(source.conf['sshkey_url'], 'SSHKEY_URL') - self.assertEquals(source.conf['retry_delay'], 'TEST_RETRY_DELAY') - self.assertEquals(source.conf['retry_max'], 'TEST_RETRY_MAX') - self.assertEquals(source.conf['tls_cacertfile'], 'TEST_TLS_CACERTFILE') - self.assertEquals(source.conf['http_proxy'], 'HTTP_PROXY') + self.assertEqual(source.conf['passwd_url'], 'PASSWD_URL') + self.assertEqual(source.conf['group_url'], 'GROUP_URL') + self.assertEqual(source.conf['shadow_url'], 'SHADOW_URL') + self.assertEqual(source.conf['sshkey_url'], 'SSHKEY_URL') + self.assertEqual(source.conf['retry_delay'], 'TEST_RETRY_DELAY') + self.assertEqual(source.conf['retry_max'], 'TEST_RETRY_MAX') + self.assertEqual(source.conf['tls_cacertfile'], 'TEST_TLS_CACERTFILE') + self.assertEqual(source.conf['http_proxy'], 'HTTP_PROXY') class TestHttpUpdateGetter(mox.MoxTestBase): @@ -87,13 +90,13 @@ class TestHttpUpdateGetter(mox.MoxTestBase): def testFromTimestampToHttp(self): ts = 1259641025 expected_http_ts = 'Tue, 01 Dec 2009 04:17:05 GMT' - self.assertEquals(expected_http_ts, + self.assertEqual(expected_http_ts, httpsource.UpdateGetter().FromTimestampToHttp(ts)) def testFromHttpToTimestamp(self): expected_ts = 1259641025 http_ts = 'Tue, 01 Dec 2009 04:17:05 GMT' - self.assertEquals(expected_ts, + self.assertEqual(expected_ts, httpsource.UpdateGetter().FromHttpToTimestamp(http_ts)) def testAcceptHttpProtocol(self): @@ -158,7 +161,7 @@ def testNoUpdatesForTemporaryFailure(self): source = httpsource.HttpFilesSource(config) result = httpsource.UpdateGetter().GetUpdates( source, 'https://TEST_URL', 37) - self.assertEquals(result, []) + self.assertEqual(result, []) def testGetUpdatesIfTimestampNotMatch(self): ts = 1259641025 diff --git a/nss_cache/sources/ldapsource.py b/nss_cache/sources/ldapsource.py index 466201f7..b7d94331 100644 --- a/nss_cache/sources/ldapsource.py +++ b/nss_cache/sources/ldapsource.py @@ -24,10 +24,13 @@ import time import ldap import ldap.sasl -import urllib import re from binascii import b2a_hex from distutils.version import StrictVersion +try: + from urllib import quote +except ImportError: + from urllib.parse import quote from nss_cache import error from nss_cache.maps import automount @@ -175,7 +178,7 @@ def _SetDefaults(self, configuration): # LDAPI URLs must be url escaped socket filenames; rewrite if necessary. if 'uri' in configuration: if configuration['uri'].startswith('ldapi://'): - configuration['uri'] = 'ldapi://' + urllib.quote(configuration['uri'][8:], '') + configuration['uri'] = 'ldapi://' + quote(configuration['uri'][8:], '') if not 'bind_dn' in configuration: configuration['bind_dn'] = self.BIND_DN if not 'bind_password' in configuration: @@ -259,7 +262,7 @@ def Bind(self, configuration): self.conn.simple_bind_s(who=configuration['bind_dn'], cred=str(configuration['bind_password'])) break - except ldap.SERVER_DOWN, e: + except ldap.SERVER_DOWN as e: retry_count += 1 self.log.warning('Failed LDAP connection: attempt #%s.', retry_count) self.log.debug('ldap error is %r', e) @@ -671,7 +674,7 @@ def GetUpdates(self, source, search_base, search_filter, try: if not data_map.Add(self.Transform(obj)): logging.info('could not add obj: %r', obj) - except AttributeError, e: + except AttributeError as e: logging.warning('error %r, discarding malformed obj: %r', str(e), obj) # Perform some post processing on the data_map. diff --git a/nss_cache/sources/ldapsource_test.py b/nss_cache/sources/ldapsource_test.py index 28ff2d13..1290a046 100644 --- a/nss_cache/sources/ldapsource_test.py +++ b/nss_cache/sources/ldapsource_test.py @@ -21,9 +21,12 @@ import time import unittest - import ldap -import mox + +try: + import mox +except ImportError: + import mox3 from nss_cache import error from nss_cache.maps import automount @@ -76,18 +79,18 @@ def testDefaultConfiguration(self): self.mox.ReplayAll() source = ldapsource.LdapSource(config) - self.assertEquals(source.conf['bind_dn'], + self.assertEqual(source.conf['bind_dn'], ldapsource.LdapSource.BIND_DN) - self.assertEquals(source.conf['bind_password'], + self.assertEqual(source.conf['bind_password'], ldapsource.LdapSource.BIND_PASSWORD) - self.assertEquals(source.conf['retry_max'], + self.assertEqual(source.conf['retry_max'], ldapsource.LdapSource.RETRY_MAX) - self.assertEquals(source.conf['retry_delay'], + self.assertEqual(source.conf['retry_delay'], ldapsource.LdapSource.RETRY_DELAY) - self.assertEquals(source.conf['scope'], ldapsource.LdapSource.SCOPE) - self.assertEquals(source.conf['timelimit'], + self.assertEqual(source.conf['scope'], ldapsource.LdapSource.SCOPE) + self.assertEqual(source.conf['timelimit'], ldapsource.LdapSource.TIMELIMIT) - self.assertEquals(source.conf['tls_require_cert'], + self.assertEqual(source.conf['tls_require_cert'], ldap.OPT_X_TLS_DEMAND) def testOverrideDefaultConfiguration(self): @@ -103,15 +106,15 @@ def testOverrideDefaultConfiguration(self): self.mox.ReplayAll() source = ldapsource.LdapSource(config) - self.assertEquals(source.conf['scope'], ldap.SCOPE_BASE) - self.assertEquals(source.conf['bind_dn'], 'TEST_BIND_DN') - self.assertEquals(source.conf['bind_password'], 'TEST_BIND_PASSWORD') - self.assertEquals(source.conf['retry_delay'], 'TEST_RETRY_DELAY') - self.assertEquals(source.conf['retry_max'], 'TEST_RETRY_MAX') - self.assertEquals(source.conf['timelimit'], 'TEST_TIMELIMIT') - self.assertEquals(source.conf['tls_require_cert'], 0) - self.assertEquals(source.conf['tls_cacertdir'], 'TEST_TLS_CACERTDIR') - self.assertEquals(source.conf['tls_cacertfile'], + self.assertEqual(source.conf['scope'], ldap.SCOPE_BASE) + self.assertEqual(source.conf['bind_dn'], 'TEST_BIND_DN') + self.assertEqual(source.conf['bind_password'], 'TEST_BIND_PASSWORD') + self.assertEqual(source.conf['retry_delay'], 'TEST_RETRY_DELAY') + self.assertEqual(source.conf['retry_max'], 'TEST_RETRY_MAX') + self.assertEqual(source.conf['timelimit'], 'TEST_TIMELIMIT') + self.assertEqual(source.conf['tls_require_cert'], 0) + self.assertEqual(source.conf['tls_cacertdir'], 'TEST_TLS_CACERTDIR') + self.assertEqual(source.conf['tls_cacertfile'], 'TEST_TLS_CACERTFILE') def testDebugLevelSet(self): @@ -1454,7 +1457,7 @@ def testVerify(self): self.mox.ReplayAll() source = ldapsource.LdapSource(self.config) - self.assertEquals(0, source.Verify(0)) + self.assertEqual(0, source.Verify(0)) class TestUpdateGetter(unittest.TestCase): @@ -1477,13 +1480,13 @@ def Search(self, search_base, search_filter, search_scope, attrs): def testFromTimestampToLdap(self): ts = 1259641025 expected_ldap_ts = '20091201041705Z' - self.assertEquals(expected_ldap_ts, + self.assertEqual(expected_ldap_ts, ldapsource.UpdateGetter({}).FromTimestampToLdap(ts)) def testFromLdapToTimestamp(self): expected_ts = 1259641025 ldap_ts = '20091201041705Z' - self.assertEquals(expected_ts, + self.assertEqual(expected_ts, ldapsource.UpdateGetter({}).FromLdapToTimestamp(ldap_ts)) def testPasswdEmptySourceGetUpdates(self): diff --git a/nss_cache/sources/s3source_test.py b/nss_cache/sources/s3source_test.py index 0505796a..ea7f2ef1 100644 --- a/nss_cache/sources/s3source_test.py +++ b/nss_cache/sources/s3source_test.py @@ -2,7 +2,7 @@ __author__ = 'alexey.pikin@gmail.com' -import StringIO +from io import BytesIO as StringIO import unittest from nss_cache.maps import group @@ -23,14 +23,14 @@ def setUp(self): def testDefaultConfiguration(self): source = s3source.S3FilesSource({}) - self.assertEquals(source.conf['bucket'], s3source.S3FilesSource.BUCKET) - self.assertEquals(source.conf['passwd_object'], s3source.S3FilesSource.PASSWD_OBJECT) + self.assertEqual(source.conf['bucket'], s3source.S3FilesSource.BUCKET) + self.assertEqual(source.conf['passwd_object'], s3source.S3FilesSource.PASSWD_OBJECT) def testOverrideDefaultConfiguration(self): source = s3source.S3FilesSource(self.config) - self.assertEquals(source.conf['bucket'], 'TEST_BUCKET') - self.assertEquals(source.conf['passwd_object'], 'PASSWD_OBJ') - self.assertEquals(source.conf['group_object'], 'GROUP_OBJ') + self.assertEqual(source.conf['bucket'], 'TEST_BUCKET') + self.assertEqual(source.conf['passwd_object'], 'PASSWD_OBJ') + self.assertEqual(source.conf['group_object'], 'GROUP_OBJ') class TestPasswdMapParser(unittest.TestCase): @@ -48,35 +48,35 @@ def setUp(self): def testGetMap(self): passwd_map = passwd.PasswdMap() - cache_info = StringIO.StringIO('''[ - { "Key": "foo", - "Value": { - "uid": 10, "gid": 10, "home": "/home/foo", - "shell": "/bin/bash", "comment": "How Now Brown Cow", - "irrelevant_key":"bacon" - } - } - ]''') + cache_info = StringIO('''[ + { "Key": "foo", + "Value": { + "uid": 10, "gid": 10, "home": "/home/foo", + "shell": "/bin/bash", "comment": "How Now Brown Cow", + "irrelevant_key":"bacon" + } + } + ]''') self.parser.GetMap(cache_info, passwd_map) - self.assertEquals(self.good_entry, passwd_map.PopItem()) + self.assertEqual(self.good_entry, passwd_map.PopItem()) def testReadEntry(self): data = {'uid': '10', 'gid': '10', 'comment': 'How Now Brown Cow', 'shell': '/bin/bash', 'home': '/home/foo', 'passwd': 'x'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) def testDefaultEntryValues(self): data = {'uid': '10', 'gid': '10'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(entry.shell, '/bin/bash') - self.assertEquals(entry.dir, '/home/foo') - self.assertEquals(entry.gecos, '') - self.assertEquals(entry.passwd, 'x') + self.assertEqual(entry.shell, '/bin/bash') + self.assertEqual(entry.dir, '/home/foo') + self.assertEqual(entry.gecos, '') + self.assertEqual(entry.passwd, 'x') def testInvalidEntry(self): data = {'irrelevant_key': 'bacon'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(entry, None) + self.assertEqual(entry, None) class TestS3GroupMapParser(unittest.TestCase): @@ -91,37 +91,37 @@ def setUp(self): def testGetMap(self): group_map = group.GroupMap() - cache_info = StringIO.StringIO('''[ - { "Key": "foo", - "Value": { - "gid": 10, - "members": "foo\\nbar", - "irrelevant_key": "bacon" - } - } - ]''') + cache_info = StringIO('''[ + { "Key": "foo", + "Value": { + "gid": 10, + "members": "foo\\nbar", + "irrelevant_key": "bacon" + } + } + ]''') self.parser.GetMap(cache_info, group_map) - self.assertEquals(self.good_entry, group_map.PopItem()) + self.assertEqual(self.good_entry, group_map.PopItem()) def testReadEntry(self): data = {'passwd': 'x', 'gid': '10', 'members': 'foo\nbar'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) def testDefaultPasswd(self): data = {'gid': '10', 'members': 'foo\nbar'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) def testNoMembers(self): data = {'gid': '10', 'members': ''} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(entry.members, ['']) + self.assertEqual(entry.members, ['']) def testInvalidEntry(self): data = {'irrelevant_key': 'bacon'} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(entry, None) + self.assertEqual(entry, None) class TestS3ShadowMapParser(unittest.TestCase): @@ -138,26 +138,26 @@ def setUp(self): def testGetMap(self): shadow_map = shadow.ShadowMap() - cache_info = StringIO.StringIO('''[ - { "Key": "foo", - "Value": { - "passwd": "*", "lstchg": 17246, "min": 0, - "max": 99999, "warn": 7 - } - } - ]''') + cache_info = StringIO('''[ + { "Key": "foo", + "Value": { + "passwd": "*", "lstchg": 17246, "min": 0, + "max": 99999, "warn": 7 + } + } + ]''') self.parser.GetMap(cache_info, shadow_map) - self.assertEquals(self.good_entry, shadow_map.PopItem()) + self.assertEqual(self.good_entry, shadow_map.PopItem()) def testReadEntry(self): data = {'passwd': '*', 'lstchg': 17246, 'min': 0, 'max': 99999, 'warn': 7} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) def testDefaultPasswd(self): data = {'lstchg': 17246, 'min': 0, 'max': 99999, 'warn': 7} entry = self.parser._ReadEntry('foo', data) - self.assertEquals(self.good_entry, entry) + self.assertEqual(self.good_entry, entry) if __name__ == '__main__': diff --git a/nss_cache/sources/source_factory.py b/nss_cache/sources/source_factory.py index 28e398fd..f78203a6 100644 --- a/nss_cache/sources/source_factory.py +++ b/nss_cache/sources/source_factory.py @@ -84,7 +84,7 @@ def Create(conf): source_name = conf['name'] - if source_name not in _source_implementations.keys(): + if source_name not in list(_source_implementations.keys()): raise RuntimeError('source not implemented: %r' % (source_name,)) return _source_implementations[source_name](conf) diff --git a/nss_cache/sources/source_factory_test.py b/nss_cache/sources/source_factory_test.py index f1406155..25de2fe4 100644 --- a/nss_cache/sources/source_factory_test.py +++ b/nss_cache/sources/source_factory_test.py @@ -35,9 +35,9 @@ class DummySource(source.Source): name = 'dummy' source_factory.RegisterImplementation(DummySource) - self.failUnlessEqual(number_of_sources + 1, + self.assertEqual(number_of_sources + 1, len(source_factory._source_implementations)) - self.failUnlessEqual(DummySource, + self.assertEqual(DummySource, source_factory._source_implementations['dummy']) def testRegisterWithoutName(self): diff --git a/nss_cache/update/files_updater.py b/nss_cache/update/files_updater.py index ce916b2c..6dd3df08 100644 --- a/nss_cache/update/files_updater.py +++ b/nss_cache/update/files_updater.py @@ -87,7 +87,7 @@ def UpdateCacheFromSource(self, cache, source, incremental=False, finally: try: os.unlink(new_file) - except OSError, e: + except OSError as e: # If we're using zsync source, it already renames the file for us. if e.errno != errno.ENOENT: raise diff --git a/nss_cache/update/files_updater_test.py b/nss_cache/update/files_updater_test.py index ecdfa139..be55dab1 100644 --- a/nss_cache/update/files_updater_test.py +++ b/nss_cache/update/files_updater_test.py @@ -27,7 +27,10 @@ import time import unittest -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache import config from nss_cache import error @@ -60,7 +63,7 @@ def testFullUpdate(self): # Construct a fake source. def GetFile(map_name, dst_file, current_file, location): - print "GetFile: %s" % dst_file + print("GetFile: %s" % dst_file) f = open(dst_file, 'w') f.write('root:x:0:0:root:/root:/bin/bash\n') f.close() diff --git a/nss_cache/update/map_updater_test.py b/nss_cache/update/map_updater_test.py index 2d55c3b8..d9873339 100644 --- a/nss_cache/update/map_updater_test.py +++ b/nss_cache/update/map_updater_test.py @@ -25,7 +25,10 @@ import tempfile import unittest -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache.caches import caches from nss_cache.caches import files diff --git a/nss_cache/update/updater.py b/nss_cache/update/updater.py index 9aa3fc96..05069df4 100644 --- a/nss_cache/update/updater.py +++ b/nss_cache/update/updater.py @@ -119,7 +119,7 @@ def _ReadTimestamp(self, filename): try: timestamp_file = open(filename, 'r') timestamp_string = timestamp_file.read().strip() - except IOError, e: + except IOError as e: self.log.warn('error opening timestamp file: %s', e) timestamp_string = None else: @@ -133,7 +133,7 @@ def _ReadTimestamp(self, filename): # Append UTC to force the timezone to parse the string in. timestamp = int(calendar.timegm(time.strptime(timestamp_string + ' UTC', '%Y-%m-%dT%H:%M:%SZ %Z'))) - except ValueError, e: + except ValueError as e: self.log.error('cannot parse timestamp file %r: %s', filename, e) timestamp = None @@ -167,7 +167,7 @@ def _WriteTimestamp(self, timestamp, filename): # Make sure self.timestamp_dir exists before calling tempfile.mkstemp try: os.makedirs(self.timestamp_dir) - except OSError, e: + except OSError as e: if e.errno == errno.EEXIST and os.path.isdir(self.timestamp_dir): pass # Directory already exists; squelch error else: diff --git a/nss_cache/update/updater_test.py b/nss_cache/update/updater_test.py index 1a60fb42..e6589f69 100644 --- a/nss_cache/update/updater_test.py +++ b/nss_cache/update/updater_test.py @@ -26,7 +26,10 @@ import time import unittest -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache import config from nss_cache.update import updater diff --git a/nss_cache/util/curl.py b/nss_cache/util/curl.py index 9f2bf318..18807bb9 100644 --- a/nss_cache/util/curl.py +++ b/nss_cache/util/curl.py @@ -19,7 +19,10 @@ __author__ = 'blaedd@google.com (David MacKinnon)' -import cStringIO +try: + from cStringIO import StringIO +except ImportError: + from io import BytesIO as StringIO import logging import pycurl @@ -34,13 +37,13 @@ def CurlFetch(url, conn=None, logger=None): conn = pycurl.Curl() conn.setopt(pycurl.URL, url) - conn.body = cStringIO.StringIO() - conn.headers = cStringIO.StringIO() + conn.body = StringIO() + conn.headers = StringIO() conn.setopt(pycurl.WRITEFUNCTION, conn.body.write) conn.setopt(pycurl.HEADERFUNCTION, conn.headers.write) try: conn.perform() - except pycurl.error, e: + except pycurl.error as e: HandleCurlError(e, logger) raise error.Error(e) resp_code = conn.getinfo(pycurl.RESPONSE_CODE) diff --git a/nss_cache/util/timestamps.py b/nss_cache/util/timestamps.py index 94d37f69..a12523d8 100644 --- a/nss_cache/util/timestamps.py +++ b/nss_cache/util/timestamps.py @@ -48,7 +48,7 @@ def ReadTimestamp(filename): try: timestamp_file = open(filename, 'r') timestamp_string = timestamp_file.read().strip() - except IOError, e: + except IOError as e: logging.warn('error opening timestamp file: %s', e) timestamp_string = None else: @@ -62,7 +62,7 @@ def ReadTimestamp(filename): # Append UTC to force the timezone to parse the string in. timestamp = time.strptime(timestamp_string + ' UTC', '%Y-%m-%dT%H:%M:%SZ %Z') - except ValueError, e: + except ValueError as e: logging.error('cannot parse timestamp file %r: %s', filename, e) timestamp = None diff --git a/nss_cache/util/timestamps_test.py b/nss_cache/util/timestamps_test.py index 2a0fe8b4..188ec44c 100644 --- a/nss_cache/util/timestamps_test.py +++ b/nss_cache/util/timestamps_test.py @@ -24,7 +24,10 @@ import time import unittest -import mox +try: + import mox +except ImportError: + import mox3 from nss_cache.util import timestamps