2 import sys, os, subprocess, signal, getopt
6 exepath = os.path.split(exe)[0] or '.'
7 exeprefix = os.path.split(os.path.abspath(exepath))[0]
9 # fix the PYTHONPATH to include our lib dir
10 if os.path.exists("%s/lib/bup/cmd/." % exeprefix):
11 # installed binary in /.../bin.
12 # eg. /usr/bin/bup means /usr/lib/bup/... is where our libraries are.
13 cmdpath = "%s/lib/bup/cmd" % exeprefix
14 libpath = "%s/lib/bup" % exeprefix
15 resourcepath = libpath
17 # running from the src directory without being installed first
18 cmdpath = os.path.join(exepath, 'cmd')
19 libpath = os.path.join(exepath, 'lib')
20 resourcepath = libpath
21 sys.path[:0] = [libpath]
22 os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '')
23 os.environ['BUP_MAIN_EXE'] = os.path.abspath(exe)
24 os.environ['BUP_RESOURCE_PATH'] = resourcepath
26 from bup import helpers
27 from bup.helpers import *
31 log('Usage: bup [-?|--help] [-d BUP_DIR] [--debug] '
32 '<command> [options...]\n\n')
34 ftp = 'Browse backup sets using an ftp-like client',
35 fsck = 'Check backup sets for damage and add redundancy information',
36 fuse = 'Mount your backup sets as a filesystem',
37 help = 'Print detailed help for the given command',
38 index = 'Create or display the index of files to back up',
39 on = 'Backup a remote machine to the local one',
40 save = 'Save files into a backup set (note: run "bup index" first)',
41 web = 'Launch a web server to examine backup sets',
44 log('Common commands:\n')
45 for cmd,synopsis in sorted(common.items()):
46 log(' %-10s %s\n' % (cmd, synopsis))
49 log('Other available commands:\n')
51 for c in sorted(os.listdir(cmdpath) + os.listdir(exepath)):
52 if c.startswith('bup-') and c.find('.') < 0:
54 if cname not in common:
56 log(columnate(cmds, ' '))
59 log("See 'bup help COMMAND' for more information on " +
60 "a specific command.\n")
67 # Handle global options.
69 global_args, subcmd = getopt.getopt(argv[1:], '?VDd:',
70 ['help', 'version', 'debug', 'bup-dir='])
71 except getopt.GetoptError, ex:
72 log('error: ' + ex.msg + '\n')
78 for opt in global_args:
79 if opt[0] in ['-?', '--help']:
81 elif opt[0] in ['-V', '--version']:
83 elif opt[0] in ['-D', '--debug']:
85 os.environ['BUP_DEBUG'] = str(helpers.buglvl)
86 elif opt[0] in ['-d', '--bup-dir']:
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]
108 subcmd_env = os.environ
110 subcmd_env.update({"BUP_DIR" : dest_dir})
113 sp = os.path.join(exepath, 'bup-%s' % s)
114 if not os.path.exists(sp):
115 sp = os.path.join(cmdpath, 'bup-%s' % s)
118 if not os.path.exists(subpath(subcmd_name)):
119 log('error: unknown command "%s"\n' % subcmd_name)
122 already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
123 if subcmd_name in ['ftp', 'help']:
125 fix_stdout = not already_fixed and os.isatty(1)
126 fix_stderr = not already_fixed and os.isatty(2)
129 if fix_stdout or fix_stderr:
130 amt = (fix_stdout and 1 or 0) + (fix_stderr and 2 or 0)
131 os.environ['BUP_FORCE_TTY'] = str(amt)
132 os.setsid() # make sure ctrl-c is sent just to us, not to child too
134 if fix_stdout or fix_stderr:
135 realf = fix_stderr and 2 or 1
136 drealf = os.dup(realf) # Popen goes crazy with stdout=2
137 n = subprocess.Popen([subpath('newliner')],
138 stdin=subprocess.PIPE, stdout=drealf,
139 close_fds=True, preexec_fn=force_tty)
141 outf = fix_stdout and n.stdin.fileno() or None
142 errf = fix_stderr and n.stdin.fileno() or None
149 class SigException(Exception):
150 def __init__(self, signum):
152 Exception.__init__(self, 'signal %d received' % signum)
153 def handler(signum, frame):
154 raise SigException(signum)
156 signal.signal(signal.SIGTERM, handler)
157 signal.signal(signal.SIGINT, handler)
163 p = subprocess.Popen([subpath(subcmd_name)] + subcmd[1:],
164 stdout=outf, stderr=errf, preexec_fn=force_tty)
166 # if we get a signal while waiting, we have to keep waiting, just
167 # in case our child doesn't die.
171 except SigException, e:
172 log('\nbup: %s\n' % e)
173 os.kill(p.pid, e.signum)
176 log('%s: %s\n' % (subpath(subcmd_name), e))
179 if p and p.poll() == None:
180 os.kill(p.pid, signal.SIGTERM)