]> arthur.ath.cx Git - bup.git/blobdiff - lib/bup/cmd/margin.py
Fully (and explicitly) close PackIdxLists
[bup.git] / lib / bup / cmd / margin.py
index 07f2b0f7780eff21f173284cf6cde2e1b68b897c..7836c7198a59819a0a916d9e9cb75a9c3dc576c6 100755 (executable)
@@ -24,44 +24,46 @@ def main(argv):
 
     git.check_repo_or_die()
 
-    mi = git.PackIdxList(git.repo(b'objects/pack'), ignore_midx=opt.ignore_midx)
+    with git.PackIdxList(git.repo(b'objects/pack'),
+                         ignore_midx=opt.ignore_midx) as mi:
 
-    def do_predict(ix, out):
-        total = len(ix)
-        maxdiff = 0
-        for count,i in enumerate(ix):
-            prefix = struct.unpack('!Q', i[:8])[0]
-            expected = prefix * total // (1 << 64)
-            diff = count - expected
-            maxdiff = max(maxdiff, abs(diff))
-        out.write(b'%d of %d (%.3f%%) '
-                  % (maxdiff, len(ix), maxdiff * 100.0 / len(ix)))
-        out.flush()
-        assert(count+1 == len(ix))
+        def do_predict(ix, out):
+            total = len(ix)
+            maxdiff = 0
+            for count,i in enumerate(ix):
+                prefix = struct.unpack('!Q', i[:8])[0]
+                expected = prefix * total // (1 << 64)
+                diff = count - expected
+                maxdiff = max(maxdiff, abs(diff))
+            out.write(b'%d of %d (%.3f%%) '
+                      % (maxdiff, len(ix), maxdiff * 100.0 / len(ix)))
+            out.flush()
+            assert(count+1 == len(ix))
 
-    sys.stdout.flush()
-    out = byte_stream(sys.stdout)
+        sys.stdout.flush()
+        out = byte_stream(sys.stdout)
 
-    if opt.predict:
-        if opt.ignore_midx:
-            for pack in mi.packs:
-                do_predict(pack, out)
+        if opt.predict:
+            if opt.ignore_midx:
+                for pack in mi.packs:
+                    do_predict(pack, out)
+            else:
+                do_predict(mi, out)
         else:
-            do_predict(mi, out)
-    else:
-        # default mode: find longest matching prefix
-        last = b'\0'*20
-        longmatch = 0
-        for i in mi:
-            if i == last:
-                continue
-            #assert(str(i) >= last)
-            pm = _helpers.bitmatch(last, i)
-            longmatch = max(longmatch, pm)
-            last = i
-        out.write(b'%d\n' % longmatch)
-        log('%d matching prefix bits\n' % longmatch)
-        doublings = math.log(len(mi), 2)
+            # default mode: find longest matching prefix
+            last = b'\0'*20
+            longmatch = 0
+            for i in mi:
+                if i == last:
+                    continue
+                #assert(str(i) >= last)
+                pm = _helpers.bitmatch(last, i)
+                longmatch = max(longmatch, pm)
+                last = i
+            out.write(b'%d\n' % longmatch)
+            log('%d matching prefix bits\n' % longmatch)
+            doublings = math.log(len(mi), 2)
+
         bpd = longmatch / doublings
         log('%.2f bits per doubling\n' % bpd)
         remain = 160 - longmatch