]> arthur.barton.de Git - bup.git/commitdiff
margin: convert to internal command
authorRob Browning <rlb@defaultvalue.org>
Mon, 8 Feb 2021 07:17:09 +0000 (01:17 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sat, 6 Mar 2021 18:29:38 +0000 (12:29 -0600)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/cmd/margin.py
lib/cmd/bup

index 7bb2b493bfd02cf193d1d066846131c532b0341e..07f2b0f7780eff21f173284cf6cde2e1b68b897c 100755 (executable)
@@ -1,28 +1,8 @@
-#!/bin/sh
-"""": # -*-python-*-
-# https://sourceware.org/bugzilla/show_bug.cgi?id=26034
-export "BUP_ARGV_0"="$0"
-arg_i=1
-for arg in "$@"; do
-    export "BUP_ARGV_${arg_i}"="$arg"
-    shift
-    arg_i=$((arg_i + 1))
-done
-# Here to end of preamble replaced during install
-bup_python="$(dirname "$0")/../../../config/bin/python" || exit $?
-exec "$bup_python" "$0"
-"""
-# end of bup preamble
 
 from __future__ import absolute_import
+import math, struct, sys
 
-# Intentionally replace the dirname "$0" that python prepends
-import os, sys
-sys.path[0] = os.path.dirname(os.path.realpath(__file__)) + '/../..'
-
-import math, struct
-
-from bup import compat, options, git, _helpers
+from bup import options, git, _helpers
 from bup.helpers import log
 from bup.io import byte_stream
 
@@ -34,60 +14,62 @@ bup margin
 predict    Guess object offsets and report the maximum deviation
 ignore-midx  Don't use midx files; use only plain pack idx files.
 """
-o = options.Options(optspec)
-opt, flags, extra = o.parse(compat.argv[1:])
 
-if extra:
-    o.fatal("no arguments expected")
+def main(argv):
+    o = options.Options(optspec)
+    opt, flags, extra = o.parse_bytes(argv[1:])
+
+    if extra:
+        o.fatal("no arguments expected")
 
-git.check_repo_or_die()
+    git.check_repo_or_die()
 
-mi = git.PackIdxList(git.repo(b'objects/pack'), ignore_midx=opt.ignore_midx)
+    mi = git.PackIdxList(git.repo(b'objects/pack'), ignore_midx=opt.ignore_midx)
 
-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)
-    bpd = longmatch / doublings
-    log('%.2f bits per doubling\n' % bpd)
-    remain = 160 - longmatch
-    rdoublings = remain / bpd
-    log('%d bits (%.2f doublings) remaining\n' % (remain, rdoublings))
-    larger = 2**rdoublings
-    log('%g times larger is possible\n' % larger)
-    perperson = larger/POPULATION_OF_EARTH
-    log('\nEveryone on earth could have %d data sets like yours, all in one\n'
-        'repository, and we would expect 1 object collision.\n'
-        % int(perperson))
+        # 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
+        rdoublings = remain / bpd
+        log('%d bits (%.2f doublings) remaining\n' % (remain, rdoublings))
+        larger = 2**rdoublings
+        log('%g times larger is possible\n' % larger)
+        perperson = larger/POPULATION_OF_EARTH
+        log('\nEveryone on earth could have %d data sets like yours, all in one\n'
+            'repository, and we would expect 1 object collision.\n'
+            % int(perperson))
index 1add47d7508ca1974478af2cc18967e35e032203..b4b11802d39c48856d8c9c36026e0ed214fff83b 100755 (executable)
@@ -194,6 +194,7 @@ try:
                            b'init',
                            b'join',
                            b'ls',
+                           b'margin',
                            b'meta',
                            b'rm',
                            b'split',