~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/local.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-06 07:25:55 UTC
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061006072555-b41c9a6f481fd1d6
Make bzrlib.config use lazy importing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
 
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
20
"""
21
21
 
22
22
import os
 
23
from stat import ST_MODE, S_ISDIR, ST_SIZE, S_IMODE
 
24
import sys
 
25
 
 
26
from bzrlib.lazy_import import lazy_import
 
27
lazy_import(globals(), """
 
28
import errno
23
29
import shutil
24
 
import sys
25
 
from stat import ST_MODE, S_ISDIR, ST_SIZE
26
 
import tempfile
27
30
 
28
 
from bzrlib.osutils import (abspath, realpath, normpath, pathjoin, rename, 
29
 
                            check_legal_path, rmtree)
30
 
from bzrlib.symbol_versioning import warn
 
31
from bzrlib import (
 
32
    atomicfile,
 
33
    osutils,
 
34
    urlutils,
 
35
    symbol_versioning,
 
36
    )
31
37
from bzrlib.trace import mutter
 
38
""")
 
39
 
32
40
from bzrlib.transport import Transport, Server
33
 
import bzrlib.urlutils as urlutils
 
41
 
 
42
 
 
43
_append_flags = os.O_CREAT | os.O_APPEND | os.O_WRONLY | osutils.O_BINARY
 
44
_put_non_atomic_flags = os.O_CREAT | os.O_TRUNC | os.O_WRONLY | osutils.O_BINARY
34
45
 
35
46
 
36
47
class LocalTransport(Transport):
39
50
    def __init__(self, base):
40
51
        """Set the base path where files will be stored."""
41
52
        if not base.startswith('file://'):
42
 
            warn("Instantiating LocalTransport with a filesystem path"
 
53
            symbol_versioning.warn(
 
54
                "Instantiating LocalTransport with a filesystem path"
43
55
                " is deprecated as of bzr 0.8."
44
56
                " Please use bzrlib.transport.get_transport()"
45
57
                " or pass in a file:// url.",
51
63
            base = base + '/'
52
64
        super(LocalTransport, self).__init__(base)
53
65
        self._local_base = urlutils.local_path_from_url(base)
54
 
        ## mutter("_local_base: %r => %r", base, self._local_base)
55
66
 
56
67
    def should_cache(self):
57
68
        return False
83
94
        assert isinstance(relpath, basestring), (type(relpath), relpath)
84
95
        # jam 20060426 Using normpath on the real path, because that ensures
85
96
        #       proper handling of stuff like
86
 
        path = normpath(pathjoin(self._local_base, urlutils.unescape(relpath)))
 
97
        path = osutils.normpath(osutils.pathjoin(
 
98
                    self._local_base, urlutils.unescape(relpath)))
87
99
        return urlutils.local_path_to_url(path)
88
100
 
89
101
    def local_abspath(self, relpath):
125
137
        except (IOError, OSError),e:
126
138
            self._translate_error(e, path)
127
139
 
128
 
    def put(self, relpath, f, mode=None):
129
 
        """Copy the file-like or string object into the location.
 
140
    def put_file(self, relpath, f, mode=None):
 
141
        """Copy the file-like object into the location.
130
142
 
131
143
        :param relpath: Location to put the contents, relative to base.
132
 
        :param f:       File-like or string object.
 
144
        :param f:       File-like object.
 
145
        :param mode: The mode for the newly created file, 
 
146
                     None means just use the default
133
147
        """
134
 
        from bzrlib.atomicfile import AtomicFile
135
148
 
136
149
        path = relpath
137
150
        try:
138
151
            path = self._abspath(relpath)
139
 
            check_legal_path(path)
140
 
            fp = AtomicFile(path, 'wb', new_mode=mode)
 
152
            osutils.check_legal_path(path)
 
153
            fp = atomicfile.AtomicFile(path, 'wb', new_mode=mode)
141
154
        except (IOError, OSError),e:
142
155
            self._translate_error(e, path)
143
156
        try:
146
159
        finally:
147
160
            fp.close()
148
161
 
 
162
    def put_bytes(self, relpath, bytes, mode=None):
 
163
        """Copy the string into the location.
 
164
 
 
165
        :param relpath: Location to put the contents, relative to base.
 
166
        :param bytes:   String
 
167
        """
 
168
 
 
169
        path = relpath
 
170
        try:
 
171
            path = self._abspath(relpath)
 
172
            osutils.check_legal_path(path)
 
173
            fp = atomicfile.AtomicFile(path, 'wb', new_mode=mode)
 
174
        except (IOError, OSError),e:
 
175
            self._translate_error(e, path)
 
176
        try:
 
177
            fp.write(bytes)
 
178
            fp.commit()
 
179
        finally:
 
180
            fp.close()
 
181
 
 
182
    def _put_non_atomic_helper(self, relpath, writer,
 
183
                               mode=None,
 
184
                               create_parent_dir=False,
 
185
                               dir_mode=None):
 
186
        """Common functionality information for the put_*_non_atomic.
 
187
 
 
188
        This tracks all the create_parent_dir stuff.
 
189
 
 
190
        :param relpath: the path we are putting to.
 
191
        :param writer: A function that takes an os level file descriptor
 
192
            and writes whatever data it needs to write there.
 
193
        :param mode: The final file mode.
 
194
        :param create_parent_dir: Should we be creating the parent directory
 
195
            if it doesn't exist?
 
196
        """
 
197
        abspath = self._abspath(relpath)
 
198
        if mode is None:
 
199
            # os.open() will automatically use the umask
 
200
            local_mode = 0666
 
201
        else:
 
202
            local_mode = mode
 
203
        try:
 
204
            fd = os.open(abspath, _put_non_atomic_flags, local_mode)
 
205
        except (IOError, OSError),e:
 
206
            # We couldn't create the file, maybe we need to create
 
207
            # the parent directory, and try again
 
208
            if (not create_parent_dir
 
209
                or e.errno not in (errno.ENOENT,errno.ENOTDIR)):
 
210
                self._translate_error(e, relpath)
 
211
            parent_dir = os.path.dirname(abspath)
 
212
            if not parent_dir:
 
213
                self._translate_error(e, relpath)
 
214
            self._mkdir(parent_dir, mode=dir_mode)
 
215
            # We created the parent directory, lets try to open the
 
216
            # file again
 
217
            try:
 
218
                fd = os.open(abspath, _put_non_atomic_flags, local_mode)
 
219
            except (IOError, OSError), e:
 
220
                self._translate_error(e, relpath)
 
221
        try:
 
222
            st = os.fstat(fd)
 
223
            if mode is not None and mode != S_IMODE(st.st_mode):
 
224
                # Because of umask, we may still need to chmod the file.
 
225
                # But in the general case, we won't have to
 
226
                os.chmod(abspath, mode)
 
227
            writer(fd)
 
228
        finally:
 
229
            os.close(fd)
 
230
 
 
231
    def put_file_non_atomic(self, relpath, f, mode=None,
 
232
                            create_parent_dir=False,
 
233
                            dir_mode=None):
 
234
        """Copy the file-like object into the target location.
 
235
 
 
236
        This function is not strictly safe to use. It is only meant to
 
237
        be used when you already know that the target does not exist.
 
238
        It is not safe, because it will open and truncate the remote
 
239
        file. So there may be a time when the file has invalid contents.
 
240
 
 
241
        :param relpath: The remote location to put the contents.
 
242
        :param f:       File-like object.
 
243
        :param mode:    Possible access permissions for new file.
 
244
                        None means do not set remote permissions.
 
245
        :param create_parent_dir: If we cannot create the target file because
 
246
                        the parent directory does not exist, go ahead and
 
247
                        create it, and then try again.
 
248
        """
 
249
        def writer(fd):
 
250
            self._pump_to_fd(f, fd)
 
251
        self._put_non_atomic_helper(relpath, writer, mode=mode,
 
252
                                    create_parent_dir=create_parent_dir,
 
253
                                    dir_mode=dir_mode)
 
254
 
 
255
    def put_bytes_non_atomic(self, relpath, bytes, mode=None,
 
256
                             create_parent_dir=False, dir_mode=None):
 
257
        def writer(fd):
 
258
            os.write(fd, bytes)
 
259
        self._put_non_atomic_helper(relpath, writer, mode=mode,
 
260
                                    create_parent_dir=create_parent_dir,
 
261
                                    dir_mode=dir_mode)
 
262
 
149
263
    def iter_files_recursive(self):
150
264
        """Iter the relative paths of files in the transports sub-tree."""
151
265
        queue = list(self.list_dir(u'.'))
158
272
            else:
159
273
                yield relpath
160
274
 
 
275
    def _mkdir(self, abspath, mode=None):
 
276
        """Create a real directory, filtering through mode"""
 
277
        if mode is None:
 
278
            # os.mkdir() will filter through umask
 
279
            local_mode = 0777
 
280
        else:
 
281
            local_mode = mode
 
282
        try:
 
283
            os.mkdir(abspath, local_mode)
 
284
            if mode is not None:
 
285
                # It is probably faster to just do the chmod, rather than
 
286
                # doing a stat, and then trying to compare
 
287
                os.chmod(abspath, mode)
 
288
        except (IOError, OSError),e:
 
289
            self._translate_error(e, abspath)
 
290
 
161
291
    def mkdir(self, relpath, mode=None):
162
292
        """Create a directory at the given path."""
163
 
        path = relpath
 
293
        self._mkdir(self._abspath(relpath), mode=mode)
 
294
 
 
295
    def _get_append_file(self, relpath, mode=None):
 
296
        """Call os.open() for the given relpath"""
 
297
        file_abspath = self._abspath(relpath)
 
298
        if mode is None:
 
299
            # os.open() will automatically use the umask
 
300
            local_mode = 0666
 
301
        else:
 
302
            local_mode = mode
164
303
        try:
165
 
            path = self._abspath(relpath)
166
 
            os.mkdir(path)
167
 
            if mode is not None:
168
 
                os.chmod(path, mode)
 
304
            return file_abspath, os.open(file_abspath, _append_flags, local_mode)
169
305
        except (IOError, OSError),e:
170
 
            self._translate_error(e, path)
171
 
 
172
 
    def append(self, relpath, f, mode=None):
 
306
            self._translate_error(e, relpath)
 
307
 
 
308
    def _check_mode_and_size(self, file_abspath, fd, mode=None):
 
309
        """Check the mode of the file, and return the current size"""
 
310
        st = os.fstat(fd)
 
311
        if mode is not None and mode != S_IMODE(st.st_mode):
 
312
            # Because of umask, we may still need to chmod the file.
 
313
            # But in the general case, we won't have to
 
314
            os.chmod(file_abspath, mode)
 
315
        return st.st_size
 
316
 
 
317
    def append_file(self, relpath, f, mode=None):
173
318
        """Append the text in the file-like object into the final location."""
174
 
        abspath = self._abspath(relpath)
175
 
        fp = None
176
 
        try:
177
 
            try:
178
 
                fp = open(abspath, 'ab')
179
 
                # FIXME should we really be chmodding every time ? RBC 20060523
180
 
                if mode is not None:
181
 
                    os.chmod(abspath, mode)
182
 
            except (IOError, OSError),e:
183
 
                self._translate_error(e, relpath)
184
 
            # win32 workaround (tell on an unwritten file returns 0)
185
 
            fp.seek(0, 2)
186
 
            result = fp.tell()
187
 
            self._pump(f, fp)
188
 
        finally:
189
 
            if fp is not None:
190
 
                fp.close()
191
 
        return result
 
319
        file_abspath, fd = self._get_append_file(relpath, mode=mode)
 
320
        try:
 
321
            result = self._check_mode_and_size(file_abspath, fd, mode=mode)
 
322
            self._pump_to_fd(f, fd)
 
323
        finally:
 
324
            os.close(fd)
 
325
        return result
 
326
 
 
327
    def append_bytes(self, relpath, bytes, mode=None):
 
328
        """Append the text in the string into the final location."""
 
329
        file_abspath, fd = self._get_append_file(relpath, mode=mode)
 
330
        try:
 
331
            result = self._check_mode_and_size(file_abspath, fd, mode=mode)
 
332
            os.write(fd, bytes)
 
333
        finally:
 
334
            os.close(fd)
 
335
        return result
 
336
 
 
337
    def _pump_to_fd(self, fromfile, to_fd):
 
338
        """Copy contents of one file to another."""
 
339
        BUFSIZE = 32768
 
340
        while True:
 
341
            b = fromfile.read(BUFSIZE)
 
342
            if not b:
 
343
                break
 
344
            os.write(to_fd, b)
192
345
 
193
346
    def copy(self, rel_from, rel_to):
194
347
        """Copy the item at rel_from to the location at rel_to"""
217
370
 
218
371
        try:
219
372
            # this version will delete the destination if necessary
220
 
            rename(path_from, path_to)
 
373
            osutils.rename(path_from, path_to)
221
374
        except (IOError, OSError),e:
222
375
            # TODO: What about path_to?
223
376
            self._translate_error(e, path_from)
268
421
        """
269
422
        path = self._abspath(relpath)
270
423
        try:
271
 
            return [urlutils.escape(entry) for entry in os.listdir(path)]
 
424
            entries = os.listdir(path)
272
425
        except (IOError, OSError), e:
273
426
            self._translate_error(e, path)
 
427
        return [urlutils.escape(entry) for entry in entries]
274
428
 
275
429
    def stat(self, relpath):
276
430
        """Return the stat information for a file.