2 """": # -*-python-*- # -*-python-*-
6 # loop because macos doesn't have recursive readlink/realpath utils
7 while test -L "$cmdpath"; do
8 link="$(readlink "$cmdpath")"
9 cd "$(dirname "$cmdpath")"
12 script_home="$(cd "$(dirname "$cmdpath")" && pwd -P)"
14 exec "$script_home/bup-python" "$0" ${1+"$@"}
18 from __future__ import absolute_import, print_function
19 import errno, getopt, os, re, select, signal, subprocess, sys
20 from subprocess import PIPE
22 from bup.compat import environ, restore_lc_env
23 from bup.io import path_msg
25 if sys.version_info[0] != 2 \
26 and not environ.get(b'BUP_ALLOW_UNEXPECTED_PYTHON_VERSION') == b'true':
27 print('error: bup may crash with python versions other than 2, or eat your data',
33 from bup import compat, path, helpers
34 from bup.compat import add_ex_tb, add_ex_ctx, argv_bytes, wrap_main
35 from bup.helpers import atoi, columnate, debug1, log, merge_dict, tty_width
36 from bup.io import byte_stream, path_msg
38 cmdpath = path.cmddir()
40 # We manipulate the subcmds here as strings, but they must be ASCII
41 # compatible, since we're going to be looking for exactly
42 # b'bup-SUBCMD' to exec.
45 log('Usage: bup [-?|--help] [-d BUP_DIR] [--debug] [--profile] '
46 '<command> [options...]\n\n')
48 ftp = 'Browse backup sets using an ftp-like client',
49 fsck = 'Check backup sets for damage and add redundancy information',
50 fuse = 'Mount your backup sets as a filesystem',
51 help = 'Print detailed help for the given command',
52 index = 'Create or display the index of files to back up',
53 on = 'Backup a remote machine to the local one',
54 restore = 'Extract files from a backup set',
55 save = 'Save files into a backup set (note: run "bup index" first)',
56 tag = 'Tag commits for easier access',
57 web = 'Launch a web server to examine backup sets',
60 log('Common commands:\n')
61 for cmd,synopsis in sorted(common.items()):
62 log(' %-10s %s\n' % (cmd, synopsis))
65 log('Other available commands:\n')
67 for c in sorted(os.listdir(cmdpath)):
68 if c.startswith(b'bup-') and c.find(b'.') < 0:
69 cname = c[4:].decode('iso-8859-1')
70 if cname not in common:
72 log(columnate(cmds, ' '))
75 log("See 'bup help COMMAND' for more information on " +
76 "a specific command.\n")
85 # Handle global options.
87 optspec = ['help', 'version', 'debug', 'profile', 'bup-dir=']
88 global_args, subcmd = getopt.getopt(sys.argv[1:], '?VDd:', optspec)
89 except getopt.GetoptError as ex:
90 usage('error: %s' % ex.msg)
92 subcmd = [argv_bytes(x) for x in subcmd]
97 for opt in global_args:
98 if opt[0] in ['-?', '--help']:
100 elif opt[0] in ['-V', '--version']:
101 subcmd = [b'version']
102 elif opt[0] in ['-D', '--debug']:
104 environ[b'BUP_DEBUG'] = b'%d' % helpers.buglvl
105 elif opt[0] in ['--profile']:
107 elif opt[0] in ['-d', '--bup-dir']:
108 bup_dir = argv_bytes(opt[1])
110 usage('error: unexpected option "%s"' % opt[0])
113 bup_dir = argv_bytes(bup_dir)
115 # Make BUP_DIR absolute, so we aren't affected by chdir (i.e. save -C, etc.).
117 environ[b'BUP_DIR'] = os.path.abspath(bup_dir)
125 if help_requested and subcmd[0] != b'help':
126 subcmd = [b'help'] + subcmd
128 if len(subcmd) > 1 and subcmd[1] == b'--help' and subcmd[0] != b'help':
129 subcmd = [b'help', subcmd[0]] + subcmd[2:]
131 subcmd_name = subcmd[0]
136 return os.path.join(cmdpath, b'bup-' + subcmd)
138 subcmd[0] = subpath(subcmd_name)
139 if not os.path.exists(subcmd[0]):
140 usage('error: unknown command "%s"' % path_msg(subcmd_name))
142 already_fixed = atoi(environ.get(b'BUP_FORCE_TTY'))
143 if subcmd_name in [b'mux', b'ftp', b'help']:
145 fix_stdout = not already_fixed and os.isatty(1)
146 fix_stderr = not already_fixed and os.isatty(2)
148 if fix_stdout or fix_stderr:
149 tty_env = merge_dict(environ,
150 {b'BUP_FORCE_TTY': (b'%d'
151 % ((fix_stdout and 1 or 0)
152 + (fix_stderr and 2 or 0)))})
157 sep_rx = re.compile(br'([\r\n])')
159 def print_clean_line(dest, content, width, sep=None):
160 """Write some or all of content, followed by sep, to the dest fd after
161 padding the content with enough spaces to fill the current
162 terminal width or truncating it to the terminal width if sep is a
165 assert sep in (b'\r', b'\n', None)
171 assert not sep_rx.match(x)
172 content = b''.join(content)
173 if sep == b'\r' and len(content) > width:
174 content = content[width:]
175 os.write(dest, content)
176 if len(content) < width:
177 os.write(dest, b' ' * (width - len(content)))
181 def filter_output(src_out, src_err, dest_out, dest_err):
182 """Transfer data from src_out to dest_out and src_err to dest_err via
183 print_clean_line until src_out and src_err close."""
185 assert not isinstance(src_out, bool)
186 assert not isinstance(src_err, bool)
187 assert not isinstance(dest_out, bool)
188 assert not isinstance(dest_err, bool)
189 assert src_out is not None or src_err is not None
190 assert (src_out is None) == (dest_out is None)
191 assert (src_err is None) == (dest_err is None)
195 fds = tuple([x for x in (src_out, src_err) if x is not None])
197 ready_fds, _, _ = select.select(fds, [], [])
200 buf = os.read(fd, 4096)
201 dest = dest_out if fd == src_out else dest_err
203 fds = tuple([x for x in fds if x is not fd])
204 print_clean_line(dest, pending.pop(fd, []), width)
206 split = sep_rx.split(buf)
207 while len(split) > 1:
208 content, sep = split[:2]
210 print_clean_line(dest,
211 pending.pop(fd, []) + [content],
214 assert(len(split) == 1)
216 pending.setdefault(fd, []).extend(split)
217 except BaseException as ex:
218 pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
220 # Try to finish each of the streams
221 for fd, pending_items in compat.items(pending):
222 dest = dest_out if fd == src_out else dest_err
224 print_clean_line(dest, pending_items, width)
225 except (EnvironmentError, EOFError) as ex:
226 pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
227 except BaseException as ex:
228 pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
232 def run_subcmd(subcmd):
234 c = (do_profile and [sys.executable, b'-m', b'cProfile'] or []) + subcmd
235 if not (fix_stdout or fix_stderr):
240 out = byte_stream(sys.stdout)
241 err = byte_stream(sys.stderr)
244 p = subprocess.Popen(c,
245 stdout=PIPE if fix_stdout else out,
246 stderr=PIPE if fix_stderr else err,
247 env=tty_env, bufsize=4096, close_fds=True)
248 # Assume p will receive these signals and quit, which will
249 # then cause us to quit.
250 for sig in (signal.SIGINT, signal.SIGTERM, signal.SIGQUIT):
251 signal.signal(sig, signal.SIG_IGN)
253 filter_output(fix_stdout and p.stdout.fileno() or None,
254 fix_stderr and p.stderr.fileno() or None,
255 fix_stdout and out.fileno() or None,
256 fix_stderr and err.fileno() or None)
258 except BaseException as ex:
261 if p and p.poll() == None:
262 os.kill(p.pid, signal.SIGTERM)
264 except BaseException as kill_ex:
265 raise add_ex_ctx(add_ex_tb(kill_ex), ex)
268 wrap_main(lambda : run_subcmd(subcmd))