]> arthur.barton.de Git - bup.git/blobdiff - cmd/midx-cmd.py
ls: move opt processing to opts_from_commandline
[bup.git] / cmd / midx-cmd.py
index 5c0c1266f60bd0a548ecca6babe13330857ac7d0..0224ab40238f3239c937d5b53f67e85a4802fa02 100755 (executable)
@@ -1,8 +1,17 @@
-#!/usr/bin/env python
-import sys, math, struct, glob, resource
-import tempfile
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
+
+import glob, math, os, resource, struct, sys, tempfile
+
 from bup import options, git, midx, _helpers, xstat
-from bup.helpers import *
+from bup.helpers import (Sha1, add_error, atomically_replaced_file, debug1, fdatasync,
+                         handle_ctrl_c, log, mmap_readwrite, qprogress,
+                         saved_errors, unlink)
+
 
 PAGE_SIZE=4096
 SHA_PER_PAGE=PAGE_SIZE/20.
@@ -41,7 +50,7 @@ def check_midx(name):
     log('Checking %s.\n' % nicename)
     try:
         ix = git.open_idx(name)
-    except git.GitError, e:
+    except git.GitError as e:
         add_error('%s: %s' % (name, e))
         return
     for count,subname in enumerate(ix.idxnames):
@@ -114,19 +123,21 @@ def _do_midx(outdir, outfilename, infilenames, prefixstr):
         debug1('midx: table size: %d (%d bits)\n' % (entries*4, bits))
 
         unlink(outfilename)
-        f = open(outfilename + '.tmp', 'w+b')
-        f.write('MIDX')
-        f.write(struct.pack('!II', midx.MIDX_VERSION, bits))
-        assert(f.tell() == 12)
+        with atomically_replaced_file(outfilename, 'wb') as f:
+            f.write('MIDX')
+            f.write(struct.pack('!II', midx.MIDX_VERSION, bits))
+            assert(f.tell() == 12)
 
-        f.truncate(12 + 4*entries + 20*total + 4*total)
-        f.flush()
-        fdatasync(f.fileno())
+            f.truncate(12 + 4*entries + 20*total + 4*total)
+            f.flush()
+            fdatasync(f.fileno())
 
-        fmap = mmap_readwrite(f, close=False)
+            fmap = mmap_readwrite(f, close=False)
 
-        count = merge_into(fmap, bits, total, inp)
-        del fmap # Assume this calls msync() now.
+            count = merge_into(fmap, bits, total, inp)
+            del fmap # Assume this calls msync() now.
+            f.seek(0, os.SEEK_END)
+            f.write('\0'.join(allfilenames))
     finally:
         for ix in midxs:
             if isinstance(ix, midx.PackMidx):
@@ -134,10 +145,6 @@ def _do_midx(outdir, outfilename, infilenames, prefixstr):
         midxs = None
         inp = None
 
-    f.seek(0, os.SEEK_END)
-    f.write('\0'.join(allfilenames))
-    f.close()
-    os.rename(outfilename + '.tmp', outfilename)
 
     # This is just for testing (if you enable this, don't clear inp above)
     if 0:
@@ -146,7 +153,7 @@ def _do_midx(outdir, outfilename, infilenames, prefixstr):
         print p.idxnames
         assert(len(p) == total)
         for pe, e in p, git.idxmerge(inp, final_progress=False):
-            pin = pi.next()
+            pin = next(pi)
             assert(i == pin)
             assert(p.exists(i))
 
@@ -159,7 +166,7 @@ def do_midx(outdir, outfilename, infilenames, prefixstr):
         print rv[1]
 
 
-def do_midx_dir(path):
+def do_midx_dir(path, outfilename):
     already = {}
     sizes = {}
     if opt.force and not opt.auto:
@@ -208,7 +215,7 @@ def do_midx_dir(path):
         all.sort()
         part1 = [name for sz,name in all[:len(all)-DESIRED_LWM+1]]
         part2 = all[len(all)-DESIRED_LWM+1:]
-        all = list(do_midx_group(path, part1)) + part2
+        all = list(do_midx_group(path, outfilename, part1)) + part2
         if len(all) > DESIRED_HWM:
             debug1('\nStill too many indexes (%d > %d).  Merging again.\n'
                    % (len(all), DESIRED_HWM))
@@ -219,13 +226,13 @@ def do_midx_dir(path):
                 print name
 
 
-def do_midx_group(outdir, infiles):
+def do_midx_group(outdir, outfilename, infiles):
     groups = list(_group(infiles, opt.max_files))
     gprefix = ''
     for n,sublist in enumerate(groups):
         if len(groups) != 1:
             gprefix = 'Group %d: ' % (n+1)
-        rv = _do_midx(path, None, sublist, gprefix)
+        rv = _do_midx(outdir, outfilename, sublist, gprefix)
         if rv:
             yield rv
 
@@ -267,7 +274,7 @@ else:
         paths = opt.dir and [opt.dir] or git.all_packdirs()
         for path in paths:
             debug1('midx: scanning %s\n' % path)
-            do_midx_dir(path)
+            do_midx_dir(path, opt.output)
     else:
         o.fatal("you must use -f or -a or provide input filenames")