annotate Demo/scripts/lpwatch.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 6a5e0c913c7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 1506
diff changeset
1 #! /usr/bin/env python
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Watch line printer queue(s).
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # Intended for BSD 4.3 lpq.
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
6 import os
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import sys
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import time
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 DEF_PRINTER = 'psc'
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 DEF_DELAY = 10
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 def main():
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
14 delay = DEF_DELAY # XXX Use getopt() later
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
15 try:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
16 thisuser = os.environ['LOGNAME']
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
17 except:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
18 thisuser = os.environ['USER']
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
19 printers = sys.argv[1:]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
20 if printers:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
21 # Strip '-P' from printer names just in case
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
22 # the user specified it...
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
23 for i, name in enumerate(printers):
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
24 if name[:2] == '-P':
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
25 printers[i] = name[2:]
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
26 else:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
27 if os.environ.has_key('PRINTER'):
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
28 printers = [os.environ['PRINTER']]
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
29 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
30 printers = [DEF_PRINTER]
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
31
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
32 clearhome = os.popen('clear', 'r').read()
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
33
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
34 while True:
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
35 text = clearhome
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
36 for name in printers:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
37 text += makestatus(name, thisuser) + '\n'
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
38 print text
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
39 time.sleep(delay)
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
502
67f6f71943c1 Modularized the code.
Guido van Rossum <guido@python.org>
parents: 297
diff changeset
41 def makestatus(name, thisuser):
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
42 pipe = os.popen('lpq -P' + name + ' 2>&1', 'r')
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
43 lines = []
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
44 users = {}
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
45 aheadbytes = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
46 aheadjobs = 0
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
47 userseen = False
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
48 totalbytes = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
49 totaljobs = 0
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
50 for line in pipe:
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
51 fields = line.split()
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
52 n = len(fields)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
53 if len(fields) >= 6 and fields[n-1] == 'bytes':
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
54 rank, user, job = fields[0:3]
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
55 files = fields[3:-2]
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
56 bytes = int(fields[n-2])
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
57 if user == thisuser:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
58 userseen = True
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
59 elif not userseen:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
60 aheadbytes += bytes
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
61 aheadjobs += 1
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
62 totalbytes += bytes
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
63 totaljobs += 1
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
64 ujobs, ubytes = users.get(user, (0, 0))
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
65 ujobs += 1
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
66 ubytes += bytes
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
67 users[user] = ujobs, ubytes
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
68 else:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
69 if fields and fields[0] != 'Rank':
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
70 line = line.strip()
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
71 if line == 'no entries':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
72 line = name + ': idle'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
73 elif line[-22:] == ' is ready and printing':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
74 line = name
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
75 lines.append(line)
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
76
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
77 if totaljobs:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
78 line = '%d K' % ((totalbytes+1023) // 1024)
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
79 if totaljobs != len(users):
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
80 line += ' (%d jobs)' % totaljobs
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
81 if len(users) == 1:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
82 line += ' for %s' % (users.keys()[0],)
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
83 else:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
84 line += ' for %d users' % len(users)
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
85 if userseen:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
86 if aheadjobs == 0:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
87 line += ' (%s first)' % thisuser
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
88 else:
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
89 line += ' (%d K before %s)' % (
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
90 (aheadbytes+1023) // 1024, thisuser)
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
91 lines.append(line)
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
92
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
93 sts = pipe.close()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
94 if sts:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
95 lines.append('lpq exit status %r' % (sts,))
56484
6a5e0c913c7c Update lpwatch script.
Georg Brandl <georg@python.org>
parents: 49164
diff changeset
96 return ': '.join(lines)
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
33456
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
98 if __name__ == "__main__":
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
99 try:
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
100 main()
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
101 except KeyboardInterrupt:
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
102 pass