]> arthur.barton.de Git - bup.git/blob - main.py
Clean subprocess output without newliner
[bup.git] / main.py
1 #!/bin/sh
2 """": # -*-python-*- # -*-python-*-
3 bup_python="$(dirname "$0")/cmd/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7
8 import errno, re, sys, os, subprocess, signal, getopt
9
10 from fcntl import F_GETFL, F_SETFL
11 from subprocess import PIPE
12 from sys import stderr, stdout
13 import fcntl, select
14
15 argv = sys.argv
16 exe = os.path.realpath(argv[0])
17 exepath = os.path.split(exe)[0] or '.'
18 exeprefix = os.path.split(os.path.abspath(exepath))[0]
19
20 # fix the PYTHONPATH to include our lib dir
21 if os.path.exists("%s/lib/bup/cmd/." % exeprefix):
22     # installed binary in /.../bin.
23     # eg. /usr/bin/bup means /usr/lib/bup/... is where our libraries are.
24     cmdpath = "%s/lib/bup/cmd" % exeprefix
25     libpath = "%s/lib/bup" % exeprefix
26     resourcepath = libpath
27 else:
28     # running from the src directory without being installed first
29     cmdpath = os.path.join(exepath, 'cmd')
30     libpath = os.path.join(exepath, 'lib')
31     resourcepath = libpath
32 sys.path[:0] = [libpath]
33 os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '')
34 os.environ['BUP_MAIN_EXE'] = os.path.abspath(exe)
35 os.environ['BUP_RESOURCE_PATH'] = resourcepath
36
37
38 from bup import helpers
39 from bup.compat import add_ex_tb, chain_ex, wrap_main
40 from bup.helpers import atoi, columnate, debug1, log, tty_width
41
42
43 def usage(msg=""):
44     log('Usage: bup [-?|--help] [-d BUP_DIR] [--debug] [--profile] '
45         '<command> [options...]\n\n')
46     common = dict(
47         ftp = 'Browse backup sets using an ftp-like client',
48         fsck = 'Check backup sets for damage and add redundancy information',
49         fuse = 'Mount your backup sets as a filesystem',
50         help = 'Print detailed help for the given command',
51         index = 'Create or display the index of files to back up',
52         on = 'Backup a remote machine to the local one',
53         restore = 'Extract files from a backup set',
54         save = 'Save files into a backup set (note: run "bup index" first)',
55         tag = 'Tag commits for easier access',
56         web = 'Launch a web server to examine backup sets',
57     )
58
59     log('Common commands:\n')
60     for cmd,synopsis in sorted(common.items()):
61         log('    %-10s %s\n' % (cmd, synopsis))
62     log('\n')
63     
64     log('Other available commands:\n')
65     cmds = []
66     for c in sorted(os.listdir(cmdpath) + os.listdir(exepath)):
67         if c.startswith('bup-') and c.find('.') < 0:
68             cname = c[4:]
69             if cname not in common:
70                 cmds.append(c[4:])
71     log(columnate(cmds, '    '))
72     log('\n')
73     
74     log("See 'bup help COMMAND' for more information on " +
75         "a specific command.\n")
76     if msg:
77         log("\n%s\n" % msg)
78     sys.exit(99)
79
80
81 if len(argv) < 2:
82     usage()
83
84 # Handle global options.
85 try:
86     optspec = ['help', 'version', 'debug', 'profile', 'bup-dir=']
87     global_args, subcmd = getopt.getopt(argv[1:], '?VDd:', optspec)
88 except getopt.GetoptError as ex:
89     usage('error: %s' % ex.msg)
90
91 help_requested = None
92 do_profile = False
93
94 for opt in global_args:
95     if opt[0] in ['-?', '--help']:
96         help_requested = True
97     elif opt[0] in ['-V', '--version']:
98         subcmd = ['version']
99     elif opt[0] in ['-D', '--debug']:
100         helpers.buglvl += 1
101         os.environ['BUP_DEBUG'] = str(helpers.buglvl)
102     elif opt[0] in ['--profile']:
103         do_profile = True
104     elif opt[0] in ['-d', '--bup-dir']:
105         os.environ['BUP_DIR'] = opt[1]
106     else:
107         usage('error: unexpected option "%s"' % opt[0])
108
109 # Make BUP_DIR absolute, so we aren't affected by chdir (i.e. save -C, etc.).
110 if 'BUP_DIR' in os.environ:
111     os.environ['BUP_DIR'] = os.path.abspath(os.environ['BUP_DIR'])
112
113 if len(subcmd) == 0:
114     if help_requested:
115         subcmd = ['help']
116     else:
117         usage()
118
119 if help_requested and subcmd[0] != 'help':
120     subcmd = ['help'] + subcmd
121
122 if len(subcmd) > 1 and subcmd[1] == '--help' and subcmd[0] != 'help':
123     subcmd = ['help', subcmd[0]] + subcmd[2:]
124
125 subcmd_name = subcmd[0]
126 if not subcmd_name:
127     usage()
128
129 def subpath(s):
130     sp = os.path.join(exepath, 'bup-%s' % s)
131     if not os.path.exists(sp):
132         sp = os.path.join(cmdpath, 'bup-%s' % s)
133     return sp
134
135 subcmd[0] = subpath(subcmd_name)
136 if not os.path.exists(subcmd[0]):
137     usage('error: unknown command "%s"' % subcmd_name)
138
139 already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
140 if subcmd_name in ['mux', 'ftp', 'help']:
141     already_fixed = True
142 fix_stdout = not already_fixed and os.isatty(1)
143 fix_stderr = not already_fixed and os.isatty(2)
144
145 def force_tty():
146     if fix_stdout or fix_stderr:
147         amt = (fix_stdout and 1 or 0) + (fix_stderr and 2 or 0)
148         os.environ['BUP_FORCE_TTY'] = str(amt)
149
150
151 sep_rx = re.compile(r'([\r\n])')
152
153 def print_clean_line(dest, content, width, sep=None):
154     """Write some or all of content, followed by sep, to the dest fd after
155     padding the content with enough spaces to fill the current
156     terminal width or truncating it to the terminal width if sep is a
157     carriage return."""
158     global sep_rx
159     assert sep in ('\r', '\n', None)
160     if not content:
161         if sep:
162             os.write(dest, sep)
163         return
164     for x in content:
165         assert not sep_rx.match(x)
166     content = ''.join(content)
167     if sep == '\r' and len(content) > width:
168         content = content[width:]
169     os.write(dest, content)
170     if len(content) < width:
171         os.write(dest, ' ' * (width - len(content)))
172     os.write(dest, sep)
173
174 def filter_output(src_out, src_err, dest_out, dest_err):
175     """Transfer data from src_out to dest_out and src_err to dest_err via
176     print_clean_line until src_out and src_err close."""
177     global sep_rx
178     assert not isinstance(src_out, bool)
179     assert not isinstance(src_err, bool)
180     assert not isinstance(dest_out, bool)
181     assert not isinstance(dest_err, bool)
182     assert src_out is not None or src_err is not None
183     assert (src_out is None) == (dest_out is None)
184     assert (src_err is None) == (dest_err is None)
185     pending = {}
186     pending_ex = None
187     try:
188         fds = tuple([x for x in (src_out, src_err) if x is not None])
189         for fd in fds:
190             flags = fcntl.fcntl(fd, F_GETFL)
191             assert fcntl.fcntl(fd, F_SETFL, flags | os.O_NONBLOCK) == 0
192         while fds:
193             ready_fds, _, _ = select.select(fds, [], [])
194             width = tty_width()
195             for fd in ready_fds:
196                 buf = os.read(fd, 4096)
197                 dest = dest_out if fd == src_out else dest_err
198                 if not buf:
199                     fds = tuple([x for x in fds if x is not fd])
200                     print_clean_line(dest, pending.pop(fd, []), width)
201                 else:
202                     split = sep_rx.split(buf)
203                     if len(split) > 2:
204                         while len(split) > 1:
205                             content, sep = split[:2]
206                             split = split[2:]
207                             print_clean_line(dest,
208                                              pending.pop(fd, []) + [content],
209                                              width,
210                                              sep)
211                     else:
212                         assert(len(split) == 1)
213                         pending.setdefault(fd, []).extend(split)
214     except BaseException as ex:
215         pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
216     try:
217         # Try to finish each of the streams
218         for fd, pending_items in pending.iteritems():
219             dest = dest_out if fd == src_out else dest_err
220             try:
221                 print_clean_line(dest, pending_items, width)
222             except (EnvironmentError, EOFError) as ex:
223                 pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
224     except BaseException as ex:
225         pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
226     if pending_ex:
227         raise pending_ex
228
229 def run_subcmd(subcmd):
230
231     c = (do_profile and [sys.executable, '-m', 'cProfile'] or []) + subcmd
232     if not (fix_stdout or fix_stderr):
233         os.execvp(c[0], c)
234
235     p = None
236     try:
237         p = subprocess.Popen(c,
238                              stdout=PIPE if fix_stdout else sys.stdout,
239                              stderr=PIPE if fix_stderr else sys.stderr,
240                              preexec_fn=force_tty,
241                              bufsize=4096,
242                              close_fds=True)
243         # Assume p will receive these signals and quit, which will
244         # then cause us to quit.
245         for sig in (signal.SIGINT, signal.SIGTERM, signal.SIGQUIT):
246             signal.signal(sig, signal.SIG_IGN)
247
248         filter_output(fix_stdout and p.stdout.fileno() or None,
249                       fix_stderr and p.stderr.fileno() or None,
250                       fix_stdout and sys.stdout.fileno() or None,
251                       fix_stderr and sys.stderr.fileno() or None)
252         return p.wait()
253     except BaseException as ex:
254         add_ex_tb(ex)
255         try:
256             if p and p.poll() == None:
257                 os.kill(p.pid, signal.SIGTERM)
258                 p.wait()
259         except BaseException as kill_ex:
260             raise chain_ex(add_ex_tb(kill_ex), ex)
261         raise ex
262         
263 wrap_main(lambda : run_subcmd(subcmd))