|
Revision 649, 0.6 kB
(checked in by hodgestar, 3 years ago)
|
|
Tiny utility for tapping into stdout occassionally.
|
-
Property svn:mime-type set to
text/python-source
-
Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import signal |
|---|
| 4 | import sys |
|---|
| 5 | |
|---|
| 6 | TAP_ON = False |
|---|
| 7 | CHUNK_SIZE = 10 |
|---|
| 8 | |
|---|
| 9 | def tap_handler(signo, frame): |
|---|
| 10 | global TAP_ON |
|---|
| 11 | TAP_ON = not TAP_ON |
|---|
| 12 | |
|---|
| 13 | def main(args): |
|---|
| 14 | if len(args) < 2: |
|---|
| 15 | print "Usage: stdtap.py <file>" |
|---|
| 16 | return 1 |
|---|
| 17 | |
|---|
| 18 | f = open(args[1],"w") |
|---|
| 19 | filewrite = f.write |
|---|
| 20 | fileflush = f.flush |
|---|
| 21 | |
|---|
| 22 | signal.signal(signal.SIGHUP, tap_handler) |
|---|
| 23 | write = sys.stdout.write |
|---|
| 24 | read = sys.stdin.read |
|---|
| 25 | |
|---|
| 26 | while True: |
|---|
| 27 | buf = read(CHUNK_SIZE) |
|---|
| 28 | write(buf) |
|---|
| 29 | if TAP_ON: |
|---|
| 30 | filewrite(buf) |
|---|
| 31 | fileflush() |
|---|
| 32 | |
|---|
| 33 | if __name__ == "__main__": |
|---|
| 34 | sys.exit(main(sys.argv)) |
|---|