#!/usr/bin/env python
-import sys, os, git
+import sys, os, subprocess
+import git
+from helpers import *
argv = sys.argv
exe = argv[0]
exepath = os.path.split(exe)[0] or '.'
-def log(s):
- sys.stderr.write(s)
-
def usage():
log('Usage: bup <subcmd> <options...>\n\n')
log('Available subcommands:\n')
if subcmd == 'help':
usage()
-subpath = os.path.join(exepath, 'bup-%s' % subcmd)
+def subpath(s):
+ return os.path.join(exepath, 'bup-%s' % s)
-if not os.path.exists(subpath):
+if not os.path.exists(subpath(subcmd)):
log('error: unknown command "%s"\n' % subcmd)
usage()
+
+already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
+if subcmd in ['ftp']:
+ already_fixed = True
+fix_stdout = not already_fixed and os.isatty(1)
+fix_stderr = not already_fixed and os.isatty(2)
+
+def force_tty():
+ if fix_stdout or fix_stderr:
+ os.environ['BUP_FORCE_TTY'] = '1'
+
+if fix_stdout or fix_stderr:
+ realf = fix_stderr and 2 or 1
+ n = subprocess.Popen([subpath('newliner')],
+ stdin=subprocess.PIPE, stdout=os.dup(realf),
+ close_fds=True, preexec_fn=force_tty)
+ outf = fix_stdout and n.stdin.fileno() or 1
+ errf = fix_stderr and n.stdin.fileno() or 2
+else:
+ n = None
+ outf = 1
+ errf = 2
+
+ret = 95
try:
- os.execv(subpath, [subpath] + argv[2:])
-except OSError, e:
- log('%s: %s\n' % (subpath, e))
- sys.exit(98)
+ try:
+ p = subprocess.Popen([subpath(subcmd)] + argv[2:],
+ stdout=outf, stderr=errf, preexec_fn=force_tty)
+ ret = p.wait()
+ except OSError, e:
+ log('%s: %s\n' % (subpath(subcmd), e))
+ ret = 98
+ except KeyboardInterrupt, e:
+ ret = 94
+finally:
+ if n:
+ n.stdin.close()
+ try:
+ n.wait()
+ except:
+ pass
+sys.exit(ret)
total = 0
for (path,pst) in drecurse.recursive_dirlist([top], xdev=opt.xdev):
if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
- sys.stdout.write('%-70s\n' % path)
+ sys.stdout.write('%s\n' % path)
sys.stdout.flush()
progress('Indexing: %d\r' % total)
elif not (total % 128):
--- /dev/null
+#!/usr/bin/env python
+import sys, os, re
+import options
+
+optspec = """
+bup newliner
+"""
+o = options.Options('bup newliner', optspec)
+(opt, flags, extra) = o.parse(sys.argv[1:])
+
+if extra:
+ o.fatal("no arguments expected")
+
+r = re.compile(r'([\r\n])')
+lastlen = 0
+all = ''
+while 1:
+ l = r.split(all, 1)
+ if len(l) <= 1:
+ try:
+ b = os.read(sys.stdin.fileno(), 4096)
+ except KeyboardInterrupt:
+ break
+ if not b:
+ break
+ all += b
+ else:
+ assert(len(l) == 3)
+ (line, splitchar, all) = l
+ #splitchar = '\n'
+ sys.stdout.write('%-*s%s' % (lastlen, line, splitchar))
+ if splitchar == '\r':
+ lastlen = len(line)
+ else:
+ lastlen = 0
+ sys.stdout.flush()
+
+if lastlen or all:
+ sys.stdout.write('%-*s\n' % (lastlen, all))
remainstr = '%dm%d' % (mins, secs)
else:
remainstr = '%ds' % secs
- progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s \r'
+ progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
% (pct, count/1024, total/1024, fcount, ftotal,
remainstr, kpsstr))