blob: 9f051ebbf396c31d2f2ac0dbf7ff36fc0d5b3335 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumec758ea1991-06-04 20:36:54 +00002
3# Watch line printer queue(s).
4# Intended for BSD 4.3 lpq.
5
6import posix
7import sys
8import time
9import string
10
11DEF_PRINTER = 'psc'
12DEF_DELAY = 10
13
14def 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 Rossumc3f2a3b1991-07-01 18:21:32 +000021 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 Rossumbdfcfcc1992-01-01 19:35:13 +000025 if printers[i][:2] == '-P':
Guido van Rossumc3f2a3b1991-07-01 18:21:32 +000026 printers[i] = printers[i][2:]
27 else:
Guido van Rossumec758ea1991-06-04 20:36:54 +000028 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 Rossum7030d0a1991-12-18 13:38:58 +000036 text = clearhome
Guido van Rossumec758ea1991-06-04 20:36:54 +000037 for name in printers:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000038 text = text + makestatus(name, thisuser) + '\n'
39 print text
Guido van Rossumec758ea1991-06-04 20:36:54 +000040 time.sleep(delay)
41
Guido van Rossum7030d0a1991-12-18 13:38:58 +000042def makestatus(name, thisuser):
43 pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
44 lines = []
Guido van Rossumec758ea1991-06-04 20:36:54 +000045 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 Rossumbdfcfcc1992-01-01 19:35:13 +000056 if len(fields) >= 6 and fields[n-1] == 'bytes':
Guido van Rossumec758ea1991-06-04 20:36:54 +000057 rank = fields[0]
58 user = fields[1]
59 job = fields[2]
60 files = fields[3:-2]
61 bytes = eval(fields[n-2])
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000062 if user == thisuser:
Guido van Rossumec758ea1991-06-04 20:36:54 +000063 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 Rossum7030d0a1991-12-18 13:38:58 +000078 line = string.strip(line)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000079 if line == 'no entries':
Guido van Rossum7030d0a1991-12-18 13:38:58 +000080 line = name + ': idle'
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000081 elif line[-22:] == ' is ready and printing':
Guido van Rossum7030d0a1991-12-18 13:38:58 +000082 line = name
83 lines.append(line)
84 #
Guido van Rossumec758ea1991-06-04 20:36:54 +000085 if totaljobs:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000086 line = `(totalbytes+1023)/1024` + ' K'
Guido van Rossumec758ea1991-06-04 20:36:54 +000087 if totaljobs <> len(users):
Guido van Rossum7030d0a1991-12-18 13:38:58 +000088 line = line + ' (' + `totaljobs` + ' jobs)'
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000089 if len(users) == 1:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000090 line = line + ' for ' + users.keys()[0]
Guido van Rossumec758ea1991-06-04 20:36:54 +000091 else:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000092 line = line + ' for ' + `len(users)` + ' users'
Guido van Rossumc3f2a3b1991-07-01 18:21:32 +000093 if userseen:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000094 if aheadjobs == 0:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000095 line = line + ' (' + thisuser + ' first)'
Guido van Rossumc3f2a3b1991-07-01 18:21:32 +000096 else:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000097 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 Rossumec758ea1991-06-04 20:36:54 +0000105
106try:
107 main()
108except KeyboardInterrupt:
109 pass