]> arthur.barton.de Git - bup.git/blob - main.py
main.py: clean up subprocesses dying on signal.
[bup.git] / main.py
1 #!/usr/bin/env python
2 import sys, os, subprocess, signal
3
4 argv = sys.argv
5 exe = argv[0]
6 exepath = os.path.split(exe)[0] or '.'
7
8 # fix the PYTHONPATH to include our lib dir
9 libpath = os.path.join(exepath, 'lib')
10 cmdpath = os.path.join(exepath, 'cmd')
11 sys.path[:0] = [libpath]
12 os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '')
13
14 from bup.helpers import *
15
16
17 def columnate(l, prefix):
18     l = l[:]
19     clen = max(len(s) for s in l)
20     ncols = (78 - len(prefix)) / (clen + 2)
21     if ncols <= 1:
22         ncols = 1
23         clen = 0
24     cols = []
25     while len(l) % ncols:
26         l.append('')
27     rows = len(l)/ncols
28     for s in range(0, len(l), rows):
29         cols.append(l[s:s+rows])
30     for row in zip(*cols):
31         print prefix + ''.join(('%-*s' % (clen+2, s)) for s in row)
32
33
34 def usage():
35     log('Usage: bup <command> <options...>\n\n')
36     common = dict(
37         ftp = 'Browse backup sets using an ftp-like client',
38         fsck = 'Check backup sets for damage and add redundancy information',
39         fuse = 'Mount your backup sets as a filesystem',
40         index = 'Create or display the index of files to back up',
41         join = 'The reverse operation to "bup split"',
42         ls = 'Browse the files in your backup sets',
43         midx = 'Index objects to speed up future backups',
44         save = 'Save files into a backup set (note: run "bup index" first)',
45         split = 'Split a single file into its own backup set',
46     )
47
48     log('Common commands:\n')
49     for cmd,synopsis in sorted(common.items()):
50         print '    %-10s %s' % (cmd, synopsis)
51     log('\n')
52     
53     log('Other available commands:\n')
54     cmds = []
55     for c in sorted(os.listdir(cmdpath) + os.listdir(exepath)):
56         if c.startswith('bup-') and c.find('.') < 0:
57             cname = c[4:]
58             if cname not in common:
59                 cmds.append(c[4:])
60     columnate(cmds, '    ')
61     log('\n')
62     
63     log("See 'bup help <command>' for more information on " +
64         "a specific command.\n")
65     sys.exit(99)
66
67
68 if len(argv) < 2 or not argv[1] or argv[1][0] == '-':
69     usage()
70
71 subcmd = argv[1]
72
73 def subpath(s):
74     sp = os.path.join(exepath, 'bup-%s' % s)
75     if not os.path.exists(sp):
76         sp = os.path.join(cmdpath, 'bup-%s' % s)
77     return sp
78
79 if not os.path.exists(subpath(subcmd)):
80     log('error: unknown command "%s"\n' % subcmd)
81     usage()
82
83
84 already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
85 if subcmd in ['ftp', 'help']:
86     already_fixed = True
87 fix_stdout = not already_fixed and os.isatty(1)
88 fix_stderr = not already_fixed and os.isatty(2)
89
90 def force_tty():
91     if fix_stdout or fix_stderr:
92         os.environ['BUP_FORCE_TTY'] = '1'
93
94 if fix_stdout or fix_stderr:
95     realf = fix_stderr and 2 or 1
96     n = subprocess.Popen([subpath('newliner')],
97                          stdin=subprocess.PIPE, stdout=os.dup(realf),
98                          close_fds=True, preexec_fn=force_tty)
99     outf = fix_stdout and n.stdin.fileno() or 1
100     errf = fix_stderr and n.stdin.fileno() or 2
101 else:
102     n = None
103     outf = 1
104     errf = 2
105
106
107 class SigException(Exception):
108     pass
109 def handler(signum, frame):
110     raise SigException('signal %d received' % signum)
111
112 signal.signal(signal.SIGTERM, handler)
113 signal.signal(signal.SIGINT, handler)
114
115 ret = 95
116 try:
117     try:
118         p = subprocess.Popen([subpath(subcmd)] + argv[2:],
119                              stdout=outf, stderr=errf, preexec_fn=force_tty)
120         ret = p.wait()
121     except OSError, e:
122         log('%s: %s\n' % (subpath(subcmd), e))
123         ret = 98
124     except SigException, e:
125         ret = 94
126 finally:
127     if p and p.poll() == None:
128         os.kill(p.pid, signal.SIGTERM)
129         p.wait()
130     if n:
131         n.stdin.close()
132         try:
133             n.wait()
134         except:
135             pass
136 sys.exit(ret)