Whitespace normalization. Ran reindent.py over the entire source tree.

This commit is contained in:
Tim Peters 2004-07-18 05:56:09 +00:00
parent 4fba4521e8
commit e6ddc8b20b
62 changed files with 5050 additions and 5061 deletions

View file

@ -1,9 +1,9 @@
#! /usr/bin/env python
# Fix Python source files to use the new equality test operator, i.e.,
# if x = y: ...
# if x = y: ...
# is changed to
# if x == y: ...
# if x == y: ...
# The script correctly tokenizes the Python program to reliably
# distinguish between assignments and equality tests.
#
@ -39,160 +39,160 @@ dbg = err
rep = sys.stdout.write
def main():
bad = 0
if not sys.argv[1:]: # No arguments
err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')
sys.exit(2)
for arg in sys.argv[1:]:
if os.path.isdir(arg):
if recursedown(arg): bad = 1
elif os.path.islink(arg):
err(arg + ': will not process symbolic links\n')
bad = 1
else:
if fix(arg): bad = 1
sys.exit(bad)
bad = 0
if not sys.argv[1:]: # No arguments
err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')
sys.exit(2)
for arg in sys.argv[1:]:
if os.path.isdir(arg):
if recursedown(arg): bad = 1
elif os.path.islink(arg):
err(arg + ': will not process symbolic links\n')
bad = 1
else:
if fix(arg): bad = 1
sys.exit(bad)
ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
def ispython(name):
return ispythonprog.match(name) >= 0
return ispythonprog.match(name) >= 0
def recursedown(dirname):
dbg('recursedown(%r)\n' % (dirname,))
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err('%s: cannot list directory: %r\n' % (dirname, msg))
return 1
names.sort()
subdirs = []
for name in names:
if name in (os.curdir, os.pardir): continue
fullname = os.path.join(dirname, name)
if os.path.islink(fullname): pass
elif os.path.isdir(fullname):
subdirs.append(fullname)
elif ispython(name):
if fix(fullname): bad = 1
for fullname in subdirs:
if recursedown(fullname): bad = 1
return bad
dbg('recursedown(%r)\n' % (dirname,))
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err('%s: cannot list directory: %r\n' % (dirname, msg))
return 1
names.sort()
subdirs = []
for name in names:
if name in (os.curdir, os.pardir): continue
fullname = os.path.join(dirname, name)
if os.path.islink(fullname): pass
elif os.path.isdir(fullname):
subdirs.append(fullname)
elif ispython(name):
if fix(fullname): bad = 1
for fullname in subdirs:
if recursedown(fullname): bad = 1
return bad
def fix(filename):
## dbg('fix(%r)\n' % (dirname,))
try:
f = open(filename, 'r')
except IOError, msg:
err('%s: cannot open: %r\n' % (filename, msg))
return 1
head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail)
g = None
# If we find a match, we rewind the file and start over but
# now copy everything to a temp file.
lineno = 0
while 1:
line = f.readline()
if not line: break
lineno = lineno + 1
if g is None and '\0' in line:
# Check for binary files
err(filename + ': contains null bytes; not fixed\n')
f.close()
return 1
if lineno == 1 and g is None and line[:2] == '#!':
# Check for non-Python scripts
words = string.split(line[2:])
if words and regex.search('[pP]ython', words[0]) < 0:
msg = filename + ': ' + words[0]
msg = msg + ' script; not fixed\n'
err(msg)
f.close()
return 1
while line[-2:] == '\\\n':
nextline = f.readline()
if not nextline: break
line = line + nextline
lineno = lineno + 1
newline = fixline(line)
if newline != line:
if g is None:
try:
g = open(tempname, 'w')
except IOError, msg:
f.close()
err('%s: cannot create: %r\n' % (tempname, msg))
return 1
f.seek(0)
lineno = 0
rep(filename + ':\n')
continue # restart from the beginning
rep(repr(lineno) + '\n')
rep('< ' + line)
rep('> ' + newline)
if g is not None:
g.write(newline)
## dbg('fix(%r)\n' % (dirname,))
try:
f = open(filename, 'r')
except IOError, msg:
err('%s: cannot open: %r\n' % (filename, msg))
return 1
head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail)
g = None
# If we find a match, we rewind the file and start over but
# now copy everything to a temp file.
lineno = 0
while 1:
line = f.readline()
if not line: break
lineno = lineno + 1
if g is None and '\0' in line:
# Check for binary files
err(filename + ': contains null bytes; not fixed\n')
f.close()
return 1
if lineno == 1 and g is None and line[:2] == '#!':
# Check for non-Python scripts
words = string.split(line[2:])
if words and regex.search('[pP]ython', words[0]) < 0:
msg = filename + ': ' + words[0]
msg = msg + ' script; not fixed\n'
err(msg)
f.close()
return 1
while line[-2:] == '\\\n':
nextline = f.readline()
if not nextline: break
line = line + nextline
lineno = lineno + 1
newline = fixline(line)
if newline != line:
if g is None:
try:
g = open(tempname, 'w')
except IOError, msg:
f.close()
err('%s: cannot create: %r\n' % (tempname, msg))
return 1
f.seek(0)
lineno = 0
rep(filename + ':\n')
continue # restart from the beginning
rep(repr(lineno) + '\n')
rep('< ' + line)
rep('> ' + newline)
if g is not None:
g.write(newline)
# End of file
f.close()
if not g: return 0 # No changes
# Finishing touch -- move files
# End of file
f.close()
if not g: return 0 # No changes
# First copy the file's mode to the temp file
try:
statbuf = os.stat(filename)
os.chmod(tempname, statbuf[ST_MODE] & 07777)
except os.error, msg:
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err('%s: warning: backup failed (%r)\n' % (filename, msg))
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err('%s: rename failed (%r)\n' % (filename, msg))
return 1
# Return succes
return 0
# Finishing touch -- move files
# First copy the file's mode to the temp file
try:
statbuf = os.stat(filename)
os.chmod(tempname, statbuf[ST_MODE] & 07777)
except os.error, msg:
err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err('%s: warning: backup failed (%r)\n' % (filename, msg))
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err('%s: rename failed (%r)\n' % (filename, msg))
return 1
# Return succes
return 0
from tokenize import tokenprog
match = {'if':':', 'elif':':', 'while':':', 'return':'\n', \
'(':')', '[':']', '{':'}', '`':'`'}
'(':')', '[':']', '{':'}', '`':'`'}
def fixline(line):
# Quick check for easy case
if '=' not in line: return line
i, n = 0, len(line)
stack = []
while i < n:
j = tokenprog.match(line, i)
if j < 0:
# A bad token; forget about the rest of this line
print '(Syntax error:)'
print line,
return line
a, b = tokenprog.regs[3] # Location of the token proper
token = line[a:b]
i = i+j
if stack and token == stack[-1]:
del stack[-1]
elif match.has_key(token):
stack.append(match[token])
elif token == '=' and stack:
line = line[:a] + '==' + line[b:]
i, n = a + len('=='), len(line)
elif token == '==' and not stack:
print '(Warning: \'==\' at top level:)'
print line,
return line
# Quick check for easy case
if '=' not in line: return line
i, n = 0, len(line)
stack = []
while i < n:
j = tokenprog.match(line, i)
if j < 0:
# A bad token; forget about the rest of this line
print '(Syntax error:)'
print line,
return line
a, b = tokenprog.regs[3] # Location of the token proper
token = line[a:b]
i = i+j
if stack and token == stack[-1]:
del stack[-1]
elif match.has_key(token):
stack.append(match[token])
elif token == '=' and stack:
line = line[:a] + '==' + line[b:]
i, n = a + len('=='), len(line)
elif token == '==' and not stack:
print '(Warning: \'==\' at top level:)'
print line,
return line
main()

View file

@ -8,41 +8,41 @@
import sys
from math import sqrt
error = 'fact.error' # exception
error = 'fact.error' # exception
def fact(n):
if n < 1: raise error # fact() argument should be >= 1
if n == 1: return [] # special case
res = []
# Treat even factors special, so we can use i = i+2 later
while n%2 == 0:
res.append(2)
n = n/2
# Try odd numbers up to sqrt(n)
limit = sqrt(float(n+1))
i = 3
while i <= limit:
if n%i == 0:
res.append(i)
n = n/i
limit = sqrt(n+1)
else:
i = i+2
if n != 1:
res.append(n)
return res
if n < 1: raise error # fact() argument should be >= 1
if n == 1: return [] # special case
res = []
# Treat even factors special, so we can use i = i+2 later
while n%2 == 0:
res.append(2)
n = n/2
# Try odd numbers up to sqrt(n)
limit = sqrt(float(n+1))
i = 3
while i <= limit:
if n%i == 0:
res.append(i)
n = n/i
limit = sqrt(n+1)
else:
i = i+2
if n != 1:
res.append(n)
return res
def main():
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
n = eval(arg)
print n, fact(n)
else:
try:
while 1:
n = input()
print n, fact(n)
except EOFError:
pass
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
n = eval(arg)
print n, fact(n)
else:
try:
while 1:
n = input()
print n, fact(n)
except EOFError:
pass
main()

View file

@ -21,124 +21,124 @@ pat = '^\([a-zA-Z0-9 :]*\)!\(.*\)!\(.*\)!\([<>].*\)!\([0-9]+\)!\([0-9]+\)$'
prog = regex.compile(pat)
def main():
maxitems = 25
search = None
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:s:')
except getopt.error, msg:
print msg
print 'usage: ftpstats [-m maxitems] [file]'
sys.exit(2)
for o, a in opts:
if o == '-m':
maxitems = string.atoi(a)
if o == '-s':
search = a
file = '/usr/adm/ftpd'
if args: file = args[0]
if file == '-':
f = sys.stdin
else:
try:
f = open(file, 'r')
except IOError, msg:
print file, ':', msg
sys.exit(1)
bydate = {}
bytime = {}
byfile = {}
bydir = {}
byhost = {}
byuser = {}
bytype = {}
lineno = 0
try:
while 1:
line = f.readline()
if not line: break
lineno = lineno + 1
if search and string.find(line, search) < 0:
continue
if prog.match(line) < 0:
print 'Bad line', lineno, ':', repr(line)
continue
items = prog.group(1, 2, 3, 4, 5, 6)
(logtime, loguser, loghost, logfile, logbytes,
logxxx2) = items
## print logtime
## print '-->', loguser
## print '--> -->', loghost
## print '--> --> -->', logfile
## print '--> --> --> -->', logbytes
## print '--> --> --> --> -->', logxxx2
## for i in logtime, loghost, logbytes, logxxx2:
## if '!' in i: print '???', i
add(bydate, logtime[-4:] + ' ' + logtime[:6], items)
add(bytime, logtime[7:9] + ':00-59', items)
direction, logfile = logfile[0], logfile[1:]
# The real path probably starts at the last //...
while 1:
i = string.find(logfile, '//')
if i < 0: break
logfile = logfile[i+1:]
add(byfile, logfile + ' ' + direction, items)
logdir = os.path.dirname(logfile)
## logdir = os.path.normpath(logdir) + '/.'
while 1:
add(bydir, logdir + ' ' + direction, items)
dirhead = os.path.dirname(logdir)
if dirhead == logdir: break
logdir = dirhead
add(byhost, loghost, items)
add(byuser, loguser, items)
add(bytype, direction, items)
except KeyboardInterrupt:
print 'Interrupted at line', lineno
show(bytype, 'by transfer direction', maxitems)
show(bydir, 'by directory', maxitems)
show(byfile, 'by file', maxitems)
show(byhost, 'by host', maxitems)
show(byuser, 'by user', maxitems)
showbar(bydate, 'by date')
showbar(bytime, 'by time of day')
maxitems = 25
search = None
try:
opts, args = getopt.getopt(sys.argv[1:], 'm:s:')
except getopt.error, msg:
print msg
print 'usage: ftpstats [-m maxitems] [file]'
sys.exit(2)
for o, a in opts:
if o == '-m':
maxitems = string.atoi(a)
if o == '-s':
search = a
file = '/usr/adm/ftpd'
if args: file = args[0]
if file == '-':
f = sys.stdin
else:
try:
f = open(file, 'r')
except IOError, msg:
print file, ':', msg
sys.exit(1)
bydate = {}
bytime = {}
byfile = {}
bydir = {}
byhost = {}
byuser = {}
bytype = {}
lineno = 0
try:
while 1:
line = f.readline()
if not line: break
lineno = lineno + 1
if search and string.find(line, search) < 0:
continue
if prog.match(line) < 0:
print 'Bad line', lineno, ':', repr(line)
continue
items = prog.group(1, 2, 3, 4, 5, 6)
(logtime, loguser, loghost, logfile, logbytes,
logxxx2) = items
## print logtime
## print '-->', loguser
## print '--> -->', loghost
## print '--> --> -->', logfile
## print '--> --> --> -->', logbytes
## print '--> --> --> --> -->', logxxx2
## for i in logtime, loghost, logbytes, logxxx2:
## if '!' in i: print '???', i
add(bydate, logtime[-4:] + ' ' + logtime[:6], items)
add(bytime, logtime[7:9] + ':00-59', items)
direction, logfile = logfile[0], logfile[1:]
# The real path probably starts at the last //...
while 1:
i = string.find(logfile, '//')
if i < 0: break
logfile = logfile[i+1:]
add(byfile, logfile + ' ' + direction, items)
logdir = os.path.dirname(logfile)
## logdir = os.path.normpath(logdir) + '/.'
while 1:
add(bydir, logdir + ' ' + direction, items)
dirhead = os.path.dirname(logdir)
if dirhead == logdir: break
logdir = dirhead
add(byhost, loghost, items)
add(byuser, loguser, items)
add(bytype, direction, items)
except KeyboardInterrupt:
print 'Interrupted at line', lineno
show(bytype, 'by transfer direction', maxitems)
show(bydir, 'by directory', maxitems)
show(byfile, 'by file', maxitems)
show(byhost, 'by host', maxitems)
show(byuser, 'by user', maxitems)
showbar(bydate, 'by date')
showbar(bytime, 'by time of day')
def showbar(dict, title):
n = len(title)
print '='*((70-n)/2), title, '='*((71-n)/2)
list = []
keys = dict.keys()
keys.sort()
for key in keys:
n = len(str(key))
list.append((len(dict[key]), key))
maxkeylength = 0
maxcount = 0
for count, key in list:
maxkeylength = max(maxkeylength, len(key))
maxcount = max(maxcount, count)
maxbarlength = 72 - maxkeylength - 7
for count, key in list:
barlength = int(round(maxbarlength*float(count)/maxcount))
bar = '*'*barlength
print '%5d %-*s %s' % (count, maxkeylength, key, bar)
n = len(title)
print '='*((70-n)/2), title, '='*((71-n)/2)
list = []
keys = dict.keys()
keys.sort()
for key in keys:
n = len(str(key))
list.append((len(dict[key]), key))
maxkeylength = 0
maxcount = 0
for count, key in list:
maxkeylength = max(maxkeylength, len(key))
maxcount = max(maxcount, count)
maxbarlength = 72 - maxkeylength - 7
for count, key in list:
barlength = int(round(maxbarlength*float(count)/maxcount))
bar = '*'*barlength
print '%5d %-*s %s' % (count, maxkeylength, key, bar)
def show(dict, title, maxitems):
if len(dict) > maxitems:
title = title + ' (first %d)'%maxitems
n = len(title)
print '='*((70-n)/2), title, '='*((71-n)/2)
list = []
keys = dict.keys()
for key in keys:
list.append((-len(dict[key]), key))
list.sort()
for count, key in list[:maxitems]:
print '%5d %s' % (-count, key)
if len(dict) > maxitems:
title = title + ' (first %d)'%maxitems
n = len(title)
print '='*((70-n)/2), title, '='*((71-n)/2)
list = []
keys = dict.keys()
for key in keys:
list.append((-len(dict[key]), key))
list.sort()
for count, key in list[:maxitems]:
print '%5d %s' % (-count, key)
def add(dict, key, item):
if dict.has_key(key):
dict[key].append(item)
else:
dict[key] = [item]
if dict.has_key(key):
dict[key].append(item)
else:
dict[key] = [item]
main()

View file

@ -12,98 +12,98 @@ DEF_PRINTER = 'psc'
DEF_DELAY = 10
def main():
delay = DEF_DELAY # XXX Use getopt() later
try:
thisuser = posix.environ['LOGNAME']
except:
thisuser = posix.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:]
else:
if posix.environ.has_key('PRINTER'):
printers = [posix.environ['PRINTER']]
else:
printers = [DEF_PRINTER]
#
clearhome = posix.popen('clear', 'r').read()
#
while 1:
text = clearhome
for name in printers:
text = text + makestatus(name, thisuser) + '\n'
print text
time.sleep(delay)
delay = DEF_DELAY # XXX Use getopt() later
try:
thisuser = posix.environ['LOGNAME']
except:
thisuser = posix.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:]
else:
if posix.environ.has_key('PRINTER'):
printers = [posix.environ['PRINTER']]
else:
printers = [DEF_PRINTER]
#
clearhome = posix.popen('clear', 'r').read()
#
while 1:
text = clearhome
for name in printers:
text = text + makestatus(name, thisuser) + '\n'
print text
time.sleep(delay)
def makestatus(name, thisuser):
pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
lines = []
users = {}
aheadbytes = 0
aheadjobs = 0
userseen = 0
totalbytes = 0
totaljobs = 0
while 1:
line = pipe.readline()
if not line: break
fields = string.split(line)
n = len(fields)
if len(fields) >= 6 and fields[n-1] == 'bytes':
rank = fields[0]
user = fields[1]
job = fields[2]
files = fields[3:-2]
bytes = eval(fields[n-2])
if user == thisuser:
userseen = 1
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
users[user] = ujobs, ubytes
else:
if fields and fields[0] <> 'Rank':
line = string.strip(line)
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
if len(users) == 1:
line = line + ' for %s' % (users.keys()[0],)
else:
line = line + ' for %d users' % len(users)
if userseen:
if aheadjobs == 0:
line = line + ' (%s first)' % thisuser
else:
line = 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, ': ')
pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
lines = []
users = {}
aheadbytes = 0
aheadjobs = 0
userseen = 0
totalbytes = 0
totaljobs = 0
while 1:
line = pipe.readline()
if not line: break
fields = string.split(line)
n = len(fields)
if len(fields) >= 6 and fields[n-1] == 'bytes':
rank = fields[0]
user = fields[1]
job = fields[2]
files = fields[3:-2]
bytes = eval(fields[n-2])
if user == thisuser:
userseen = 1
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
users[user] = ujobs, ubytes
else:
if fields and fields[0] <> 'Rank':
line = string.strip(line)
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
if len(users) == 1:
line = line + ' for %s' % (users.keys()[0],)
else:
line = line + ' for %d users' % len(users)
if userseen:
if aheadjobs == 0:
line = line + ' (%s first)' % thisuser
else:
line = 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, ': ')
try:
main()
main()
except KeyboardInterrupt:
pass
pass

View file

@ -8,13 +8,13 @@
import sys, os
def main():
for p in sys.argv[1:]:
makedirs(p)
for p in sys.argv[1:]:
makedirs(p)
def makedirs(p):
if p and not os.path.isdir(p):
head, tail = os.path.split(p)
makedirs(head)
os.mkdir(p, 0777)
if p and not os.path.isdir(p):
head, tail = os.path.split(p)
makedirs(head)
os.mkdir(p, 0777)
main()

View file

@ -1,116 +1,116 @@
#! /usr/bin/env python
class Markov:
def __init__(self, histsize, choice):
self.histsize = histsize
self.choice = choice
self.trans = {}
def add(self, state, next):
if not self.trans.has_key(state):
self.trans[state] = [next]
else:
self.trans[state].append(next)
def put(self, seq):
n = self.histsize
add = self.add
add(None, seq[:0])
for i in range(len(seq)):
add(seq[max(0, i-n):i], seq[i:i+1])
add(seq[len(seq)-n:], None)
def get(self):
choice = self.choice
trans = self.trans
n = self.histsize
seq = choice(trans[None])
while 1:
subseq = seq[max(0, len(seq)-n):]
options = trans[subseq]
next = choice(options)
if not next: break
seq = seq + next
return seq
def __init__(self, histsize, choice):
self.histsize = histsize
self.choice = choice
self.trans = {}
def add(self, state, next):
if not self.trans.has_key(state):
self.trans[state] = [next]
else:
self.trans[state].append(next)
def put(self, seq):
n = self.histsize
add = self.add
add(None, seq[:0])
for i in range(len(seq)):
add(seq[max(0, i-n):i], seq[i:i+1])
add(seq[len(seq)-n:], None)
def get(self):
choice = self.choice
trans = self.trans
n = self.histsize
seq = choice(trans[None])
while 1:
subseq = seq[max(0, len(seq)-n):]
options = trans[subseq]
next = choice(options)
if not next: break
seq = seq + next
return seq
def test():
import sys, string, random, getopt
args = sys.argv[1:]
try:
opts, args = getopt.getopt(args, '0123456789cdw')
except getopt.error:
print 'Usage: markov [-#] [-cddqw] [file] ...'
print 'Options:'
print '-#: 1-digit history size (default 2)'
print '-c: characters (default)'
print '-w: words'
print '-d: more debugging output'
print '-q: no debugging output'
print 'Input files (default stdin) are split in paragraphs'
print 'separated blank lines and each paragraph is split'
print 'in words by whitespace, then reconcatenated with'
print 'exactly one space separating words.'
print 'Output consists of paragraphs separated by blank'
print 'lines, where lines are no longer than 72 characters.'
histsize = 2
do_words = 0
debug = 1
for o, a in opts:
if '-0' <= o <= '-9': histsize = eval(o[1:])
if o == '-c': do_words = 0
if o == '-d': debug = debug + 1
if o == '-q': debug = 0
if o == '-w': do_words = 1
if not args: args = ['-']
m = Markov(histsize, random.choice)
try:
for filename in args:
if filename == '-':
f = sys.stdin
if f.isatty():
print 'Sorry, need stdin from file'
continue
else:
f = open(filename, 'r')
if debug: print 'processing', filename, '...'
text = f.read()
f.close()
paralist = string.splitfields(text, '\n\n')
for para in paralist:
if debug > 1: print 'feeding ...'
words = string.split(para)
if words:
if do_words: data = tuple(words)
else: data = string.joinfields(words, ' ')
m.put(data)
except KeyboardInterrupt:
print 'Interrupted -- continue with data read so far'
if not m.trans:
print 'No valid input files'
return
if debug: print 'done.'
if debug > 1:
for key in m.trans.keys():
if key is None or len(key) < histsize:
print repr(key), m.trans[key]
if histsize == 0: print repr(''), m.trans['']
print
while 1:
data = m.get()
if do_words: words = data
else: words = string.split(data)
n = 0
limit = 72
for w in words:
if n + len(w) > limit:
print
n = 0
print w,
n = n + len(w) + 1
print
print
import sys, string, random, getopt
args = sys.argv[1:]
try:
opts, args = getopt.getopt(args, '0123456789cdw')
except getopt.error:
print 'Usage: markov [-#] [-cddqw] [file] ...'
print 'Options:'
print '-#: 1-digit history size (default 2)'
print '-c: characters (default)'
print '-w: words'
print '-d: more debugging output'
print '-q: no debugging output'
print 'Input files (default stdin) are split in paragraphs'
print 'separated blank lines and each paragraph is split'
print 'in words by whitespace, then reconcatenated with'
print 'exactly one space separating words.'
print 'Output consists of paragraphs separated by blank'
print 'lines, where lines are no longer than 72 characters.'
histsize = 2
do_words = 0
debug = 1
for o, a in opts:
if '-0' <= o <= '-9': histsize = eval(o[1:])
if o == '-c': do_words = 0
if o == '-d': debug = debug + 1
if o == '-q': debug = 0
if o == '-w': do_words = 1
if not args: args = ['-']
m = Markov(histsize, random.choice)
try:
for filename in args:
if filename == '-':
f = sys.stdin
if f.isatty():
print 'Sorry, need stdin from file'
continue
else:
f = open(filename, 'r')
if debug: print 'processing', filename, '...'
text = f.read()
f.close()
paralist = string.splitfields(text, '\n\n')
for para in paralist:
if debug > 1: print 'feeding ...'
words = string.split(para)
if words:
if do_words: data = tuple(words)
else: data = string.joinfields(words, ' ')
m.put(data)
except KeyboardInterrupt:
print 'Interrupted -- continue with data read so far'
if not m.trans:
print 'No valid input files'
return
if debug: print 'done.'
if debug > 1:
for key in m.trans.keys():
if key is None or len(key) < histsize:
print repr(key), m.trans[key]
if histsize == 0: print repr(''), m.trans['']
print
while 1:
data = m.get()
if do_words: words = data
else: words = string.split(data)
n = 0
limit = 72
for w in words:
if n + len(w) > limit:
print
n = 0
print w,
n = n + len(w) + 1
print
print
def tuple(list):
if len(list) == 0: return ()
if len(list) == 1: return (list[0],)
i = len(list)/2
return tuple(list[:i]) + tuple(list[i:])
if len(list) == 0: return ()
if len(list) == 1: return (list[0],)
i = len(list)/2
return tuple(list[:i]) + tuple(list[i:])
test()

View file

@ -13,111 +13,111 @@ import getopt
import regex
def main():
dofile = mmdf
try:
opts, args = getopt.getopt(sys.argv[1:], 'f')
except getopt.error, msg:
sys.stderr.write('%s\n' % msg)
sys.exit(2)
for o, a in opts:
if o == '-f':
dofile = message
if not args:
args = ['-']
sts = 0
for arg in args:
if arg == '-' or arg == '':
sts = dofile(sys.stdin) or sts
elif os.path.isdir(arg):
sts = mh(arg) or sts
elif os.path.isfile(arg):
try:
f = open(arg)
except IOError, msg:
sys.stderr.write('%s: %s\n' % (arg, msg))
sts = 1
continue
sts = dofile(f) or sts
f.close()
else:
sys.stderr.write('%s: not found\n' % arg)
sts = 1
if sts:
sys.exit(sts)
dofile = mmdf
try:
opts, args = getopt.getopt(sys.argv[1:], 'f')
except getopt.error, msg:
sys.stderr.write('%s\n' % msg)
sys.exit(2)
for o, a in opts:
if o == '-f':
dofile = message
if not args:
args = ['-']
sts = 0
for arg in args:
if arg == '-' or arg == '':
sts = dofile(sys.stdin) or sts
elif os.path.isdir(arg):
sts = mh(arg) or sts
elif os.path.isfile(arg):
try:
f = open(arg)
except IOError, msg:
sys.stderr.write('%s: %s\n' % (arg, msg))
sts = 1
continue
sts = dofile(f) or sts
f.close()
else:
sys.stderr.write('%s: not found\n' % arg)
sts = 1
if sts:
sys.exit(sts)
numeric = regex.compile('[1-9][0-9]*')
def mh(dir):
sts = 0
msgs = os.listdir(dir)
for msg in msgs:
if numeric.match(msg) != len(msg):
continue
fn = os.path.join(dir, msg)
try:
f = open(fn)
except IOError, msg:
sys.stderr.write('%s: %s\n' % (fn, msg))
sts = 1
continue
sts = message(f) or sts
return sts
sts = 0
msgs = os.listdir(dir)
for msg in msgs:
if numeric.match(msg) != len(msg):
continue
fn = os.path.join(dir, msg)
try:
f = open(fn)
except IOError, msg:
sys.stderr.write('%s: %s\n' % (fn, msg))
sts = 1
continue
sts = message(f) or sts
return sts
def mmdf(f):
sts = 0
while 1:
line = f.readline()
if not line:
break
if line == '\1\1\1\1\n':
sts = message(f, line) or sts
else:
sys.stderr.write(
'Bad line in MMFD mailbox: %r\n' % (line,))
return sts
sts = 0
while 1:
line = f.readline()
if not line:
break
if line == '\1\1\1\1\n':
sts = message(f, line) or sts
else:
sys.stderr.write(
'Bad line in MMFD mailbox: %r\n' % (line,))
return sts
counter = 0 # for generating unique Message-ID headers
def message(f, delimiter = ''):
sts = 0
# Parse RFC822 header
m = rfc822.Message(f)
# Write unix header line
fullname, email = m.getaddr('From')
tt = m.getdate('Date')
if tt:
t = time.mktime(tt)
else:
sys.stderr.write(
'Unparseable date: %r\n' % (m.getheader('Date'),))
t = os.fstat(f.fileno())[stat.ST_MTIME]
print 'From', email, time.ctime(t)
# Copy RFC822 header
for line in m.headers:
print line,
# Invent Message-ID header if none is present
if not m.has_key('message-id'):
global counter
counter = counter + 1
msgid = "<%s.%d>" % (hex(t), counter)
sys.stderr.write("Adding Message-ID %s (From %s)\n" %
(msgid, email))
print "Message-ID:", msgid
print
# Copy body
while 1:
line = f.readline()
if line == delimiter:
break
if not line:
sys.stderr.write('Unexpected EOF in message\n')
sts = 1
break
if line[:5] == 'From ':
line = '>' + line
print line,
# Print trailing newline
print
return sts
sts = 0
# Parse RFC822 header
m = rfc822.Message(f)
# Write unix header line
fullname, email = m.getaddr('From')
tt = m.getdate('Date')
if tt:
t = time.mktime(tt)
else:
sys.stderr.write(
'Unparseable date: %r\n' % (m.getheader('Date'),))
t = os.fstat(f.fileno())[stat.ST_MTIME]
print 'From', email, time.ctime(t)
# Copy RFC822 header
for line in m.headers:
print line,
# Invent Message-ID header if none is present
if not m.has_key('message-id'):
global counter
counter = counter + 1
msgid = "<%s.%d>" % (hex(t), counter)
sys.stderr.write("Adding Message-ID %s (From %s)\n" %
(msgid, email))
print "Message-ID:", msgid
print
# Copy body
while 1:
line = f.readline()
if line == delimiter:
break
if not line:
sys.stderr.write('Unexpected EOF in message\n')
sts = 1
break
if line[:5] == 'From ':
line = '>' + line
print line,
# Print trailing newline
print
return sts
main()

View file

@ -9,52 +9,52 @@
import os
def main():
rcstree = 'RCStree'
rcs = 'RCS'
if os.path.islink(rcs):
print '%r is a symlink to %r' % (rcs, os.readlink(rcs))
return
if os.path.isdir(rcs):
print '%r is an ordinary directory' % (rcs,)
return
if os.path.exists(rcs):
print '%r is a file?!?!' % (rcs,)
return
#
p = os.getcwd()
up = ''
down = ''
# Invariants:
# (1) join(p, down) is the current directory
# (2) up is the same directory as p
# Ergo:
# (3) join(up, down) is the current directory
#print 'p =', repr(p)
while not os.path.isdir(os.path.join(p, rcstree)):
head, tail = os.path.split(p)
#print 'head = %r; tail = %r' % (head, tail)
if not tail:
print 'Sorry, no ancestor dir contains %r' % (rcstree,)
return
p = head
up = os.path.join(os.pardir, up)
down = os.path.join(tail, down)
#print 'p = %r; up = %r; down = %r' % (p, up, down)
there = os.path.join(up, rcstree)
there = os.path.join(there, down)
there = os.path.join(there, rcs)
if os.path.isdir(there):
print '%r already exists' % (there, )
else:
print 'making %r' % (there,)
makedirs(there)
print 'making symlink %r -> %r' % (rcs, there)
os.symlink(there, rcs)
rcstree = 'RCStree'
rcs = 'RCS'
if os.path.islink(rcs):
print '%r is a symlink to %r' % (rcs, os.readlink(rcs))
return
if os.path.isdir(rcs):
print '%r is an ordinary directory' % (rcs,)
return
if os.path.exists(rcs):
print '%r is a file?!?!' % (rcs,)
return
#
p = os.getcwd()
up = ''
down = ''
# Invariants:
# (1) join(p, down) is the current directory
# (2) up is the same directory as p
# Ergo:
# (3) join(up, down) is the current directory
#print 'p =', repr(p)
while not os.path.isdir(os.path.join(p, rcstree)):
head, tail = os.path.split(p)
#print 'head = %r; tail = %r' % (head, tail)
if not tail:
print 'Sorry, no ancestor dir contains %r' % (rcstree,)
return
p = head
up = os.path.join(os.pardir, up)
down = os.path.join(tail, down)
#print 'p = %r; up = %r; down = %r' % (p, up, down)
there = os.path.join(up, rcstree)
there = os.path.join(there, down)
there = os.path.join(there, rcs)
if os.path.isdir(there):
print '%r already exists' % (there, )
else:
print 'making %r' % (there,)
makedirs(there)
print 'making symlink %r -> %r' % (rcs, there)
os.symlink(there, rcs)
def makedirs(p):
if not os.path.isdir(p):
head, tail = os.path.split(p)
makedirs(head)
os.mkdir(p, 0777)
if not os.path.isdir(p):
head, tail = os.path.split(p)
makedirs(head)
os.mkdir(p, 0777)
main()

View file

@ -7,57 +7,57 @@ import sys, math, audiodev
DOT = 30
DAH = 3 * DOT
OCTAVE = 2 # 1 == 441 Hz, 2 == 882 Hz, ...
OCTAVE = 2 # 1 == 441 Hz, 2 == 882 Hz, ...
morsetab = {
'A': '.-', 'a': '.-',
'B': '-...', 'b': '-...',
'C': '-.-.', 'c': '-.-.',
'D': '-..', 'd': '-..',
'E': '.', 'e': '.',
'F': '..-.', 'f': '..-.',
'G': '--.', 'g': '--.',
'H': '....', 'h': '....',
'I': '..', 'i': '..',
'J': '.---', 'j': '.---',
'K': '-.-', 'k': '-.-',
'L': '.-..', 'l': '.-..',
'M': '--', 'm': '--',
'N': '-.', 'n': '-.',
'O': '---', 'o': '---',
'P': '.--.', 'p': '.--.',
'Q': '--.-', 'q': '--.-',
'R': '.-.', 'r': '.-.',
'S': '...', 's': '...',
'T': '-', 't': '-',
'U': '..-', 'u': '..-',
'V': '...-', 'v': '...-',
'W': '.--', 'w': '.--',
'X': '-..-', 'x': '-..-',
'Y': '-.--', 'y': '-.--',
'Z': '--..', 'z': '--..',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
',': '--..--',
'.': '.-.-.-',
'?': '..--..',
';': '-.-.-.',
':': '---...',
"'": '.----.',
'-': '-....-',
'/': '-..-.',
'(': '-.--.-',
')': '-.--.-',
'_': '..--.-',
' ': ' '
'A': '.-', 'a': '.-',
'B': '-...', 'b': '-...',
'C': '-.-.', 'c': '-.-.',
'D': '-..', 'd': '-..',
'E': '.', 'e': '.',
'F': '..-.', 'f': '..-.',
'G': '--.', 'g': '--.',
'H': '....', 'h': '....',
'I': '..', 'i': '..',
'J': '.---', 'j': '.---',
'K': '-.-', 'k': '-.-',
'L': '.-..', 'l': '.-..',
'M': '--', 'm': '--',
'N': '-.', 'n': '-.',
'O': '---', 'o': '---',
'P': '.--.', 'p': '.--.',
'Q': '--.-', 'q': '--.-',
'R': '.-.', 'r': '.-.',
'S': '...', 's': '...',
'T': '-', 't': '-',
'U': '..-', 'u': '..-',
'V': '...-', 'v': '...-',
'W': '.--', 'w': '.--',
'X': '-..-', 'x': '-..-',
'Y': '-.--', 'y': '-.--',
'Z': '--..', 'z': '--..',
'0': '-----',
'1': '.----',
'2': '..---',
'3': '...--',
'4': '....-',
'5': '.....',
'6': '-....',
'7': '--...',
'8': '---..',
'9': '----.',
',': '--..--',
'.': '.-.-.-',
'?': '..--..',
';': '-.-.-.',
':': '---...',
"'": '.----.',
'-': '-....-',
'/': '-..-.',
'(': '-.--.-',
')': '-.--.-',
'_': '..--.-',
' ': ' '
}
# If we play at 44.1 kHz (which we do), then if we produce one sine
@ -65,85 +65,85 @@ morsetab = {
# sine waves in these 100 samples, we get a tone of 882 Hz. 882 Hz
# appears to be a nice one for playing morse code.
def mkwave(octave):
global sinewave, nowave
sinewave = ''
for i in range(100):
val = int(math.sin(math.pi * float(i) * octave / 50.0) * 30000)
sinewave = sinewave + chr((val >> 8) & 255) + chr(val & 255)
nowave = '\0' * 200
global sinewave, nowave
sinewave = ''
for i in range(100):
val = int(math.sin(math.pi * float(i) * octave / 50.0) * 30000)
sinewave = sinewave + chr((val >> 8) & 255) + chr(val & 255)
nowave = '\0' * 200
mkwave(OCTAVE)
def main():
import getopt, string
try:
opts, args = getopt.getopt(sys.argv[1:], 'o:p:')
except getopt.error:
sys.stderr.write('Usage ' + sys.argv[0] +
' [ -o outfile ] [ args ] ...\n')
sys.exit(1)
dev = None
for o, a in opts:
if o == '-o':
import aifc
dev = aifc.open(a, 'w')
dev.setframerate(44100)
dev.setsampwidth(2)
dev.setnchannels(1)
if o == '-p':
mkwave(string.atoi(a))
if not dev:
import audiodev
dev = audiodev.AudioDev()
dev.setoutrate(44100)
dev.setsampwidth(2)
dev.setnchannels(1)
dev.close = dev.stop
dev.writeframesraw = dev.writeframes
if args:
line = string.join(args)
else:
line = sys.stdin.readline()
while line:
mline = morse(line)
play(mline, dev)
if hasattr(dev, 'wait'):
dev.wait()
if not args:
line = sys.stdin.readline()
else:
line = ''
dev.close()
import getopt, string
try:
opts, args = getopt.getopt(sys.argv[1:], 'o:p:')
except getopt.error:
sys.stderr.write('Usage ' + sys.argv[0] +
' [ -o outfile ] [ args ] ...\n')
sys.exit(1)
dev = None
for o, a in opts:
if o == '-o':
import aifc
dev = aifc.open(a, 'w')
dev.setframerate(44100)
dev.setsampwidth(2)
dev.setnchannels(1)
if o == '-p':
mkwave(string.atoi(a))
if not dev:
import audiodev
dev = audiodev.AudioDev()
dev.setoutrate(44100)
dev.setsampwidth(2)
dev.setnchannels(1)
dev.close = dev.stop
dev.writeframesraw = dev.writeframes
if args:
line = string.join(args)
else:
line = sys.stdin.readline()
while line:
mline = morse(line)
play(mline, dev)
if hasattr(dev, 'wait'):
dev.wait()
if not args:
line = sys.stdin.readline()
else:
line = ''
dev.close()
# Convert a string to morse code with \001 between the characters in
# the string.
def morse(line):
res = ''
for c in line:
try:
res = res + morsetab[c] + '\001'
except KeyError:
pass
return res
res = ''
for c in line:
try:
res = res + morsetab[c] + '\001'
except KeyError:
pass
return res
# Play a line of morse code.
def play(line, dev):
for c in line:
if c == '.':
sine(dev, DOT)
elif c == '-':
sine(dev, DAH)
else: # space
pause(dev, DAH + DOT)
pause(dev, DOT)
for c in line:
if c == '.':
sine(dev, DOT)
elif c == '-':
sine(dev, DAH)
else: # space
pause(dev, DAH + DOT)
pause(dev, DOT)
def sine(dev, length):
for i in range(length):
dev.writeframesraw(sinewave)
for i in range(length):
dev.writeframesraw(sinewave)
def pause(dev, length):
for i in range(length):
dev.writeframesraw(nowave)
for i in range(length):
dev.writeframesraw(nowave)
if __name__ == '__main__' or sys.argv[0] == __name__:
main()
main()

View file

@ -11,24 +11,24 @@ import sys
from mpz import mpz
def main():
mpzone, mpztwo, mpzten = mpz(1), mpz(2), mpz(10)
k, a, b, a1, b1 = mpz(2), mpz(4), mpz(1), mpz(12), mpz(4)
while 1:
# Next approximation
p, q, k = k*k, mpztwo*k+mpzone, k+mpzone
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
# Print common digits
d, d1 = a/b, a1/b1
while d == d1:
output(d)
a, a1 = mpzten*(a%b), mpzten*(a1%b1)
d, d1 = a/b, a1/b1
mpzone, mpztwo, mpzten = mpz(1), mpz(2), mpz(10)
k, a, b, a1, b1 = mpz(2), mpz(4), mpz(1), mpz(12), mpz(4)
while 1:
# Next approximation
p, q, k = k*k, mpztwo*k+mpzone, k+mpzone
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
# Print common digits
d, d1 = a/b, a1/b1
while d == d1:
output(d)
a, a1 = mpzten*(a%b), mpzten*(a1%b1)
d, d1 = a/b, a1/b1
def output(d):
# Use write() to avoid spaces between the digits
# Use int(d) to avoid a trailing L after each digit
sys.stdout.write(repr(int(d)))
# Flush so the output is seen immediately
sys.stdout.flush()
# Use write() to avoid spaces between the digits
# Use int(d) to avoid a trailing L after each digit
sys.stdout.write(repr(int(d)))
# Flush so the output is seen immediately
sys.stdout.flush()
main()

View file

@ -5,27 +5,27 @@
# Syntax:
# newslist [ -a ]
#
# This is a program to create a directory full of HTML pages
# This is a program to create a directory full of HTML pages
# which between them contain links to all the newsgroups available
# on your server.
#
# The -a option causes a complete list of all groups to be read from
# The -a option causes a complete list of all groups to be read from
# the server rather than just the ones which have appeared since last
# execution. This recreates the local list from scratch. Use this on
# the first invocation of the program, and from time to time thereafter.
# When new groups are first created they may appear on your server as
# When new groups are first created they may appear on your server as
# empty groups. By default, empty groups are ignored by the -a option.
# However, these new groups will not be created again, and so will not
# appear in the server's list of 'new groups' at a later date. Hence it
# won't appear until you do a '-a' after some articles have appeared.
#
#
# I should really keep a list of ignored empty groups and re-check them
# for articles on every run, but I haven't got around to it yet.
#
# This assumes an NNTP news feed.
#
# Feel free to copy, distribute and modify this code for
# non-commercial use. If you make any useful modifications, let me
# Feel free to copy, distribute and modify this code for
# non-commercial use. If you make any useful modifications, let me
# know!
#
# (c) Quentin Stafford-Fraser 1994
@ -42,9 +42,9 @@ import sys,nntplib, string, marshal, time, os, posix, string
topdir='/anfs/qsbigdisc/web/html/newspage'
# The name of your NNTP host
# eg.
# eg.
# newshost = 'nntp-serv.cl.cam.ac.uk'
# or use following to get the name from the NNTPSERVER environment
# or use following to get the name from the NNTPSERVER environment
# variable:
# newshost = posix.environ['NNTPSERVER']
newshost = 'nntp-serv.cl.cam.ac.uk'
@ -60,17 +60,17 @@ descfile = 'newsgroups'
# The directory in which HTML pages should be created
# eg.
# pagedir = '/usr/local/lib/html/newspage'
# pagedir = 'pages'
# pagedir = 'pages'
pagedir = topdir
# The html prefix which will refer to this directory
# eg.
# httppref = '/newspage/',
# eg.
# httppref = '/newspage/',
# or leave blank for relative links between pages: (Recommended)
# httppref = ''
httppref = ''
# The name of the 'root' news page in this directory.
# The name of the 'root' news page in this directory.
# A .html suffix will be added.
rootpage = 'root'
@ -88,7 +88,7 @@ pagelinkicon='... <img src="http://pelican.cl.cam.ac.uk/icons/page.xbm"> '
# Sublistsize controls the maximum number of items the will appear as
# an indented sub-list before the whole thing is moved onto a different
# page. The smaller this is, the more pages you will have, but the
# page. The smaller this is, the more pages you will have, but the
# shorter each will be.
sublistsize = 4
@ -118,246 +118,246 @@ page = os.path.join(topdir,pagedir)
# Addtotree creates/augments a tree from a list of group names
def addtotree(tree, groups):
print 'Updating tree...'
for i in groups:
print 'Updating tree...'
for i in groups:
parts = string.splitfields(i,'.')
makeleaf(tree, parts)
# Makeleaf makes a leaf and the branch leading to it if necessary
def makeleaf(tree,path):
j = path[0]
l = len(path)
j = path[0]
l = len(path)
if not tree.has_key(j):
tree[j] = {}
if l == 1:
tree[j]['.'] = '.'
if l > 1:
makeleaf(tree[j],path[1:])
if not tree.has_key(j):
tree[j] = {}
if l == 1:
tree[j]['.'] = '.'
if l > 1:
makeleaf(tree[j],path[1:])
# Then the bits for outputting trees as pages ----------------
# Then the bits for outputting trees as pages ----------------
# Createpage creates an HTML file named <root>.html containing links
# to those groups beginning with <root>.
def createpage(root, tree, p):
filename = os.path.join(pagedir,root+'.html')
if root == rootpage:
detail = ''
else:
detail = ' under ' + root
f = open(filename,'w')
# f.write('Content-Type: text/html\n')
f.write('<TITLE>Newsgroups available' + detail + '</TITLE>\n')
f.write('<H1>Newsgroups available' + detail +'</H1>\n')
f.write('<A HREF="'+httppref+rootpage+'.html">Back to top level</A><P>\n')
printtree(f,tree,0,p)
f.write('<I>This page automatically created by \'newslist\' v. '+rcsrev+'.')
f.write(time.ctime(time.time()) + '</I><P>')
f.close()
filename = os.path.join(pagedir,root+'.html')
if root == rootpage:
detail = ''
else:
detail = ' under ' + root
f = open(filename,'w')
# f.write('Content-Type: text/html\n')
f.write('<TITLE>Newsgroups available' + detail + '</TITLE>\n')
f.write('<H1>Newsgroups available' + detail +'</H1>\n')
f.write('<A HREF="'+httppref+rootpage+'.html">Back to top level</A><P>\n')
printtree(f,tree,0,p)
f.write('<I>This page automatically created by \'newslist\' v. '+rcsrev+'.')
f.write(time.ctime(time.time()) + '</I><P>')
f.close()
# Printtree prints the groups as a bulleted list. Groups with
# more than <sublistsize> subgroups will be put on a separate page.
# Other sets of subgroups are just indented.
def printtree(f, tree, indent, p):
global desc
l = len(tree)
global desc
l = len(tree)
if l > sublistsize and indent>0:
# Create a new page and a link to it
f.write('<LI><B><A HREF="'+httppref+p[1:]+'.html">')
f.write(p[1:]+'.*')
f.write('</A></B>'+pagelinkicon+'\n')
createpage(p[1:], tree, p)
return
if l > sublistsize and indent>0:
# Create a new page and a link to it
f.write('<LI><B><A HREF="'+httppref+p[1:]+'.html">')
f.write(p[1:]+'.*')
f.write('</A></B>'+pagelinkicon+'\n')
createpage(p[1:], tree, p)
return
kl = tree.keys()
kl = tree.keys()
if l > 1:
kl.sort()
if indent > 0:
# Create a sub-list
f.write('<LI>'+p[1:]+'\n<UL>')
else:
# Create a main list
f.write('<UL>')
indent = indent + 1
for i in kl:
if i == '.':
# Output a newsgroup
f.write('<LI><A HREF="news:' + p[1:] + '">'+ p[1:] + '</A> ')
if desc.has_key(p[1:]):
f.write(' <I>'+desc[p[1:]]+'</I>\n')
else:
f.write('\n')
else:
# Output a hierarchy
printtree(f,tree[i], indent, p+'.'+i)
if l > 1:
kl.sort()
if indent > 0:
# Create a sub-list
f.write('<LI>'+p[1:]+'\n<UL>')
else:
# Create a main list
f.write('<UL>')
indent = indent + 1
if l > 1:
f.write('\n</UL>')
for i in kl:
if i == '.':
# Output a newsgroup
f.write('<LI><A HREF="news:' + p[1:] + '">'+ p[1:] + '</A> ')
if desc.has_key(p[1:]):
f.write(' <I>'+desc[p[1:]]+'</I>\n')
else:
f.write('\n')
else:
# Output a hierarchy
printtree(f,tree[i], indent, p+'.'+i)
if l > 1:
f.write('\n</UL>')
# Reading descriptions file ---------------------------------------
# This returns an array mapping group name to its description
def readdesc(descfile):
global desc
global desc
desc = {}
desc = {}
if descfile == '':
if descfile == '':
return
try:
d = open(descfile, 'r')
print 'Reading descriptions...'
except (IOError):
print 'Failed to open description file ' + descfile
return
l = d.readline()
while l != '':
bits = string.split(l)
try:
grp = bits[0]
dsc = string.join(bits[1:])
if len(dsc)>1:
desc[grp] = dsc
except (IndexError):
pass
l = d.readline()
try:
d = open(descfile, 'r')
print 'Reading descriptions...'
except (IOError):
print 'Failed to open description file ' + descfile
return
l = d.readline()
while l != '':
bits = string.split(l)
try:
grp = bits[0]
dsc = string.join(bits[1:])
if len(dsc)>1:
desc[grp] = dsc
except (IndexError):
pass
l = d.readline()
# Check that ouput directory exists, ------------------------------
# and offer to create it if not
def checkopdir(pagedir):
if not os.path.isdir(pagedir):
print 'Directory '+pagedir+' does not exist.'
print 'Shall I create it for you? (y/n)'
if sys.stdin.readline()[0] == 'y':
try:
os.mkdir(pagedir,0777)
except:
print 'Sorry - failed!'
if not os.path.isdir(pagedir):
print 'Directory '+pagedir+' does not exist.'
print 'Shall I create it for you? (y/n)'
if sys.stdin.readline()[0] == 'y':
try:
os.mkdir(pagedir,0777)
except:
print 'Sorry - failed!'
sys.exit(1)
else:
print 'OK. Exiting.'
sys.exit(1)
else:
print 'OK. Exiting.'
sys.exit(1)
# Read and write current local tree ----------------------------------
def readlocallist(treefile):
print 'Reading current local group list...'
tree = {}
try:
treetime = time.localtime(os.stat(treefile)[ST_MTIME])
except:
print '\n*** Failed to open local group cache '+treefile
print 'If this is the first time you have run newslist, then'
print 'use the -a option to create it.'
sys.exit(1)
treedate = '%02d%02d%02d' % (treetime[0] % 100 ,treetime[1], treetime[2])
try:
dump = open(treefile,'r')
tree = marshal.load(dump)
dump.close()
except (IOError):
print 'Cannot open local group list ' + treefile
return (tree, treedate)
print 'Reading current local group list...'
tree = {}
try:
treetime = time.localtime(os.stat(treefile)[ST_MTIME])
except:
print '\n*** Failed to open local group cache '+treefile
print 'If this is the first time you have run newslist, then'
print 'use the -a option to create it.'
sys.exit(1)
treedate = '%02d%02d%02d' % (treetime[0] % 100 ,treetime[1], treetime[2])
try:
dump = open(treefile,'r')
tree = marshal.load(dump)
dump.close()
except (IOError):
print 'Cannot open local group list ' + treefile
return (tree, treedate)
def writelocallist(treefile, tree):
try:
dump = open(treefile,'w')
groups = marshal.dump(tree,dump)
dump.close()
print 'Saved list to '+treefile+'\n'
except:
print 'Sorry - failed to write to local group cache '+treefile
print 'Does it (or its directory) have the correct permissions?'
sys.exit(1)
try:
dump = open(treefile,'w')
groups = marshal.dump(tree,dump)
dump.close()
print 'Saved list to '+treefile+'\n'
except:
print 'Sorry - failed to write to local group cache '+treefile
print 'Does it (or its directory) have the correct permissions?'
sys.exit(1)
# Return list of all groups on server -----------------------------
def getallgroups(server):
print 'Getting list of all groups...'
treedate='010101'
info = server.list()[1]
groups = []
print 'Processing...'
if skipempty:
print '\nIgnoring following empty groups:'
for i in info:
grpname = string.split(i[0])[0]
if skipempty and string.atoi(i[1]) < string.atoi(i[2]):
print grpname+' ',
else:
groups.append(grpname)
print '\n'
if skipempty:
print '(End of empty groups)'
return groups
print 'Getting list of all groups...'
treedate='010101'
info = server.list()[1]
groups = []
print 'Processing...'
if skipempty:
print '\nIgnoring following empty groups:'
for i in info:
grpname = string.split(i[0])[0]
if skipempty and string.atoi(i[1]) < string.atoi(i[2]):
print grpname+' ',
else:
groups.append(grpname)
print '\n'
if skipempty:
print '(End of empty groups)'
return groups
# Return list of new groups on server -----------------------------
def getnewgroups(server, treedate):
print 'Getting list of new groups since start of '+treedate+'...',
info = server.newgroups(treedate,'000001')[1]
print 'got %d.' % len(info)
print 'Processing...',
groups = []
for i in info:
grpname = string.split(i)[0]
groups.append(grpname)
print 'Done'
return groups
print 'Getting list of new groups since start of '+treedate+'...',
info = server.newgroups(treedate,'000001')[1]
print 'got %d.' % len(info)
print 'Processing...',
groups = []
for i in info:
grpname = string.split(i)[0]
groups.append(grpname)
print 'Done'
return groups
# Now the main program --------------------------------------------
def main():
global desc
global desc
tree={}
tree={}
# Check that the output directory exists
checkopdir(pagedir);
# Check that the output directory exists
checkopdir(pagedir);
try:
print 'Connecting to '+newshost+'...'
if sys.version[0] == '0':
s = NNTP.init(newshost)
else:
s = NNTP(newshost)
connected = 1
except (nntplib.error_temp, nntplib.error_perm), x:
print 'Error connecting to host:', x
print 'I\'ll try to use just the local list.'
connected = 0
try:
print 'Connecting to '+newshost+'...'
if sys.version[0] == '0':
s = NNTP.init(newshost)
else:
s = NNTP(newshost)
connected = 1
except (nntplib.error_temp, nntplib.error_perm), x:
print 'Error connecting to host:', x
print 'I\'ll try to use just the local list.'
connected = 0
# If -a is specified, read the full list of groups from server
if connected and len(sys.argv) > 1 and sys.argv[1] == '-a':
# If -a is specified, read the full list of groups from server
if connected and len(sys.argv) > 1 and sys.argv[1] == '-a':
groups = getallgroups(s)
groups = getallgroups(s)
# Otherwise just read the local file and then add
# groups created since local file last modified.
else:
# Otherwise just read the local file and then add
# groups created since local file last modified.
else:
(tree, treedate) = readlocallist(treefile)
if connected:
groups = getnewgroups(s, treedate)
if connected:
addtotree(tree, groups)
writelocallist(treefile,tree)
(tree, treedate) = readlocallist(treefile)
if connected:
groups = getnewgroups(s, treedate)
# Read group descriptions
readdesc(descfile)
if connected:
addtotree(tree, groups)
writelocallist(treefile,tree)
print 'Creating pages...'
createpage(rootpage, tree, '')
print 'Done'
# Read group descriptions
readdesc(descfile)
print 'Creating pages...'
createpage(rootpage, tree, '')
print 'Done'
main()

View file

@ -34,89 +34,89 @@ NFLAG = 0
PFLAG = 0
try:
optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np')
optlist, ARGS = getopt.getopt(sys.argv[1:], 'acde:F:np')
except getopt.error, msg:
sys.stderr.write(sys.argv[0] + ': ' + msg + '\n')
sys.exit(2)
sys.stderr.write(sys.argv[0] + ': ' + msg + '\n')
sys.exit(2)
for option, optarg in optlist:
if option == '-a':
AFLAG = 1
elif option == '-c':
CFLAG = 1
elif option == '-d':
DFLAG = 1
elif option == '-e':
for line in string.splitfields(optarg, '\n'):
SCRIPT.append(line)
elif option == '-F':
FS = optarg
elif option == '-n':
NFLAG = 1
PFLAG = 0
elif option == '-p':
NFLAG = 1
PFLAG = 1
else:
print option, 'not recognized???'
if option == '-a':
AFLAG = 1
elif option == '-c':
CFLAG = 1
elif option == '-d':
DFLAG = 1
elif option == '-e':
for line in string.splitfields(optarg, '\n'):
SCRIPT.append(line)
elif option == '-F':
FS = optarg
elif option == '-n':
NFLAG = 1
PFLAG = 0
elif option == '-p':
NFLAG = 1
PFLAG = 1
else:
print option, 'not recognized???'
if not ARGS: ARGS.append('-')
if not SCRIPT:
if ARGS[0] == '-':
fp = sys.stdin
else:
fp = open(ARGS[0], 'r')
while 1:
line = fp.readline()
if not line: break
SCRIPT.append(line[:-1])
del fp
del ARGS[0]
if not ARGS: ARGS.append('-')
if ARGS[0] == '-':
fp = sys.stdin
else:
fp = open(ARGS[0], 'r')
while 1:
line = fp.readline()
if not line: break
SCRIPT.append(line[:-1])
del fp
del ARGS[0]
if not ARGS: ARGS.append('-')
if CFLAG:
prologue = ['if 0:']
epilogue = []
prologue = ['if 0:']
epilogue = []
elif NFLAG:
# Note that it is on purpose that AFLAG and PFLAG are
# tested dynamically each time through the loop
prologue = [ \
'LINECOUNT = 0', \
'for FILE in ARGS:', \
' \tif FILE == \'-\':', \
' \t \tFP = sys.stdin', \
' \telse:', \
' \t \tFP = open(FILE, \'r\')', \
' \tLINENO = 0', \
' \twhile 1:', \
' \t \tLINE = FP.readline()', \
' \t \tif not LINE: break', \
' \t \tLINENO = LINENO + 1', \
' \t \tLINECOUNT = LINECOUNT + 1', \
' \t \tL = LINE[:-1]', \
' \t \taflag = AFLAG', \
' \t \tif aflag:', \
' \t \t \tif FS: F = string.splitfields(L, FS)', \
' \t \t \telse: F = string.split(L)' \
]
epilogue = [ \
' \t \tif not PFLAG: continue', \
' \t \tif aflag:', \
' \t \t \tif FS: print string.joinfields(F, FS)', \
' \t \t \telse: print string.join(F)', \
' \t \telse: print L', \
]
# Note that it is on purpose that AFLAG and PFLAG are
# tested dynamically each time through the loop
prologue = [ \
'LINECOUNT = 0', \
'for FILE in ARGS:', \
' \tif FILE == \'-\':', \
' \t \tFP = sys.stdin', \
' \telse:', \
' \t \tFP = open(FILE, \'r\')', \
' \tLINENO = 0', \
' \twhile 1:', \
' \t \tLINE = FP.readline()', \
' \t \tif not LINE: break', \
' \t \tLINENO = LINENO + 1', \
' \t \tLINECOUNT = LINECOUNT + 1', \
' \t \tL = LINE[:-1]', \
' \t \taflag = AFLAG', \
' \t \tif aflag:', \
' \t \t \tif FS: F = string.splitfields(L, FS)', \
' \t \t \telse: F = string.split(L)' \
]
epilogue = [ \
' \t \tif not PFLAG: continue', \
' \t \tif aflag:', \
' \t \t \tif FS: print string.joinfields(F, FS)', \
' \t \t \telse: print string.join(F)', \
' \t \telse: print L', \
]
else:
prologue = ['if 1:']
epilogue = []
prologue = ['if 1:']
epilogue = []
# Note that we indent using tabs only, so that any indentation style
# used in 'command' will come out right after re-indentation.
program = string.joinfields(prologue, '\n') + '\n'
for line in SCRIPT:
program = program + (' \t \t' + line + '\n')
program = program + (' \t \t' + line + '\n')
program = program + (string.joinfields(epilogue, '\n') + '\n')
import tempfile
@ -124,7 +124,7 @@ fp = tempfile.NamedTemporaryFile()
fp.write(program)
fp.flush()
if DFLAG:
import pdb
pdb.run('execfile(%r)' % (tfn,))
import pdb
pdb.run('execfile(%r)' % (tfn,))
else:
execfile(tfn)
execfile(tfn)

View file

@ -3,24 +3,24 @@
# Print prime numbers in a given range
def main():
import sys
min, max = 2, 0x7fffffff
if sys.argv[1:]:
min = int(eval(sys.argv[1]))
if sys.argv[2:]:
max = int(eval(sys.argv[2]))
primes(min, max)
import sys
min, max = 2, 0x7fffffff
if sys.argv[1:]:
min = int(eval(sys.argv[1]))
if sys.argv[2:]:
max = int(eval(sys.argv[2]))
primes(min, max)
def primes(min, max):
if 2 >= min: print 2
primes = [2]
i = 3
while i <= max:
for p in primes:
if i%p == 0 or p*p > i: break
if i%p <> 0:
primes.append(i)
if i >= min: print i
i = i+2
if 2 >= min: print 2
primes = [2]
i = 3
while i <= max:
for p in primes:
if i%p == 0 or p*p > i: break
if i%p <> 0:
primes.append(i)
if i >= min: print i
i = i+2
main()

View file

@ -1,8 +1,8 @@
#! /usr/bin/env python
# script.py -- Make typescript of terminal session.
# Usage:
# -a Append to typescript.
# -p Use Python as shell.
# -a Append to typescript.
# -p Use Python as shell.
# Author: Steen Lumholt.
@ -10,19 +10,19 @@ import os, time, sys
import pty
def read(fd):
data = os.read(fd, 1024)
file.write(data)
return data
data = os.read(fd, 1024)
file.write(data)
return data
shell = 'sh'
filename = 'typescript'
mode = 'w'
if os.environ.has_key('SHELL'):
shell = os.environ['SHELL']
shell = os.environ['SHELL']
if '-a' in sys.argv:
mode = 'a'
mode = 'a'
if '-p' in sys.argv:
shell = 'python'
shell = 'python'
file = open(filename, mode)

View file

@ -4,7 +4,7 @@
# The input file contains lines of the form <filename>:<lineno>:<text>,
# meaning that the given line of the given file is to be replaced
# by the given text. This is useful for performing global substitutions
# on grep output:
# on grep output:
import os
import sys
@ -14,78 +14,78 @@ pat = '^\([^: \t\n]+\):\([1-9][0-9]*\):'
prog = regex.compile(pat)
class FileObj:
def __init__(self, filename):
self.filename = filename
self.changed = 0
try:
self.lines = open(filename, 'r').readlines()
except IOError, msg:
print '*** Can\'t open "%s":' % filename, msg
self.lines = None
return
print 'diffing', self.filename
def __init__(self, filename):
self.filename = filename
self.changed = 0
try:
self.lines = open(filename, 'r').readlines()
except IOError, msg:
print '*** Can\'t open "%s":' % filename, msg
self.lines = None
return
print 'diffing', self.filename
def finish(self):
if not self.changed:
print 'no changes to', self.filename
return
try:
os.rename(self.filename, self.filename + '~')
fp = open(self.filename, 'w')
except (os.error, IOError), msg:
print '*** Can\'t rewrite "%s":' % self.filename, msg
return
print 'writing', self.filename
for line in self.lines:
fp.write(line)
fp.close()
self.changed = 0
def finish(self):
if not self.changed:
print 'no changes to', self.filename
return
try:
os.rename(self.filename, self.filename + '~')
fp = open(self.filename, 'w')
except (os.error, IOError), msg:
print '*** Can\'t rewrite "%s":' % self.filename, msg
return
print 'writing', self.filename
for line in self.lines:
fp.write(line)
fp.close()
self.changed = 0
def process(self, lineno, rest):
if self.lines is None:
print '(not processed): %s:%s:%s' % (
self.filename, lineno, rest),
return
i = eval(lineno) - 1
if not 0 <= i < len(self.lines):
print '*** Line number out of range: %s:%s:%s' % (
self.filename, lineno, rest),
return
if self.lines[i] == rest:
print '(no change): %s:%s:%s' % (
self.filename, lineno, rest),
return
if not self.changed:
self.changed = 1
print '%sc%s' % (lineno, lineno)
print '<', self.lines[i],
print '---'
self.lines[i] = rest
print '>', self.lines[i],
def process(self, lineno, rest):
if self.lines is None:
print '(not processed): %s:%s:%s' % (
self.filename, lineno, rest),
return
i = eval(lineno) - 1
if not 0 <= i < len(self.lines):
print '*** Line number out of range: %s:%s:%s' % (
self.filename, lineno, rest),
return
if self.lines[i] == rest:
print '(no change): %s:%s:%s' % (
self.filename, lineno, rest),
return
if not self.changed:
self.changed = 1
print '%sc%s' % (lineno, lineno)
print '<', self.lines[i],
print '---'
self.lines[i] = rest
print '>', self.lines[i],
def main():
if sys.argv[1:]:
try:
fp = open(sys.argv[1], 'r')
except IOError, msg:
print 'Can\'t open "%s":' % sys.argv[1], msg
sys.exit(1)
else:
fp = sys.stdin
curfile = None
while 1:
line = fp.readline()
if not line:
if curfile: curfile.finish()
break
n = prog.match(line)
if n < 0:
print 'Funny line:', line,
continue
filename, lineno = prog.group(1, 2)
if not curfile or filename <> curfile.filename:
if curfile: curfile.finish()
curfile = FileObj(filename)
curfile.process(lineno, line[n:])
if sys.argv[1:]:
try:
fp = open(sys.argv[1], 'r')
except IOError, msg:
print 'Can\'t open "%s":' % sys.argv[1], msg
sys.exit(1)
else:
fp = sys.stdin
curfile = None
while 1:
line = fp.readline()
if not line:
if curfile: curfile.finish()
break
n = prog.match(line)
if n < 0:
print 'Funny line:', line,
continue
filename, lineno = prog.group(1, 2)
if not curfile or filename <> curfile.filename:
if curfile: curfile.finish()
curfile = FileObj(filename)
curfile.process(lineno, line[n:])
main()