~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: mbp at sourcefrog
  • Date: 2005-04-09 06:21:44 UTC
  • Revision ID: mbp@sourcefrog.net-20050409062144-e47a4b64106e4c21af99beaf
debugĀ output

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
23
22
__copyright__ = "Copyright (C) 2005 Canonical Ltd."
24
23
__author__ = "Martin Pool <mbp@canonical.com>"
25
24
 
26
 
import os, tempfile, types, osutils
 
25
import os, tempfile, types, osutils, gzip, errno
27
26
from stat import ST_SIZE
28
27
from StringIO import StringIO
29
28
from trace import mutter
30
29
 
31
 
 
32
30
######################################################################
33
31
# stores
34
32
 
81
79
    def __repr__(self):
82
80
        return "%s(%r)" % (self.__class__.__name__, self._basedir)
83
81
 
84
 
    def add(self, f, fileid):
 
82
    def add(self, f, fileid, compressed=True):
85
83
        """Add contents of a file into the store.
86
84
 
87
85
        :param f: An open file, or file-like object."""
93
91
            content = f
94
92
        else:
95
93
            content = f.read()
96
 
        if fileid not in self:
97
 
            filename = self._path(fileid)
98
 
            f = file(filename, 'wb')
99
 
            f.write(content)
100
 
            ## f.flush()
101
 
            ## os.fsync(f.fileno())
102
 
            f.close()
103
 
            osutils.make_readonly(filename)
 
94
 
 
95
        p = self._path(fileid)
 
96
        if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
 
97
            bailout("store %r already contains id %r" % (self._basedir, fileid))
 
98
 
 
99
        if compressed:
 
100
            f = gzip.GzipFile(p + '.gz', 'wb')
 
101
            os.chmod(p + '.gz', 0444)
 
102
        else:
 
103
            f = file(p, 'wb')
 
104
            os.chmod(p, 0444)
 
105
            
 
106
        f.write(content)
 
107
        f.close()
104
108
 
105
109
 
106
110
    def __contains__(self, fileid):
107
111
        """"""
108
 
        return os.access(self._path(fileid), os.R_OK)
 
112
        p = self._path(fileid)
 
113
        return (os.access(p, os.R_OK)
 
114
                or os.access(p + '.gz', os.R_OK))
109
115
 
 
116
    # TODO: Guard against the same thing being stored twice, compressed and uncompresse
110
117
 
111
118
    def __iter__(self):
112
 
        return iter(os.listdir(self._basedir))
 
119
        for f in os.listdir(self._basedir):
 
120
            if f[-3:] == '.gz':
 
121
                # TODO: case-insensitive?
 
122
                yield f[:-3]
 
123
            else:
 
124
                yield f
113
125
 
114
126
    def __len__(self):
115
127
        return len(os.listdir(self._basedir))
116
128
 
117
129
    def __getitem__(self, fileid):
118
130
        """Returns a file reading from a particular entry."""
119
 
        return file(self._path(fileid), 'rb')
 
131
        p = self._path(fileid)
 
132
        try:
 
133
            return gzip.GzipFile(p + '.gz', 'rb')
 
134
        except IOError, e:
 
135
            if e.errno == errno.ENOENT:
 
136
                return file(p, 'rb')
 
137
            else:
 
138
                raise e
120
139
 
121
140
    def total_size(self):
122
 
        """Return (count, bytes)"""
 
141
        """Return (count, bytes)
 
142
 
 
143
        This is the (compressed) size stored on disk, not the size of
 
144
        the content."""
123
145
        total = 0
124
146
        count = 0
125
147
        for fid in self:
126
148
            count += 1
127
 
            total += os.stat(self._path(fid))[ST_SIZE]
 
149
            p = self._path(fid)
 
150
            try:
 
151
                total += os.stat(p)[ST_SIZE]
 
152
            except OSError:
 
153
                total += os.stat(p + '.gz')[ST_SIZE]
 
154
                
128
155
        return count, total
129
156
 
130
 
    def delete_all(self):
131
 
        for fileid in self:
132
 
            self.delete(fileid)
133
 
 
134
 
    def delete(self, fileid):
135
 
        """Remove nominated store entry.
136
 
 
137
 
        Most stores will be add-only."""
138
 
        filename = self._path(fileid)
139
 
        ## osutils.make_writable(filename)
140
 
        os.remove(filename)
141
 
 
142
 
    def destroy(self):
143
 
        """Remove store; only allowed if it is empty."""
144
 
        os.rmdir(self._basedir)
145
 
        mutter("%r destroyed" % self)
146
157
 
147
158
 
148
159
 
156
167
        ImmutableStore.__init__(self, tempfile.mkdtemp())
157
168
 
158
169
    def __del__(self):
159
 
        self.delete_all()
160
 
        self.destroy()
 
170
        for f in os.listdir(self._basedir):
 
171
            fpath = os.path.join(self._basedir, f)
 
172
            # needed on windows, and maybe some other filesystems
 
173
            os.chmod(fpath, 0600)
 
174
            os.remove(fpath)
 
175
        os.rmdir(self._basedir)
 
176
        mutter("%r destroyed" % self)