Skip to content

Commit 2cdee08

Browse files
author
Richard Jones
committed
don't try to import all backends in backends.__init__ unless we *want* to
1 parent e799f8f commit 2cdee08

File tree

10 files changed

+59
-53
lines changed

10 files changed

+59
-53
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Feature:
3333
recognized by roundup-server (but not tracker specification arguments).
3434
Use this to specify server configuration file for the service.
3535
- added experimental multi-thread server
36+
- don't try to import all backends in backends.__init__ unless we *want* to
3637

3738
Fixed:
3839
- postgres backend open doesn't hide corruption in schema (sf bug 956375)

demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Copyright (c) 2003 Richard Jones ([email protected])
44
#
5-
# $Id: demo.py,v 1.21 2004-10-29 15:55:16 a1s Exp $
5+
# $Id: demo.py,v 1.22 2004-11-03 01:34:20 richard Exp $
66

77
import errno
88
import os
@@ -40,7 +40,7 @@ def install_demo(home, backend, template):
4040
config['RDBMS_NAME'] = 'rounduptest'
4141

4242
# see if we have further db nuking to perform
43-
module = getattr(backends, backend)
43+
module = backends.get_backend(backend)
4444
if module.db_exists(config):
4545
module.db_nuke(config)
4646

roundup/admin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1717
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1818
#
19-
# $Id: admin.py,v 1.83 2004-10-20 04:45:01 richard Exp $
19+
# $Id: admin.py,v 1.84 2004-11-03 01:34:21 richard Exp $
2020

2121
'''Administration commands for maintaining Roundup trackers.
2222
'''
@@ -336,7 +336,7 @@ def help_initopts(self):
336336
templates = self.listTemplates()
337337
print _('Templates:'), ', '.join(templates.keys())
338338
import roundup.backends
339-
backends = roundup.backends.__all__
339+
backends = roundup.backends.list_backends()
340340
print _('Back ends:'), ', '.join(backends)
341341

342342
def do_install(self, tracker_home, args):
@@ -392,7 +392,7 @@ def do_install(self, tracker_home, args):
392392

393393
# select hyperdb backend
394394
import roundup.backends
395-
backends = roundup.backends.__all__
395+
backends = roundup.backends.list_backends()
396396
backend = len(args) > 2 and args[2] or ''
397397
if backend not in backends:
398398
print _('Back ends:'), ', '.join(backends)

roundup/backends/__init__.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,38 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: __init__.py,v 1.28 2004-10-24 08:23:16 a1s Exp $
18+
# $Id: __init__.py,v 1.29 2004-11-03 01:34:21 richard Exp $
1919

2020
'''Container for the hyperdb storage backend implementations.
21-
22-
The __all__ variable is constructed containing only the backends which are
23-
available.
2421
'''
2522
__docformat__ = 'restructuredtext'
2623

27-
__all__ = []
24+
_modules = {
25+
'mysql': 'MySQLdb',
26+
'postgresql': 'psycopg',
27+
}
28+
29+
def get_backend(name):
30+
'''Get a specific backend by name.'''
31+
return __import__('back_%s'%name, globals())
2832

29-
for backend in ['anydbm', ('mysql', 'MySQLdb'),
30-
'sqlite', 'metakit', ('postgresql', 'psycopg')]:
31-
if len(backend) == 2:
32-
backend, backend_module = backend
33-
else:
34-
backend_module = backend
33+
def have_backend(name):
34+
'''Is backend "name" available?'''
35+
module = _modules.get(name, name)
3536
try:
36-
globals()[backend] = __import__('back_%s'%backend, globals())
37-
__all__.append(backend)
37+
get_backend(name)
38+
return 1
3839
except ImportError, e:
39-
if not str(e).startswith('No module named %s'%backend_module):
40+
if not str(e).startswith('No module named %s'%module):
4041
raise
42+
return 0
43+
44+
def list_backends():
45+
'''List all available backend names.'''
46+
l = []
47+
for name in 'anydbm', 'mysql', 'sqlite', 'metakit', 'postgresql':
48+
if have_backend(name):
49+
l.append(name)
50+
return l
4151

4252
# vim: set filetype=python ts=4 sw=4 et si

roundup/instance.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: instance.py,v 1.27 2004-11-02 10:00:36 a1s Exp $
18+
# $Id: instance.py,v 1.28 2004-11-03 01:34:21 richard Exp $
1919

2020
'''Tracker handling (open tracker).
2121
@@ -61,8 +61,7 @@ def get_backend_name(self):
6161
return name
6262

6363
def get_backend(self):
64-
name = self.get_backend_name()
65-
return getattr(backends, name)
64+
return backends.get_backend(self.get_backend_name())
6665

6766
def open(self, name=None):
6867
backend = self.get_backend()

test/test_anydbm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_anydbm.py,v 1.3 2004-03-18 01:58:46 richard Exp $
18+
# $Id: test_anydbm.py,v 1.4 2004-11-03 01:34:21 richard Exp $
1919

2020
import unittest, os, shutil, time
21+
from roundup.backends import get_backend
2122

2223
from db_test_base import DBTest, ROTest, SchemaTest, ClassicInitTest, config
2324

2425
class anydbmOpener:
25-
from roundup.backends import anydbm as module
26+
module = get_backend('anydbm')
2627

2728
def nuke_database(self):
2829
shutil.rmtree(config.DATABASE)

test/test_metakit.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_metakit.py,v 1.5 2004-03-24 05:33:13 richard Exp $
18+
# $Id: test_metakit.py,v 1.6 2004-11-03 01:34:21 richard Exp $
1919
import unittest, os, shutil, time, weakref
2020

2121
from db_test_base import DBTest, ROTest, SchemaTest, ClassicInitTest, config, password
2222

23-
from roundup import backends
23+
from roundup.backends import get_backend, have_backend
2424

2525
class metakitOpener:
26-
if hasattr(backends, 'metakit'):
27-
from roundup.backends import metakit as module
26+
if have_backend('metakit'):
27+
module = get_backend('metakit')
2828
module._instances = weakref.WeakValueDictionary()
2929

3030
def nuke_database(self):
@@ -65,7 +65,7 @@ class metakitSessionTest(metakitOpener, DBMTest):
6565

6666
def test_suite():
6767
suite = unittest.TestSuite()
68-
if not hasattr(backends, 'metakit'):
68+
if not have_backend('metakit'):
6969
print 'Skipping metakit tests'
7070
return suite
7171
print 'Including metakit tests'

test/test_mysql.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_mysql.py,v 1.13 2004-09-26 15:18:28 a1s Exp $
18+
# $Id: test_mysql.py,v 1.14 2004-11-03 01:34:21 richard Exp $
1919

2020
import unittest, os, shutil, time, imp
2121

2222
from roundup.hyperdb import DatabaseError
23-
from roundup import init, backends
23+
from roundup.backends import get_backend, have_backend
2424

2525
from db_test_base import DBTest, ROTest, config, SchemaTest, ClassicInitTest
2626

2727

2828
class mysqlOpener:
29-
if hasattr(backends, 'mysql'):
30-
from roundup.backends import mysql as module
29+
if have_backend('mysql'):
30+
module = get_backends('mysql')
3131

3232
def setUp(self):
3333
self.module.db_nuke(config)
@@ -74,16 +74,15 @@ def tearDown(self):
7474

7575
def test_suite():
7676
suite = unittest.TestSuite()
77-
if not hasattr(backends, 'mysql'):
77+
if not have_backend('mysql'):
7878
print "Skipping mysql tests"
7979
return suite
8080

81-
from roundup.backends import mysql
8281
import MySQLdb
8382
try:
8483
# Check if we can connect to the server.
8584
# use db_exists() to make a connection, ignore it's return value
86-
mysql.db_exists(config)
85+
mysqlOpener.module.db_exists(config)
8786
except (MySQLdb.MySQLError, DatabaseError), msg:
8887
print "Skipping mysql tests (%s)"%msg
8988
else:

test/test_postgresql.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,29 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_postgresql.py,v 1.11 2004-09-25 15:50:36 a1s Exp $
18+
# $Id: test_postgresql.py,v 1.12 2004-11-03 01:34:21 richard Exp $
1919

2020
import unittest
2121

2222
from roundup.hyperdb import DatabaseError
2323

2424
from db_test_base import DBTest, ROTest, config, SchemaTest, ClassicInitTest
2525

26-
from roundup import backends
26+
from roundup.backends import get_backend, have_backend
2727

2828
class postgresqlOpener:
29-
if hasattr(backends, 'postgresql'):
30-
from roundup.backends import postgresql as module
29+
if have_backend('postgresql'):
30+
module = get_backend('postgresql')
3131

3232
def setUp(self):
33-
#from roundup.backends.back_postgresql import db_nuke
34-
#db_nuke(config, 1)
3533
pass
3634

3735
def tearDown(self):
3836
self.nuke_database()
3937

4038
def nuke_database(self):
4139
# clear out the database - easiest way is to nuke and re-create it
42-
from roundup.backends.back_postgresql import db_nuke
43-
db_nuke(config)
40+
self.module.db_nuke(config)
4441

4542
class postgresqlDBTest(postgresqlOpener, DBTest):
4643
def setUp(self):
@@ -99,14 +96,13 @@ def tearDown(self):
9996

10097
def test_suite():
10198
suite = unittest.TestSuite()
102-
if not hasattr(backends, 'postgresql'):
99+
if not have_backend('postgresql'):
103100
print "Skipping postgresql tests"
104101
return suite
105102

106103
# make sure we start with a clean slate
107-
from roundup.backends.back_postgresql import db_nuke, db_exists
108-
if db_exists(config):
109-
db_nuke(config, 1)
104+
if postgresqlOpener.module.db_exists(config):
105+
postgresqlOpener.module.db_nuke(config, 1)
110106

111107
# TODO: Check if we can run postgresql tests
112108
print 'Including postgresql tests'

test/test_sqlite.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_sqlite.py,v 1.4 2004-03-18 01:58:46 richard Exp $
18+
# $Id: test_sqlite.py,v 1.5 2004-11-03 01:34:21 richard Exp $
1919

2020
import unittest, os, shutil, time
21+
from roundup.backends import get_backend, have_backend
2122

2223
from db_test_base import DBTest, ROTest, SchemaTest, ClassicInitTest, config
2324

2425
class sqliteOpener:
25-
from roundup import backends
26-
if hasattr(backends, 'sqlite'):
27-
from roundup.backends import sqlite as module
26+
if have_backend('sqlite'):
27+
module = get_backend('sqlite')
2828

2929
def nuke_database(self):
3030
shutil.rmtree(config.DATABASE)
@@ -48,7 +48,7 @@ class sqliteSessionTest(sqliteOpener, RDBMSTest):
4848
def test_suite():
4949
suite = unittest.TestSuite()
5050
from roundup import backends
51-
if not hasattr(backends, 'sqlite'):
51+
if not have_backend('sqlite'):
5252
print 'Skipping sqlite tests'
5353
return suite
5454
print 'Including sqlite tests'

0 commit comments

Comments
 (0)