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