~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/local.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-06-30 18:28:17 UTC
  • mfrom: (5967.10.2 test-cat)
  • Revision ID: pqm@pqm.ubuntu.com-20110630182817-83a5q9r9rxfkdn8r
(mbp) don't use subprocesses for testing cat (Martin Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
This is a fairly thin wrapper on regular file IO.
20
20
"""
21
21
 
22
 
from __future__ import absolute_import
23
 
 
24
22
import os
25
23
from stat import ST_MODE, S_ISDIR, S_IMODE
26
24
import sys
52
50
    def __init__(self, base):
53
51
        """Set the base path where files will be stored."""
54
52
        if not base.startswith('file://'):
55
 
            raise AssertionError("not a file:// url: %r" % base)
 
53
            symbol_versioning.warn(
 
54
                "Instantiating LocalTransport with a filesystem path"
 
55
                " is deprecated as of bzr 0.8."
 
56
                " Please use bzrlib.transport.get_transport()"
 
57
                " or pass in a file:// url.",
 
58
                 DeprecationWarning,
 
59
                 stacklevel=2
 
60
                 )
 
61
            base = urlutils.local_path_to_url(base)
56
62
        if base[-1] != '/':
57
63
            base = base + '/'
58
64
 
66
72
 
67
73
        super(LocalTransport, self).__init__(base)
68
74
        self._local_base = urlutils.local_path_from_url(base)
69
 
        if self._local_base[-1] != '/':
70
 
            self._local_base = self._local_base + '/'
71
75
 
72
76
    def clone(self, offset=None):
73
77
        """Return a new LocalTransport with root at self.base + offset
140
144
        if abspath is None:
141
145
            abspath = u'.'
142
146
 
143
 
        return urlutils.file_relpath(self.base, abspath)
 
147
        return urlutils.file_relpath(
 
148
            urlutils.strip_trailing_slash(self.base),
 
149
            urlutils.strip_trailing_slash(abspath))
144
150
 
145
151
    def has(self, relpath):
146
152
        return os.access(self._abspath(relpath), os.F_OK)
249
255
            if mode is not None and mode != S_IMODE(st.st_mode):
250
256
                # Because of umask, we may still need to chmod the file.
251
257
                # But in the general case, we won't have to
252
 
                osutils.chmod_if_possible(abspath, mode)
 
258
                os.chmod(abspath, mode)
253
259
            writer(fd)
254
260
        finally:
255
261
            os.close(fd)
308
314
            local_mode = mode
309
315
        try:
310
316
            os.mkdir(abspath, local_mode)
 
317
            if mode is not None:
 
318
                # It is probably faster to just do the chmod, rather than
 
319
                # doing a stat, and then trying to compare
 
320
                os.chmod(abspath, mode)
311
321
        except (IOError, OSError),e:
312
322
            self._translate_error(e, abspath)
313
 
        if mode is not None:
314
 
            try:
315
 
                osutils.chmod_if_possible(abspath, mode)
316
 
            except (IOError, OSError), e:
317
 
                self._translate_error(e, abspath)
318
323
 
319
324
    def mkdir(self, relpath, mode=None):
320
325
        """Create a directory at the given path."""
322
327
 
323
328
    def open_write_stream(self, relpath, mode=None):
324
329
        """See Transport.open_write_stream."""
 
330
        # initialise the file
 
331
        self.put_bytes_non_atomic(relpath, "", mode=mode)
325
332
        abspath = self._abspath(relpath)
326
 
        try:
327
 
            handle = osutils.open_file(abspath, 'wb')
328
 
        except (IOError, OSError),e:
329
 
            self._translate_error(e, abspath)
330
 
        handle.truncate()
 
333
        handle = osutils.open_file(abspath, 'wb')
331
334
        if mode is not None:
332
335
            self._check_mode_and_size(abspath, handle.fileno(), mode)
333
336
        transport._file_streams[self.abspath(relpath)] = handle
352
355
        if mode is not None and mode != S_IMODE(st.st_mode):
353
356
            # Because of umask, we may still need to chmod the file.
354
357
            # But in the general case, we won't have to
355
 
            osutils.chmod_if_possible(file_abspath, mode)
 
358
            os.chmod(file_abspath, mode)
356
359
        return st.st_size
357
360
 
358
361
    def append_file(self, relpath, f, mode=None):
451
454
                    otherpath = other._abspath(path)
452
455
                    shutil.copy(mypath, otherpath)
453
456
                    if mode is not None:
454
 
                        osutils.chmod_if_possible(otherpath, mode)
 
457
                        os.chmod(otherpath, mode)
455
458
                except (IOError, OSError),e:
456
459
                    self._translate_error(e, path)
457
460
                count += 1
533
536
            """See Transport.symlink."""
534
537
            abs_link_dirpath = urlutils.dirname(self.abspath(link_name))
535
538
            source_rel = urlutils.file_relpath(
536
 
                abs_link_dirpath, self.abspath(source))
 
539
                urlutils.strip_trailing_slash(abs_link_dirpath),
 
540
                urlutils.strip_trailing_slash(self.abspath(source))
 
541
            )
537
542
 
538
543
            try:
539
544
                os.symlink(source_rel, self._abspath(link_name))
558
563
        self._local_base = urlutils._win32_local_path_from_url(base)
559
564
 
560
565
    def abspath(self, relpath):
561
 
        path = osutils._win32_normpath(osutils.pathjoin(
 
566
        path = osutils.normpath(osutils.pathjoin(
562
567
                    self._local_base, urlutils.unescape(relpath)))
563
568
        return urlutils._win32_local_path_to_url(path)
564
569