Mercurial > cpython-lru_cache-weakref
annotate Demo/scripts/lpwatch.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | ef082c1072a7 |
| children | 73758db2f464 |
| rev | line source |
|---|---|
| 1267 | 1 #! /usr/local/bin/python |
| 274 | 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:] | |
|
297
abccb0f9f735
Strip redundant -P from arguments; only print 'ahead' count
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
21 if printers: |
|
abccb0f9f735
Strip redundant -P from arguments; only print 'ahead' count
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
22 # Strip '-P' from printer names just in case |
|
abccb0f9f735
Strip redundant -P from arguments; only print 'ahead' count
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
23 # the user specified it... |
|
abccb0f9f735
Strip redundant -P from arguments; only print 'ahead' count
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
24 for i in range(len(printers)): |
| 559 | 25 if printers[i][:2] == '-P': |
|
297
abccb0f9f735
Strip redundant -P from arguments; only print 'ahead' count
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
26 printers[i] = printers[i][2:] |
|
abccb0f9f735
Strip redundant -P from arguments; only print 'ahead' count
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
27 else: |
| 274 | 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: | |
| 502 | 36 text = clearhome |
| 274 | 37 for name in printers: |
| 502 | 38 text = text + makestatus(name, thisuser) + '\n' |
| 39 print text | |
| 274 | 40 time.sleep(delay) |
| 41 | |
| 502 | 42 def makestatus(name, thisuser): |
| 43 pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r') | |
| 44 lines = [] | |
| 274 | 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) | |
| 559 | 56 if len(fields) >= 6 and fields[n-1] == 'bytes': |
| 274 | 57 rank = fields[0] |
| 58 user = fields[1] | |
| 59 job = fields[2] | |
| 60 files = fields[3:-2] | |
| 61 bytes = eval(fields[n-2]) | |
| 559 | 62 if user == thisuser: |
| 274 | 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': | |
| 502 | 78 line = string.strip(line) |
| 559 | 79 if line == 'no entries': |
| 502 | 80 line = name + ': idle' |
| 559 | 81 elif line[-22:] == ' is ready and printing': |
| 502 | 82 line = name |
| 83 lines.append(line) | |
| 84 # | |
| 274 | 85 if totaljobs: |
| 502 | 86 line = `(totalbytes+1023)/1024` + ' K' |
| 274 | 87 if totaljobs <> len(users): |
| 502 | 88 line = line + ' (' + `totaljobs` + ' jobs)' |
| 559 | 89 if len(users) == 1: |
| 502 | 90 line = line + ' for ' + users.keys()[0] |
| 274 | 91 else: |
| 502 | 92 line = line + ' for ' + `len(users)` + ' users' |
|
297
abccb0f9f735
Strip redundant -P from arguments; only print 'ahead' count
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
93 if userseen: |
| 559 | 94 if aheadjobs == 0: |
| 502 | 95 line = line + ' (' + thisuser + ' first)' |
|
297
abccb0f9f735
Strip redundant -P from arguments; only print 'ahead' count
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
96 else: |
| 502 | 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, ': ') | |
| 274 | 105 |
| 106 try: | |
| 107 main() | |
| 108 except KeyboardInterrupt: | |
| 109 pass |
