blob: 715cbb8ff29c5cdf4c4dcdca6a22b88070c2ec2d [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():
Tim Peterse6ddc8b2004-07-18 05:56:09 +000015 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:]
21 if printers:
22 # Strip '-P' from printer names just in case
23 # the user specified it...
24 for i in range(len(printers)):
25 if printers[i][:2] == '-P':
26 printers[i] = printers[i][2:]
27 else:
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:
36 text = clearhome
37 for name in printers:
38 text = text + makestatus(name, thisuser) + '\n'
39 print text
40 time.sleep(delay)
Guido van Rossumec758ea1991-06-04 20:36:54 +000041
Guido van Rossum7030d0a1991-12-18 13:38:58 +000042def makestatus(name, thisuser):
Tim Peterse6ddc8b2004-07-18 05:56:09 +000043 pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
44 lines = []
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)
56 if len(fields) >= 6 and fields[n-1] == 'bytes':
57 rank = fields[0]
58 user = fields[1]
59 job = fields[2]
60 files = fields[3:-2]
61 bytes = eval(fields[n-2])
62 if user == thisuser:
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':
78 line = string.strip(line)
79 if line == 'no entries':
80 line = name + ': idle'
81 elif line[-22:] == ' is ready and printing':
82 line = name
83 lines.append(line)
84 #
85 if totaljobs:
Andrew M. Kuchling2a9b9cb2008-09-13 01:43:28 +000086 line = '%d K' % ((totalbytes+1023)//1024)
Tim Peterse6ddc8b2004-07-18 05:56:09 +000087 if totaljobs <> len(users):
88 line = line + ' (%d jobs)' % totaljobs
89 if len(users) == 1:
90 line = line + ' for %s' % (users.keys()[0],)
91 else:
92 line = line + ' for %d users' % len(users)
93 if userseen:
94 if aheadjobs == 0:
95 line = line + ' (%s first)' % thisuser
96 else:
97 line = line + ' (%d K before %s)' % (
Andrew M. Kuchling2a9b9cb2008-09-13 01:43:28 +000098 (aheadbytes+1023)//1024, thisuser)
Tim Peterse6ddc8b2004-07-18 05:56:09 +000099 lines.append(line)
100 #
101 sts = pipe.close()
102 if sts:
103 lines.append('lpq exit status %r' % (sts,))
104 return string.joinfields(lines, ': ')
Guido van Rossumec758ea1991-06-04 20:36:54 +0000105
Johannes Gijsbers7a8c43e2004-09-11 16:34:35 +0000106if __name__ == "__main__":
107 try:
108 main()
109 except KeyboardInterrupt:
110 pass