~sakuag333/mailman/lmtp-duplicate-id

« back to all changes in this revision

Viewing changes to Mailman/database/model/__init__.py

  • Committer: Barry Warsaw
  • Date: 2007-12-08 16:51:36 UTC
  • Revision ID: barry@python.org-20071208165136-gcm3v8d7o3jbb0tt
Reorganize the database subpackage, primarily by removing the 'model'
subdirectory and updating all relevant imports.  Move of the circular
import problems have been eliminated in the process.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 by the Free Software Foundation, Inc.
2
 
#
3
 
# This program is free software; you can redistribute it and/or
4
 
# modify it under the terms of the GNU General Public License
5
 
# as published by the Free Software Foundation; either version 2
6
 
# of the License, or (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16
 
# USA.
17
 
 
18
 
from __future__ import with_statement
19
 
 
20
 
__all__ = [
21
 
    'Address',
22
 
    'Language',
23
 
    'MailingList',
24
 
    'Message',
25
 
    'Pendings',
26
 
    'Preferences',
27
 
    'User',
28
 
    'Version',
29
 
    ]
30
 
 
31
 
import os
32
 
import sys
33
 
 
34
 
from storm import database
35
 
from storm.locals import create_database, Store
36
 
from string import Template
37
 
from urlparse import urlparse
38
 
 
39
 
import Mailman.Version
40
 
 
41
 
from Mailman import constants
42
 
from Mailman.Errors import SchemaVersionMismatchError
43
 
from Mailman.configuration import config
44
 
from Mailman.database.model.address import Address
45
 
from Mailman.database.model.language import Language
46
 
from Mailman.database.model.mailinglist import MailingList
47
 
from Mailman.database.model.member import Member
48
 
from Mailman.database.model.message import Message
49
 
from Mailman.database.model.pending import Pendings
50
 
from Mailman.database.model.preferences import Preferences
51
 
from Mailman.database.model.requests import Requests
52
 
from Mailman.database.model.user import User
53
 
from Mailman.database.model.version import Version
54
 
 
55
 
 
56
 
 
57
 
def initialize(debug):
58
 
    # Calculate the engine url.
59
 
    url = Template(config.DEFAULT_DATABASE_URL).safe_substitute(config.paths)
60
 
    # XXX By design of SQLite, database file creation does not honor
61
 
    # umask.  See their ticket #1193:
62
 
    # http://www.sqlite.org/cvstrac/tktview?tn=1193,31
63
 
    #
64
 
    # This sucks for us because the mailman.db file /must/ be group writable,
65
 
    # however even though we guarantee our umask is 002 here, it still gets
66
 
    # created without the necessary g+w permission, due to SQLite's policy.
67
 
    # This should only affect SQLite engines because its the only one that
68
 
    # creates a little file on the local file system.  This kludges around
69
 
    # their bug by "touch"ing the database file before SQLite has any chance
70
 
    # to create it, thus honoring the umask and ensuring the right
71
 
    # permissions.  We only try to do this for SQLite engines, and yes, we
72
 
    # could have chmod'd the file after the fact, but half dozen and all...
73
 
    touch(url)
74
 
    database = create_database(url)
75
 
    store = Store(database)
76
 
    database.DEBUG = (config.DEFAULT_DATABASE_ECHO if debug is None else debug)
77
 
    # XXX Storm does not currently have schema creation.  This is not an ideal
78
 
    # way to handle creating the database, but it's cheap and easy for now.
79
 
    import Mailman.database.model
80
 
    schema_file = os.path.join(
81
 
        os.path.dirname(Mailman.database.model.__file__),
82
 
        'mailman.sql')
83
 
    with open(schema_file) as fp:
84
 
        sql = fp.read()
85
 
    for statement in sql.split(';'):
86
 
        store.execute(statement + ';')
87
 
    # Validate schema version.
88
 
    v = store.find(Version, component=u'schema').one()
89
 
    if not v:
90
 
        # Database has not yet been initialized
91
 
        v = Version(component=u'schema',
92
 
                    version=Mailman.Version.DATABASE_SCHEMA_VERSION)
93
 
        store.add(v)
94
 
    elif v.version <> Mailman.Version.DATABASE_SCHEMA_VERSION:
95
 
        # XXX Update schema
96
 
        raise SchemaVersionMismatchError(v.version)
97
 
    return store
98
 
 
99
 
 
100
 
def touch(url):
101
 
    parts = urlparse(url)
102
 
    if parts.scheme <> 'sqlite':
103
 
        return
104
 
    path = os.path.normpath(parts.path)
105
 
    fd = os.open(path, os.O_WRONLY |  os.O_NONBLOCK | os.O_CREAT, 0666)
106
 
    # Ignore errors
107
 
    if fd > 0:
108
 
        os.close(fd)