~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-29 08:01:30 UTC
  • Revision ID: mbp@sourcefrog.net-20050329080130-89b78af279b196b3
match ignore patterns like ./config.h

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
 
1
#! /usr/bin/env python
 
2
# -*- coding: UTF-8 -*-
2
3
 
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
34
35
    pass
35
36
 
36
37
 
37
 
class ImmutableStore(object):
 
38
class ImmutableStore:
38
39
    """Store that holds files indexed by unique names.
39
40
 
40
41
    Files can be added, but not modified once they are in.  Typically
57
58
    >>> st['123123'].read()
58
59
    'goodbye'
59
60
 
60
 
    TODO: Atomic add by writing to a temporary file and renaming.
 
61
    :todo: Atomic add by writing to a temporary file and renaming.
61
62
 
62
 
    TODO: Perhaps automatically transform to/from XML in a method?
 
63
    :todo: Perhaps automatically transform to/from XML in a method?
63
64
           Would just need to tell the constructor what class to
64
65
           use...
65
66
 
66
 
    TODO: Even within a simple disk store like this, we could
 
67
    :todo: Even within a simple disk store like this, we could
67
68
           gzip the files.  But since many are less than one disk
68
69
           block, that might not help a lot.
69
70
 
74
75
        self._basedir = basedir
75
76
 
76
77
    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
 
        f -- An open file, or file-like object."""
 
86
        :param 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.
95
95
 
96
96
        p = self._path(fileid)
97
97
        if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
98
 
            raise BzrError("store %r already contains id %r" % (self._basedir, fileid))
 
98
            bailout("store %r already contains id %r" % (self._basedir, fileid))
99
99
 
100
100
        if compressed:
101
101
            f = gzip.GzipFile(p + '.gz', 'wb')
108
108
        f.close()
109
109
 
110
110
 
111
 
    def copy_multi(self, other, ids):
112
 
        """Copy texts for ids from other into self.
113
 
 
114
 
        If an id is present in self, it is skipped.  A count of copied
115
 
        ids is returned, which may be less than len(ids).
116
 
        """
117
 
        from bzrlib.progress import ProgressBar
118
 
        pb = ProgressBar()
119
 
        pb.update('preparing to copy')
120
 
        to_copy = [id for id in ids if id not in self]
121
 
        count = 0
122
 
        for id in to_copy:
123
 
            count += 1
124
 
            pb.update('copy', count, len(to_copy))
125
 
            self.add(other[id], id)
126
 
        assert count == len(to_copy)
127
 
        pb.clear()
128
 
        return count
129
 
    
130
 
 
131
111
    def __contains__(self, fileid):
132
112
        """"""
133
113
        p = self._path(fileid)
189
169
 
190
170
    def __del__(self):
191
171
        for f in os.listdir(self._basedir):
192
 
            fpath = os.path.join(self._basedir, f)
193
 
            # needed on windows, and maybe some other filesystems
194
 
            os.chmod(fpath, 0600)
195
 
            os.remove(fpath)
 
172
            os.remove(os.path.join(self._basedir, f))
196
173
        os.rmdir(self._basedir)
197
174
        mutter("%r destroyed" % self)