184
by mbp at sourcefrog
pychecker fixups |
1 |
# This program is free software; you can redistribute it and/or modify
|
1
by mbp at sourcefrog
import from baz patch-364 |
2 |
# it under the terms of the GNU General Public License as published by
|
3 |
# the Free Software Foundation; either version 2 of the License, or
|
|
4 |
# (at your option) any later version.
|
|
5 |
||
6 |
# This program is distributed in the hope that it will be useful,
|
|
7 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9 |
# GNU General Public License for more details.
|
|
10 |
||
11 |
# You should have received a copy of the GNU General Public License
|
|
12 |
# along with this program; if not, write to the Free Software
|
|
13 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
14 |
||
15 |
"""Stores are the main data-storage mechanism for Bazaar-NG.
|
|
16 |
||
17 |
A store is a simple write-once container indexed by a universally
|
|
18 |
unique ID, which is typically the SHA-1 of the content."""
|
|
19 |
||
20 |
__copyright__ = "Copyright (C) 2005 Canonical Ltd." |
|
21 |
__author__ = "Martin Pool <mbp@canonical.com>" |
|
22 |
||
23 |
import os, tempfile, types, osutils, gzip, errno |
|
24 |
from stat import ST_SIZE |
|
127
by mbp at sourcefrog
- store support for retrieving compressed files |
25 |
from StringIO import StringIO |
81
by mbp at sourcefrog
show space usage for various stores in the info command |
26 |
from trace import mutter |
1
by mbp at sourcefrog
import from baz patch-364 |
27 |
|
28 |
######################################################################
|
|
29 |
# stores
|
|
30 |
||
31 |
class StoreError(Exception): |
|
32 |
pass
|
|
33 |
||
34 |
||
35 |
class ImmutableStore: |
|
36 |
"""Store that holds files indexed by unique names.
|
|
37 |
||
38 |
Files can be added, but not modified once they are in. Typically
|
|
39 |
the hash is used as the name, or something else known to be unique,
|
|
40 |
such as a UUID.
|
|
41 |
||
42 |
>>> st = ImmutableScratchStore()
|
|
43 |
||
44 |
>>> st.add(StringIO('hello'), 'aa')
|
|
45 |
>>> 'aa' in st
|
|
46 |
True
|
|
47 |
>>> 'foo' in st
|
|
48 |
False
|
|
49 |
||
50 |
You are not allowed to add an id that is already present.
|
|
51 |
||
52 |
Entries can be retrieved as files, which may then be read.
|
|
53 |
||
54 |
>>> st.add(StringIO('goodbye'), '123123')
|
|
55 |
>>> st['123123'].read()
|
|
56 |
'goodbye'
|
|
57 |
||
58 |
TODO: Atomic add by writing to a temporary file and renaming.
|
|
59 |
||
254
by Martin Pool
- Doc cleanups from Magnus Therning |
60 |
TODO: Perhaps automatically transform to/from XML in a method?
|
1
by mbp at sourcefrog
import from baz patch-364 |
61 |
Would just need to tell the constructor what class to
|
254
by Martin Pool
- Doc cleanups from Magnus Therning |
62 |
use...
|
1
by mbp at sourcefrog
import from baz patch-364 |
63 |
|
64 |
TODO: Even within a simple disk store like this, we could
|
|
65 |
gzip the files. But since many are less than one disk
|
|
254
by Martin Pool
- Doc cleanups from Magnus Therning |
66 |
block, that might not help a lot.
|
1
by mbp at sourcefrog
import from baz patch-364 |
67 |
|
68 |
"""
|
|
69 |
||
70 |
def __init__(self, basedir): |
|
71 |
"""ImmutableStore constructor."""
|
|
72 |
self._basedir = basedir |
|
73 |
||
74 |
def _path(self, id): |
|
75 |
return os.path.join(self._basedir, id) |
|
76 |
||
77 |
def __repr__(self): |
|
78 |
return "%s(%r)" % (self.__class__.__name__, self._basedir) |
|
79 |
||
80 |
def add(self, f, fileid, compressed=True): |
|
81 |
"""Add contents of a file into the store.
|
|
129
by mbp at sourcefrog
Store.add defaults to adding gzipped files |
82 |
|
1
by mbp at sourcefrog
import from baz patch-364 |
83 |
f -- An open file, or file-like object."""
|
84 |
# FIXME: Only works on smallish files
|
|
254
by Martin Pool
- Doc cleanups from Magnus Therning |
85 |
# TODO: Can be optimized by copying at the same time as
|
1
by mbp at sourcefrog
import from baz patch-364 |
86 |
# computing the sum.
|
87 |
mutter("add store entry %r" % (fileid)) |
|
88 |
if isinstance(f, types.StringTypes): |
|
89 |
content = f |
|
90 |
else: |
|
91 |
content = f.read() |
|
92 |
||
93 |
p = self._path(fileid) |
|
129
by mbp at sourcefrog
Store.add defaults to adding gzipped files |
94 |
if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK): |
95 |
bailout("store %r already contains id %r" % (self._basedir, fileid)) |
|
96 |
||
97 |
if compressed: |
|
98 |
f = gzip.GzipFile(p + '.gz', 'wb') |
|
99 |
os.chmod(p + '.gz', 0444) |
|
100 |
else: |
|
101 |
f = file(p, 'wb') |
|
102 |
os.chmod(p, 0444) |
|
103 |
||
104 |
f.write(content) |
|
105 |
f.close() |
|
106 |
||
107 |
||
1
by mbp at sourcefrog
import from baz patch-364 |
108 |
def __contains__(self, fileid): |
109 |
""""""
|
|
110 |
p = self._path(fileid) |
|
111 |
return (os.access(p, os.R_OK) |
|
128
by mbp at sourcefrog
More support for compressed files in stores |
112 |
or os.access(p + '.gz', os.R_OK)) |
113 |
||
114 |
# TODO: Guard against the same thing being stored twice, compressed and uncompresse
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
115 |
|
128
by mbp at sourcefrog
More support for compressed files in stores |
116 |
def __iter__(self): |
1
by mbp at sourcefrog
import from baz patch-364 |
117 |
for f in os.listdir(self._basedir): |
118 |
if f[-3:] == '.gz': |
|
128
by mbp at sourcefrog
More support for compressed files in stores |
119 |
# TODO: case-insensitive?
|
120 |
yield f[:-3] |
|
121 |
else: |
|
122 |
yield f |
|
123 |
||
124 |
def __len__(self): |
|
1
by mbp at sourcefrog
import from baz patch-364 |
125 |
return len(os.listdir(self._basedir)) |
80
by mbp at sourcefrog
show_info: Show number of entries in the branch stores |
126 |
|
127 |
def __getitem__(self, fileid): |
|
128 |
"""Returns a file reading from a particular entry."""
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
129 |
p = self._path(fileid) |
130 |
try: |
|
127
by mbp at sourcefrog
- store support for retrieving compressed files |
131 |
return gzip.GzipFile(p + '.gz', 'rb') |
132 |
except IOError, e: |
|
133 |
if e.errno == errno.ENOENT: |
|
134 |
return file(p, 'rb') |
|
135 |
else: |
|
136 |
raise e |
|
137 |
||
138 |
def total_size(self): |
|
1
by mbp at sourcefrog
import from baz patch-364 |
139 |
"""Return (count, bytes)
|
81
by mbp at sourcefrog
show space usage for various stores in the info command |
140 |
|
127
by mbp at sourcefrog
- store support for retrieving compressed files |
141 |
This is the (compressed) size stored on disk, not the size of
|
142 |
the content."""
|
|
143 |
total = 0 |
|
144 |
count = 0 |
|
81
by mbp at sourcefrog
show space usage for various stores in the info command |
145 |
for fid in self: |
146 |
count += 1 |
|
147 |
p = self._path(fid) |
|
148 |
try: |
|
128
by mbp at sourcefrog
More support for compressed files in stores |
149 |
total += os.stat(p)[ST_SIZE] |
150 |
except OSError: |
|
151 |
total += os.stat(p + '.gz')[ST_SIZE] |
|
152 |
||
153 |
return count, total |
|
154 |
||
81
by mbp at sourcefrog
show space usage for various stores in the info command |
155 |
|
156 |
||
1
by mbp at sourcefrog
import from baz patch-364 |
157 |
|
158 |
class ImmutableScratchStore(ImmutableStore): |
|
159 |
"""Self-destructing test subclass of ImmutableStore.
|
|
160 |
||
161 |
The Store only exists for the lifetime of the Python object.
|
|
162 |
Obviously you should not put anything precious in it.
|
|
163 |
"""
|
|
164 |
def __init__(self): |
|
165 |
ImmutableStore.__init__(self, tempfile.mkdtemp()) |
|
166 |
||
167 |
def __del__(self): |
|
168 |
for f in os.listdir(self._basedir): |
|
169 |
fpath = os.path.join(self._basedir, f) |
|
130
by mbp at sourcefrog
- fixup checks on retrieved files to cope with compression, |
170 |
# needed on windows, and maybe some other filesystems
|
163
by mbp at sourcefrog
merge win32 portability fixes |
171 |
os.chmod(fpath, 0600) |
172 |
os.remove(fpath) |
|
173 |
os.rmdir(self._basedir) |
|
174 |
mutter("%r destroyed" % self) |
|
130
by mbp at sourcefrog
- fixup checks on retrieved files to cope with compression, |
175 |
|
176 |