~bzr-pqm/bzr/bzr.dev

1442.1.44 by Robert Collins
Many transport related tweaks:
1
# Copyright (C) 2005 Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
"""Implementation of Transport that uses memory for its storage."""
17
18
import os
19
import errno
20
from cStringIO import StringIO
21
22
from bzrlib.trace import mutter
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
23
from bzrlib.errors import TransportError, NoSuchFile, FileExists
24
from bzrlib.transport import Transport
1442.1.44 by Robert Collins
Many transport related tweaks:
25
26
class MemoryStat(object):
27
28
    def __init__(self, size):
29
        self.st_size = size
30
31
32
class MemoryTransport(Transport):
33
    """This is the transport agent for local filesystem access."""
34
35
    def __init__(self):
36
        """Set the 'base' path where files will be stored."""
37
        super(MemoryTransport, self).__init__('in-memory:')
38
        self._dirs = set()
39
        self._files = {}
40
41
    def clone(self, offset=None):
42
        """See Transport.clone()."""
43
        return self
44
45
    def abspath(self, relpath):
46
        """See Transport.abspath()."""
47
        return self.base + relpath
48
49
    def append(self, relpath, f):
50
        """See Transport.append()."""
51
        self._check_parent(relpath)
52
        self._files[relpath] = self._files.get(relpath, "") + f.read()
53
54
    def _check_parent(self, relpath):
55
        dir = os.path.dirname(relpath)
56
        if dir != '':
57
            if not dir in self._dirs:
58
                raise NoSuchFile(relpath)
59
60
    def has(self, relpath):
61
        """See Transport.has()."""
62
        return relpath in self._files
63
64
    def get(self, relpath):
65
        """See Transport.get()."""
66
        if not relpath in self._files:
67
            raise NoSuchFile(relpath)
68
        return StringIO(self._files[relpath])
69
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
70
    def put(self, relpath, f, mode=None):
1442.1.44 by Robert Collins
Many transport related tweaks:
71
        """See Transport.put()."""
72
        self._check_parent(relpath)
73
        self._files[relpath] = f.read()
74
1185.58.4 by John Arbash Meinel
Added permission checking to Branch, and propogated that change into the stores.
75
    def mkdir(self, relpath, mode=None):
1442.1.44 by Robert Collins
Many transport related tweaks:
76
        """See Transport.mkdir()."""
77
        self._check_parent(relpath)
78
        if relpath in self._dirs:
79
            raise FileExists(relpath)
80
        self._dirs.add(relpath)
81
82
    def listable(self):
83
        """See Transport.listable."""
84
        return True
85
86
    def iter_files_recursive(self):
87
        return iter(self._files)
88
    
89
#    def list_dir(self, relpath):
90
#    TODO if needed
91
    
92
    def stat(self, relpath):
93
        """See Transport.stat()."""
94
        return MemoryStat(len(self._files[relpath]))
95
96
#    def lock_read(self, relpath):
97
#   TODO if needed
98
#
99
#    def lock_write(self, relpath):
100
#   TODO if needed