~barry/mailman/lp1423756

« back to all changes in this revision

Viewing changes to src/mailman/model/messagestore.py

  • Committer: Barry Warsaw
  • Date: 2015-01-05 01:20:33 UTC
  • mfrom: (7264.4.66 py3)
  • Revision ID: barry@list.org-20150105012033-zdrw9c2odhpf22fz
Merge the Python 3 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Model for message stores."""
19
19
 
20
 
from __future__ import absolute_import, print_function, unicode_literals
21
 
 
22
 
__metaclass__ = type
23
20
__all__ = [
24
21
    'MessageStore',
25
22
    ]
28
25
import os
29
26
import errno
30
27
import base64
 
28
import pickle
31
29
import hashlib
32
 
import cPickle as pickle
33
 
 
34
 
from zope.interface import implementer
35
30
 
36
31
from mailman.config import config
37
32
from mailman.database.transaction import dbconnection
38
33
from mailman.interfaces.messages import IMessageStore
39
34
from mailman.model.message import Message
40
35
from mailman.utilities.filesystem import makedirs
 
36
from zope.interface import implementer
41
37
 
42
38
 
43
39
# It could be very bad if you have already stored files and you change this
68
64
            raise ValueError(
69
65
                'Message ID already exists in message store: {0}'.format(
70
66
                    message_id))
71
 
        shaobj = hashlib.sha1(message_id)
72
 
        hash32 = base64.b32encode(shaobj.digest())
 
67
        shaobj = hashlib.sha1(message_id.encode('utf-8'))
 
68
        hash32 = base64.b32encode(shaobj.digest()).decode('utf-8')
73
69
        del message['X-Message-ID-Hash']
74
70
        message['X-Message-ID-Hash'] = hash32
75
71
        # Calculate the path on disk where we're going to store this message
94
90
        # them and try again.
95
91
        while True:
96
92
            try:
97
 
                with open(path, 'w') as fp:
 
93
                with open(path, 'wb') as fp:
98
94
                    # -1 says to use the highest protocol available.
99
95
                    pickle.dump(message, fp, -1)
100
96
                    break
106
102
 
107
103
    def _get_message(self, row):
108
104
        path = os.path.join(config.MESSAGES_DIR, row.path)
109
 
        with open(path) as fp:
 
105
        with open(path, 'rb') as fp:
110
106
            return pickle.load(fp)
111
107
 
112
108
    @dbconnection
118
114
 
119
115
    @dbconnection
120
116
    def get_message_by_hash(self, store, message_id_hash):
121
 
        # It's possible the hash came from a message header, in which case it
122
 
        # will be a Unicode.  However when coming from source code, it may be
123
 
        # bytes object.  Coerce to the latter if necessary; it must be ASCII.
124
 
        if not isinstance(message_id_hash, bytes):
125
 
            message_id_hash = message_id_hash.encode('ascii')
126
117
        row = store.query(Message).filter_by(
127
118
            message_id_hash=message_id_hash).first()
128
119
        if row is None: