Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 1 | #! /ufs/guido/bin/sgi/python |
| 2 | #! /usr/local/python |
| 3 | |
| 4 | # Watch line printer queue(s). |
| 5 | # Intended for BSD 4.3 lpq. |
| 6 | |
| 7 | import posix |
| 8 | import sys |
| 9 | import time |
| 10 | import string |
| 11 | |
| 12 | DEF_PRINTER = 'psc' |
| 13 | DEF_DELAY = 10 |
| 14 | |
| 15 | def main(): |
| 16 | delay = DEF_DELAY # XXX Use getopt() later |
| 17 | try: |
| 18 | thisuser = posix.environ['LOGNAME'] |
| 19 | except: |
| 20 | thisuser = posix.environ['USER'] |
| 21 | printers = sys.argv[1:] |
| 22 | if not printers: |
| 23 | if posix.environ.has_key('PRINTER'): |
| 24 | printers = [posix.environ['PRINTER']] |
| 25 | else: |
| 26 | printers = [DEF_PRINTER] |
| 27 | # |
| 28 | clearhome = posix.popen('clear', 'r').read() |
| 29 | # |
| 30 | while 1: |
| 31 | # Pipe output through cat for extra buffering, |
| 32 | # so the output (which overwrites the previous) |
| 33 | # appears instantaneous. |
| 34 | sys.stdout = posix.popen('exec cat', 'w') |
| 35 | sys.stdout.write(clearhome) |
| 36 | for name in printers: |
| 37 | pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r') |
| 38 | showstatus(name, pipe, thisuser) |
| 39 | sts = pipe.close() |
| 40 | if sts: |
| 41 | print name + ': *** lpq exit status', sts |
| 42 | sts = sys.stdout.close() |
| 43 | time.sleep(delay) |
| 44 | |
| 45 | def showstatus(name, pipe, thisuser): |
| 46 | lines = 0 |
| 47 | users = {} |
| 48 | aheadbytes = 0 |
| 49 | aheadjobs = 0 |
| 50 | userseen = 0 |
| 51 | totalbytes = 0 |
| 52 | totaljobs = 0 |
| 53 | while 1: |
| 54 | line = pipe.readline() |
| 55 | if not line: break |
| 56 | fields = string.split(line) |
| 57 | n = len(fields) |
| 58 | if len(fields) >= 6 and fields[n-1] = 'bytes': |
| 59 | rank = fields[0] |
| 60 | user = fields[1] |
| 61 | job = fields[2] |
| 62 | files = fields[3:-2] |
| 63 | bytes = eval(fields[n-2]) |
| 64 | if user = thisuser: |
| 65 | userseen = 1 |
| 66 | elif not userseen: |
| 67 | aheadbytes = aheadbytes + bytes |
| 68 | aheadjobs = aheadjobs + 1 |
| 69 | totalbytes = totalbytes + bytes |
| 70 | totaljobs = totaljobs + 1 |
| 71 | if users.has_key(user): |
| 72 | ujobs, ubytes = users[user] |
| 73 | else: |
| 74 | ujobs, ubytes = 0, 0 |
| 75 | ujobs = ujobs + 1 |
| 76 | ubytes = ubytes + bytes |
| 77 | users[user] = ujobs, ubytes |
| 78 | else: |
| 79 | if fields and fields[0] <> 'Rank': |
| 80 | if line[-1:] = '\n': |
| 81 | line = line[:-1] |
| 82 | if not lines: |
| 83 | print name + ':', |
| 84 | else: |
| 85 | print |
| 86 | print line, |
| 87 | lines = lines + 1 |
| 88 | if totaljobs: |
| 89 | if lines > 1: |
| 90 | print |
| 91 | lines = lines+1 |
| 92 | print (totalbytes+1023)/1024, 'K', |
| 93 | if totaljobs <> len(users): |
| 94 | print '(' + `totaljobs` + ' jobs)', |
| 95 | if len(users) = 1: |
| 96 | print 'for', users.keys()[0], |
| 97 | else: |
| 98 | print 'for', len(users), 'users', |
| 99 | if userseen: |
| 100 | if aheadjobs = 0: |
| 101 | print '(' + thisuser + ' first)', |
| 102 | else: |
| 103 | print '(' + `(aheadbytes+1023)/1024`, |
| 104 | print 'K before', thisuser + ')' |
| 105 | if lines: |
| 106 | print |
| 107 | |
| 108 | try: |
| 109 | main() |
| 110 | except KeyboardInterrupt: |
| 111 | pass |