]> arthur.barton.de Git - bup.git/blob - lib/bup/main.py
Convert top level executables to binaries and clean up clean
[bup.git] / lib / bup / main.py
1
2 from __future__ import absolute_import, print_function
3 from importlib import import_module
4 from pkgutil import iter_modules
5 from subprocess import PIPE
6 from threading import Thread
7 import errno, getopt, os, re, select, signal, subprocess, sys
8
9 from bup import compat, path, helpers
10 from bup.compat import (
11     ModuleNotFoundError,
12     add_ex_ctx,
13     add_ex_tb,
14     argv_bytes,
15     environ,
16     fsdecode,
17     int_types,
18     wrap_main
19 )
20 from bup.compat import add_ex_tb, add_ex_ctx, argv_bytes, wrap_main
21 from bup.helpers import (
22     columnate,
23     debug1,
24     handle_ctrl_c,
25     log,
26     merge_dict,
27     tty_width
28 )
29 from bup.git import close_catpipes
30 from bup.io import byte_stream, path_msg
31 from bup.options import _tty_width
32 import bup.cmd
33
34 def maybe_import_early(argv):
35     """Scan argv and import any modules specified by --import-py-module."""
36     while argv:
37         if argv[0] != '--import-py-module':
38             argv = argv[1:]
39             continue
40         if len(argv) < 2:
41             log("bup: --import-py-module must have an argument\n")
42             exit(2)
43         mod = argv[1]
44         import_module(mod)
45         argv = argv[2:]
46
47 maybe_import_early(compat.get_argv())
48
49 handle_ctrl_c()
50
51 cmdpath = path.cmddir()
52
53 # Remove once we finish the internal command transition
54 transition_cmdpath = path.libdir() + b'/bup/cmd'
55
56 # We manipulate the subcmds here as strings, but they must be ASCII
57 # compatible, since we're going to be looking for exactly
58 # b'bup-SUBCMD' to exec.
59
60 def usage(msg=""):
61     log('Usage: bup [-?|--help] [-d BUP_DIR] [--debug] [--profile] '
62         '<command> [options...]\n\n')
63     common = dict(
64         ftp = 'Browse backup sets using an ftp-like client',
65         fsck = 'Check backup sets for damage and add redundancy information',
66         fuse = 'Mount your backup sets as a filesystem',
67         help = 'Print detailed help for the given command',
68         index = 'Create or display the index of files to back up',
69         on = 'Backup a remote machine to the local one',
70         restore = 'Extract files from a backup set',
71         save = 'Save files into a backup set (note: run "bup index" first)',
72         tag = 'Tag commits for easier access',
73         web = 'Launch a web server to examine backup sets',
74     )
75
76     log('Common commands:\n')
77     for cmd,synopsis in sorted(common.items()):
78         log('    %-10s %s\n' % (cmd, synopsis))
79     log('\n')
80     
81     log('Other available commands:\n')
82     cmds = set()
83     for c in sorted(os.listdir(cmdpath)):
84         if c.startswith(b'bup-') and c.find(b'.') < 0:
85             cname = fsdecode(c[4:])
86             if cname not in common:
87                 cmds.add(c[4:].decode(errors='backslashreplace'))
88     # built-in commands take precedence
89     for _, name, _ in iter_modules(path=bup.cmd.__path__):
90         name = name.replace('_','-')
91         if name not in common:
92             cmds.add(name)
93
94     log(columnate(sorted(cmds), '    '))
95     log('\n')
96     
97     log("See 'bup help COMMAND' for more information on " +
98         "a specific command.\n")
99     if msg:
100         log("\n%s\n" % msg)
101     sys.exit(99)
102
103 argv = compat.get_argv()
104 if len(argv) < 2:
105     usage()
106
107 # Handle global options.
108 try:
109     optspec = ['help', 'version', 'debug', 'profile', 'bup-dir=',
110                'import-py-module=']
111     global_args, subcmd = getopt.getopt(argv[1:], '?VDd:', optspec)
112 except getopt.GetoptError as ex:
113     usage('error: %s' % ex.msg)
114
115 subcmd = [argv_bytes(x) for x in subcmd]
116 help_requested = None
117 do_profile = False
118 bup_dir = None
119
120 for opt in global_args:
121     if opt[0] in ['-?', '--help']:
122         help_requested = True
123     elif opt[0] in ['-V', '--version']:
124         subcmd = [b'version']
125     elif opt[0] in ['-D', '--debug']:
126         helpers.buglvl += 1
127         environ[b'BUP_DEBUG'] = b'%d' % helpers.buglvl
128     elif opt[0] in ['--profile']:
129         do_profile = True
130     elif opt[0] in ['-d', '--bup-dir']:
131         bup_dir = argv_bytes(opt[1])
132     elif opt[0] == '--import-py-module':
133         pass
134     else:
135         usage('error: unexpected option "%s"' % opt[0])
136
137 # Make BUP_DIR absolute, so we aren't affected by chdir (i.e. save -C, etc.).
138 if bup_dir:
139     environ[b'BUP_DIR'] = os.path.abspath(bup_dir)
140
141 if len(subcmd) == 0:
142     if help_requested:
143         subcmd = [b'help']
144     else:
145         usage()
146
147 if help_requested and subcmd[0] != b'help':
148     subcmd = [b'help'] + subcmd
149
150 if len(subcmd) > 1 and subcmd[1] == b'--help' and subcmd[0] != b'help':
151     subcmd = [b'help', subcmd[0]] + subcmd[2:]
152
153 subcmd_name = subcmd[0]
154 if not subcmd_name:
155     usage()
156
157 try:
158     if subcmd_name not in (b'bloom',
159                            b'cat-file',
160                            b'daemon',
161                            b'drecurse',
162                            b'damage',
163                            b'features',
164                            b'ftp',
165                            b'fsck',
166                            b'fuse',
167                            b'gc',
168                            b'get',
169                            b'help',
170                            b'import-duplicity',
171                            b'index',
172                            b'init',
173                            b'join',
174                            b'list-idx',
175                            b'ls',
176                            b'margin',
177                            b'memtest',
178                            b'meta',
179                            b'midx',
180                            b'mux',
181                            b'on',
182                            b'on--server',
183                            b'prune-older',
184                            b'random',
185                            b'restore',
186                            b'rm',
187                            b'save',
188                            b'server',
189                            b'split',
190                            b'tag',
191                            b'tick',
192                            b'version',
193                            b'web',
194                            b'xstat'):
195         raise ModuleNotFoundError()
196     cmd_module = import_module('bup.cmd.'
197                                + subcmd_name.decode('ascii').replace('-', '_'))
198 except ModuleNotFoundError as ex:
199     cmd_module = None
200
201 if not cmd_module:
202     subcmd[0] = os.path.join(cmdpath, b'bup-' + subcmd_name)
203     if not os.path.exists(subcmd[0]):
204         subcmd[0] = b'%s/%s.py' % (transition_cmdpath,
205                                    subcmd_name.replace(b'-', b'_'))
206     if not os.path.exists(subcmd[0]):
207         usage('error: unknown command "%s"' % path_msg(subcmd_name))
208
209 already_fixed = int(environ.get(b'BUP_FORCE_TTY', 0))
210 if subcmd_name in [b'mux', b'ftp', b'help']:
211     already_fixed = True
212 fix_stdout = not already_fixed and os.isatty(1)
213 fix_stderr = not already_fixed and os.isatty(2)
214
215 if fix_stdout or fix_stderr:
216     tty_env = merge_dict(environ,
217                          {b'BUP_FORCE_TTY': (b'%d'
218                                              % ((fix_stdout and 1 or 0)
219                                                 + (fix_stderr and 2 or 0))),
220                           b'BUP_TTY_WIDTH': b'%d' % _tty_width(), })
221 else:
222     tty_env = environ
223
224
225 sep_rx = re.compile(br'([\r\n])')
226
227 def print_clean_line(dest, content, width, sep=None):
228     """Write some or all of content, followed by sep, to the dest fd after
229     padding the content with enough spaces to fill the current
230     terminal width or truncating it to the terminal width if sep is a
231     carriage return."""
232     global sep_rx
233     assert sep in (b'\r', b'\n', None)
234     if not content:
235         if sep:
236             os.write(dest, sep)
237         return
238     for x in content:
239         assert not sep_rx.match(x)
240     content = b''.join(content)
241     if sep == b'\r' and len(content) > width:
242         content = content[width:]
243     os.write(dest, content)
244     if len(content) < width:
245         os.write(dest, b' ' * (width - len(content)))
246     if sep:
247         os.write(dest, sep)
248
249 def filter_output(srcs, dests):
250     """Transfer data from file descriptors in srcs to the corresponding
251     file descriptors in dests print_clean_line until all of the srcs
252     have closed.
253
254     """
255     global sep_rx
256     assert all(type(x) in int_types for x in srcs)
257     assert all(type(x) in int_types for x in srcs)
258     assert len(srcs) == len(dests)
259     srcs = tuple(srcs)
260     dest_for = dict(zip(srcs, dests))
261     pending = {}
262     pending_ex = None
263     try:
264         while srcs:
265             ready_fds, _, _ = select.select(srcs, [], [])
266             width = tty_width()
267             for fd in ready_fds:
268                 buf = os.read(fd, 4096)
269                 dest = dest_for[fd]
270                 if not buf:
271                     srcs = tuple([x for x in srcs if x is not fd])
272                     print_clean_line(dest, pending.pop(fd, []), width)
273                 else:
274                     split = sep_rx.split(buf)
275                     while len(split) > 1:
276                         content, sep = split[:2]
277                         split = split[2:]
278                         print_clean_line(dest,
279                                          pending.pop(fd, []) + [content],
280                                          width,
281                                          sep)
282                     assert len(split) == 1
283                     if split[0]:
284                         pending.setdefault(fd, []).extend(split)
285     except BaseException as ex:
286         pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
287     try:
288         # Try to finish each of the streams
289         for fd, pending_items in compat.items(pending):
290             dest = dest_for[fd]
291             width = tty_width()
292             try:
293                 print_clean_line(dest, pending_items, width)
294             except (EnvironmentError, EOFError) as ex:
295                 pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
296     except BaseException as ex:
297         pending_ex = add_ex_ctx(add_ex_tb(ex), pending_ex)
298     if pending_ex:
299         raise pending_ex
300
301
302 def import_and_run_main(module, args):
303     if do_profile:
304         import cProfile
305         f = compile('module.main(args)', __file__, 'exec')
306         cProfile.runctx(f, globals(), locals())
307     else:
308         module.main(args)
309
310
311 def run_module_cmd(module, args):
312     if not (fix_stdout or fix_stderr):
313         import_and_run_main(module, args)
314         return
315     # Interpose filter_output between all attempts to write to the
316     # stdout/stderr and the real stdout/stderr (e.g. the fds that
317     # connect directly to the terminal) via a thread that runs
318     # filter_output in a pipeline.
319     srcs = []
320     dests = []
321     real_out_fd = real_err_fd = stdout_pipe = stderr_pipe = None
322     filter_thread = filter_thread_started = None
323     pending_ex = None
324     try:
325         if fix_stdout:
326             sys.stdout.flush()
327             stdout_pipe = os.pipe()  # monitored_by_filter, stdout_everyone_uses
328             real_out_fd = os.dup(sys.stdout.fileno())
329             os.dup2(stdout_pipe[1], sys.stdout.fileno())
330             srcs.append(stdout_pipe[0])
331             dests.append(real_out_fd)
332         if fix_stderr:
333             sys.stderr.flush()
334             stderr_pipe = os.pipe()  # monitored_by_filter, stderr_everyone_uses
335             real_err_fd = os.dup(sys.stderr.fileno())
336             os.dup2(stderr_pipe[1], sys.stderr.fileno())
337             srcs.append(stderr_pipe[0])
338             dests.append(real_err_fd)
339
340         filter_thread = Thread(name='output filter',
341                                target=lambda : filter_output(srcs, dests))
342         filter_thread.start()
343         filter_thread_started = True
344         import_and_run_main(module, args)
345     except Exception as ex:
346         add_ex_tb(ex)
347         pending_ex = ex
348         raise
349     finally:
350         # Try to make sure that whatever else happens, we restore
351         # stdout and stderr here, if that's possible, so that we don't
352         # risk just losing some output.
353         try:
354             real_out_fd is not None and os.dup2(real_out_fd, sys.stdout.fileno())
355         except Exception as ex:
356             add_ex_tb(ex)
357             add_ex_ctx(ex, pending_ex)
358         try:
359             real_err_fd is not None and os.dup2(real_err_fd, sys.stderr.fileno())
360         except Exception as ex:
361             add_ex_tb(ex)
362             add_ex_ctx(ex, pending_ex)
363         # Kick filter loose
364         try:
365             stdout_pipe is not None and os.close(stdout_pipe[1])
366         except Exception as ex:
367             add_ex_tb(ex)
368             add_ex_ctx(ex, pending_ex)
369         try:
370             stderr_pipe is not None and os.close(stderr_pipe[1])
371         except Exception as ex:
372             add_ex_tb(ex)
373             add_ex_ctx(ex, pending_ex)
374         try:
375             close_catpipes()
376         except Exception as ex:
377             add_ex_tb(ex)
378             add_ex_ctx(ex, pending_ex)
379     if pending_ex:
380         raise pending_ex
381     # There's no point in trying to join unless we finished the finally block.
382     if filter_thread_started:
383         filter_thread.join()
384
385
386 def run_subproc_cmd(args):
387
388     c = (do_profile and [sys.executable, b'-m', b'cProfile'] or []) + args
389     if not (fix_stdout or fix_stderr):
390         os.execvp(c[0], c)
391
392     sys.stdout.flush()
393     sys.stderr.flush()
394     out = byte_stream(sys.stdout)
395     err = byte_stream(sys.stderr)
396     p = None
397     try:
398         p = subprocess.Popen(c,
399                              stdout=PIPE if fix_stdout else out,
400                              stderr=PIPE if fix_stderr else err,
401                              env=tty_env, bufsize=4096, close_fds=True)
402         # Assume p will receive these signals and quit, which will
403         # then cause us to quit.
404         for sig in (signal.SIGINT, signal.SIGTERM, signal.SIGQUIT):
405             signal.signal(sig, signal.SIG_IGN)
406
407         srcs = []
408         dests = []
409         if fix_stdout:
410             srcs.append(p.stdout.fileno())
411             dests.append(out.fileno())
412         if fix_stderr:
413             srcs.append(p.stderr.fileno())
414             dests.append(err.fileno())
415         filter_output(srcs, dests)
416         return p.wait()
417     except BaseException as ex:
418         add_ex_tb(ex)
419         try:
420             if p and p.poll() == None:
421                 os.kill(p.pid, signal.SIGTERM)
422                 p.wait()
423         except BaseException as kill_ex:
424             raise add_ex_ctx(add_ex_tb(kill_ex), ex)
425         raise ex
426
427
428 def run_subcmd(module, args):
429     if module:
430         run_module_cmd(module, args)
431     else:
432         run_subproc_cmd(args)
433
434 def main():
435     wrap_main(lambda : run_subcmd(cmd_module, subcmd))
436
437 if __name__ == "__main__":
438     main()