~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: Martin Pool
  • Date: 2005-06-06 11:53:29 UTC
  • Revision ID: mbp@sourcefrog.net-20050606115329-1596352add25bffd
- merge aaron's updated merge/pull code

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
# -*- coding: UTF-8 -*-
 
1
 
3
2
 
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
35
34
    pass
36
35
 
37
36
 
38
 
class ImmutableStore:
 
37
class ImmutableStore(object):
39
38
    """Store that holds files indexed by unique names.
40
39
 
41
40
    Files can be added, but not modified once they are in.  Typically
58
57
    >>> st['123123'].read()
59
58
    'goodbye'
60
59
 
61
 
    :todo: Atomic add by writing to a temporary file and renaming.
 
60
    TODO: Atomic add by writing to a temporary file and renaming.
62
61
 
63
 
    :todo: Perhaps automatically transform to/from XML in a method?
 
62
    TODO: Perhaps automatically transform to/from XML in a method?
64
63
           Would just need to tell the constructor what class to
65
64
           use...
66
65
 
67
 
    :todo: Even within a simple disk store like this, we could
 
66
    TODO: Even within a simple disk store like this, we could
68
67
           gzip the files.  But since many are less than one disk
69
68
           block, that might not help a lot.
70
69
 
75
74
        self._basedir = basedir
76
75
 
77
76
    def _path(self, id):
 
77
        assert '/' not in id
78
78
        return os.path.join(self._basedir, id)
79
79
 
80
80
    def __repr__(self):
83
83
    def add(self, f, fileid, compressed=True):
84
84
        """Add contents of a file into the store.
85
85
 
86
 
        :param f: An open file, or file-like object."""
 
86
        f -- An open file, or file-like object."""
87
87
        # FIXME: Only works on smallish files
88
88
        # TODO: Can be optimized by copying at the same time as
89
89
        # computing the sum.
107
107
        f.write(content)
108
108
        f.close()
109
109
 
 
110
    def copy_multi(self, other, ids):
 
111
        """Copy texts for ids from other into self.
 
112
 
 
113
        If an id is present in self, it is skipped.  A count of copied
 
114
        ids is returned, which may be less than len(ids).
 
115
        """
 
116
        count = 0
 
117
        for id in ids:
 
118
            if id in self:
 
119
                continue
 
120
            self.add(other[id], id)
 
121
            count += 1
 
122
        return count
110
123
 
111
124
    def __contains__(self, fileid):
112
125
        """"""
169
182
 
170
183
    def __del__(self):
171
184
        for f in os.listdir(self._basedir):
172
 
            os.remove(os.path.join(self._basedir, f))
 
185
            fpath = os.path.join(self._basedir, f)
 
186
            # needed on windows, and maybe some other filesystems
 
187
            os.chmod(fpath, 0600)
 
188
            os.remove(fpath)
173
189
        os.rmdir(self._basedir)
174
190
        mutter("%r destroyed" % self)