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:] |
Guido van Rossum | c3f2a3b | 1991-07-01 18:21:32 +0000 | [diff] [blame] | 22 | if printers: |
| 23 | # Strip '-P' from printer names just in case |
| 24 | # the user specified it... |
| 25 | for i in range(len(printers)): |
| 26 | if printers[i][:2] = '-P': |
| 27 | printers[i] = printers[i][2:] |
| 28 | else: |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 29 | if posix.environ.has_key('PRINTER'): |
| 30 | printers = [posix.environ['PRINTER']] |
| 31 | else: |
| 32 | printers = [DEF_PRINTER] |
| 33 | # |
| 34 | clearhome = posix.popen('clear', 'r').read() |
| 35 | # |
| 36 | while 1: |
| 37 | # Pipe output through cat for extra buffering, |
| 38 | # so the output (which overwrites the previous) |
| 39 | # appears instantaneous. |
| 40 | sys.stdout = posix.popen('exec cat', 'w') |
| 41 | sys.stdout.write(clearhome) |
| 42 | for name in printers: |
| 43 | pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r') |
| 44 | showstatus(name, pipe, thisuser) |
| 45 | sts = pipe.close() |
| 46 | if sts: |
| 47 | print name + ': *** lpq exit status', sts |
| 48 | sts = sys.stdout.close() |
| 49 | time.sleep(delay) |
| 50 | |
| 51 | def showstatus(name, pipe, thisuser): |
| 52 | lines = 0 |
| 53 | users = {} |
| 54 | aheadbytes = 0 |
| 55 | aheadjobs = 0 |
| 56 | userseen = 0 |
| 57 | totalbytes = 0 |
| 58 | totaljobs = 0 |
| 59 | while 1: |
| 60 | line = pipe.readline() |
| 61 | if not line: break |
| 62 | fields = string.split(line) |
| 63 | n = len(fields) |
| 64 | if len(fields) >= 6 and fields[n-1] = 'bytes': |
| 65 | rank = fields[0] |
| 66 | user = fields[1] |
| 67 | job = fields[2] |
| 68 | files = fields[3:-2] |
| 69 | bytes = eval(fields[n-2]) |
| 70 | if user = thisuser: |
| 71 | userseen = 1 |
| 72 | elif not userseen: |
| 73 | aheadbytes = aheadbytes + bytes |
| 74 | aheadjobs = aheadjobs + 1 |
| 75 | totalbytes = totalbytes + bytes |
| 76 | totaljobs = totaljobs + 1 |
| 77 | if users.has_key(user): |
| 78 | ujobs, ubytes = users[user] |
| 79 | else: |
| 80 | ujobs, ubytes = 0, 0 |
| 81 | ujobs = ujobs + 1 |
| 82 | ubytes = ubytes + bytes |
| 83 | users[user] = ujobs, ubytes |
| 84 | else: |
| 85 | if fields and fields[0] <> 'Rank': |
| 86 | if line[-1:] = '\n': |
| 87 | line = line[:-1] |
| 88 | if not lines: |
| 89 | print name + ':', |
| 90 | else: |
| 91 | print |
| 92 | print line, |
| 93 | lines = lines + 1 |
| 94 | if totaljobs: |
| 95 | if lines > 1: |
| 96 | print |
| 97 | lines = lines+1 |
| 98 | print (totalbytes+1023)/1024, 'K', |
| 99 | if totaljobs <> len(users): |
| 100 | print '(' + `totaljobs` + ' jobs)', |
| 101 | if len(users) = 1: |
| 102 | print 'for', users.keys()[0], |
| 103 | else: |
| 104 | print 'for', len(users), 'users', |
Guido van Rossum | c3f2a3b | 1991-07-01 18:21:32 +0000 | [diff] [blame] | 105 | if userseen: |
| 106 | if aheadjobs = 0: |
| 107 | print '(' + thisuser + ' first)', |
| 108 | else: |
| 109 | print '(' + `(aheadbytes+1023)/1024`, |
| 110 | print 'K before', thisuser + ')' |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 111 | if lines: |
| 112 | print |
| 113 | |
| 114 | try: |
| 115 | main() |
| 116 | except KeyboardInterrupt: |
| 117 | pass |