Mercurial > cpython-lru_cache-weakref
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 |
| 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 | 2 |
| 3 # Watch line printer queue(s). | |
| 4 # Intended for BSD 4.3 lpq. | |
| 5 | |
| 56484 | 6 import os |
| 274 | 7 import sys |
| 8 import time | |
| 9 | |
| 10 DEF_PRINTER = 'psc' | |
| 11 DEF_DELAY = 10 | |
| 12 | |
| 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 | 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 | 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 | 23 for i, name in enumerate(printers): |
| 24 if name[:2] == '-P': | |
| 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 | 27 if os.environ.has_key('PRINTER'): |
| 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 | 31 |
| 32 clearhome = os.popen('clear', 'r').read() | |
| 33 | |
| 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 | 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 | 40 |
| 502 | 41 def makestatus(name, thisuser): |
| 56484 | 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 | 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 | 50 for line in pipe: |
| 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 | 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 | 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 | 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 | 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 | |
|
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 | 69 if fields and fields[0] != 'Rank': |
| 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 | 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 | 78 line = '%d K' % ((totalbytes+1023) // 1024) |
| 79 if totaljobs != len(users): | |
| 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 | 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 | 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 | 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 | 89 line += ' (%d K before %s)' % ( |
| 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 | 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 | 96 return ': '.join(lines) |
| 274 | 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 |
