2 import sys, os, subprocess, signal
6 exepath = os.path.split(exe)[0] or '.'
8 # fix the PYTHONPATH to include our lib dir
9 libpath = os.path.join(exepath, 'lib')
10 cmdpath = os.path.join(exepath, 'cmd')
11 sys.path[:0] = [libpath]
12 os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '')
14 from bup.helpers import *
17 def columnate(l, prefix):
19 clen = max(len(s) for s in l)
20 ncols = (78 - len(prefix)) / (clen + 2)
28 for s in range(0, len(l), rows):
29 cols.append(l[s:s+rows])
30 for row in zip(*cols):
31 print prefix + ''.join(('%-*s' % (clen+2, s)) for s in row)
35 log('Usage: bup <command> <options...>\n\n')
37 ftp = 'Browse backup sets using an ftp-like client',
38 fsck = 'Check backup sets for damage and add redundancy information',
39 fuse = 'Mount your backup sets as a filesystem',
40 index = 'Create or display the index of files to back up',
41 join = 'The reverse operation to "bup split"',
42 ls = 'Browse the files in your backup sets',
43 midx = 'Index objects to speed up future backups',
44 save = 'Save files into a backup set (note: run "bup index" first)',
45 split = 'Split a single file into its own backup set',
48 log('Common commands:\n')
49 for cmd,synopsis in sorted(common.items()):
50 print ' %-10s %s' % (cmd, synopsis)
53 log('Other available commands:\n')
55 for c in sorted(os.listdir(cmdpath) + os.listdir(exepath)):
56 if c.startswith('bup-') and c.find('.') < 0:
58 if cname not in common:
63 log("See 'bup help <command>' for more information on " +
64 "a specific command.\n")
68 if len(argv) < 2 or not argv[1] or argv[1][0] == '-':
74 sp = os.path.join(exepath, 'bup-%s' % s)
75 if not os.path.exists(sp):
76 sp = os.path.join(cmdpath, 'bup-%s' % s)
79 if not os.path.exists(subpath(subcmd)):
80 log('error: unknown command "%s"\n' % subcmd)
84 already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
85 if subcmd in ['ftp', 'help']:
87 fix_stdout = not already_fixed and os.isatty(1)
88 fix_stderr = not already_fixed and os.isatty(2)
91 if fix_stdout or fix_stderr:
92 os.environ['BUP_FORCE_TTY'] = '1'
94 if fix_stdout or fix_stderr:
95 realf = fix_stderr and 2 or 1
96 n = subprocess.Popen([subpath('newliner')],
97 stdin=subprocess.PIPE, stdout=os.dup(realf),
98 close_fds=True, preexec_fn=force_tty)
99 outf = fix_stdout and n.stdin.fileno() or 1
100 errf = fix_stderr and n.stdin.fileno() or 2
107 class SigException(Exception):
109 def handler(signum, frame):
110 raise SigException('signal %d received' % signum)
112 signal.signal(signal.SIGTERM, handler)
113 signal.signal(signal.SIGINT, handler)
118 p = subprocess.Popen([subpath(subcmd)] + argv[2:],
119 stdout=outf, stderr=errf, preexec_fn=force_tty)
122 log('%s: %s\n' % (subpath(subcmd), e))
124 except SigException, e:
127 if p and p.poll() == None:
128 os.kill(p.pid, signal.SIGTERM)