~bzr-pqm/bzr/bzr.dev

907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1
#!/usr/bin/env python
2
"""\
3
An implementation of the Transport object for local
4
filesystem access.
5
"""
6
7
from bzrlib.transport import Transport, protocol_handlers
8
import os
9
10
class LocalTransport(Transport):
11
    """This is the transport agent for local filesystem access."""
12
13
    def __init__(self, base):
14
        """Set the base path where files will be stored."""
15
        from os.path import realpath
16
        super(LocalTransport, self).__init__(realpath(base))
17
18
    def _join(self, relpath):
19
        return os.path.join(self.base, relpath)
20
21
    def has(self, relpath):
22
        return os.access(self._join(relpath), os.F_OK)
23
24
    def get(self, relpath):
25
        """Get the file at the given relative path.
26
        """
27
        return open(self._join(relpath), 'rb')
28
29
    def put(self, relpath, f):
30
        """Copy the file-like object into the location.
31
        """
32
        from bzrlib.atomicfile import AtomicFile
33
34
        fp = AtomicFile(self._join(relpath), 'wb')
35
        self._pump(f, fp)
36
        fp.commit()
37
38
    def append(self, relpath, f):
39
        """Append the text in the file-like object into the final
40
        location.
41
        """
42
        fp = open(self._join(relpath), 'a+b')
43
        self._pump(f, fp)
44
45
    def copy(self, rel_from, rel_to):
46
        """Copy the item at rel_from to the location at rel_to"""
47
        import shutil
48
        path_from = self._join(rel_from)
49
        path_to = self._join(rel_to)
50
        shutil.copy(path_from, path_to)
51
52
    def move(self, rel_from, rel_to):
53
        """Move the item at rel_from to the location at rel_to"""
54
        path_from = self._join(rel_from)
55
        path_to = self._join(rel_to)
56
57
        os.rename(path_from, path_to)
58
59
    def delete(self, relpath):
60
        """Delete the item at relpath"""
61
        os.remove(self._join(relpath))
62
63
    def async_get(self, relpath):
64
        """Make a request for an file at the given location, but
65
        don't worry about actually getting it yet.
66
67
        :rtype: AsyncFile
68
        """
69
        raise NotImplementedError
70
71
# If nothing else matches, try the LocalTransport
72
protocol_handlers[None] = LocalTransport
73