3 import sys, os, subprocess, signal, getopt
7 exepath = os.path.split(exe)[0] or '.'
9 # fix the PYTHONPATH to include our lib dir
10 libpath = os.path.join(exepath, 'lib')
11 cmdpath = os.path.join(exepath, 'cmd')
12 sys.path[:0] = [libpath]
13 os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '')
14 os.environ['BUP_MAIN_EXE'] = os.path.abspath(exe)
16 from bup.helpers import *
19 def columnate(l, prefix):
21 clen = max(len(s) for s in l)
22 ncols = (78 - len(prefix)) / (clen + 2)
30 for s in range(0, len(l), rows):
31 cols.append(l[s:s+rows])
33 for row in zip(*cols):
34 out += prefix + ''.join(('%-*s' % (clen+2, s)) for s in row) + '\n'
39 log('Usage: bup [-?|--help] COMMAND [ARGS]\n\n')
41 ftp = 'Browse backup sets using an ftp-like client',
42 fsck = 'Check backup sets for damage and add redundancy information',
43 fuse = 'Mount your backup sets as a filesystem',
44 help = 'Print detailed help for the given command',
45 index = 'Create or display the index of files to back up',
46 join = 'Retrieve a file backed up using "bup split"',
47 ls = 'Browse the files in your backup sets',
48 midx = 'Index objects to speed up future backups',
49 save = 'Save files into a backup set (note: run "bup index" first)',
50 split = 'Split a single file into its own backup set',
53 log('Common commands:\n')
54 for cmd,synopsis in sorted(common.items()):
55 log(' %-10s %s\n' % (cmd, synopsis))
58 log('Other available commands:\n')
60 for c in sorted(os.listdir(cmdpath) + os.listdir(exepath)):
61 if c.startswith('bup-') and c.find('.') < 0:
63 if cname not in common:
65 log(columnate(cmds, ' '))
68 log("See 'bup help COMMAND' for more information on " +
69 "a specific command.\n")
76 # Handle global options.
78 global_args, subcmd = getopt.getopt(argv[1:], '?', ['help'])
79 except getopt.GetoptError, ex:
80 log('error: ' + ex.msg + '\n')
85 for opt in global_args:
86 if opt[0] == '-?' or opt[0] == '--help':
89 log('error: unexpected option "%s"\n' % opt[0])
98 if help_requested and subcmd[0] != 'help':
99 subcmd = ['help'] + subcmd
101 if len(subcmd) > 1 and subcmd[1] == '--help' and subcmd[0] != 'help':
102 subcmd = ['help', subcmd[0]] + subcmd[2:]
104 subcmd_name = subcmd[0]
109 sp = os.path.join(exepath, 'bup-%s' % s)
110 if not os.path.exists(sp):
111 sp = os.path.join(cmdpath, 'bup-%s' % s)
114 if not os.path.exists(subpath(subcmd_name)):
115 log('error: unknown command "%s"\n' % subcmd_name)
118 already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
119 if subcmd_name in ['ftp', 'help']:
121 fix_stdout = not already_fixed and os.isatty(1)
122 fix_stderr = not already_fixed and os.isatty(2)
125 if fix_stdout or fix_stderr:
126 amt = (fix_stdout and 1 or 0) + (fix_stderr and 2 or 0)
127 os.environ['BUP_FORCE_TTY'] = str(amt)
128 os.setsid() # make sure ctrl-c is sent just to us, not to child too
130 if fix_stdout or fix_stderr:
131 realf = fix_stderr and 2 or 1
132 drealf = os.dup(realf) # Popen goes crazy with stdout=2
133 n = subprocess.Popen([subpath('newliner')],
134 stdin=subprocess.PIPE, stdout=drealf,
135 close_fds=True, preexec_fn=force_tty)
137 outf = fix_stdout and n.stdin.fileno() or None
138 errf = fix_stderr and n.stdin.fileno() or None
145 class SigException(Exception):
146 def __init__(self, signum):
148 Exception.__init__(self, 'signal %d received' % signum)
149 def handler(signum, frame):
150 raise SigException(signum)
152 signal.signal(signal.SIGTERM, handler)
153 signal.signal(signal.SIGINT, handler)
159 p = subprocess.Popen([subpath(subcmd_name)] + subcmd[1:],
160 stdout=outf, stderr=errf, preexec_fn=force_tty)
162 # if we get a signal while waiting, we have to keep waiting, just
163 # in case our child doesn't die.
167 except SigException, e:
168 log('\nbup: %s\n' % e)
169 os.kill(p.pid, e.signum)
172 log('%s: %s\n' % (subpath(subcmd_name), e))
175 if p and p.poll() == None:
176 os.kill(p.pid, signal.SIGTERM)