/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
23
from bzrlib.transport import Transport, \
24
    TransportError, NoSuchFile, FileExists
25
26
27
class MemoryStat(object):
28
29
    def __init__(self, size):
30
        self.st_size = size
31
32
33
class MemoryTransport(Transport):
34
    """This is the transport agent for local filesystem access."""
35
36
    def __init__(self):
37
        """Set the 'base' path where files will be stored."""
38
        super(MemoryTransport, self).__init__('in-memory:')
39
        self._dirs = set()
40
        self._files = {}
41
42
    def clone(self, offset=None):
43
        """See Transport.clone()."""
44
        return self
45
46
    def abspath(self, relpath):
47
        """See Transport.abspath()."""
48
        return self.base + relpath
49
50
    def append(self, relpath, f):
51
        """See Transport.append()."""
52
        self._check_parent(relpath)
53
        self._files[relpath] = self._files.get(relpath, "") + f.read()
54
55
    def _check_parent(self, relpath):
56
        dir = os.path.dirname(relpath)
57
        if dir != '':
58
            if not dir in self._dirs:
59
                raise NoSuchFile(relpath)
60
61
    def has(self, relpath):
62
        """See Transport.has()."""
63
        return relpath in self._files
64
65
    def get(self, relpath):
66
        """See Transport.get()."""
67
        if not relpath in self._files:
68
            raise NoSuchFile(relpath)
69
        return StringIO(self._files[relpath])
70
71
    def put(self, relpath, f):
72
        """See Transport.put()."""
73
        self._check_parent(relpath)
74
        self._files[relpath] = f.read()
75
76
    def mkdir(self, relpath):
77
        """See Transport.mkdir()."""
78
        self._check_parent(relpath)
79
        if relpath in self._dirs:
80
            raise FileExists(relpath)
81
        self._dirs.add(relpath)
82
83
    def listable(self):
84
        """See Transport.listable."""
85
        return True
86
87
    def iter_files_recursive(self):
88
        return iter(self._files)
89
    
90
#    def list_dir(self, relpath):
91
#    TODO if needed
92
    
93
    def stat(self, relpath):
94
        """See Transport.stat()."""
95
        return MemoryStat(len(self._files[relpath]))
96
97
#    def lock_read(self, relpath):
98
#   TODO if needed
99
#
100
#    def lock_write(self, relpath):
101
#   TODO if needed