X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=bup.py;h=56f3afe20b6362de0f49f6d886d13b63706f5ce8;hb=073b383882e73904ae04179e69a68419d8d57199;hp=d15cc4893e564589f2a8697821f53f2815fe6142;hpb=9e4ef171d9e8a2f17205496cf0444db62edbd0f1;p=bup.git diff --git a/bup.py b/bup.py index d15cc48..56f3afe 100755 --- a/bup.py +++ b/bup.py @@ -1,21 +1,19 @@ #!/usr/bin/env python -import sys, os +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: %s \n\n' % exe) + log('Usage: bup \n\n') log('Available subcommands:\n') - for c in os.listdir(exepath): + for c in sorted(os.listdir(exepath)): if c.startswith('bup-') and c.find('.') < 0: log('\t%s\n' % c[4:]) - exit(99) - + sys.exit(99) if len(argv) < 2 or not argv[1] or argv[1][0] == '-': usage() @@ -24,14 +22,52 @@ subcmd = argv[1] 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): - log('%s: unknown command "%s"\n' % (exe, subcmd)) +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)) - 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)