Update lpwatch script.

This commit is contained in:
Georg Brandl 2009-10-11 14:23:49 +00:00
parent 8ec30e835f
commit 393c823245
3 changed files with 44 additions and 52 deletions

View File

@ -3,10 +3,9 @@
# Watch line printer queue(s). # Watch line printer queue(s).
# Intended for BSD 4.3 lpq. # Intended for BSD 4.3 lpq.
import posix import os
import sys import sys
import time import time
import string
DEF_PRINTER = 'psc' DEF_PRINTER = 'psc'
DEF_DELAY = 10 DEF_DELAY = 10
@ -14,94 +13,87 @@ DEF_DELAY = 10
def main(): def main():
delay = DEF_DELAY # XXX Use getopt() later delay = DEF_DELAY # XXX Use getopt() later
try: try:
thisuser = posix.environ['LOGNAME'] thisuser = os.environ['LOGNAME']
except: except:
thisuser = posix.environ['USER'] thisuser = os.environ['USER']
printers = sys.argv[1:] printers = sys.argv[1:]
if printers: if printers:
# Strip '-P' from printer names just in case # Strip '-P' from printer names just in case
# the user specified it... # the user specified it...
for i in range(len(printers)): for i, name in enumerate(printers):
if printers[i][:2] == '-P': if name[:2] == '-P':
printers[i] = printers[i][2:] printers[i] = name[2:]
else: else:
if posix.environ.has_key('PRINTER'): if os.environ.has_key('PRINTER'):
printers = [posix.environ['PRINTER']] printers = [os.environ['PRINTER']]
else: else:
printers = [DEF_PRINTER] printers = [DEF_PRINTER]
#
clearhome = posix.popen('clear', 'r').read() clearhome = os.popen('clear', 'r').read()
#
while 1: while True:
text = clearhome text = clearhome
for name in printers: for name in printers:
text = text + makestatus(name, thisuser) + '\n' text += makestatus(name, thisuser) + '\n'
print text print text
time.sleep(delay) time.sleep(delay)
def makestatus(name, thisuser): def makestatus(name, thisuser):
pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r') pipe = os.popen('lpq -P' + name + ' 2>&1', 'r')
lines = [] lines = []
users = {} users = {}
aheadbytes = 0 aheadbytes = 0
aheadjobs = 0 aheadjobs = 0
userseen = 0 userseen = False
totalbytes = 0 totalbytes = 0
totaljobs = 0 totaljobs = 0
while 1: for line in pipe:
line = pipe.readline() fields = line.split()
if not line: break
fields = string.split(line)
n = len(fields) n = len(fields)
if len(fields) >= 6 and fields[n-1] == 'bytes': if len(fields) >= 6 and fields[n-1] == 'bytes':
rank = fields[0] rank, user, job = fields[0:3]
user = fields[1]
job = fields[2]
files = fields[3:-2] files = fields[3:-2]
bytes = eval(fields[n-2]) bytes = int(fields[n-2])
if user == thisuser: if user == thisuser:
userseen = 1 userseen = True
elif not userseen: elif not userseen:
aheadbytes = aheadbytes + bytes aheadbytes += bytes
aheadjobs = aheadjobs + 1 aheadjobs += 1
totalbytes = totalbytes + bytes totalbytes += bytes
totaljobs = totaljobs + 1 totaljobs += 1
if users.has_key(user): ujobs, ubytes = users.get(user, (0, 0))
ujobs, ubytes = users[user] ujobs += 1
else: ubytes += bytes
ujobs, ubytes = 0, 0
ujobs = ujobs + 1
ubytes = ubytes + bytes
users[user] = ujobs, ubytes users[user] = ujobs, ubytes
else: else:
if fields and fields[0] <> 'Rank': if fields and fields[0] != 'Rank':
line = string.strip(line) line = line.strip()
if line == 'no entries': if line == 'no entries':
line = name + ': idle' line = name + ': idle'
elif line[-22:] == ' is ready and printing': elif line[-22:] == ' is ready and printing':
line = name line = name
lines.append(line) lines.append(line)
#
if totaljobs: if totaljobs:
line = '%d K' % ((totalbytes+1023)//1024) line = '%d K' % ((totalbytes+1023) // 1024)
if totaljobs <> len(users): if totaljobs != len(users):
line = line + ' (%d jobs)' % totaljobs line += ' (%d jobs)' % totaljobs
if len(users) == 1: if len(users) == 1:
line = line + ' for %s' % (users.keys()[0],) line += ' for %s' % (users.keys()[0],)
else: else:
line = line + ' for %d users' % len(users) line += ' for %d users' % len(users)
if userseen: if userseen:
if aheadjobs == 0: if aheadjobs == 0:
line = line + ' (%s first)' % thisuser line += ' (%s first)' % thisuser
else: else:
line = line + ' (%d K before %s)' % ( line += ' (%d K before %s)' % (
(aheadbytes+1023)//1024, thisuser) (aheadbytes+1023) // 1024, thisuser)
lines.append(line) lines.append(line)
#
sts = pipe.close() sts = pipe.close()
if sts: if sts:
lines.append('lpq exit status %r' % (sts,)) lines.append('lpq exit status %r' % (sts,))
return string.joinfields(lines, ': ') return ': '.join(lines)
if __name__ == "__main__": if __name__ == "__main__":
try: try:

View File

@ -128,7 +128,7 @@ def makeleaf(tree,path):
j = path[0] j = path[0]
l = len(path) l = len(path)
if not tree.has_key(j): if j not in tree:
tree[j] = {} tree[j] = {}
if l == 1: if l == 1:
tree[j]['.'] = '.' tree[j]['.'] = '.'

View File

@ -19,8 +19,8 @@ class Queens:
def reset(self): def reset(self):
n = self.n n = self.n
self.y = [None]*n # Where is the queen in column x self.y = [None] * n # Where is the queen in column x
self.row = [0]*n # Is row[y] safe? self.row = [0] * n # Is row[y] safe?
self.up = [0] * (2*n-1) # Is upward diagonal[x-y] safe? self.up = [0] * (2*n-1) # Is upward diagonal[x-y] safe?
self.down = [0] * (2*n-1) # Is downward diagonal[x+y] safe? self.down = [0] * (2*n-1) # Is downward diagonal[x+y] safe?
self.nfound = 0 # Instrumentation self.nfound = 0 # Instrumentation
@ -50,7 +50,7 @@ class Queens:
self.up[x-y] = 0 self.up[x-y] = 0
self.down[x+y] = 0 self.down[x+y] = 0
silent = 0 # If set, count solutions only silent = 0 # If true, count solutions only
def display(self): def display(self):
self.nfound = self.nfound + 1 self.nfound = self.nfound + 1