1 | #! /usr/bin/env python
|
---|
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:]
|
---|
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)
|
---|
41 |
|
---|
42 | def makestatus(name, thisuser):
|
---|
43 | 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:
|
---|
86 | line = '%d K' % ((totalbytes+1023)//1024)
|
---|
87 | 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)' % (
|
---|
98 | (aheadbytes+1023)//1024, thisuser)
|
---|
99 | lines.append(line)
|
---|
100 | #
|
---|
101 | sts = pipe.close()
|
---|
102 | if sts:
|
---|
103 | lines.append('lpq exit status %r' % (sts,))
|
---|
104 | return string.joinfields(lines, ': ')
|
---|
105 |
|
---|
106 | if __name__ == "__main__":
|
---|
107 | try:
|
---|
108 | main()
|
---|
109 | except KeyboardInterrupt:
|
---|
110 | pass
|
---|