~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: Martin Pool
  • Date: 2005-06-30 08:40:59 UTC
  • mto: This revision was merged to the branch mainline in revision 852.
  • Revision ID: mbp@sourcefrog.net-20050630084059-d6eb6cb46972365b
Rename Weave.get_included to inclusions and getiter to get_iter

Refactor annotate() code 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
# -*- coding: UTF-8 -*-
3
 
 
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
 
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
 
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 
18
 
"""Stores are the main data-storage mechanism for Bazaar-NG.
19
 
 
20
 
A store is a simple write-once container indexed by a universally
21
 
unique ID, which is typically the SHA-1 of the content."""
22
 
 
23
 
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
24
 
__author__ = "Martin Pool <mbp@canonical.com>"
25
 
 
26
 
import os, tempfile, types, osutils
27
 
from StringIO import StringIO
28
 
from trace import mutter
29
 
 
30
 
 
31
 
######################################################################
32
 
# stores
33
 
 
34
 
class StoreError(Exception):
35
 
    pass
36
 
 
37
 
 
38
 
class ImmutableStore:
39
 
    """Store that holds files indexed by unique names.
40
 
 
41
 
    Files can be added, but not modified once they are in.  Typically
42
 
    the hash is used as the name, or something else known to be unique,
43
 
    such as a UUID.
44
 
 
45
 
    >>> st = ImmutableScratchStore()
46
 
 
47
 
    >>> st.add(StringIO('hello'), 'aa')
48
 
    >>> 'aa' in st
49
 
    True
50
 
    >>> 'foo' in st
51
 
    False
52
 
 
53
 
    You are not allowed to add an id that is already present.
54
 
 
55
 
    Entries can be retrieved as files, which may then be read.
56
 
 
57
 
    >>> st.add(StringIO('goodbye'), '123123')
58
 
    >>> st['123123'].read()
59
 
    'goodbye'
60
 
 
61
 
    :todo: Atomic add by writing to a temporary file and renaming.
62
 
 
63
 
    :todo: Perhaps automatically transform to/from XML in a method?
64
 
           Would just need to tell the constructor what class to
65
 
           use...
66
 
 
67
 
    :todo: Even within a simple disk store like this, we could
68
 
           gzip the files.  But since many are less than one disk
69
 
           block, that might not help a lot.
70
 
 
71
 
    """
72
 
 
73
 
    def __init__(self, basedir):
74
 
        """ImmutableStore constructor."""
75
 
        self._basedir = basedir
76
 
 
77
 
    def _path(self, id):
78
 
        return os.path.join(self._basedir, id)
79
 
 
80
 
    def __repr__(self):
81
 
        return "%s(%r)" % (self.__class__.__name__, self._basedir)
82
 
 
83
 
    def add(self, f, fileid):
84
 
        """Add contents of a file into the store.
85
 
 
86
 
        :param f: An open file, or file-like object."""
87
 
        # FIXME: Only works on smallish files
88
 
        # TODO: Can be optimized by copying at the same time as
89
 
        # computing the sum.
90
 
        mutter("add store entry %r" % (fileid))
91
 
        if isinstance(f, types.StringTypes):
92
 
            content = f
93
 
        else:
94
 
            content = f.read()
95
 
        if fileid not in self:
96
 
            filename = self._path(fileid)
97
 
            f = file(filename, 'wb')
98
 
            f.write(content)
99
 
            f.flush()
100
 
            os.fsync(f.fileno())
101
 
            f.close()
102
 
            osutils.make_readonly(filename)
103
 
 
104
 
 
105
 
    def __contains__(self, fileid):
106
 
        """"""
107
 
        return os.access(self._path(fileid), os.R_OK)
108
 
 
109
 
 
110
 
    def __iter__(self):
111
 
        return iter(os.listdir(self._basedir))
112
 
 
113
 
    def __getitem__(self, fileid):
114
 
        """Returns a file reading from a particular entry."""
115
 
        return file(self._path(fileid), 'rb')
116
 
 
117
 
    def delete_all(self):
118
 
        for fileid in self:
119
 
            self.delete(fileid)
120
 
 
121
 
    def delete(self, fileid):
122
 
        """Remove nominated store entry.
123
 
 
124
 
        Most stores will be add-only."""
125
 
        filename = self._path(fileid)
126
 
        ## osutils.make_writable(filename)
127
 
        os.remove(filename)
128
 
 
129
 
    def destroy(self):
130
 
        """Remove store; only allowed if it is empty."""
131
 
        os.rmdir(self._basedir)
132
 
        mutter("%r destroyed" % self)
133
 
 
134
 
 
135
 
 
136
 
class ImmutableScratchStore(ImmutableStore):
137
 
    """Self-destructing test subclass of ImmutableStore.
138
 
 
139
 
    The Store only exists for the lifetime of the Python object.
140
 
    Obviously you should not put anything precious in it.
141
 
    """
142
 
    def __init__(self):
143
 
        ImmutableStore.__init__(self, tempfile.mkdtemp())
144
 
 
145
 
    def __del__(self):
146
 
        self.delete_all()
147
 
        self.destroy()