]> arthur.barton.de Git - bup.git/blob - main.py
Move python library files to lib/bup/
[bup.git] / main.py
1 #!/usr/bin/env python
2 import sys, os, subprocess
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 sys.path[:0] = [libpath]
11 os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '')
12
13 from bup.helpers import *
14
15 def usage():
16     log('Usage: bup <subcmd> <options...>\n\n')
17     log('Available subcommands:\n')
18     for c in sorted(os.listdir(exepath)):
19         if c.startswith('bup-') and c.find('.') < 0:
20             log('\t%s\n' % c[4:])
21     sys.exit(99)
22
23 if len(argv) < 2 or not argv[1] or argv[1][0] == '-':
24     usage()
25
26 subcmd = argv[1]
27 if subcmd == 'help':
28     usage()
29
30 def subpath(s):
31     return os.path.join(exepath, 'bup-%s' % s)
32
33 if not os.path.exists(subpath(subcmd)):
34     log('error: unknown command "%s"\n' % subcmd)
35     usage()
36
37
38 already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
39 if subcmd in ['ftp']:
40     already_fixed = True
41 fix_stdout = not already_fixed and os.isatty(1)
42 fix_stderr = not already_fixed and os.isatty(2)
43
44 def force_tty():
45     if fix_stdout or fix_stderr:
46         os.environ['BUP_FORCE_TTY'] = '1'
47
48 if fix_stdout or fix_stderr:
49     realf = fix_stderr and 2 or 1
50     n = subprocess.Popen([subpath('newliner')],
51                          stdin=subprocess.PIPE, stdout=os.dup(realf),
52                          close_fds=True, preexec_fn=force_tty)
53     outf = fix_stdout and n.stdin.fileno() or 1
54     errf = fix_stderr and n.stdin.fileno() or 2
55 else:
56     n = None
57     outf = 1
58     errf = 2
59
60 ret = 95
61 try:
62     try:
63         p = subprocess.Popen([subpath(subcmd)] + argv[2:],
64                              stdout=outf, stderr=errf, preexec_fn=force_tty)
65         ret = p.wait()
66     except OSError, e:
67         log('%s: %s\n' % (subpath(subcmd), e))
68         ret = 98
69     except KeyboardInterrupt, e:
70         ret = 94
71 finally:
72     if n:
73         n.stdin.close()
74         try:
75             n.wait()
76         except:
77             pass
78 sys.exit(ret)