]> git.michaelhowe.org Git - packages/b/bup.git/commitdiff
bup random: fix progress output and don't print to a tty.
authorAvery Pennarun <apenwarr@gmail.com>
Tue, 2 Mar 2010 21:20:41 +0000 (16:20 -0500)
committerAvery Pennarun <apenwarr@gmail.com>
Tue, 2 Mar 2010 21:36:32 +0000 (16:36 -0500)
We were printing output using a series of dots, which interacted badly with
bup newliner (and for good reason).  Change it to actually display the
number of megabytes done so far.

Also, don't print random binary data to a tty unless -f is given.  It's
just more polite that way.

cmd/random-cmd.py
lib/bup/_hashsplit.c
main.py

index 91820a858441384c93d1651322658bbf673295e1..34035d57cda3ad7160f9af93ea23d2dae66f0c20 100755 (executable)
@@ -7,6 +7,7 @@ optspec = """
 bup random [-S seed] <numbytes>
 --
 S,seed=   optional random number seed (default 1)
+f,force   print random data to stdout even if it's a tty
 """
 o = options.Options('bup random', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
@@ -15,4 +16,10 @@ if len(extra) != 1:
     o.fatal("exactly one argument expected")
 
 total = parse_num(extra[0])
-_hashsplit.write_random(sys.stdout.fileno(), total, opt.seed or 0)
+
+if opt.force or (not os.isatty(1) and
+                 not atoi(os.environ.get('BUP_FORCE_TTY')) & 1):
+    _hashsplit.write_random(sys.stdout.fileno(), total, opt.seed or 0)
+else:
+    log('error: not writing binary data to a terminal. Use -f to force.\n')
+    sys.exit(1)
index e78f5972eb82704139af4eb8a60bdbc2ca4d0350..cde92017cc48d7896855bc1083293695423b3151 100644 (file)
@@ -108,7 +108,7 @@ static PyObject *write_random(PyObject *self, PyObject *args)
     
     srandom(seed);
     
-    for (kbytes = len/1024; kbytes > 0; kbytes--)
+    for (kbytes = 0; kbytes < len/1024; kbytes++)
     {
        int i;
        for (i = 0; i < sizeof(buf)/sizeof(buf[0]); i++)
@@ -119,10 +119,12 @@ static PyObject *write_random(PyObject *self, PyObject *args)
        written += ret;
        if (ret < sizeof(buf))
            break;
-       if (!(kbytes%1024))
-           fprintf(stderr, ".");
+       if (kbytes/1024 > 0 && !(kbytes%1024))
+           fprintf(stderr, "Random: %lld Mbytes\r", kbytes/1024);
     }
     
+    if (kbytes/1024 > 0)
+       fprintf(stderr, "Random: %lld Mbytes, done.\n", kbytes/1024);
     return Py_BuildValue("L", written);
 }
 
diff --git a/main.py b/main.py
index d5b4ccc64a6df4fa931273a124067d9d958a8525..579a1096ad2ca31f584e1a98c810a4657c42f680 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -89,7 +89,8 @@ fix_stderr = not already_fixed and os.isatty(2)
 
 def force_tty():
     if fix_stdout or fix_stderr:
-        os.environ['BUP_FORCE_TTY'] = '1'
+        amt = (fix_stdout and 1 or 0) + (fix_stderr and 2 or 0)
+        os.environ['BUP_FORCE_TTY'] = str(amt)
 
 if fix_stdout or fix_stderr:
     realf = fix_stderr and 2 or 1