~bzr/ubuntu/karmic/dulwich/bzr-ppa

« back to all changes in this revision

Viewing changes to dulwich/pack.py

  • Committer: Max Bowsher
  • Date: 2011-04-13 01:50:24 UTC
  • mfrom: (419.1.20 lucid)
  • Revision ID: maxb@f2s.com-20110413015024-ply0macbxppq7ag1
Tags: 0.7.1-1~bazaar1~karmic1
MergeĀ 0.7.1-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
    imap,
48
48
    izip,
49
49
    )
50
 
import mmap
 
50
try:
 
51
    import mmap
 
52
except ImportError:
 
53
    has_mmap = False
 
54
else:
 
55
    has_mmap = True
51
56
import os
52
57
import struct
53
58
try:
163
168
        fd = f.fileno()
164
169
        if size is None:
165
170
            size = os.fstat(fd).st_size
166
 
        try:
167
 
            contents = mmap.mmap(fd, size, access=mmap.ACCESS_READ)
168
 
        except mmap.error:
169
 
            # Perhaps a socket?
170
 
            pass
171
 
        else:
172
 
            return contents, size
 
171
        if has_mmap:
 
172
            try:
 
173
                contents = mmap.mmap(fd, size, access=mmap.ACCESS_READ)
 
174
            except mmap.error:
 
175
                # Perhaps a socket?
 
176
                pass
 
177
            else:
 
178
                return contents, size
173
179
    contents = f.read()
174
180
    size = len(contents)
175
181
    return contents, size
1388
1394
 
1389
1395
    @classmethod
1390
1396
    def from_lazy_objects(self, data_fn, idx_fn):
1391
 
        """Create a new pack object from callables to load pack data and 
 
1397
        """Create a new pack object from callables to load pack data and
1392
1398
        index objects."""
1393
1399
        ret = Pack("")
1394
1400
        ret._data_load = data_fn
1492
1498
            yield ShaFile.from_raw_chunks(
1493
1499
              *self.data.resolve_object(offset, type, obj))
1494
1500
 
 
1501
    def keep(self, msg=None):
 
1502
        """Add a .keep file for the pack, preventing git from garbage collecting it.
 
1503
 
 
1504
        :param msg: A message written inside the .keep file; can be used later to
 
1505
                    determine whether or not a .keep file is obsolete.
 
1506
        :return: The path of the .keep file, as a string.
 
1507
        """
 
1508
        keepfile_name = '%s.keep' % self._basename
 
1509
        keepfile = GitFile(keepfile_name, 'wb')
 
1510
        try:
 
1511
            if msg:
 
1512
                keepfile.write(msg)
 
1513
                keepfile.write('\n')
 
1514
        finally:
 
1515
            keepfile.close()
 
1516
        return keepfile_name
 
1517
 
1495
1518
 
1496
1519
try:
1497
1520
    from dulwich._pack import apply_delta, bisect_find_sha