7
from bzrlib.transport import Transport, register_transport, TransportError
7
from bzrlib.transport import Transport, register_transport, \
8
TransportError, NoSuchFile, FileExists
10
11
class LocalTransportError(TransportError):
48
49
def has(self, relpath):
49
50
return os.access(self.abspath(relpath), os.F_OK)
51
def get(self, relpath, decode=False):
52
def get(self, relpath):
52
53
"""Get the file at the given relative path.
54
55
:param relpath: The relative path to the file
55
:param decode: If True, assume the file is utf-8 encoded and
56
decode it into Unicode
61
return codecs.open(self.abspath(relpath), 'rb',
62
encoding='utf-8', buffering=60000)
64
return open(self.abspath(relpath), 'rb')
58
path = self.abspath(relpath)
59
return open(path, 'rb')
66
raise LocalTransportError(e)
61
if e.errno == errno.ENOENT:
62
raise NoSuchFile('File %r does not exist' % path, orig_error=e)
63
raise LocalTransportError(orig_error=e)
68
def put(self, relpath, f, encode=False):
65
def put(self, relpath, f):
69
66
"""Copy the file-like or string object into the location.
71
68
:param relpath: Location to put the contents, relative to base.
72
69
:param f: File-like or string object.
73
:param encode: If True, translate the contents into utf-8 encoded text.
75
71
from bzrlib.atomicfile import AtomicFile
79
fp = AtomicFile(self.abspath(relpath), 'wb', encoding='utf-8')
81
fp = AtomicFile(self.abspath(relpath), 'wb')
74
path = self.abspath(relpath)
75
fp = AtomicFile(path, 'wb')
83
raise LocalTransportError(e)
77
if e.errno == errno.ENOENT:
78
raise NoSuchFile('File %r does not exist' % path, orig_error=e)
79
raise LocalTransportError(orig_error=e)
93
89
os.mkdir(self.abspath(relpath))
95
raise LocalTransportError(e)
91
if e.errno == errno.EEXIST:
92
raise FileExists(orig_error=e)
93
elif e.errno == errno.ENOENT:
94
raise NoSuchFile(orig_error=e)
95
raise LocalTransportError(orig_error=e)
97
def append(self, relpath, f, encode=False):
97
def append(self, relpath, f):
98
98
"""Append the text in the file-like object into the final
102
fp = codecs.open(self.abspath(relpath), 'ab',
103
encoding='utf-8', buffering=60000)
105
fp = open(self.abspath(relpath), 'ab')
101
fp = open(self.abspath(relpath), 'ab')
106
102
self._pump(f, fp)
108
104
def copy(self, rel_from, rel_to):
114
110
shutil.copy(path_from, path_to)
115
111
except OSError,e:
116
raise LocalTransportError(e)
112
raise LocalTransportError(orig_error=e)
118
114
def move(self, rel_from, rel_to):
119
115
"""Move the item at rel_from to the location at rel_to"""
124
120
os.rename(path_from, path_to)
125
121
except OSError,e:
126
raise LocalTransportError(e)
122
raise LocalTransportError(orig_error=e)
128
124
def delete(self, relpath):
129
125
"""Delete the item at relpath"""
131
127
os.remove(self.abspath(relpath))
132
128
except OSError,e:
133
raise LocalTransportError(e)
129
raise LocalTransportError(orig_error=e)
135
131
def copy_to(self, relpaths, other, pb=None):
136
132
"""Copy a set of entries from self into another Transport.
179
175
return os.stat(self.abspath(relpath))
180
176
except OSError,e:
181
raise LocalTransportError(e)
177
raise LocalTransportError(orig_error=e)
183
179
def lock_read(self, relpath):
184
180
"""Lock the given file for shared (read) access.