22
23
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
23
24
__author__ = "Martin Pool <mbp@canonical.com>"
25
import os, tempfile, types, osutils, gzip, errno
26
import os, tempfile, types, osutils
26
27
from stat import ST_SIZE
27
28
from StringIO import StringIO
28
29
from trace import mutter
30
32
######################################################################
57
59
>>> st['123123'].read()
60
TODO: Atomic add by writing to a temporary file and renaming.
62
:todo: Atomic add by writing to a temporary file and renaming.
62
TODO: Perhaps automatically transform to/from XML in a method?
64
:todo: Perhaps automatically transform to/from XML in a method?
63
65
Would just need to tell the constructor what class to
66
TODO: Even within a simple disk store like this, we could
68
:todo: Even within a simple disk store like this, we could
67
69
gzip the files. But since many are less than one disk
68
70
block, that might not help a lot.
79
81
def __repr__(self):
80
82
return "%s(%r)" % (self.__class__.__name__, self._basedir)
82
def add(self, f, fileid, compressed=True):
84
def add(self, f, fileid):
83
85
"""Add contents of a file into the store.
85
f -- An open file, or file-like object."""
87
:param f: An open file, or file-like object."""
86
88
# FIXME: Only works on smallish files
87
89
# TODO: Can be optimized by copying at the same time as
88
90
# computing the sum.
95
p = self._path(fileid)
96
if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
97
bailout("store %r already contains id %r" % (self._basedir, fileid))
100
f = gzip.GzipFile(p + '.gz', 'wb')
101
os.chmod(p + '.gz', 0444)
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)
110
106
def __contains__(self, fileid):
112
p = self._path(fileid)
113
return (os.access(p, os.R_OK)
114
or os.access(p + '.gz', os.R_OK))
108
return os.access(self._path(fileid), os.R_OK)
116
# TODO: Guard against the same thing being stored twice, compressed and uncompresse
118
111
def __iter__(self):
119
for f in os.listdir(self._basedir):
121
# TODO: case-insensitive?
112
return iter(os.listdir(self._basedir))
126
114
def __len__(self):
127
115
return len(os.listdir(self._basedir))
129
117
def __getitem__(self, fileid):
130
118
"""Returns a file reading from a particular entry."""
131
p = self._path(fileid)
133
return gzip.GzipFile(p + '.gz', 'rb')
135
if e.errno == errno.ENOENT:
119
return file(self._path(fileid), 'rb')
140
121
def total_size(self):
141
"""Return (count, bytes)
143
This is the (compressed) size stored on disk, not the size of
122
"""Return (count, bytes)"""
151
total += os.stat(p)[ST_SIZE]
153
total += os.stat(p + '.gz')[ST_SIZE]
127
total += os.stat(self._path(fid))[ST_SIZE]
155
128
return count, total
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)
167
156
ImmutableStore.__init__(self, tempfile.mkdtemp())
169
158
def __del__(self):
170
for f in os.listdir(self._basedir):
171
fpath = os.path.join(self._basedir, f)
172
# needed on windows, and maybe some other filesystems
173
os.chmod(fpath, 0600)
175
os.rmdir(self._basedir)
176
mutter("%r destroyed" % self)