]> arthur.barton.de Git - bup.git/blob - cmd/newliner-cmd.py
cleanup-mounts-under: Don't fail when /proc/mounts isn't readable
[bup.git] / cmd / newliner-cmd.py
1 #!/usr/bin/env python
2 import sys, os, re
3 from bup import options
4 from bup import _helpers   # fixes up sys.argv on import
5
6 optspec = """
7 bup newliner
8 """
9 o = options.Options(optspec)
10 (opt, flags, extra) = o.parse(sys.argv[1:])
11
12 if extra:
13     o.fatal("no arguments expected")
14
15 r = re.compile(r'([\r\n])')
16 lastlen = 0
17 all = ''
18 width = options._tty_width() or 78
19 while 1:
20     l = r.split(all, 1)
21     if len(l) <= 1:
22         if len(all) >= 160:
23             sys.stdout.write('%s\n' % all[:78])
24             sys.stdout.flush()
25             all = all[78:]
26         try:
27             b = os.read(sys.stdin.fileno(), 4096)
28         except KeyboardInterrupt:
29             break
30         if not b:
31             break
32         all += b
33     else:
34         assert(len(l) == 3)
35         (line, splitchar, all) = l
36         if splitchar == '\r':
37             line = line[:width]
38         sys.stdout.write('%-*s%s' % (lastlen, line, splitchar))
39         if splitchar == '\r':
40             lastlen = len(line)
41         else:
42             lastlen = 0
43         sys.stdout.flush()
44
45 if lastlen:
46     sys.stdout.write('%-*s\r' % (lastlen, ''))
47 if all:
48     sys.stdout.write('%s\n' % all)