1185.11.19
by John Arbash Meinel
Testing put and append, also testing agaist file-like objects as well as strings. |
1 |
# Copyright (C) 2005 Canonical Ltd
|
2 |
||
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
"""Implementation of Transport for the local filesystem.
|
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
17 |
"""
|
18 |
||
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
19 |
from bzrlib.transport import Transport, register_transport, \ |
20 |
TransportError, NoSuchFile, FileExists |
|
21 |
import os, errno |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
22 |
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
23 |
class LocalTransportError(TransportError): |
24 |
pass
|
|
25 |
||
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
26 |
class LocalTransport(Transport): |
27 |
"""This is the transport agent for local filesystem access."""
|
|
28 |
||
29 |
def __init__(self, base): |
|
30 |
"""Set the base path where files will be stored."""
|
|
1185.11.1
by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet. |
31 |
if base.startswith('file://'): |
32 |
base = base[7:] |
|
33 |
super(LocalTransport, self).__init__(os.path.realpath(base)) |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
34 |
|
907.1.32
by John Arbash Meinel
Renaming is_remote to should_cache as it is more appropriate. |
35 |
def should_cache(self): |
907.1.22
by John Arbash Meinel
Fixed some encoding issues, added is_remote function for Transport objects. |
36 |
return False |
37 |
||
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
38 |
def clone(self, offset=None): |
39 |
"""Return a new LocalTransport with root at self.base + offset
|
|
40 |
Because the local filesystem does not require a connection,
|
|
41 |
we can just return a new object.
|
|
42 |
"""
|
|
43 |
if offset is None: |
|
44 |
return LocalTransport(self.base) |
|
45 |
else: |
|
46 |
return LocalTransport(self.abspath(offset)) |
|
47 |
||
907.1.8
by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport |
48 |
def abspath(self, relpath): |
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
49 |
"""Return the full url to the given relative path.
|
907.1.8
by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport |
50 |
This can be supplied with a string or a list
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
51 |
"""
|
907.1.8
by John Arbash Meinel
Changed the format for abspath. Updated branch to use a hidden _transport |
52 |
if isinstance(relpath, basestring): |
53 |
relpath = [relpath] |
|
54 |
return os.path.join(self.base, *relpath) |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
55 |
|
907.1.24
by John Arbash Meinel
Remote functionality work. |
56 |
def relpath(self, abspath): |
57 |
"""Return the local path portion from a given absolute path.
|
|
58 |
"""
|
|
1185.11.9
by John Arbash Meinel
Most tests pass, some problems with unavailable socket recv |
59 |
from bzrlib.branch import _relpath |
907.1.24
by John Arbash Meinel
Remote functionality work. |
60 |
return _relpath(self.base, abspath) |
61 |
||
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
62 |
def has(self, relpath): |
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
63 |
return os.access(self.abspath(relpath), os.F_OK) |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
64 |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
65 |
def get(self, relpath): |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
66 |
"""Get the file at the given relative path.
|
907.1.20
by John Arbash Meinel
Removed Transport.open(), making get + put encode/decode to utf-8 |
67 |
|
68 |
:param relpath: The relative path to the file
|
|
69 |
"""
|
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
70 |
try: |
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
71 |
path = self.abspath(relpath) |
72 |
return open(path, 'rb') |
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
73 |
except IOError,e: |
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
74 |
if e.errno == errno.ENOENT: |
75 |
raise NoSuchFile('File %r does not exist' % path, orig_error=e) |
|
76 |
raise LocalTransportError(orig_error=e) |
|
907.1.20
by John Arbash Meinel
Removed Transport.open(), making get + put encode/decode to utf-8 |
77 |
|
1185.11.21
by John Arbash Meinel
Added implementations and tests for get_partial |
78 |
def get_partial(self, relpath, start, length=None): |
79 |
"""Get just part of a file.
|
|
80 |
||
81 |
:param relpath: Path to the file, relative to base
|
|
82 |
:param start: The starting position to read from
|
|
83 |
:param length: The length to read. A length of None indicates
|
|
84 |
read to the end of the file.
|
|
85 |
:return: A file-like object containing at least the specified bytes.
|
|
86 |
Some implementations may return objects which can be read
|
|
87 |
past this length, but this is not guaranteed.
|
|
88 |
"""
|
|
89 |
# LocalTransport.get_partial() doesn't care about the length
|
|
90 |
# argument, because it is using a local file, and thus just
|
|
91 |
# returns the file seek'ed to the appropriate location.
|
|
92 |
try: |
|
93 |
path = self.abspath(relpath) |
|
94 |
f = open(path, 'rb') |
|
95 |
f.seek(start, 0) |
|
96 |
return f |
|
97 |
except IOError,e: |
|
98 |
if e.errno == errno.ENOENT: |
|
99 |
raise NoSuchFile('File %r does not exist' % path, orig_error=e) |
|
100 |
raise LocalTransportError(orig_error=e) |
|
101 |
||
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
102 |
def put(self, relpath, f): |
907.1.20
by John Arbash Meinel
Removed Transport.open(), making get + put encode/decode to utf-8 |
103 |
"""Copy the file-like or string object into the location.
|
104 |
||
105 |
:param relpath: Location to put the contents, relative to base.
|
|
106 |
:param f: File-like or string object.
|
|
107 |
"""
|
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
108 |
from bzrlib.atomicfile import AtomicFile |
109 |
||
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
110 |
try: |
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
111 |
path = self.abspath(relpath) |
112 |
fp = AtomicFile(path, 'wb') |
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
113 |
except IOError, e: |
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
114 |
if e.errno == errno.ENOENT: |
115 |
raise NoSuchFile('File %r does not exist' % path, orig_error=e) |
|
116 |
raise LocalTransportError(orig_error=e) |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
117 |
try: |
118 |
self._pump(f, fp) |
|
119 |
fp.commit() |
|
120 |
finally: |
|
121 |
fp.close() |
|
122 |
||
123 |
def mkdir(self, relpath): |
|
124 |
"""Create a directory at the given path."""
|
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
125 |
try: |
126 |
os.mkdir(self.abspath(relpath)) |
|
127 |
except OSError,e: |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
128 |
if e.errno == errno.EEXIST: |
129 |
raise FileExists(orig_error=e) |
|
130 |
elif e.errno == errno.ENOENT: |
|
131 |
raise NoSuchFile(orig_error=e) |
|
132 |
raise LocalTransportError(orig_error=e) |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
133 |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
134 |
def append(self, relpath, f): |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
135 |
"""Append the text in the file-like object into the final
|
136 |
location.
|
|
137 |
"""
|
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
138 |
fp = open(self.abspath(relpath), 'ab') |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
139 |
self._pump(f, fp) |
140 |
||
141 |
def copy(self, rel_from, rel_to): |
|
142 |
"""Copy the item at rel_from to the location at rel_to"""
|
|
143 |
import shutil |
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
144 |
path_from = self.abspath(rel_from) |
145 |
path_to = self.abspath(rel_to) |
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
146 |
try: |
147 |
shutil.copy(path_from, path_to) |
|
148 |
except OSError,e: |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
149 |
raise LocalTransportError(orig_error=e) |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
150 |
|
151 |
def move(self, rel_from, rel_to): |
|
152 |
"""Move the item at rel_from to the location at rel_to"""
|
|
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
153 |
path_from = self.abspath(rel_from) |
154 |
path_to = self.abspath(rel_to) |
|
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
155 |
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
156 |
try: |
157 |
os.rename(path_from, path_to) |
|
158 |
except OSError,e: |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
159 |
raise LocalTransportError(orig_error=e) |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
160 |
|
161 |
def delete(self, relpath): |
|
162 |
"""Delete the item at relpath"""
|
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
163 |
try: |
164 |
os.remove(self.abspath(relpath)) |
|
165 |
except OSError,e: |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
166 |
raise LocalTransportError(orig_error=e) |
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
167 |
|
907.1.28
by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function. |
168 |
def copy_to(self, relpaths, other, pb=None): |
169 |
"""Copy a set of entries from self into another Transport.
|
|
170 |
||
171 |
:param relpaths: A list/generator of entries to be copied.
|
|
172 |
"""
|
|
173 |
if isinstance(other, LocalTransport): |
|
174 |
# Both from & to are on the local filesystem
|
|
175 |
# Unfortunately, I can't think of anything faster than just
|
|
176 |
# copying them across, one by one :(
|
|
177 |
import shutil |
|
178 |
||
179 |
total = self._get_total(relpaths) |
|
180 |
count = 0 |
|
181 |
for path in relpaths: |
|
182 |
self._update_pb(pb, 'copy-to', count, total) |
|
183 |
shutil.copy(self.abspath(path), other.abspath(path)) |
|
184 |
count += 1 |
|
185 |
return count |
|
186 |
else: |
|
187 |
return super(LocalTransport, self).copy_to(relpaths, other, pb=pb) |
|
188 |
||
189 |
||
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
190 |
def list_dir(self, relpath): |
191 |
"""Return a list of all files at the given location.
|
|
192 |
WARNING: many transports do not support this, so trying avoid using
|
|
193 |
it if at all possible.
|
|
194 |
"""
|
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
195 |
try: |
196 |
return os.listdir(self.abspath(relpath)) |
|
197 |
except OSError,e: |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
198 |
raise LocalTransportError(orig_error=e) |
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
199 |
|
200 |
def stat(self, relpath): |
|
201 |
"""Return the stat information for a file.
|
|
202 |
"""
|
|
907.1.48
by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass. |
203 |
try: |
204 |
return os.stat(self.abspath(relpath)) |
|
205 |
except OSError,e: |
|
907.1.50
by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown. |
206 |
raise LocalTransportError(orig_error=e) |
907.1.2
by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport. |
207 |
|
907.1.24
by John Arbash Meinel
Remote functionality work. |
208 |
def lock_read(self, relpath): |
209 |
"""Lock the given file for shared (read) access.
|
|
210 |
:return: A lock object, which should be passed to Transport.unlock()
|
|
211 |
"""
|
|
212 |
from bzrlib.lock import ReadLock |
|
213 |
return ReadLock(self.abspath(relpath)) |
|
214 |
||
215 |
def lock_write(self, relpath): |
|
216 |
"""Lock the given file for exclusive (write) access.
|
|
217 |
WARNING: many transports do not support this, so trying avoid using it
|
|
218 |
||
219 |
:return: A lock object, which should be passed to Transport.unlock()
|
|
220 |
"""
|
|
221 |
from bzrlib.lock import WriteLock |
|
222 |
return WriteLock(self.abspath(relpath)) |
|
223 |
||
907.1.1
by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer. |
224 |
# If nothing else matches, try the LocalTransport
|
907.1.45
by John Arbash Meinel
Switch to registering protocol handlers, rather than just updating a dictionary. |
225 |
register_transport(None, LocalTransport) |
1185.11.1
by John Arbash Meinel
(broken) Transport work is merged in. Tests do not pass yet. |
226 |
register_transport('file://', LocalTransport) |