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', '')
13 os.environ['BUP_MAIN_EXE'] = os.path.abspath(exe)
15 from bup.helpers import *
18 def columnate(l, prefix):
20 clen = max(len(s) for s in l)
21 ncols = (78 - len(prefix)) / (clen + 2)
29 for s in range(0, len(l), rows):
30 cols.append(l[s:s+rows])
31 for row in zip(*cols):
32 print prefix + ''.join(('%-*s' % (clen+2, s)) for s in row)
36 log('Usage: bup <command> <options...>\n\n')
38 ftp = 'Browse backup sets using an ftp-like client',
39 fsck = 'Check backup sets for damage and add redundancy information',
40 fuse = 'Mount your backup sets as a filesystem',
41 help = 'Print detailed help for the given command',
42 index = 'Create or display the index of files to back up',
43 join = 'Retrieve a file backed up using "bup split"',
44 ls = 'Browse the files in your backup sets',
45 midx = 'Index objects to speed up future backups',
46 save = 'Save files into a backup set (note: run "bup index" first)',
47 split = 'Split a single file into its own backup set',
50 log('Common commands:\n')
51 for cmd,synopsis in sorted(common.items()):
52 print ' %-10s %s' % (cmd, synopsis)
55 log('Other available commands:\n')
57 for c in sorted(os.listdir(cmdpath) + os.listdir(exepath)):
58 if c.startswith('bup-') and c.find('.') < 0:
60 if cname not in common:
65 log("See 'bup help <command>' for more information on " +
66 "a specific command.\n")
70 if len(argv) < 2 or not argv[1] or argv[1][0] == '-':
76 sp = os.path.join(exepath, 'bup-%s' % s)
77 if not os.path.exists(sp):
78 sp = os.path.join(cmdpath, 'bup-%s' % s)
81 if not os.path.exists(subpath(subcmd)):
82 log('error: unknown command "%s"\n' % subcmd)
86 already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
87 if subcmd in ['ftp', 'help']:
89 fix_stdout = not already_fixed and os.isatty(1)
90 fix_stderr = not already_fixed and os.isatty(2)
93 if fix_stdout or fix_stderr:
94 amt = (fix_stdout and 1 or 0) + (fix_stderr and 2 or 0)
95 os.environ['BUP_FORCE_TTY'] = str(amt)
97 if fix_stdout or fix_stderr:
98 realf = fix_stderr and 2 or 1
99 n = subprocess.Popen([subpath('newliner')],
100 stdin=subprocess.PIPE, stdout=os.dup(realf),
101 close_fds=True, preexec_fn=force_tty)
102 outf = fix_stdout and n.stdin.fileno() or None
103 errf = fix_stderr and n.stdin.fileno() or None
110 class SigException(Exception):
112 def handler(signum, frame):
113 raise SigException('signal %d received' % signum)
115 signal.signal(signal.SIGTERM, handler)
116 signal.signal(signal.SIGINT, handler)
122 p = subprocess.Popen([subpath(subcmd)] + argv[2:],
123 stdout=outf, stderr=errf, preexec_fn=force_tty)
126 log('%s: %s\n' % (subpath(subcmd), e))
128 except SigException, e:
131 if p and p.poll() == None:
132 os.kill(p.pid, signal.SIGTERM)