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: |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 37 | text = clearhome |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 38 | for name in printers: |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 39 | text = text + makestatus(name, thisuser) + '\n' |
| 40 | print text |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 41 | time.sleep(delay) |
| 42 | |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 43 | def makestatus(name, thisuser): |
| 44 | pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r') |
| 45 | lines = [] |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 46 | users = {} |
| 47 | aheadbytes = 0 |
| 48 | aheadjobs = 0 |
| 49 | userseen = 0 |
| 50 | totalbytes = 0 |
| 51 | totaljobs = 0 |
| 52 | while 1: |
| 53 | line = pipe.readline() |
| 54 | if not line: break |
| 55 | fields = string.split(line) |
| 56 | n = len(fields) |
| 57 | if len(fields) >= 6 and fields[n-1] = 'bytes': |
| 58 | rank = fields[0] |
| 59 | user = fields[1] |
| 60 | job = fields[2] |
| 61 | files = fields[3:-2] |
| 62 | bytes = eval(fields[n-2]) |
| 63 | if user = thisuser: |
| 64 | userseen = 1 |
| 65 | elif not userseen: |
| 66 | aheadbytes = aheadbytes + bytes |
| 67 | aheadjobs = aheadjobs + 1 |
| 68 | totalbytes = totalbytes + bytes |
| 69 | totaljobs = totaljobs + 1 |
| 70 | if users.has_key(user): |
| 71 | ujobs, ubytes = users[user] |
| 72 | else: |
| 73 | ujobs, ubytes = 0, 0 |
| 74 | ujobs = ujobs + 1 |
| 75 | ubytes = ubytes + bytes |
| 76 | users[user] = ujobs, ubytes |
| 77 | else: |
| 78 | if fields and fields[0] <> 'Rank': |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 79 | line = string.strip(line) |
| 80 | if line = 'no entries': |
| 81 | line = name + ': idle' |
| 82 | elif line[-22:] = ' is ready and printing': |
| 83 | line = name |
| 84 | lines.append(line) |
| 85 | # |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 86 | if totaljobs: |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 87 | line = `(totalbytes+1023)/1024` + ' K' |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 88 | if totaljobs <> len(users): |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 89 | line = line + ' (' + `totaljobs` + ' jobs)' |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 90 | if len(users) = 1: |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 91 | line = line + ' for ' + users.keys()[0] |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 92 | else: |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 93 | line = line + ' for ' + `len(users)` + ' users' |
Guido van Rossum | c3f2a3b | 1991-07-01 18:21:32 +0000 | [diff] [blame] | 94 | if userseen: |
| 95 | if aheadjobs = 0: |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 96 | line = line + ' (' + thisuser + ' first)' |
Guido van Rossum | c3f2a3b | 1991-07-01 18:21:32 +0000 | [diff] [blame] | 97 | else: |
Guido van Rossum | 7030d0a | 1991-12-18 13:38:58 +0000 | [diff] [blame^] | 98 | line = line + ' (' + `(aheadbytes+1023)/1024` |
| 99 | line = line + ' K before ' + thisuser + ')' |
| 100 | lines.append(line) |
| 101 | # |
| 102 | sts = pipe.close() |
| 103 | if sts: |
| 104 | lines.append('lpq exit status ' + `sts`) |
| 105 | return string.joinfields(lines, ': ') |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 106 | |
| 107 | try: |
| 108 | main() |
| 109 | except KeyboardInterrupt: |
| 110 | pass |