2
# -*- coding: UTF-8 -*-
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.
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.
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
18
"""Stores are the main data-storage mechanism for Bazaar-NG.
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."""
23
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
24
__author__ = "Martin Pool <mbp@canonical.com>"
26
import os, tempfile, types, osutils
27
from stat import ST_SIZE
28
from StringIO import StringIO
29
from trace import mutter
32
######################################################################
35
class StoreError(Exception):
40
"""Store that holds files indexed by unique names.
42
Files can be added, but not modified once they are in. Typically
43
the hash is used as the name, or something else known to be unique,
46
>>> st = ImmutableScratchStore()
48
>>> st.add(StringIO('hello'), 'aa')
54
You are not allowed to add an id that is already present.
56
Entries can be retrieved as files, which may then be read.
58
>>> st.add(StringIO('goodbye'), '123123')
59
>>> st['123123'].read()
62
:todo: Atomic add by writing to a temporary file and renaming.
64
:todo: Perhaps automatically transform to/from XML in a method?
65
Would just need to tell the constructor what class to
68
:todo: Even within a simple disk store like this, we could
69
gzip the files. But since many are less than one disk
70
block, that might not help a lot.
74
def __init__(self, basedir):
75
"""ImmutableStore constructor."""
76
self._basedir = basedir
79
return os.path.join(self._basedir, id)
82
return "%s(%r)" % (self.__class__.__name__, self._basedir)
84
def add(self, f, fileid):
85
"""Add contents of a file into the store.
87
:param f: An open file, or file-like object."""
88
# FIXME: Only works on smallish files
89
# TODO: Can be optimized by copying at the same time as
91
mutter("add store entry %r" % (fileid))
92
if isinstance(f, types.StringTypes):
96
if fileid not in self:
97
filename = self._path(fileid)
98
f = file(filename, 'wb')
101
## os.fsync(f.fileno())
103
osutils.make_readonly(filename)
106
def __contains__(self, fileid):
108
return os.access(self._path(fileid), os.R_OK)
112
return iter(os.listdir(self._basedir))
115
return len(os.listdir(self._basedir))
117
def __getitem__(self, fileid):
118
"""Returns a file reading from a particular entry."""
119
return file(self._path(fileid), 'rb')
121
def total_size(self):
122
"""Return (count, bytes)"""
127
total += os.stat(self._path(fid))[ST_SIZE]
130
def delete_all(self):
134
def delete(self, fileid):
135
"""Remove nominated store entry.
137
Most stores will be add-only."""
138
filename = self._path(fileid)
139
## osutils.make_writable(filename)
143
"""Remove store; only allowed if it is empty."""
144
os.rmdir(self._basedir)
145
mutter("%r destroyed" % self)
149
class ImmutableScratchStore(ImmutableStore):
150
"""Self-destructing test subclass of ImmutableStore.
152
The Store only exists for the lifetime of the Python object.
153
Obviously you should not put anything precious in it.
156
ImmutableStore.__init__(self, tempfile.mkdtemp())