bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
1
by mbp at sourcefrog
import from baz patch-364 |
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 |
||
|
127
by mbp at sourcefrog
- store support for retrieving compressed files |
26 |
import os, tempfile, types, osutils, gzip, errno |
|
81
by mbp at sourcefrog
show space usage for various stores in the info command |
27 |
from stat import ST_SIZE |
|
1
by mbp at sourcefrog
import from baz patch-364 |
28 |
from StringIO import StringIO |
29 |
from trace import mutter |
|
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) |
|
|
90
by mbp at sourcefrog
- don't fsync files when written into store |
99 |
## f.flush()
|
100 |
## os.fsync(f.fileno())
|
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
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 |
||
|
80
by mbp at sourcefrog
show_info: Show number of entries in the branch stores |
113 |
def __len__(self): |
114 |
return len(os.listdir(self._basedir)) |
|
115 |
||
|
1
by mbp at sourcefrog
import from baz patch-364 |
116 |
def __getitem__(self, fileid): |
117 |
"""Returns a file reading from a particular entry.""" |
|
|
127
by mbp at sourcefrog
- store support for retrieving compressed files |
118 |
p = self._path(fileid) |
119 |
try: |
|
120 |
return gzip.GzipFile(p + '.gz', 'rb') |
|
121 |
except IOError, e: |
|
122 |
if e.errno == errno.ENOENT: |
|
123 |
return file(p, 'rb') |
|
124 |
else: |
|
125 |
raise e |
|
|
1
by mbp at sourcefrog
import from baz patch-364 |
126 |
|
|
81
by mbp at sourcefrog
show space usage for various stores in the info command |
127 |
def total_size(self): |
|
127
by mbp at sourcefrog
- store support for retrieving compressed files |
128 |
"""Return (count, bytes) |
129 |
||
130 |
This is the (compressed) size stored on disk, not the size of
|
|
131 |
the content."""
|
|
|
81
by mbp at sourcefrog
show space usage for various stores in the info command |
132 |
total = 0 |
133 |
count = 0 |
|
134 |
for fid in self: |
|
135 |
count += 1 |
|
136 |
total += os.stat(self._path(fid))[ST_SIZE] |
|
137 |
return count, total |
|
138 |
||
|
1
by mbp at sourcefrog
import from baz patch-364 |
139 |
def delete_all(self): |
140 |
for fileid in self: |
|
141 |
self.delete(fileid) |
|
142 |
||
143 |
def delete(self, fileid): |
|
144 |
"""Remove nominated store entry. |
|
145 |
||
146 |
Most stores will be add-only."""
|
|
147 |
filename = self._path(fileid) |
|
148 |
## osutils.make_writable(filename)
|
|
149 |
os.remove(filename) |
|
150 |
||
151 |
def destroy(self): |
|
152 |
"""Remove store; only allowed if it is empty.""" |
|
153 |
os.rmdir(self._basedir) |
|
154 |
mutter("%r destroyed" % self) |
|
155 |
||
156 |
||
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 |
self.delete_all() |
|
169 |
self.destroy() |