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

View File

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

View File

@ -19,8 +19,8 @@ class Queens:
def reset(self):
n = self.n
self.y = [None]*n # Where is the queen in column x
self.row = [0]*n # Is row[y] safe?
self.y = [None] * n # Where is the queen in column x
self.row = [0] * n # Is row[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.nfound = 0 # Instrumentation
@ -50,7 +50,7 @@ class Queens:
self.up[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):
self.nfound = self.nfound + 1