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 |
||
26 |
import os, tempfile, types, osutils |
|
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 |
######################################################################
|
|
33 |
# stores
|
|
34 |
||
35 |
class StoreError(Exception): |
|
36 |
pass
|
|
37 |
||
38 |
||
39 |
class ImmutableStore: |
|
40 |
"""Store that holds files indexed by unique names.
|
|
41 |
||
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,
|
|
44 |
such as a UUID.
|
|
45 |
||
46 |
>>> st = ImmutableScratchStore()
|
|
47 |
||
48 |
>>> st.add(StringIO('hello'), 'aa')
|
|
49 |
>>> 'aa' in st
|
|
50 |
True
|
|
51 |
>>> 'foo' in st
|
|
52 |
False
|
|
53 |
||
54 |
You are not allowed to add an id that is already present.
|
|
55 |
||
56 |
Entries can be retrieved as files, which may then be read.
|
|
57 |
||
58 |
>>> st.add(StringIO('goodbye'), '123123')
|
|
59 |
>>> st['123123'].read()
|
|
60 |
'goodbye'
|
|
61 |
||
62 |
:todo: Atomic add by writing to a temporary file and renaming.
|
|
63 |
||
64 |
:todo: Perhaps automatically transform to/from XML in a method?
|
|
65 |
Would just need to tell the constructor what class to
|
|
66 |
use...
|
|
67 |
||
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.
|
|
71 |
||
72 |
"""
|
|
73 |
||
74 |
def __init__(self, basedir): |
|
75 |
"""ImmutableStore constructor."""
|
|
76 |
self._basedir = basedir |
|
77 |
||
78 |
def _path(self, id): |
|
79 |
return os.path.join(self._basedir, id) |
|
80 |
||
81 |
def __repr__(self): |
|
82 |
return "%s(%r)" % (self.__class__.__name__, self._basedir) |
|
83 |
||
84 |
def add(self, f, fileid): |
|
85 |
"""Add contents of a file into the store.
|
|
86 |
||
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
|
|
90 |
# computing the sum.
|
|
91 |
mutter("add store entry %r" % (fileid)) |
|
92 |
if isinstance(f, types.StringTypes): |
|
93 |
content = f |
|
94 |
else: |
|
95 |
content = f.read() |
|
96 |
if fileid not in self: |
|
97 |
filename = self._path(fileid) |
|
98 |
f = file(filename, 'wb') |
|
99 |
f.write(content) |
|
100 |
f.flush() |
|
101 |
os.fsync(f.fileno()) |
|
102 |
f.close() |
|
103 |
osutils.make_readonly(filename) |
|
104 |
||
105 |
||
106 |
def __contains__(self, fileid): |
|
107 |
""""""
|
|
108 |
return os.access(self._path(fileid), os.R_OK) |
|
109 |
||
110 |
||
111 |
def __iter__(self): |
|
112 |
return iter(os.listdir(self._basedir)) |
|
113 |
||
80
by mbp at sourcefrog
show_info: Show number of entries in the branch stores |
114 |
def __len__(self): |
115 |
return len(os.listdir(self._basedir)) |
|
116 |
||
1
by mbp at sourcefrog
import from baz patch-364 |
117 |
def __getitem__(self, fileid): |
118 |
"""Returns a file reading from a particular entry."""
|
|
119 |
return file(self._path(fileid), 'rb') |
|
120 |
||
81
by mbp at sourcefrog
show space usage for various stores in the info command |
121 |
def total_size(self): |
122 |
"""Return (count, bytes)"""
|
|
123 |
total = 0 |
|
124 |
count = 0 |
|
125 |
for fid in self: |
|
126 |
count += 1 |
|
127 |
total += os.stat(self._path(fid))[ST_SIZE] |
|
128 |
return count, total |
|
129 |
||
1
by mbp at sourcefrog
import from baz patch-364 |
130 |
def delete_all(self): |
131 |
for fileid in self: |
|
132 |
self.delete(fileid) |
|
133 |
||
134 |
def delete(self, fileid): |
|
135 |
"""Remove nominated store entry.
|
|
136 |
||
137 |
Most stores will be add-only."""
|
|
138 |
filename = self._path(fileid) |
|
139 |
## osutils.make_writable(filename)
|
|
140 |
os.remove(filename) |
|
141 |
||
142 |
def destroy(self): |
|
143 |
"""Remove store; only allowed if it is empty."""
|
|
144 |
os.rmdir(self._basedir) |
|
145 |
mutter("%r destroyed" % self) |
|
146 |
||
147 |
||
148 |
||
149 |
class ImmutableScratchStore(ImmutableStore): |
|
150 |
"""Self-destructing test subclass of ImmutableStore.
|
|
151 |
||
152 |
The Store only exists for the lifetime of the Python object.
|
|
153 |
Obviously you should not put anything precious in it.
|
|
154 |
"""
|
|
155 |
def __init__(self): |
|
156 |
ImmutableStore.__init__(self, tempfile.mkdtemp()) |
|
157 |
||
158 |
def __del__(self): |
|
159 |
self.delete_all() |
|
160 |
self.destroy() |