| 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]
|
|---|
|
|---|