annotate Demo/scripts/lpwatch.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 41524f9e2a2f
children 6a5e0c913c7c
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
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import posix
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 import string
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 DEF_PRINTER = 'psc'
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 DEF_DELAY = 10
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 def main():
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
15 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
16 try:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
17 thisuser = posix.environ['LOGNAME']
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
18 except:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
19 thisuser = posix.environ['USER']
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
20 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
21 if printers:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
22 # 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
23 # the user specified it...
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
24 for i in range(len(printers)):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
25 if printers[i][:2] == '-P':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
26 printers[i] = printers[i][2:]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
27 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
28 if posix.environ.has_key('PRINTER'):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
29 printers = [posix.environ['PRINTER']]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
30 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
31 printers = [DEF_PRINTER]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
32 #
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
33 clearhome = posix.popen('clear', 'r').read()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
34 #
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
35 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
36 text = clearhome
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
37 for name in printers:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
38 text = text + makestatus(name, thisuser) + '\n'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
39 print text
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
40 time.sleep(delay)
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
502
67f6f71943c1 Modularized the code.
Guido van Rossum <guido@python.org>
parents: 297
diff changeset
42 def makestatus(name, thisuser):
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
43 pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
44 lines = []
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
45 users = {}
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
46 aheadbytes = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
47 aheadjobs = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
48 userseen = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
49 totalbytes = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
50 totaljobs = 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
51 while 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
52 line = pipe.readline()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
53 if not line: break
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
54 fields = string.split(line)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
55 n = len(fields)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
56 if len(fields) >= 6 and fields[n-1] == 'bytes':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
57 rank = fields[0]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
58 user = fields[1]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
59 job = fields[2]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
60 files = fields[3:-2]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
61 bytes = eval(fields[n-2])
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
62 if user == thisuser:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
63 userseen = 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
64 elif not userseen:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
65 aheadbytes = aheadbytes + bytes
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
66 aheadjobs = aheadjobs + 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
67 totalbytes = totalbytes + bytes
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
68 totaljobs = totaljobs + 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
69 if users.has_key(user):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
70 ujobs, ubytes = users[user]
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
71 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
72 ujobs, ubytes = 0, 0
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
73 ujobs = ujobs + 1
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
74 ubytes = ubytes + bytes
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
75 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
76 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
77 if fields and fields[0] <> 'Rank':
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
78 line = string.strip(line)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
79 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
80 line = name + ': idle'
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
81 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
82 line = name
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
83 lines.append(line)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
84 #
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
85 if totaljobs:
49164
41524f9e2a2f #687648 from Robert Schuppenies: use classic division.
Andrew M. Kuchling <amk@amk.ca>
parents: 33456
diff changeset
86 line = '%d K' % ((totalbytes+1023)//1024)
32735
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
87 if totaljobs <> len(users):
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
88 line = line + ' (%d jobs)' % totaljobs
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
89 if len(users) == 1:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
90 line = line + ' for %s' % (users.keys()[0],)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
91 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
92 line = line + ' for %d users' % len(users)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
93 if userseen:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
94 if aheadjobs == 0:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
95 line = line + ' (%s first)' % thisuser
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
96 else:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
97 line = line + ' (%d K before %s)' % (
49164
41524f9e2a2f #687648 from Robert Schuppenies: use classic division.
Andrew M. Kuchling <amk@amk.ca>
parents: 33456
diff changeset
98 (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
99 lines.append(line)
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
100 #
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
101 sts = pipe.close()
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
102 if sts:
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
103 lines.append('lpq exit status %r' % (sts,))
2bc66f1df7cc Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
104 return string.joinfields(lines, ': ')
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
33456
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
106 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
107 try:
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
108 main()
487280cf71b9 Add 'if __name__ == "__main__":' to files already as a usable as a module.
Johannes Gijsbers <jlg@dds.nl>
parents: 32735
diff changeset
109 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
110 pass