~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/local.py

  • Committer: Robert Collins
  • Date: 2005-10-17 11:41:07 UTC
  • mfrom: (1442.1.60)
  • Revision ID: robertc@robertcollins.net-20051017114107-f5586285d825c105
Merge in first part of GPG support.

This adds check_signatures config support, triams back the transport api
to be leaner and easier to hook in suffixes - non primary streams in the store
associated with the fileid that primary data is stored in, a gpg module which
will encapsulate all signing and checking operations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
"""Implementation of Transport for the local filesystem.
17
17
"""
18
18
 
 
19
import os
 
20
import errno
 
21
import shutil
 
22
from stat import ST_MODE, S_ISDIR, ST_SIZE
 
23
import tempfile
 
24
 
 
25
from bzrlib.trace import mutter
19
26
from bzrlib.transport import Transport, register_transport, \
20
27
    TransportError, NoSuchFile, FileExists
21
 
import os, errno
 
28
 
22
29
 
23
30
class LocalTransportError(TransportError):
24
31
    pass
25
32
 
 
33
 
26
34
class LocalTransport(Transport):
27
35
    """This is the transport agent for local filesystem access."""
28
36
 
78
86
                raise NoSuchFile('File or directory %r does not exist' % path, orig_error=e)
79
87
            raise LocalTransportError(orig_error=e)
80
88
 
81
 
    def get_partial(self, relpath, start, length=None):
82
 
        """Get just part of a file.
83
 
 
84
 
        :param relpath: Path to the file, relative to base
85
 
        :param start: The starting position to read from
86
 
        :param length: The length to read. A length of None indicates
87
 
                       read to the end of the file.
88
 
        :return: A file-like object containing at least the specified bytes.
89
 
                 Some implementations may return objects which can be read
90
 
                 past this length, but this is not guaranteed.
91
 
        """
92
 
        # LocalTransport.get_partial() doesn't care about the length
93
 
        # argument, because it is using a local file, and thus just
94
 
        # returns the file seek'ed to the appropriate location.
95
 
        try:
96
 
            path = self.abspath(relpath)
97
 
            f = open(path, 'rb')
98
 
            f.seek(start, 0)
99
 
            return f
100
 
        except IOError,e:
101
 
            if e.errno == errno.ENOENT:
102
 
                raise NoSuchFile('File %r does not exist' % path, orig_error=e)
103
 
            raise LocalTransportError(orig_error=e)
104
 
 
105
89
    def put(self, relpath, f):
106
90
        """Copy the file-like or string object into the location.
107
91
 
123
107
        finally:
124
108
            fp.close()
125
109
 
 
110
    def iter_files_recursive(self):
 
111
        """Iter the relative paths of files in the transports sub-tree."""
 
112
        queue = list(self.list_dir('.'))
 
113
        while queue:
 
114
            relpath = queue.pop(0)
 
115
            st = self.stat(relpath)
 
116
            if S_ISDIR(st[ST_MODE]):
 
117
                for i, basename in enumerate(self.list_dir(relpath)):
 
118
                    queue.insert(i, relpath+'/'+basename)
 
119
            else:
 
120
                yield relpath
 
121
 
126
122
    def mkdir(self, relpath):
127
123
        """Create a directory at the given path."""
128
124
        try:
227
223
        from bzrlib.lock import WriteLock
228
224
        return WriteLock(self.abspath(relpath))
229
225
 
 
226
 
 
227
class ScratchTransport(LocalTransport):
 
228
    """A transport that works in a temporary dir and cleans up after itself.
 
229
    
 
230
    The dir only exists for the lifetime of the Python object.
 
231
    Obviously you should not put anything precious in it.
 
232
    """
 
233
 
 
234
    def __init__(self, base=None):
 
235
        if base is None:
 
236
            base = tempfile.mkdtemp()
 
237
        super(ScratchTransport, self).__init__(base)
 
238
 
 
239
    def __del__(self):
 
240
        shutil.rmtree(self.base, ignore_errors=True)
 
241
        mutter("%r destroyed" % self)
 
242
 
230
243
# If nothing else matches, try the LocalTransport
231
244
register_transport(None, LocalTransport)
232
245
register_transport('file://', LocalTransport)