summaryrefslogtreecommitdiffstats
path: root/SCons/dblite.py
blob: e50b7f9adecc2f178561f94312fe0ed10b29f90c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# MIT License
#
# Copyright The SCons Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
dblite.py module contributed by Ralf W. Grosse-Kunstleve.
Extended for Unicode by Steven Knight.

This is a very simple-minded "database" used for saved signature
information, with an interface modeled on the Python dbm database
interface module.
"""

import io
import os
import pickle
import shutil
import time

from SCons.compat import PICKLE_PROTOCOL

KEEP_ALL_FILES = False
IGNORE_CORRUPT_DBFILES = False


def corruption_warning(filename) -> None:
    """Local warning for corrupt db.

    Used for self-tests. SCons overwrites this with a
    different warning function in SConsign.py.
    """
    print("Warning: Discarding corrupt database:", filename)


DBLITE_SUFFIX = ".dblite"
TMP_SUFFIX = ".tmp"


class _Dblite:
    """Lightweight signature database class.

    Behaves like a dict when in memory, loads from a pickled disk
    file on open and writes back out to it on close.

    Open the database file using a path derived from *file_base_name*.
    The optional *flag* argument can be:

    +---------+---------------------------------------------------+
    | Value   | Meaning                                           |
    +=========+===================================================+
    | ``'r'`` | Open existing database for reading only (default) |
    +---------+---------------------------------------------------+
    | ``'w'`` | Open existing database for reading and  writing   |
    +---------+---------------------------------------------------+
    | ``'c'`` | Open database for reading and writing, creating   |
    |         | it if it doesn't exist                            |
    +---------+---------------------------------------------------+
    | ``'n'`` | Always create a new, empty database, open for     |
    |         | reading and writing                               |
    +---------+---------------------------------------------------+

    The optional *mode* argument is the POSIX mode of the file, used only
    when the database has to be created.  It defaults to octal ``0o666``.
    """

    # Because open() is defined at module level, overwriting builtin open
    # in the scope of this module, we use io.open to avoid ambiguity.
    _open = staticmethod(io.open)

    # we need to squirrel away references to functions from various modules
    # that we'll use when sync() is called: this may happen at Python
    # teardown time (we call it from our __del__), and the global module
    # references themselves may already have been rebound to None.
    _pickle_dump = staticmethod(pickle.dump)
    _pickle_protocol = PICKLE_PROTOCOL
    try:
        _os_chown = staticmethod(os.chown)
    except AttributeError:
        _os_chown = None
    _os_replace = staticmethod(os.replace)
    _os_chmod = staticmethod(os.chmod)
    _shutil_copyfile = staticmethod(shutil.copyfile)
    _time_time = staticmethod(time.time)

    def __init__(self, file_base_name, flag='r', mode=0o666) -> None:
        assert flag in ("r", "w", "c", "n")

        base, ext = os.path.splitext(file_base_name)
        if ext == DBLITE_SUFFIX:
            # There's already a suffix on the file name, don't add one.
            self._file_name = file_base_name
            self._tmp_name = base + TMP_SUFFIX
        else:
            self._file_name = file_base_name + DBLITE_SUFFIX
            self._tmp_name = file_base_name + TMP_SUFFIX

        self._flag = flag
        self._mode = mode
        self._dict = {}
        self._needs_sync = False

        if self._os_chown is not None and 0 in (os.geteuid(), os.getegid()):
            # running as root; chown back to current owner/group when done
            try:
                statinfo = os.stat(self._file_name)
                self._chown_to = statinfo.st_uid
                self._chgrp_to = statinfo.st_gid
            except OSError:
                # db file doesn't exist yet.
                # Check os.environ for SUDO_UID, use if set
                self._chown_to = int(os.environ.get('SUDO_UID', -1))
                self._chgrp_to = int(os.environ.get('SUDO_GID', -1))
        else:
            self._chown_to = -1  # don't chown
            self._chgrp_to = -1  # don't chgrp

        if self._flag == "n":
            with io.open(self._file_name, "wb", opener=self.opener):
                return  # just make sure it exists
        else:
            # We only need the disk file to slurp in the data.  Updates are
            # handled on close, db is mainained only in memory until then.
            try:
                with io.open(self._file_name, "rb") as f:
                    p = f.read()
            except OSError as e:
                # an error for file not to exist, unless flag is create
                if self._flag != "c":
                    raise e
                with io.open(self._file_name, "wb", opener=self.opener):
                    return  # just make sure it exists
            if len(p) > 0:
                try:
                    self._dict = pickle.loads(p, encoding='bytes')
                except (
                    pickle.UnpicklingError,
                    # Python3 docs:
                    # Note that other exceptions may also be raised during
                    # unpickling, including (but not necessarily limited to)
                    # AttributeError, EOFError, ImportError, and IndexError.
                    AttributeError,
                    EOFError,
                    ImportError,
                    IndexError,
                ):
                    if IGNORE_CORRUPT_DBFILES:
                        corruption_warning(self._file_name)
                    else:
                        raise

    def opener(self, path, flags):
        """Database open helper when creation may be needed.

        The high-level Python open() function cannot specify a file mode
        for creation. Using this as the opener with the saved mode lets
        us do that.
        """
        return os.open(path, flags, mode=self._mode)

    def close(self) -> None:
        if self._needs_sync:
            self.sync()

    def __del__(self) -> None:
        self.close()

    def sync(self) -> None:
        """Flush the database to disk.

        This routine *must* succeed, since the in-memory and on-disk
        copies are out of sync as soon as we do anything that changes
        the in-memory version. Thus, to be cautious, flush to a
        temporary file and then move it over with some error handling.
        """
        self._check_writable()
        with self._open(self._tmp_name, "wb", opener=self.opener) as f:
            self._pickle_dump(self._dict, f, self._pickle_protocol)

        try:
            self._os_replace(self._tmp_name, self._file_name)
        except PermissionError:
            # If we couldn't replace due to perms, try to change and retry.
            # This is mainly for Windows - on POSIX the file permissions
            # don't matter, the os.replace would have worked anyway.
            # We're giving up if the retry fails, just let the Python
            # exception abort us.
            try:
                self._os_chmod(self._file_name, 0o777)
            except PermissionError:
                pass
            self._os_replace(self._tmp_name, self._file_name)

        if (
            self._os_chown is not None and self._chown_to > 0
        ):  # don't chown to root or -1
            try:
                self._os_chown(self._file_name, self._chown_to, self._chgrp_to)
            except OSError:
                pass

        self._needs_sync = False
        if KEEP_ALL_FILES:
            self._shutil_copyfile(
                self._file_name, f"{self._file_name}_{int(self._time_time())}"
            )

    def _check_writable(self):
        if self._flag == "r":
            raise OSError(f"Read-only database: {self._file_name}")

    def __getitem__(self, key):
        return self._dict[key]

    def __setitem__(self, key, value):
        self._check_writable()

        if not isinstance(key, str):
            raise TypeError(f"key `{key}' must be a string but is {type(key)}")

        if not isinstance(value, bytes):
            raise TypeError(f"value `{value}' must be bytes but is {type(value)}")

        self._dict[key] = value
        self._needs_sync = True

    def __delitem__(self, key):
        del self._dict[key]

    def keys(self):
        return self._dict.keys()

    def items(self):
        return self._dict.items()

    def values(self):
        return self._dict.values()

    __iter__ = keys

    def __contains__(self, key) -> bool:
        return key in self._dict

    def __len__(self) -> int:
        return len(self._dict)


def open(file, flag="r", mode: int = 0o666):  # pylint: disable=redefined-builtin
    return _Dblite(file, flag, mode)


def _exercise():
    db = open("tmp", "n")
    assert len(db) == 0
    db["foo"] = b"bar"
    assert db["foo"] == b"bar"
    db.sync()

    db = open("tmp", "c")
    assert len(db) == 1, len(db)
    assert db["foo"] == b"bar"
    db["bar"] = b"foo"
    assert db["bar"] == b"foo"
    db.sync()

    db = open("tmp")
    assert len(db) == 2, len(db)
    assert db["foo"] == b"bar"
    assert db["bar"] == b"foo"
    try:
        db.sync()
    except OSError as e:
        assert str(e) == "Read-only database: tmp.dblite"
    else:
        raise RuntimeError("IOError expected.")
    db = open("tmp", "w")
    assert len(db) == 2, len(db)
    db["ping"] = b"pong"
    db.sync()

    try:
        db[(1, 2)] = "tuple"
    except TypeError as e:
        assert str(e) == "key `(1, 2)' must be a string but is <class 'tuple'>", str(e)
    else:
        raise RuntimeError("TypeError exception expected")

    try:
        db["list"] = [1, 2]
    except TypeError as e:
        assert str(e) == "value `[1, 2]' must be bytes but is <class 'list'>", str(e)
    else:
        raise RuntimeError("TypeError exception expected")

    db = open("tmp")
    assert len(db) == 3, len(db)

    db = open("tmp", "n")
    assert len(db) == 0, len(db)
    _Dblite._open("tmp.dblite", "w")

    db = open("tmp")
    _Dblite._open("tmp.dblite", "w").write("x")
    try:
        db = open("tmp")
    except pickle.UnpicklingError:
        pass
    else:
        raise RuntimeError("pickle exception expected.")

    global IGNORE_CORRUPT_DBFILES
    IGNORE_CORRUPT_DBFILES = True
    db = open("tmp")
    assert len(db) == 0, len(db)
    os.unlink("tmp.dblite")
    try:
        db = open("tmp", "w")
    except OSError as e:
        assert str(e) == "[Errno 2] No such file or directory: 'tmp.dblite'", str(e)
    else:
        raise RuntimeError("IOError expected.")

    print("Completed _exercise()")


if __name__ == "__main__":
    _exercise()

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: