blob: db0f469db0d3dcba1838338a6073f5a8a8767819 [file] [log] [blame]
Guido van Rossumec758ea1991-06-04 20:36:54 +00001#! /ufs/guido/bin/sgi/python
2#! /usr/local/python
3
4# Watch line printer queue(s).
5# Intended for BSD 4.3 lpq.
6
7import posix
8import sys
9import time
10import string
11
12DEF_PRINTER = 'psc'
13DEF_DELAY = 10
14
15def 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 Rossumc3f2a3b1991-07-01 18:21:32 +000022 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 Rossumec758ea1991-06-04 20:36:54 +000029 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 Rossum7030d0a1991-12-18 13:38:58 +000037 text = clearhome
Guido van Rossumec758ea1991-06-04 20:36:54 +000038 for name in printers:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000039 text = text + makestatus(name, thisuser) + '\n'
40 print text
Guido van Rossumec758ea1991-06-04 20:36:54 +000041 time.sleep(delay)
42
Guido van Rossum7030d0a1991-12-18 13:38:58 +000043def makestatus(name, thisuser):
44 pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
45 lines = []
Guido van Rossumec758ea1991-06-04 20:36:54 +000046 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 Rossum7030d0a1991-12-18 13:38:58 +000079 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 Rossumec758ea1991-06-04 20:36:54 +000086 if totaljobs:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000087 line = `(totalbytes+1023)/1024` + ' K'
Guido van Rossumec758ea1991-06-04 20:36:54 +000088 if totaljobs <> len(users):
Guido van Rossum7030d0a1991-12-18 13:38:58 +000089 line = line + ' (' + `totaljobs` + ' jobs)'
Guido van Rossumec758ea1991-06-04 20:36:54 +000090 if len(users) = 1:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000091 line = line + ' for ' + users.keys()[0]
Guido van Rossumec758ea1991-06-04 20:36:54 +000092 else:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000093 line = line + ' for ' + `len(users)` + ' users'
Guido van Rossumc3f2a3b1991-07-01 18:21:32 +000094 if userseen:
95 if aheadjobs = 0:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000096 line = line + ' (' + thisuser + ' first)'
Guido van Rossumc3f2a3b1991-07-01 18:21:32 +000097 else:
Guido van Rossum7030d0a1991-12-18 13:38:58 +000098 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 Rossumec758ea1991-06-04 20:36:54 +0000106
107try:
108 main()
109except KeyboardInterrupt:
110 pass