|
3 | 3 | # Watch line printer queue(s). |
4 | 4 | # Intended for BSD 4.3 lpq. |
5 | 5 |
|
6 | | -import posix |
| 6 | +import os |
7 | 7 | import sys |
8 | 8 | import time |
9 | | -import string |
10 | 9 |
|
11 | 10 | DEF_PRINTER = 'psc' |
12 | 11 | DEF_DELAY = 10 |
13 | 12 |
|
14 | 13 | def main(): |
15 | 14 | delay = DEF_DELAY # XXX Use getopt() later |
16 | 15 | try: |
17 | | - thisuser = posix.environ['LOGNAME'] |
| 16 | + thisuser = os.environ['LOGNAME'] |
18 | 17 | except: |
19 | | - thisuser = posix.environ['USER'] |
| 18 | + thisuser = os.environ['USER'] |
20 | 19 | printers = sys.argv[1:] |
21 | 20 | if printers: |
22 | 21 | # Strip '-P' from printer names just in case |
23 | 22 | # the user specified it... |
24 | | - for i in range(len(printers)): |
25 | | - if printers[i][:2] == '-P': |
26 | | - printers[i] = printers[i][2:] |
| 23 | + for i, name in enumerate(printers): |
| 24 | + if name[:2] == '-P': |
| 25 | + printers[i] = name[2:] |
27 | 26 | else: |
28 | | - if 'PRINTER' in posix.environ: |
29 | | - printers = [posix.environ['PRINTER']] |
| 27 | + if 'PRINTER' in os.environ: |
| 28 | + printers = [os.environ['PRINTER']] |
30 | 29 | else: |
31 | 30 | printers = [DEF_PRINTER] |
32 | | - # |
33 | | - clearhome = posix.popen('clear', 'r').read() |
34 | | - # |
35 | | - while 1: |
| 31 | + |
| 32 | + clearhome = os.popen('clear', 'r').read() |
| 33 | + |
| 34 | + while True: |
36 | 35 | text = clearhome |
37 | 36 | for name in printers: |
38 | | - text = text + makestatus(name, thisuser) + '\n' |
| 37 | + text += makestatus(name, thisuser) + '\n' |
39 | 38 | print(text) |
40 | 39 | time.sleep(delay) |
41 | 40 |
|
42 | 41 | def makestatus(name, thisuser): |
43 | | - pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r') |
| 42 | + pipe = os.popen('lpq -P' + name + ' 2>&1', 'r') |
44 | 43 | lines = [] |
45 | 44 | users = {} |
46 | 45 | aheadbytes = 0 |
47 | 46 | aheadjobs = 0 |
48 | | - userseen = 0 |
| 47 | + userseen = False |
49 | 48 | totalbytes = 0 |
50 | 49 | totaljobs = 0 |
51 | | - while 1: |
52 | | - line = pipe.readline() |
53 | | - if not line: break |
54 | | - fields = string.split(line) |
| 50 | + for line in pipe: |
| 51 | + fields = line.split() |
55 | 52 | n = len(fields) |
56 | 53 | if len(fields) >= 6 and fields[n-1] == 'bytes': |
57 | | - rank = fields[0] |
58 | | - user = fields[1] |
59 | | - job = fields[2] |
| 54 | + rank, user, job = fields[0:3] |
60 | 55 | files = fields[3:-2] |
61 | | - bytes = eval(fields[n-2]) |
| 56 | + bytes = int(fields[n-2]) |
62 | 57 | if user == thisuser: |
63 | | - userseen = 1 |
| 58 | + userseen = True |
64 | 59 | elif not userseen: |
65 | | - aheadbytes = aheadbytes + bytes |
66 | | - aheadjobs = aheadjobs + 1 |
67 | | - totalbytes = totalbytes + bytes |
68 | | - totaljobs = totaljobs + 1 |
69 | | - if user in users: |
70 | | - ujobs, ubytes = users[user] |
71 | | - else: |
72 | | - ujobs, ubytes = 0, 0 |
73 | | - ujobs = ujobs + 1 |
74 | | - ubytes = ubytes + bytes |
| 60 | + aheadbytes += bytes |
| 61 | + aheadjobs += 1 |
| 62 | + totalbytes += bytes |
| 63 | + totaljobs += 1 |
| 64 | + ujobs, ubytes = users.get(user, (0, 0)) |
| 65 | + ujobs += 1 |
| 66 | + ubytes += bytes |
75 | 67 | users[user] = ujobs, ubytes |
76 | 68 | else: |
77 | 69 | if fields and fields[0] != 'Rank': |
78 | | - line = string.strip(line) |
| 70 | + line = line.strip() |
79 | 71 | if line == 'no entries': |
80 | 72 | line = name + ': idle' |
81 | 73 | elif line[-22:] == ' is ready and printing': |
82 | 74 | line = name |
83 | 75 | lines.append(line) |
84 | | - # |
| 76 | + |
85 | 77 | if totaljobs: |
86 | | - line = '%d K' % ((totalbytes+1023)//1024) |
| 78 | + line = '%d K' % ((totalbytes+1023) // 1024) |
87 | 79 | if totaljobs != len(users): |
88 | | - line = line + ' (%d jobs)' % totaljobs |
| 80 | + line += ' (%d jobs)' % totaljobs |
89 | 81 | if len(users) == 1: |
90 | | - line = line + ' for %s' % (list(users.keys())[0],) |
| 82 | + line += ' for %s' % next(iter(users)) |
91 | 83 | else: |
92 | | - line = line + ' for %d users' % len(users) |
| 84 | + line += ' for %d users' % len(users) |
93 | 85 | if userseen: |
94 | 86 | if aheadjobs == 0: |
95 | | - line = line + ' (%s first)' % thisuser |
| 87 | + line += ' (%s first)' % thisuser |
96 | 88 | else: |
97 | | - line = line + ' (%d K before %s)' % ( |
98 | | - (aheadbytes+1023)//1024, thisuser) |
| 89 | + line += ' (%d K before %s)' % ( |
| 90 | + (aheadbytes+1023) // 1024, thisuser) |
99 | 91 | lines.append(line) |
100 | | - # |
| 92 | + |
101 | 93 | sts = pipe.close() |
102 | 94 | if sts: |
103 | 95 | lines.append('lpq exit status %r' % (sts,)) |
104 | | - return string.joinfields(lines, ': ') |
| 96 | + return ': '.join(lines) |
105 | 97 |
|
106 | 98 | if __name__ == "__main__": |
107 | 99 | try: |
|
0 commit comments