Whitespace normalization.

This commit is contained in:
Tim Peters 2001-01-17 08:48:39 +00:00
parent a888540593
commit 70c4378dbc
57 changed files with 2434 additions and 2440 deletions

View file

@ -12,46 +12,46 @@ from stat import *
# Use lstat() to stat files if it exists, else stat()
try:
statfunc = os.lstat
statfunc = os.lstat
except AttributeError:
statfunc = os.stat
statfunc = os.stat
# Parse options
if sys.argv[1] == '-m':
itime = ST_MTIME
del sys.argv[1]
itime = ST_MTIME
del sys.argv[1]
elif sys.argv[1] == '-c':
itime = ST_CTIME
del sys.argv[1]
itime = ST_CTIME
del sys.argv[1]
elif sys.argv[1] == '-a':
itime = ST_CTIME
del sys.argv[1]
itime = ST_CTIME
del sys.argv[1]
else:
itime = ST_MTIME
itime = ST_MTIME
secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
now = time.time() # Current time, for age computations
status = 0 # Exit status, set to 1 on errors
secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
now = time.time() # Current time, for age computations
status = 0 # Exit status, set to 1 on errors
# Compute max file name length
maxlen = 1
for file in sys.argv[1:]:
if len(file) > maxlen: maxlen = len(file)
if len(file) > maxlen: maxlen = len(file)
# Process each argument in turn
for file in sys.argv[1:]:
try:
st = statfunc(file)
except os.error, msg:
sys.stderr.write('can\'t stat ' + `file` + ': ' + `msg` + '\n')
status = 1
st = ()
if st:
anytime = st[itime]
size = st[ST_SIZE]
age = now - anytime
byteyears = float(size) * float(age) / secs_per_year
print string.ljust(file, maxlen),
print string.rjust(`int(byteyears)`, 8)
try:
st = statfunc(file)
except os.error, msg:
sys.stderr.write('can\'t stat ' + `file` + ': ' + `msg` + '\n')
status = 1
st = ()
if st:
anytime = st[itime]
size = st[ST_SIZE]
age = now - anytime
byteyears = float(size) * float(age) / secs_per_year
print string.ljust(file, maxlen),
print string.rjust(`int(byteyears)`, 8)
sys.exit(status)

View file

@ -8,58 +8,58 @@ from stat import ST_MTIME
import imp
def main():
silent = 0
verbose = 0
if sys.argv[1:]:
if sys.argv[1] == '-v':
verbose = 1
elif sys.argv[1] == '-s':
silent = 1
MAGIC = imp.get_magic()
if not silent:
print 'Using MAGIC word', `MAGIC`
for dirname in sys.path:
try:
names = os.listdir(dirname)
except os.error:
print 'Cannot list directory', `dirname`
continue
if not silent:
print 'Checking', `dirname`, '...'
names.sort()
for name in names:
if name[-3:] == '.py':
name = os.path.join(dirname, name)
try:
st = os.stat(name)
except os.error:
print 'Cannot stat', `name`
continue
if verbose:
print 'Check', `name`, '...'
name_c = name + 'c'
try:
f = open(name_c, 'r')
except IOError:
print 'Cannot open', `name_c`
continue
magic_str = f.read(4)
mtime_str = f.read(4)
f.close()
if magic_str <> MAGIC:
print 'Bad MAGIC word in ".pyc" file',
print `name_c`
continue
mtime = get_long(mtime_str)
if mtime == 0 or mtime == -1:
print 'Bad ".pyc" file', `name_c`
elif mtime <> st[ST_MTIME]:
print 'Out-of-date ".pyc" file',
print `name_c`
silent = 0
verbose = 0
if sys.argv[1:]:
if sys.argv[1] == '-v':
verbose = 1
elif sys.argv[1] == '-s':
silent = 1
MAGIC = imp.get_magic()
if not silent:
print 'Using MAGIC word', `MAGIC`
for dirname in sys.path:
try:
names = os.listdir(dirname)
except os.error:
print 'Cannot list directory', `dirname`
continue
if not silent:
print 'Checking', `dirname`, '...'
names.sort()
for name in names:
if name[-3:] == '.py':
name = os.path.join(dirname, name)
try:
st = os.stat(name)
except os.error:
print 'Cannot stat', `name`
continue
if verbose:
print 'Check', `name`, '...'
name_c = name + 'c'
try:
f = open(name_c, 'r')
except IOError:
print 'Cannot open', `name_c`
continue
magic_str = f.read(4)
mtime_str = f.read(4)
f.close()
if magic_str <> MAGIC:
print 'Bad MAGIC word in ".pyc" file',
print `name_c`
continue
mtime = get_long(mtime_str)
if mtime == 0 or mtime == -1:
print 'Bad ".pyc" file', `name_c`
elif mtime <> st[ST_MTIME]:
print 'Out-of-date ".pyc" file',
print `name_c`
def get_long(s):
if len(s) <> 4:
return -1
return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
if len(s) <> 4:
return -1
return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
main()

View file

@ -4,9 +4,9 @@
#
# Fix Python source files to use the new class definition syntax, i.e.,
# the syntax used in Python versions before 0.9.8:
# class C() = base(), base(), ...: ...
# class C() = base(), base(), ...: ...
# is changed to the current syntax:
# class C(base, base, ...): ...
# class C(base, base, ...): ...
#
# The script uses heuristics to find class definitions that usually
# work but occasionally can fail; carefully check the output!
@ -39,113 +39,113 @@ 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(' + `dirname` + ')\n')
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n')
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(' + `dirname` + ')\n')
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n')
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(' + `filename` + ')\n')
try:
f = open(filename, 'r')
except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n')
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
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(tempname+': cannot create: '+\
`msg`+'\n')
return 1
f.seek(0)
lineno = 0
rep(filename + ':\n')
continue # restart from the beginning
rep(`lineno` + '\n')
rep('< ' + line)
rep('> ' + newline)
if g is not None:
g.write(newline)
## dbg('fix(' + `filename` + ')\n')
try:
f = open(filename, 'r')
except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n')
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
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(tempname+': cannot create: '+\
`msg`+'\n')
return 1
f.seek(0)
lineno = 0
rep(filename + ':\n')
continue # restart from the beginning
rep(`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
# End of file
f.close()
if not g: return 0 # No changes
# Finishing touch -- move files
# 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(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n')
return 1
# Return succes
return 0
# 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(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n')
return 1
# Return succes
return 0
# This expression doesn't catch *all* class definition headers,
# but it's pretty darn close.
@ -159,34 +159,34 @@ baseprog = regex.compile(baseexpr)
import string
def fixline(line):
if classprog.match(line) < 0: # No 'class' keyword -- no change
return line
(a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]
# a0, b0 = Whole match (up to ':')
# a1, b1 = First subexpression (up to classname)
# a2, b2 = Second subexpression (=.*)
head = line[:b1]
tail = line[b0:] # Unmatched rest of line
if a2 == b2: # No base classes -- easy case
return head + ':' + tail
# Get rid of leading '='
basepart = line[a2+1:b2]
if classprog.match(line) < 0: # No 'class' keyword -- no change
return line
# Extract list of base expressions
bases = string.splitfields(basepart, ',')
# Strip trailing '()' from each base expression
for i in range(len(bases)):
if baseprog.match(bases[i]) >= 0:
x1, y1 = baseprog.regs[1]
bases[i] = bases[i][x1:y1]
# Join the bases back again and build the new line
basepart = string.joinfields(bases, ', ')
return head + '(' + basepart + '):' + tail
(a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]
# a0, b0 = Whole match (up to ':')
# a1, b1 = First subexpression (up to classname)
# a2, b2 = Second subexpression (=.*)
head = line[:b1]
tail = line[b0:] # Unmatched rest of line
if a2 == b2: # No base classes -- easy case
return head + ':' + tail
# Get rid of leading '='
basepart = line[a2+1:b2]
# Extract list of base expressions
bases = string.splitfields(basepart, ',')
# Strip trailing '()' from each base expression
for i in range(len(bases)):
if baseprog.match(bases[i]) >= 0:
x1, y1 = baseprog.regs[1]
bases[i] = bases[i][x1:y1]
# Join the bases back again and build the new line
basepart = string.joinfields(bases, ', ')
return head + '(' + basepart + '):' + tail
main()

View file

@ -7,19 +7,19 @@ import os
from stat import ST_ATIME, ST_MTIME # Really constants 7 and 8
def main():
if len(sys.argv) <> 3:
sys.stderr.write('usage: copytime source destination\n')
sys.exit(2)
file1, file2 = sys.argv[1], sys.argv[2]
try:
stat1 = os.stat(file1)
except os.error:
sys.stderr.write(file1 + ': cannot stat\n')
sys.exit(1)
try:
os.utime(file2, (stat1[ST_ATIME], stat1[ST_MTIME]))
except os.error:
sys.stderr.write(file2 + ': cannot change time\n')
sys.exit(2)
if len(sys.argv) <> 3:
sys.stderr.write('usage: copytime source destination\n')
sys.exit(2)
file1, file2 = sys.argv[1], sys.argv[2]
try:
stat1 = os.stat(file1)
except os.error:
sys.stderr.write(file1 + ': cannot stat\n')
sys.exit(1)
try:
os.utime(file2, (stat1[ST_ATIME], stat1[ST_MTIME]))
except os.error:
sys.stderr.write(file2 + ': cannot change time\n')
sys.exit(2)
main()

View file

@ -4,56 +4,56 @@
import os, string, sys, errno
def main():
p = os.popen('du ' + string.join(sys.argv[1:]), 'r')
total, d = None, {}
for line in p.readlines():
i = 0
while line[i] in '0123456789': i = i+1
size = eval(line[:i])
while line[i] in ' \t': i = i+1
file = line[i:-1]
comps = string.splitfields(file, '/')
if comps[0] == '': comps[0] = '/'
if comps[len(comps)-1] == '': del comps[len(comps)-1]
total, d = store(size, comps, total, d)
try:
display(total, d)
except IOError, e:
if e.errno != errno.EPIPE:
raise
p = os.popen('du ' + string.join(sys.argv[1:]), 'r')
total, d = None, {}
for line in p.readlines():
i = 0
while line[i] in '0123456789': i = i+1
size = eval(line[:i])
while line[i] in ' \t': i = i+1
file = line[i:-1]
comps = string.splitfields(file, '/')
if comps[0] == '': comps[0] = '/'
if comps[len(comps)-1] == '': del comps[len(comps)-1]
total, d = store(size, comps, total, d)
try:
display(total, d)
except IOError, e:
if e.errno != errno.EPIPE:
raise
def store(size, comps, total, d):
if comps == []:
return size, d
if not d.has_key(comps[0]):
d[comps[0]] = None, {}
t1, d1 = d[comps[0]]
d[comps[0]] = store(size, comps[1:], t1, d1)
return total, d
if comps == []:
return size, d
if not d.has_key(comps[0]):
d[comps[0]] = None, {}
t1, d1 = d[comps[0]]
d[comps[0]] = store(size, comps[1:], t1, d1)
return total, d
def display(total, d):
show(total, d, '')
show(total, d, '')
def show(total, d, prefix):
if not d: return
list = []
sum = 0
for key in d.keys():
tsub, dsub = d[key]
list.append((tsub, key))
if tsub is not None: sum = sum + tsub
## if sum < total:
## list.append((total - sum, os.curdir))
list.sort()
list.reverse()
width = len(`list[0][0]`)
for tsub, key in list:
if tsub is None:
psub = prefix
else:
print prefix + string.rjust(`tsub`, width) + ' ' + key
psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1)
if d.has_key(key):
show(tsub, d[key][1], psub)
if not d: return
list = []
sum = 0
for key in d.keys():
tsub, dsub = d[key]
list.append((tsub, key))
if tsub is not None: sum = sum + tsub
## if sum < total:
## list.append((total - sum, os.curdir))
list.sort()
list.reverse()
width = len(`list[0][0]`)
for tsub, key in list:
if tsub is None:
psub = prefix
else:
print prefix + string.rjust(`tsub`, width) + ' ' + key
psub = prefix + ' '*(width-1) + '|' + ' '*(len(key)+1)
if d.has_key(key):
show(tsub, d[key][1], psub)
main()

View file

@ -10,33 +10,33 @@ import regex
import getopt
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], '')
if len(args) < 2:
raise getopt.error, 'not enough arguments'
except getopt.error, msg:
sys.stdout = sys.stderr
print msg
print 'usage: findlinksto pattern directory ...'
sys.exit(2)
pat, dirs = args[0], args[1:]
prog = regex.compile(pat)
for dirname in dirs:
os.path.walk(dirname, visit, prog)
try:
opts, args = getopt.getopt(sys.argv[1:], '')
if len(args) < 2:
raise getopt.error, 'not enough arguments'
except getopt.error, msg:
sys.stdout = sys.stderr
print msg
print 'usage: findlinksto pattern directory ...'
sys.exit(2)
pat, dirs = args[0], args[1:]
prog = regex.compile(pat)
for dirname in dirs:
os.path.walk(dirname, visit, prog)
def visit(prog, dirname, names):
if os.path.islink(dirname):
names[:] = []
return
if os.path.ismount(dirname):
print 'descend into', dirname
for name in names:
name = os.path.join(dirname, name)
try:
linkto = os.readlink(name)
if prog.search(linkto) >= 0:
print name, '->', linkto
except os.error:
pass
if os.path.islink(dirname):
names[:] = []
return
if os.path.ismount(dirname):
print 'descend into', dirname
for name in names:
name = os.path.join(dirname, name)
try:
linkto = os.readlink(name)
if prog.search(linkto) >= 0:
print name, '->', linkto
except os.error:
pass
main()

View file

@ -46,151 +46,151 @@ dbg = err
rep = sys.stdout.write
def usage():
progname = sys.argv[0]
err('Usage: ' + progname +
' [-c] [-r] [-s file] ... file-or-directory ...\n')
err('\n')
err('-c : substitute inside comments\n')
err('-r : reverse direction for following -s options\n')
err('-s substfile : add a file of substitutions\n')
err('\n')
err('Each non-empty non-comment line in a substitution file must\n')
err('contain exactly two words: an identifier and its replacement.\n')
err('Comments start with a # character and end at end of line.\n')
err('If an identifier is preceded with a *, it is not substituted\n')
err('inside a comment even when -c is specified.\n')
progname = sys.argv[0]
err('Usage: ' + progname +
' [-c] [-r] [-s file] ... file-or-directory ...\n')
err('\n')
err('-c : substitute inside comments\n')
err('-r : reverse direction for following -s options\n')
err('-s substfile : add a file of substitutions\n')
err('\n')
err('Each non-empty non-comment line in a substitution file must\n')
err('contain exactly two words: an identifier and its replacement.\n')
err('Comments start with a # character and end at end of line.\n')
err('If an identifier is preceded with a *, it is not substituted\n')
err('inside a comment even when -c is specified.\n')
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'crs:')
except getopt.error, msg:
err('Options error: ' + str(msg) + '\n')
usage()
sys.exit(2)
bad = 0
if not args: # No arguments
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-c':
setdocomments()
if opt == '-r':
setreverse()
if opt == '-s':
addsubst(arg)
for arg in args:
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)
try:
opts, args = getopt.getopt(sys.argv[1:], 'crs:')
except getopt.error, msg:
err('Options error: ' + str(msg) + '\n')
usage()
sys.exit(2)
bad = 0
if not args: # No arguments
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-c':
setdocomments()
if opt == '-r':
setreverse()
if opt == '-s':
addsubst(arg)
for arg in args:
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)
# Change this regular expression to select a different set of files
Wanted = '^[a-zA-Z0-9_]+\.[ch]$'
def wanted(name):
return regex.match(Wanted, name) >= 0
return regex.match(Wanted, name) >= 0
def recursedown(dirname):
dbg('recursedown(' + `dirname` + ')\n')
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err(dirname + ': cannot list directory: ' + str(msg) + '\n')
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 wanted(name):
if fix(fullname): bad = 1
for fullname in subdirs:
if recursedown(fullname): bad = 1
return bad
dbg('recursedown(' + `dirname` + ')\n')
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err(dirname + ': cannot list directory: ' + str(msg) + '\n')
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 wanted(name):
if fix(fullname): bad = 1
for fullname in subdirs:
if recursedown(fullname): bad = 1
return bad
def fix(filename):
## dbg('fix(' + `filename` + ')\n')
if filename == '-':
# Filter mode
f = sys.stdin
g = sys.stdout
else:
# File replacement mode
try:
f = open(filename, 'r')
except IOError, msg:
err(filename + ': cannot open: ' + str(msg) + '\n')
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
initfixline()
while 1:
line = f.readline()
if not line: break
lineno = lineno + 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(tempname+': cannot create: '+
str(msg)+'\n')
return 1
f.seek(0)
lineno = 0
initfixline()
rep(filename + ':\n')
continue # restart from the beginning
rep(`lineno` + '\n')
rep('< ' + line)
rep('> ' + newline)
if g is not None:
g.write(newline)
## dbg('fix(' + `filename` + ')\n')
if filename == '-':
# Filter mode
f = sys.stdin
g = sys.stdout
else:
# File replacement mode
try:
f = open(filename, 'r')
except IOError, msg:
err(filename + ': cannot open: ' + str(msg) + '\n')
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
initfixline()
while 1:
line = f.readline()
if not line: break
lineno = lineno + 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(tempname+': cannot create: '+
str(msg)+'\n')
return 1
f.seek(0)
lineno = 0
initfixline()
rep(filename + ':\n')
continue # restart from the beginning
rep(`lineno` + '\n')
rep('< ' + line)
rep('> ' + newline)
if g is not None:
g.write(newline)
# End of file
if filename == '-': return 0 # Done in filter mode
f.close()
if not g: return 0 # No changes
# End of file
if filename == '-': return 0 # Done in filter mode
f.close()
if not g: return 0 # No changes
# Finishing touch -- move files
# 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(tempname + ': warning: chmod failed (' + str(msg) + ')\n')
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err(filename + ': warning: backup failed (' + str(msg) + ')\n')
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err(filename + ': rename failed (' + str(msg) + ')\n')
return 1
# Return succes
return 0
# 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(tempname + ': warning: chmod failed (' + str(msg) + ')\n')
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err(filename + ': warning: backup failed (' + str(msg) + ')\n')
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err(filename + ': rename failed (' + str(msg) + ')\n')
return 1
# Return succes
return 0
# Tokenizing ANSI C (partly)
@ -221,98 +221,98 @@ InsideCommentPattern = '\(' + string.joinfields(InsideComment, '\|') + '\)'
InsideCommentProgram = regex.compile(InsideCommentPattern)
def initfixline():
global Program
Program = OutsideCommentProgram
global Program
Program = OutsideCommentProgram
def fixline(line):
global Program
## print '-->', `line`
i = 0
while i < len(line):
i = Program.search(line, i)
if i < 0: break
found = Program.group(0)
## if Program is InsideCommentProgram: print '...',
## else: print ' ',
## print found
if len(found) == 2:
if found == '/*':
Program = InsideCommentProgram
elif found == '*/':
Program = OutsideCommentProgram
n = len(found)
if Dict.has_key(found):
subst = Dict[found]
if Program is InsideCommentProgram:
if not Docomments:
print 'Found in comment:', found
i = i + n
continue
if NotInComment.has_key(found):
## print 'Ignored in comment:',
## print found, '-->', subst
## print 'Line:', line,
subst = found
## else:
## print 'Substituting in comment:',
## print found, '-->', subst
## print 'Line:', line,
line = line[:i] + subst + line[i+n:]
n = len(subst)
i = i + n
return line
global Program
## print '-->', `line`
i = 0
while i < len(line):
i = Program.search(line, i)
if i < 0: break
found = Program.group(0)
## if Program is InsideCommentProgram: print '...',
## else: print ' ',
## print found
if len(found) == 2:
if found == '/*':
Program = InsideCommentProgram
elif found == '*/':
Program = OutsideCommentProgram
n = len(found)
if Dict.has_key(found):
subst = Dict[found]
if Program is InsideCommentProgram:
if not Docomments:
print 'Found in comment:', found
i = i + n
continue
if NotInComment.has_key(found):
## print 'Ignored in comment:',
## print found, '-->', subst
## print 'Line:', line,
subst = found
## else:
## print 'Substituting in comment:',
## print found, '-->', subst
## print 'Line:', line,
line = line[:i] + subst + line[i+n:]
n = len(subst)
i = i + n
return line
Docomments = 0
def setdocomments():
global Docomments
Docomments = 1
global Docomments
Docomments = 1
Reverse = 0
def setreverse():
global Reverse
Reverse = (not Reverse)
global Reverse
Reverse = (not Reverse)
Dict = {}
NotInComment = {}
def addsubst(substfile):
try:
fp = open(substfile, 'r')
except IOError, msg:
err(substfile + ': cannot read substfile: ' + str(msg) + '\n')
sys.exit(1)
lineno = 0
while 1:
line = fp.readline()
if not line: break
lineno = lineno + 1
try:
i = string.index(line, '#')
except string.index_error:
i = -1 # Happens to delete trailing \n
words = string.split(line[:i])
if not words: continue
if len(words) == 3 and words[0] == 'struct':
words[:2] = [words[0] + ' ' + words[1]]
elif len(words) <> 2:
err(substfile + ':' + `lineno` +
': warning: bad line: ' + line)
continue
if Reverse:
[value, key] = words
else:
[key, value] = words
if value[0] == '*':
value = value[1:]
if key[0] == '*':
key = key[1:]
NotInComment[key] = value
if Dict.has_key(key):
err(substfile + ':' + `lineno` +
': warning: overriding: ' +
key + ' ' + value + '\n')
err(substfile + ':' + `lineno` +
': warning: previous: ' + Dict[key] + '\n')
Dict[key] = value
fp.close()
try:
fp = open(substfile, 'r')
except IOError, msg:
err(substfile + ': cannot read substfile: ' + str(msg) + '\n')
sys.exit(1)
lineno = 0
while 1:
line = fp.readline()
if not line: break
lineno = lineno + 1
try:
i = string.index(line, '#')
except string.index_error:
i = -1 # Happens to delete trailing \n
words = string.split(line[:i])
if not words: continue
if len(words) == 3 and words[0] == 'struct':
words[:2] = [words[0] + ' ' + words[1]]
elif len(words) <> 2:
err(substfile + ':' + `lineno` +
': warning: bad line: ' + line)
continue
if Reverse:
[value, key] = words
else:
[key, value] = words
if value[0] == '*':
value = value[1:]
if key[0] == '*':
key = key[1:]
NotInComment[key] = value
if Dict.has_key(key):
err(substfile + ':' + `lineno` +
': warning: overriding: ' +
key + ' ' + value + '\n')
err(substfile + ':' + `lineno` +
': warning: previous: ' + Dict[key] + '\n')
Dict[key] = value
fp.close()
main()

View file

@ -6,44 +6,44 @@ import sys
import string
def main():
args = sys.argv[1:]
for file in args:
process(file)
args = sys.argv[1:]
for file in args:
process(file)
def process(file):
try:
f = open(file, 'r')
except IOError, msg:
sys.stderr.write('%s: can\'t open: %s\n' % (file, str(msg)))
return
data = f.read()
f.close()
if data[:2] <> '/*':
sys.stderr.write('%s does not begin with C comment\n' % file)
return
try:
f = open(file, 'w')
except IOError, msg:
sys.stderr.write('%s: can\'t write: %s\n' % (file, str(msg)))
return
sys.stderr.write('Processing %s ...\n' % file)
magic = 'Py_'
for c in file:
if c in string.letters + string.digits:
magic = magic + string.upper(c)
else: magic = magic + '_'
sys.stdout = f
print '#ifndef', magic
print '#define', magic
print '#ifdef __cplusplus'
print 'extern "C" {'
print '#endif'
print
f.write(data)
print
print '#ifdef __cplusplus'
print '}'
print '#endif'
print '#endif /*', '!'+magic, '*/'
try:
f = open(file, 'r')
except IOError, msg:
sys.stderr.write('%s: can\'t open: %s\n' % (file, str(msg)))
return
data = f.read()
f.close()
if data[:2] <> '/*':
sys.stderr.write('%s does not begin with C comment\n' % file)
return
try:
f = open(file, 'w')
except IOError, msg:
sys.stderr.write('%s: can\'t write: %s\n' % (file, str(msg)))
return
sys.stderr.write('Processing %s ...\n' % file)
magic = 'Py_'
for c in file:
if c in string.letters + string.digits:
magic = magic + string.upper(c)
else: magic = magic + '_'
sys.stdout = f
print '#ifndef', magic
print '#define', magic
print '#ifdef __cplusplus'
print 'extern "C" {'
print '#endif'
print
f.write(data)
print
print '#ifdef __cplusplus'
print '}'
print '#endif'
print '#endif /*', '!'+magic, '*/'
main()

View file

@ -8,26 +8,26 @@ import re
def main():
for file in sys.argv[1:]:
try:
f = open(file, 'r')
except IOError, msg:
print file, ': can\'t open :', msg
continue
line = f.readline()
if not re.match('^#! */usr/local/bin/python', line):
print file, ': not a /usr/local/bin/python script'
f.close()
continue
rest = f.read()
f.close()
line = re.sub('/usr/local/bin/python',
'/usr/bin/env python', line)
print file, ':', `line`
f = open(file, "w")
f.write(line)
f.write(rest)
f.close()
for file in sys.argv[1:]:
try:
f = open(file, 'r')
except IOError, msg:
print file, ': can\'t open :', msg
continue
line = f.readline()
if not re.match('^#! */usr/local/bin/python', line):
print file, ': not a /usr/local/bin/python script'
f.close()
continue
rest = f.read()
f.close()
line = re.sub('/usr/local/bin/python',
'/usr/bin/env python', line)
print file, ':', `line`
f = open(file, "w")
f.write(line)
f.write(rest)
f.close()
main()

View file

@ -4,7 +4,7 @@
usage: ftpmirror [-v] [-q] [-i] [-m] [-n] [-r] [-s pat]
[-l username [-p passwd [-a account]]]
hostname [remotedir [localdir]]
hostname [remotedir [localdir]]
-v: verbose
-q: quiet
-i: interactive mode
@ -28,10 +28,10 @@ from fnmatch import fnmatch
# Print usage message and exit
def usage(*args):
sys.stdout = sys.stderr
for msg in args: print msg
print __doc__
sys.exit(2)
sys.stdout = sys.stderr
for msg in args: print msg
print __doc__
sys.exit(2)
verbose = 1 # 0 for -q, 2 for -v
interactive = 0
@ -42,356 +42,356 @@ skippats = ['.', '..', '.mirrorinfo']
# Main program: parse command line and start processing
def main():
global verbose, interactive, mac, rmok, nologin
try:
opts, args = getopt.getopt(sys.argv[1:], 'a:bil:mnp:qrs:v')
except getopt.error, msg:
usage(msg)
login = ''
passwd = ''
account = ''
for o, a in opts:
if o == '-l': login = a
if o == '-p': passwd = a
if o == '-a': account = a
if o == '-v': verbose = verbose + 1
if o == '-q': verbose = 0
if o == '-i': interactive = 1
if o == '-m': mac = 1; nologin = 1; skippats.append('*.o')
if o == '-n': nologin = 1
if o == '-r': rmok = 1
if o == '-s': skippats.append(a)
if not args: usage('hostname missing')
host = args[0]
remotedir = ''
localdir = ''
if args[1:]:
remotedir = args[1]
if args[2:]:
localdir = args[2]
if args[3:]: usage('too many arguments')
#
f = ftplib.FTP()
if verbose: print 'Connecting to %s...' % `host`
f.connect(host)
if not nologin:
if verbose:
print 'Logging in as %s...' % `login or 'anonymous'`
f.login(login, passwd, account)
if verbose: print 'OK.'
pwd = f.pwd()
if verbose > 1: print 'PWD =', `pwd`
if remotedir:
if verbose > 1: print 'cwd(%s)' % `remotedir`
f.cwd(remotedir)
if verbose > 1: print 'OK.'
pwd = f.pwd()
if verbose > 1: print 'PWD =', `pwd`
#
mirrorsubdir(f, localdir)
global verbose, interactive, mac, rmok, nologin
try:
opts, args = getopt.getopt(sys.argv[1:], 'a:bil:mnp:qrs:v')
except getopt.error, msg:
usage(msg)
login = ''
passwd = ''
account = ''
for o, a in opts:
if o == '-l': login = a
if o == '-p': passwd = a
if o == '-a': account = a
if o == '-v': verbose = verbose + 1
if o == '-q': verbose = 0
if o == '-i': interactive = 1
if o == '-m': mac = 1; nologin = 1; skippats.append('*.o')
if o == '-n': nologin = 1
if o == '-r': rmok = 1
if o == '-s': skippats.append(a)
if not args: usage('hostname missing')
host = args[0]
remotedir = ''
localdir = ''
if args[1:]:
remotedir = args[1]
if args[2:]:
localdir = args[2]
if args[3:]: usage('too many arguments')
#
f = ftplib.FTP()
if verbose: print 'Connecting to %s...' % `host`
f.connect(host)
if not nologin:
if verbose:
print 'Logging in as %s...' % `login or 'anonymous'`
f.login(login, passwd, account)
if verbose: print 'OK.'
pwd = f.pwd()
if verbose > 1: print 'PWD =', `pwd`
if remotedir:
if verbose > 1: print 'cwd(%s)' % `remotedir`
f.cwd(remotedir)
if verbose > 1: print 'OK.'
pwd = f.pwd()
if verbose > 1: print 'PWD =', `pwd`
#
mirrorsubdir(f, localdir)
# Core logic: mirror one subdirectory (recursively)
def mirrorsubdir(f, localdir):
pwd = f.pwd()
if localdir and not os.path.isdir(localdir):
if verbose: print 'Creating local directory', `localdir`
try:
makedir(localdir)
except os.error, msg:
print "Failed to establish local directory", `localdir`
return
infofilename = os.path.join(localdir, '.mirrorinfo')
try:
text = open(infofilename, 'r').read()
except IOError, msg:
text = '{}'
try:
info = eval(text)
except (SyntaxError, NameError):
print 'Bad mirror info in %s' % `infofilename`
info = {}
subdirs = []
listing = []
if verbose: print 'Listing remote directory %s...' % `pwd`
f.retrlines('LIST', listing.append)
filesfound = []
for line in listing:
if verbose > 1: print '-->', `line`
if mac:
# Mac listing has just filenames;
# trailing / means subdirectory
filename = string.strip(line)
mode = '-'
if filename[-1:] == '/':
filename = filename[:-1]
mode = 'd'
infostuff = ''
else:
# Parse, assuming a UNIX listing
words = string.split(line, None, 8)
if len(words) < 6:
if verbose > 1: print 'Skipping short line'
continue
filename = string.lstrip(words[-1])
i = string.find(filename, " -> ")
if i >= 0:
# words[0] had better start with 'l'...
if verbose > 1:
print 'Found symbolic link %s' % `filename`
linkto = filename[i+4:]
filename = filename[:i]
infostuff = words[-5:-1]
mode = words[0]
skip = 0
for pat in skippats:
if fnmatch(filename, pat):
if verbose > 1:
print 'Skip pattern', `pat`,
print 'matches', `filename`
skip = 1
break
if skip:
continue
if mode[0] == 'd':
if verbose > 1:
print 'Remembering subdirectory', `filename`
subdirs.append(filename)
continue
filesfound.append(filename)
if info.has_key(filename) and info[filename] == infostuff:
if verbose > 1:
print 'Already have this version of',`filename`
continue
fullname = os.path.join(localdir, filename)
tempname = os.path.join(localdir, '@'+filename)
if interactive:
doit = askabout('file', filename, pwd)
if not doit:
if not info.has_key(filename):
info[filename] = 'Not retrieved'
continue
try:
os.unlink(tempname)
except os.error:
pass
if mode[0] == 'l':
if verbose:
print "Creating symlink %s -> %s" % (
`filename`, `linkto`)
try:
os.symlink(linkto, tempname)
except IOError, msg:
print "Can't create %s: %s" % (
`tempname`, str(msg))
continue
else:
try:
fp = open(tempname, 'wb')
except IOError, msg:
print "Can't create %s: %s" % (
`tempname`, str(msg))
continue
if verbose:
print 'Retrieving %s from %s as %s...' % \
(`filename`, `pwd`, `fullname`)
if verbose:
fp1 = LoggingFile(fp, 1024, sys.stdout)
else:
fp1 = fp
t0 = time.time()
try:
f.retrbinary('RETR ' + filename,
fp1.write, 8*1024)
except ftplib.error_perm, msg:
print msg
t1 = time.time()
bytes = fp.tell()
fp.close()
if fp1 != fp:
fp1.close()
try:
os.unlink(fullname)
except os.error:
pass # Ignore the error
try:
os.rename(tempname, fullname)
except os.error, msg:
print "Can't rename %s to %s: %s" % (`tempname`,
`fullname`,
str(msg))
continue
info[filename] = infostuff
writedict(info, infofilename)
if verbose and mode[0] != 'l':
dt = t1 - t0
kbytes = bytes / 1024.0
print int(round(kbytes)),
print 'Kbytes in',
print int(round(dt)),
print 'seconds',
if t1 > t0:
print '(~%d Kbytes/sec)' % \
int(round(kbytes/dt),)
print
#
# Remove files from info that are no longer remote
deletions = 0
for filename in info.keys():
if filename not in filesfound:
if verbose:
print "Removing obsolete info entry for",
print `filename`, "in", `localdir or "."`
del info[filename]
deletions = deletions + 1
if deletions:
writedict(info, infofilename)
#
# Remove local files that are no longer in the remote directory
try:
if not localdir: names = os.listdir(os.curdir)
else: names = os.listdir(localdir)
except os.error:
names = []
for name in names:
if name[0] == '.' or info.has_key(name) or name in subdirs:
continue
skip = 0
for pat in skippats:
if fnmatch(name, pat):
if verbose > 1:
print 'Skip pattern', `pat`,
print 'matches', `name`
skip = 1
break
if skip:
continue
fullname = os.path.join(localdir, name)
if not rmok:
if verbose:
print 'Local file', `fullname`,
print 'is no longer pertinent'
continue
if verbose: print 'Removing local file/dir', `fullname`
remove(fullname)
#
# Recursively mirror subdirectories
for subdir in subdirs:
if interactive:
doit = askabout('subdirectory', subdir, pwd)
if not doit: continue
if verbose: print 'Processing subdirectory', `subdir`
localsubdir = os.path.join(localdir, subdir)
pwd = f.pwd()
if verbose > 1:
print 'Remote directory now:', `pwd`
print 'Remote cwd', `subdir`
try:
f.cwd(subdir)
except ftplib.error_perm, msg:
print "Can't chdir to", `subdir`, ":", `msg`
else:
if verbose: print 'Mirroring as', `localsubdir`
mirrorsubdir(f, localsubdir)
if verbose > 1: print 'Remote cwd ..'
f.cwd('..')
newpwd = f.pwd()
if newpwd != pwd:
print 'Ended up in wrong directory after cd + cd ..'
print 'Giving up now.'
break
else:
if verbose > 1: print 'OK.'
pwd = f.pwd()
if localdir and not os.path.isdir(localdir):
if verbose: print 'Creating local directory', `localdir`
try:
makedir(localdir)
except os.error, msg:
print "Failed to establish local directory", `localdir`
return
infofilename = os.path.join(localdir, '.mirrorinfo')
try:
text = open(infofilename, 'r').read()
except IOError, msg:
text = '{}'
try:
info = eval(text)
except (SyntaxError, NameError):
print 'Bad mirror info in %s' % `infofilename`
info = {}
subdirs = []
listing = []
if verbose: print 'Listing remote directory %s...' % `pwd`
f.retrlines('LIST', listing.append)
filesfound = []
for line in listing:
if verbose > 1: print '-->', `line`
if mac:
# Mac listing has just filenames;
# trailing / means subdirectory
filename = string.strip(line)
mode = '-'
if filename[-1:] == '/':
filename = filename[:-1]
mode = 'd'
infostuff = ''
else:
# Parse, assuming a UNIX listing
words = string.split(line, None, 8)
if len(words) < 6:
if verbose > 1: print 'Skipping short line'
continue
filename = string.lstrip(words[-1])
i = string.find(filename, " -> ")
if i >= 0:
# words[0] had better start with 'l'...
if verbose > 1:
print 'Found symbolic link %s' % `filename`
linkto = filename[i+4:]
filename = filename[:i]
infostuff = words[-5:-1]
mode = words[0]
skip = 0
for pat in skippats:
if fnmatch(filename, pat):
if verbose > 1:
print 'Skip pattern', `pat`,
print 'matches', `filename`
skip = 1
break
if skip:
continue
if mode[0] == 'd':
if verbose > 1:
print 'Remembering subdirectory', `filename`
subdirs.append(filename)
continue
filesfound.append(filename)
if info.has_key(filename) and info[filename] == infostuff:
if verbose > 1:
print 'Already have this version of',`filename`
continue
fullname = os.path.join(localdir, filename)
tempname = os.path.join(localdir, '@'+filename)
if interactive:
doit = askabout('file', filename, pwd)
if not doit:
if not info.has_key(filename):
info[filename] = 'Not retrieved'
continue
try:
os.unlink(tempname)
except os.error:
pass
if mode[0] == 'l':
if verbose:
print "Creating symlink %s -> %s" % (
`filename`, `linkto`)
try:
os.symlink(linkto, tempname)
except IOError, msg:
print "Can't create %s: %s" % (
`tempname`, str(msg))
continue
else:
try:
fp = open(tempname, 'wb')
except IOError, msg:
print "Can't create %s: %s" % (
`tempname`, str(msg))
continue
if verbose:
print 'Retrieving %s from %s as %s...' % \
(`filename`, `pwd`, `fullname`)
if verbose:
fp1 = LoggingFile(fp, 1024, sys.stdout)
else:
fp1 = fp
t0 = time.time()
try:
f.retrbinary('RETR ' + filename,
fp1.write, 8*1024)
except ftplib.error_perm, msg:
print msg
t1 = time.time()
bytes = fp.tell()
fp.close()
if fp1 != fp:
fp1.close()
try:
os.unlink(fullname)
except os.error:
pass # Ignore the error
try:
os.rename(tempname, fullname)
except os.error, msg:
print "Can't rename %s to %s: %s" % (`tempname`,
`fullname`,
str(msg))
continue
info[filename] = infostuff
writedict(info, infofilename)
if verbose and mode[0] != 'l':
dt = t1 - t0
kbytes = bytes / 1024.0
print int(round(kbytes)),
print 'Kbytes in',
print int(round(dt)),
print 'seconds',
if t1 > t0:
print '(~%d Kbytes/sec)' % \
int(round(kbytes/dt),)
print
#
# Remove files from info that are no longer remote
deletions = 0
for filename in info.keys():
if filename not in filesfound:
if verbose:
print "Removing obsolete info entry for",
print `filename`, "in", `localdir or "."`
del info[filename]
deletions = deletions + 1
if deletions:
writedict(info, infofilename)
#
# Remove local files that are no longer in the remote directory
try:
if not localdir: names = os.listdir(os.curdir)
else: names = os.listdir(localdir)
except os.error:
names = []
for name in names:
if name[0] == '.' or info.has_key(name) or name in subdirs:
continue
skip = 0
for pat in skippats:
if fnmatch(name, pat):
if verbose > 1:
print 'Skip pattern', `pat`,
print 'matches', `name`
skip = 1
break
if skip:
continue
fullname = os.path.join(localdir, name)
if not rmok:
if verbose:
print 'Local file', `fullname`,
print 'is no longer pertinent'
continue
if verbose: print 'Removing local file/dir', `fullname`
remove(fullname)
#
# Recursively mirror subdirectories
for subdir in subdirs:
if interactive:
doit = askabout('subdirectory', subdir, pwd)
if not doit: continue
if verbose: print 'Processing subdirectory', `subdir`
localsubdir = os.path.join(localdir, subdir)
pwd = f.pwd()
if verbose > 1:
print 'Remote directory now:', `pwd`
print 'Remote cwd', `subdir`
try:
f.cwd(subdir)
except ftplib.error_perm, msg:
print "Can't chdir to", `subdir`, ":", `msg`
else:
if verbose: print 'Mirroring as', `localsubdir`
mirrorsubdir(f, localsubdir)
if verbose > 1: print 'Remote cwd ..'
f.cwd('..')
newpwd = f.pwd()
if newpwd != pwd:
print 'Ended up in wrong directory after cd + cd ..'
print 'Giving up now.'
break
else:
if verbose > 1: print 'OK.'
# Helper to remove a file or directory tree
def remove(fullname):
if os.path.isdir(fullname) and not os.path.islink(fullname):
try:
names = os.listdir(fullname)
except os.error:
names = []
ok = 1
for name in names:
if not remove(os.path.join(fullname, name)):
ok = 0
if not ok:
return 0
try:
os.rmdir(fullname)
except os.error, msg:
print "Can't remove local directory %s: %s" % \
(`fullname`, str(msg))
return 0
else:
try:
os.unlink(fullname)
except os.error, msg:
print "Can't remove local file %s: %s" % \
(`fullname`, str(msg))
return 0
return 1
if os.path.isdir(fullname) and not os.path.islink(fullname):
try:
names = os.listdir(fullname)
except os.error:
names = []
ok = 1
for name in names:
if not remove(os.path.join(fullname, name)):
ok = 0
if not ok:
return 0
try:
os.rmdir(fullname)
except os.error, msg:
print "Can't remove local directory %s: %s" % \
(`fullname`, str(msg))
return 0
else:
try:
os.unlink(fullname)
except os.error, msg:
print "Can't remove local file %s: %s" % \
(`fullname`, str(msg))
return 0
return 1
# Wrapper around a file for writing to write a hash sign every block.
class LoggingFile:
def __init__(self, fp, blocksize, outfp):
self.fp = fp
self.bytes = 0
self.hashes = 0
self.blocksize = blocksize
self.outfp = outfp
def write(self, data):
self.bytes = self.bytes + len(data)
hashes = int(self.bytes) / self.blocksize
while hashes > self.hashes:
self.outfp.write('#')
self.outfp.flush()
self.hashes = self.hashes + 1
self.fp.write(data)
def close(self):
self.outfp.write('\n')
def __init__(self, fp, blocksize, outfp):
self.fp = fp
self.bytes = 0
self.hashes = 0
self.blocksize = blocksize
self.outfp = outfp
def write(self, data):
self.bytes = self.bytes + len(data)
hashes = int(self.bytes) / self.blocksize
while hashes > self.hashes:
self.outfp.write('#')
self.outfp.flush()
self.hashes = self.hashes + 1
self.fp.write(data)
def close(self):
self.outfp.write('\n')
# Ask permission to download a file.
def askabout(filetype, filename, pwd):
prompt = 'Retrieve %s %s from %s ? [ny] ' % (filetype, filename, pwd)
while 1:
reply = string.lower(string.strip(raw_input(prompt)))
if reply in ['y', 'ye', 'yes']:
return 1
if reply in ['', 'n', 'no', 'nop', 'nope']:
return 0
print 'Please answer yes or no.'
prompt = 'Retrieve %s %s from %s ? [ny] ' % (filetype, filename, pwd)
while 1:
reply = string.lower(string.strip(raw_input(prompt)))
if reply in ['y', 'ye', 'yes']:
return 1
if reply in ['', 'n', 'no', 'nop', 'nope']:
return 0
print 'Please answer yes or no.'
# Create a directory if it doesn't exist. Recursively create the
# parent directory as well if needed.
def makedir(pathname):
if os.path.isdir(pathname):
return
dirname = os.path.dirname(pathname)
if dirname: makedir(dirname)
os.mkdir(pathname, 0777)
if os.path.isdir(pathname):
return
dirname = os.path.dirname(pathname)
if dirname: makedir(dirname)
os.mkdir(pathname, 0777)
# Write a dictionary to a file in a way that can be read back using
# rval() but is still somewhat readable (i.e. not a single long line).
# Also creates a backup file.
def writedict(dict, filename):
dir, file = os.path.split(filename)
tempname = os.path.join(dir, '@' + file)
backup = os.path.join(dir, file + '~')
try:
os.unlink(backup)
except os.error:
pass
fp = open(tempname, 'w')
fp.write('{\n')
for key, value in dict.items():
fp.write('%s: %s,\n' % (`key`, `value`))
fp.write('}\n')
fp.close()
try:
os.rename(filename, backup)
except os.error:
pass
os.rename(tempname, filename)
dir, file = os.path.split(filename)
tempname = os.path.join(dir, '@' + file)
backup = os.path.join(dir, file + '~')
try:
os.unlink(backup)
except os.error:
pass
fp = open(tempname, 'w')
fp.write('{\n')
for key, value in dict.items():
fp.write('%s: %s,\n' % (`key`, `value`))
fp.write('}\n')
fp.close()
try:
os.rename(filename, backup)
except os.error:
pass
os.rename(tempname, filename)
if __name__ == '__main__':
main()
main()

View file

@ -180,14 +180,14 @@ class Codec(codecs.Codec):
def encode(self,input,errors='strict'):
return codecs.charmap_encode(input,errors,encoding_map)
def decode(self,input,errors='strict'):
return codecs.charmap_decode(input,errors,decoding_map)
class StreamWriter(Codec,codecs.StreamWriter):
pass
class StreamReader(Codec,codecs.StreamReader):
pass
@ -210,7 +210,7 @@ def getregentry():
else:
l.append("decoding_map = {")
splits = 0
mappings = map.items()
mappings.sort()
append = l.append
@ -290,7 +290,7 @@ def convertdir(dir,prefix='',comments=1):
print '* conversion failed'
def rewritepythondir(dir,prefix='',comments=1):
mapnames = os.listdir(dir)
for mapname in mapnames:
if not mapname.endswith('.mapping'):

View file

@ -41,110 +41,110 @@ p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
filedict = {}
try:
searchdirs=string.splitfields(os.environ['include'],';')
searchdirs=string.splitfields(os.environ['include'],';')
except KeyError:
try:
searchdirs=string.splitfields(os.environ['INCLUDE'],';')
except KeyError:
try:
if string.find( sys.platform, "beos" ) == 0:
searchdirs=string.splitfields(os.environ['BEINCLUDES'],';')
else:
raise KeyError
except KeyError:
searchdirs=['/usr/include']
try:
searchdirs=string.splitfields(os.environ['INCLUDE'],';')
except KeyError:
try:
if string.find( sys.platform, "beos" ) == 0:
searchdirs=string.splitfields(os.environ['BEINCLUDES'],';')
else:
raise KeyError
except KeyError:
searchdirs=['/usr/include']
def main():
global filedict
opts, args = getopt.getopt(sys.argv[1:], 'i:')
for o, a in opts:
if o == '-i':
ignores.append(regex.compile(a))
if not args:
args = ['-']
for filename in args:
if filename == '-':
sys.stdout.write('# Generated by h2py from stdin\n')
process(sys.stdin, sys.stdout)
else:
fp = open(filename, 'r')
outfile = os.path.basename(filename)
i = string.rfind(outfile, '.')
if i > 0: outfile = outfile[:i]
outfile = string.upper(outfile)
outfile = outfile + '.py'
outfp = open(outfile, 'w')
outfp.write('# Generated by h2py from %s\n' % filename)
filedict = {}
for dir in searchdirs:
if filename[:len(dir)] == dir:
filedict[filename[len(dir)+1:]] = None # no '/' trailing
break
process(fp, outfp)
outfp.close()
fp.close()
global filedict
opts, args = getopt.getopt(sys.argv[1:], 'i:')
for o, a in opts:
if o == '-i':
ignores.append(regex.compile(a))
if not args:
args = ['-']
for filename in args:
if filename == '-':
sys.stdout.write('# Generated by h2py from stdin\n')
process(sys.stdin, sys.stdout)
else:
fp = open(filename, 'r')
outfile = os.path.basename(filename)
i = string.rfind(outfile, '.')
if i > 0: outfile = outfile[:i]
outfile = string.upper(outfile)
outfile = outfile + '.py'
outfp = open(outfile, 'w')
outfp.write('# Generated by h2py from %s\n' % filename)
filedict = {}
for dir in searchdirs:
if filename[:len(dir)] == dir:
filedict[filename[len(dir)+1:]] = None # no '/' trailing
break
process(fp, outfp)
outfp.close()
fp.close()
def process(fp, outfp, env = {}):
lineno = 0
while 1:
line = fp.readline()
if not line: break
lineno = lineno + 1
n = p_define.match(line)
if n >= 0:
# gobble up continuation lines
while line[-2:] == '\\\n':
nextline = fp.readline()
if not nextline: break
lineno = lineno + 1
line = line + nextline
name = p_define.group(1)
body = line[n:]
# replace ignored patterns by spaces
for p in ignores:
body = regsub.gsub(p, ' ', body)
# replace char literals by ord(...)
body = regsub.gsub(p_char, 'ord(\\0)', body)
stmt = '%s = %s\n' % (name, string.strip(body))
ok = 0
try:
exec stmt in env
except:
sys.stderr.write('Skipping: %s' % stmt)
else:
outfp.write(stmt)
n =p_macro.match(line)
if n >= 0:
macro, arg = p_macro.group(1, 2)
body = line[n:]
for p in ignores:
body = regsub.gsub(p, ' ', body)
body = regsub.gsub(p_char, 'ord(\\0)', body)
stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
try:
exec stmt in env
except:
sys.stderr.write('Skipping: %s' % stmt)
else:
outfp.write(stmt)
if p_include.match(line) >= 0:
regs = p_include.regs
a, b = regs[1]
filename = line[a:b]
if not filedict.has_key(filename):
filedict[filename] = None
inclfp = None
for dir in searchdirs:
try:
inclfp = open(dir + '/' + filename, 'r')
break
except IOError:
pass
if inclfp:
outfp.write(
'\n# Included from %s\n' % filename)
process(inclfp, outfp, env)
else:
sys.stderr.write('Warning - could not find file %s' % filename)
lineno = 0
while 1:
line = fp.readline()
if not line: break
lineno = lineno + 1
n = p_define.match(line)
if n >= 0:
# gobble up continuation lines
while line[-2:] == '\\\n':
nextline = fp.readline()
if not nextline: break
lineno = lineno + 1
line = line + nextline
name = p_define.group(1)
body = line[n:]
# replace ignored patterns by spaces
for p in ignores:
body = regsub.gsub(p, ' ', body)
# replace char literals by ord(...)
body = regsub.gsub(p_char, 'ord(\\0)', body)
stmt = '%s = %s\n' % (name, string.strip(body))
ok = 0
try:
exec stmt in env
except:
sys.stderr.write('Skipping: %s' % stmt)
else:
outfp.write(stmt)
n =p_macro.match(line)
if n >= 0:
macro, arg = p_macro.group(1, 2)
body = line[n:]
for p in ignores:
body = regsub.gsub(p, ' ', body)
body = regsub.gsub(p_char, 'ord(\\0)', body)
stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
try:
exec stmt in env
except:
sys.stderr.write('Skipping: %s' % stmt)
else:
outfp.write(stmt)
if p_include.match(line) >= 0:
regs = p_include.regs
a, b = regs[1]
filename = line[a:b]
if not filedict.has_key(filename):
filedict[filename] = None
inclfp = None
for dir in searchdirs:
try:
inclfp = open(dir + '/' + filename, 'r')
break
except IOError:
pass
if inclfp:
outfp.write(
'\n# Included from %s\n' % filename)
process(inclfp, outfp, env)
else:
sys.stderr.write('Warning - could not find file %s' % filename)
main()

View file

@ -3,7 +3,7 @@
# Selectively preprocess #ifdef / #ifndef statements.
# Usage:
# ifdef [-Dname] ... [-Uname] ... [file] ...
#
#
# This scans the file(s), looking for #ifdef and #ifndef preprocessor
# commands that test for one of the names mentioned in the -D and -U
# options. On standard output it writes a copy of the input file(s)
@ -35,79 +35,79 @@ defs = []
undefs = []
def main():
opts, args = getopt.getopt(sys.argv[1:], 'D:U:')
for o, a in opts:
if o == '-D':
defs.append(a)
if o == '-U':
undefs.append(a)
if not args:
args = ['-']
for file in args:
if file == '-':
process(sys.stdin, sys.stdout)
else:
f = open(file, 'r')
process(f, sys.stdout)
f.close()
opts, args = getopt.getopt(sys.argv[1:], 'D:U:')
for o, a in opts:
if o == '-D':
defs.append(a)
if o == '-U':
undefs.append(a)
if not args:
args = ['-']
for file in args:
if file == '-':
process(sys.stdin, sys.stdout)
else:
f = open(file, 'r')
process(f, sys.stdout)
f.close()
def process(fpi, fpo):
keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif')
ok = 1
stack = []
while 1:
line = fpi.readline()
if not line: break
while line[-2:] == '\\\n':
nextline = fpi.readline()
if not nextline: break
line = line + nextline
tmp = string.strip(line)
if tmp[:1] != '#':
if ok: fpo.write(line)
continue
tmp = string.strip(tmp[1:])
words = string.split(tmp)
keyword = words[0]
if keyword not in keywords:
if ok: fpo.write(line)
continue
if keyword in ('ifdef', 'ifndef') and len(words) == 2:
if keyword == 'ifdef':
ko = 1
else:
ko = 0
word = words[1]
if word in defs:
stack.append((ok, ko, word))
if not ko: ok = 0
elif word in undefs:
stack.append((ok, not ko, word))
if ko: ok = 0
else:
stack.append((ok, -1, word))
if ok: fpo.write(line)
elif keyword == 'if':
stack.append((ok, -1, ''))
if ok: fpo.write(line)
elif keyword == 'else' and stack:
s_ok, s_ko, s_word = stack[-1]
if s_ko < 0:
if ok: fpo.write(line)
else:
s_ko = not s_ko
ok = s_ok
if not s_ko: ok = 0
stack[-1] = s_ok, s_ko, s_word
elif keyword == 'endif' and stack:
s_ok, s_ko, s_word = stack[-1]
if s_ko < 0:
if ok: fpo.write(line)
del stack[-1]
ok = s_ok
else:
sys.stderr.write('Unknown keyword %s\n' % keyword)
if stack:
sys.stderr.write('stack: %s\n' % stack)
keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif')
ok = 1
stack = []
while 1:
line = fpi.readline()
if not line: break
while line[-2:] == '\\\n':
nextline = fpi.readline()
if not nextline: break
line = line + nextline
tmp = string.strip(line)
if tmp[:1] != '#':
if ok: fpo.write(line)
continue
tmp = string.strip(tmp[1:])
words = string.split(tmp)
keyword = words[0]
if keyword not in keywords:
if ok: fpo.write(line)
continue
if keyword in ('ifdef', 'ifndef') and len(words) == 2:
if keyword == 'ifdef':
ko = 1
else:
ko = 0
word = words[1]
if word in defs:
stack.append((ok, ko, word))
if not ko: ok = 0
elif word in undefs:
stack.append((ok, not ko, word))
if ko: ok = 0
else:
stack.append((ok, -1, word))
if ok: fpo.write(line)
elif keyword == 'if':
stack.append((ok, -1, ''))
if ok: fpo.write(line)
elif keyword == 'else' and stack:
s_ok, s_ko, s_word = stack[-1]
if s_ko < 0:
if ok: fpo.write(line)
else:
s_ko = not s_ko
ok = s_ok
if not s_ko: ok = 0
stack[-1] = s_ok, s_ko, s_word
elif keyword == 'endif' and stack:
s_ok, s_ko, s_word = stack[-1]
if s_ko < 0:
if ok: fpo.write(line)
del stack[-1]
ok = s_ok
else:
sys.stderr.write('Unknown keyword %s\n' % keyword)
if stack:
sys.stderr.write('stack: %s\n' % stack)
main()

View file

@ -17,63 +17,63 @@ LINK = '.LINK' # Name of special symlink at the top.
debug = 0
def main():
if not 3 <= len(sys.argv) <= 4:
print 'usage:', sys.argv[0], 'oldtree newtree [linkto]'
return 2
oldtree, newtree = sys.argv[1], sys.argv[2]
if len(sys.argv) > 3:
link = sys.argv[3]
link_may_fail = 1
else:
link = LINK
link_may_fail = 0
if not os.path.isdir(oldtree):
print oldtree + ': not a directory'
return 1
try:
os.mkdir(newtree, 0777)
except os.error, msg:
print newtree + ': cannot mkdir:', msg
return 1
linkname = os.path.join(newtree, link)
try:
os.symlink(os.path.join(os.pardir, oldtree), linkname)
except os.error, msg:
if not link_may_fail:
print linkname + ': cannot symlink:', msg
return 1
else:
print linkname + ': warning: cannot symlink:', msg
linknames(oldtree, newtree, link)
return 0
if not 3 <= len(sys.argv) <= 4:
print 'usage:', sys.argv[0], 'oldtree newtree [linkto]'
return 2
oldtree, newtree = sys.argv[1], sys.argv[2]
if len(sys.argv) > 3:
link = sys.argv[3]
link_may_fail = 1
else:
link = LINK
link_may_fail = 0
if not os.path.isdir(oldtree):
print oldtree + ': not a directory'
return 1
try:
os.mkdir(newtree, 0777)
except os.error, msg:
print newtree + ': cannot mkdir:', msg
return 1
linkname = os.path.join(newtree, link)
try:
os.symlink(os.path.join(os.pardir, oldtree), linkname)
except os.error, msg:
if not link_may_fail:
print linkname + ': cannot symlink:', msg
return 1
else:
print linkname + ': warning: cannot symlink:', msg
linknames(oldtree, newtree, link)
return 0
def linknames(old, new, link):
if debug: print 'linknames', (old, new, link)
try:
names = os.listdir(old)
except os.error, msg:
print old + ': warning: cannot listdir:', msg
return
for name in names:
if name not in (os.curdir, os.pardir):
oldname = os.path.join(old, name)
linkname = os.path.join(link, name)
newname = os.path.join(new, name)
if debug > 1: print oldname, newname, linkname
if os.path.isdir(oldname) and \
not os.path.islink(oldname):
try:
os.mkdir(newname, 0777)
ok = 1
except:
print newname + \
': warning: cannot mkdir:', msg
ok = 0
if ok:
linkname = os.path.join(os.pardir,
linkname)
linknames(oldname, newname, linkname)
else:
os.symlink(linkname, newname)
if debug: print 'linknames', (old, new, link)
try:
names = os.listdir(old)
except os.error, msg:
print old + ': warning: cannot listdir:', msg
return
for name in names:
if name not in (os.curdir, os.pardir):
oldname = os.path.join(old, name)
linkname = os.path.join(link, name)
newname = os.path.join(new, name)
if debug > 1: print oldname, newname, linkname
if os.path.isdir(oldname) and \
not os.path.islink(oldname):
try:
os.mkdir(newname, 0777)
ok = 1
except:
print newname + \
': warning: cannot mkdir:', msg
ok = 0
if ok:
linkname = os.path.join(os.pardir,
linkname)
linknames(oldname, newname, linkname)
else:
os.symlink(linkname, newname)
sys.exit(main())

View file

@ -8,18 +8,18 @@
import sys, os
def lll(dirname):
for name in os.listdir(dirname):
if name not in (os.curdir, os.pardir):
full = os.path.join(dirname, name)
if os.path.islink(full):
print name, '->', os.readlink(full)
for name in os.listdir(dirname):
if name not in (os.curdir, os.pardir):
full = os.path.join(dirname, name)
if os.path.islink(full):
print name, '->', os.readlink(full)
args = sys.argv[1:]
if not args: args = [os.curdir]
first = 1
for arg in args:
if len(args) > 1:
if not first: print
first = 0
print arg + ':'
lll(arg)
if len(args) > 1:
if not first: print
first = 0
print arg + ':'
lll(arg)

View file

@ -116,7 +116,7 @@ def digest_chunk(chunk):
text.insert(0, revline)
records.append((date, working_file, rev, author, text))
return records
def format_output(database):
prevtext = None
prev = []

View file

@ -166,7 +166,7 @@ def parsedir(dir, modify):
# find all numeric file names and sort them
files = filter(lambda fn, pat=pat: pat.match(fn) is not None, os.listdir('.'))
files.sort(sort_numeric)
for fn in files:
# Lets try to parse the file.
fp = open(fn)

View file

@ -1,9 +1,9 @@
#! /usr/bin/env python
# Fix Python source files to avoid using
# def method(self, (arg1, ..., argn)):
# Fix Python source files to avoid using
# def method(self, (arg1, ..., argn)):
# instead of the more rational
# def method(self, arg1, ..., argn):
# def method(self, arg1, ..., argn):
#
# Command line arguments are files or directories to be processed.
# Directories are searched recursively for files whose name looks
@ -37,137 +37,137 @@ 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(' + `dirname` + ')\n')
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n')
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(' + `dirname` + ')\n')
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n')
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(' + `filename` + ')\n')
try:
f = open(filename, 'r')
except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n')
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(tempname+': cannot create: '+\
`msg`+'\n')
return 1
f.seek(0)
lineno = 0
rep(filename + ':\n')
continue # restart from the beginning
rep(`lineno` + '\n')
rep('< ' + line)
rep('> ' + newline)
if g is not None:
g.write(newline)
## dbg('fix(' + `filename` + ')\n')
try:
f = open(filename, 'r')
except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n')
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(tempname+': cannot create: '+\
`msg`+'\n')
return 1
f.seek(0)
lineno = 0
rep(filename + ':\n')
continue # restart from the beginning
rep(`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(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n')
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(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n')
return 1
# Return succes
return 0
fixpat = '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *\(( *\(.*\) *)\) *) *:'
fixprog = regex.compile(fixpat)
def fixline(line):
if fixprog.match(line) >= 0:
(a, b), (c, d) = fixprog.regs[1:3]
line = line[:a] + line[c:d] + line[b:]
return line
if fixprog.match(line) >= 0:
(a, b), (c, d) = fixprog.regs[1:3]
line = line[:a] + line[c:d] + line[b:]
return line
main()

View file

@ -15,51 +15,51 @@ error = 'mkreal error'
BUFSIZE = 32*1024
def mkrealfile(name):
st = os.stat(name) # Get the mode
mode = S_IMODE(st[ST_MODE])
linkto = os.readlink(name) # Make sure again it's a symlink
f_in = open(name, 'r') # This ensures it's a file
os.unlink(name)
f_out = open(name, 'w')
while 1:
buf = f_in.read(BUFSIZE)
if not buf: break
f_out.write(buf)
del f_out # Flush data to disk before changing mode
os.chmod(name, mode)
st = os.stat(name) # Get the mode
mode = S_IMODE(st[ST_MODE])
linkto = os.readlink(name) # Make sure again it's a symlink
f_in = open(name, 'r') # This ensures it's a file
os.unlink(name)
f_out = open(name, 'w')
while 1:
buf = f_in.read(BUFSIZE)
if not buf: break
f_out.write(buf)
del f_out # Flush data to disk before changing mode
os.chmod(name, mode)
def mkrealdir(name):
st = os.stat(name) # Get the mode
mode = S_IMODE(st[ST_MODE])
linkto = os.readlink(name)
files = os.listdir(name)
os.unlink(name)
os.mkdir(name, mode)
os.chmod(name, mode)
linkto = join(os.pardir, linkto)
#
for file in files:
if file not in (os.curdir, os.pardir):
os.symlink(join(linkto, file), join(name, file))
st = os.stat(name) # Get the mode
mode = S_IMODE(st[ST_MODE])
linkto = os.readlink(name)
files = os.listdir(name)
os.unlink(name)
os.mkdir(name, mode)
os.chmod(name, mode)
linkto = join(os.pardir, linkto)
#
for file in files:
if file not in (os.curdir, os.pardir):
os.symlink(join(linkto, file), join(name, file))
def main():
sys.stdout = sys.stderr
progname = os.path.basename(sys.argv[0])
if progname == '-c': progname = 'mkreal'
args = sys.argv[1:]
if not args:
print 'usage:', progname, 'path ...'
sys.exit(2)
status = 0
for name in args:
if not os.path.islink(name):
print progname+':', name+':', 'not a symlink'
status = 1
else:
if os.path.isdir(name):
mkrealdir(name)
else:
mkrealfile(name)
sys.exit(status)
sys.stdout = sys.stderr
progname = os.path.basename(sys.argv[0])
if progname == '-c': progname = 'mkreal'
args = sys.argv[1:]
if not args:
print 'usage:', progname, 'path ...'
sys.exit(2)
status = 0
for name in args:
if not os.path.islink(name):
print progname+':', name+':', 'not a symlink'
status = 1
else:
if os.path.isdir(name):
mkrealdir(name)
else:
mkrealfile(name)
sys.exit(status)
main()

View file

@ -98,6 +98,6 @@ def main():
f = sys.stdout # open('PC/python_nt.def','w')
f.write(DEF_TEMPLATE % (exports))
f.close()
if __name__ == '__main__':
main()

View file

@ -40,19 +40,19 @@ matcher = regex.compile('\(.*\):\t?........ \(.\) \(.*\)$')
# If there is no list for the key yet, it is created.
#
def store(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]
# Return a flattened version of a list of strings: the concatenation
# of its elements with intervening spaces.
#
def flat(list):
s = ''
for item in list:
s = s + ' ' + item
return s[1:]
s = ''
for item in list:
s = s + ' ' + item
return s[1:]
# Global variables mapping defined/undefined names to files and back.
#
@ -65,151 +65,151 @@ undef2file = {}
# Argument is an open file.
#
def readinput(file):
while 1:
s = file.readline()
if not s:
break
# If you get any output from this line,
# it is probably caused by an unexpected input line:
if matcher.search(s) < 0: s; continue # Shouldn't happen
(ra, rb), (r1a, r1b), (r2a, r2b), (r3a, r3b) = matcher.regs[:4]
fn, name, type = s[r1a:r1b], s[r3a:r3b], s[r2a:r2b]
if type in definitions:
store(def2file, name, fn)
store(file2def, fn, name)
elif type in externals:
store(file2undef, fn, name)
store(undef2file, name, fn)
elif not type in ignore:
print fn + ':' + name + ': unknown type ' + type
while 1:
s = file.readline()
if not s:
break
# If you get any output from this line,
# it is probably caused by an unexpected input line:
if matcher.search(s) < 0: s; continue # Shouldn't happen
(ra, rb), (r1a, r1b), (r2a, r2b), (r3a, r3b) = matcher.regs[:4]
fn, name, type = s[r1a:r1b], s[r3a:r3b], s[r2a:r2b]
if type in definitions:
store(def2file, name, fn)
store(file2def, fn, name)
elif type in externals:
store(file2undef, fn, name)
store(undef2file, name, fn)
elif not type in ignore:
print fn + ':' + name + ': unknown type ' + type
# Print all names that were undefined in some module and where they are
# defined.
#
def printcallee():
flist = file2undef.keys()
flist.sort()
for file in flist:
print file + ':'
elist = file2undef[file]
elist.sort()
for ext in elist:
if len(ext) >= 8:
tabs = '\t'
else:
tabs = '\t\t'
if not def2file.has_key(ext):
print '\t' + ext + tabs + ' *undefined'
else:
print '\t' + ext + tabs + flat(def2file[ext])
flist = file2undef.keys()
flist.sort()
for file in flist:
print file + ':'
elist = file2undef[file]
elist.sort()
for ext in elist:
if len(ext) >= 8:
tabs = '\t'
else:
tabs = '\t\t'
if not def2file.has_key(ext):
print '\t' + ext + tabs + ' *undefined'
else:
print '\t' + ext + tabs + flat(def2file[ext])
# Print for each module the names of the other modules that use it.
#
def printcaller():
files = file2def.keys()
files.sort()
for file in files:
callers = []
for label in file2def[file]:
if undef2file.has_key(label):
callers = callers + undef2file[label]
if callers:
callers.sort()
print file + ':'
lastfn = ''
for fn in callers:
if fn <> lastfn:
print '\t' + fn
lastfn = fn
else:
print file + ': unused'
files = file2def.keys()
files.sort()
for file in files:
callers = []
for label in file2def[file]:
if undef2file.has_key(label):
callers = callers + undef2file[label]
if callers:
callers.sort()
print file + ':'
lastfn = ''
for fn in callers:
if fn <> lastfn:
print '\t' + fn
lastfn = fn
else:
print file + ': unused'
# Print undefine names and where they are used.
#
def printundef():
undefs = {}
for file in file2undef.keys():
for ext in file2undef[file]:
if not def2file.has_key(ext):
store(undefs, ext, file)
elist = undefs.keys()
elist.sort()
for ext in elist:
print ext + ':'
flist = undefs[ext]
flist.sort()
for file in flist:
print '\t' + file
undefs = {}
for file in file2undef.keys():
for ext in file2undef[file]:
if not def2file.has_key(ext):
store(undefs, ext, file)
elist = undefs.keys()
elist.sort()
for ext in elist:
print ext + ':'
flist = undefs[ext]
flist.sort()
for file in flist:
print '\t' + file
# Print warning messages about names defined in more than one file.
#
def warndups():
savestdout = sys.stdout
sys.stdout = sys.stderr
names = def2file.keys()
names.sort()
for name in names:
if len(def2file[name]) > 1:
print 'warning:', name, 'multiply defined:',
print flat(def2file[name])
sys.stdout = savestdout
savestdout = sys.stdout
sys.stdout = sys.stderr
names = def2file.keys()
names.sort()
for name in names:
if len(def2file[name]) > 1:
print 'warning:', name, 'multiply defined:',
print flat(def2file[name])
sys.stdout = savestdout
# Main program
#
def main():
try:
optlist, args = getopt.getopt(sys.argv[1:], 'cdu')
except getopt.error:
sys.stdout = sys.stderr
print 'Usage:', os.path.basename(sys.argv[0]),
print '[-cdu] [file] ...'
print '-c: print callers per objectfile'
print '-d: print callees per objectfile'
print '-u: print usage of undefined symbols'
print 'If none of -cdu is specified, all are assumed.'
print 'Use "nm -o" to generate the input (on IRIX: "nm -Bo"),'
print 'e.g.: nm -o /lib/libc.a | objgraph'
return 1
optu = optc = optd = 0
for opt, void in optlist:
if opt == '-u':
optu = 1
elif opt == '-c':
optc = 1
elif opt == '-d':
optd = 1
if optu == optc == optd == 0:
optu = optc = optd = 1
if not args:
args = ['-']
for file in args:
if file == '-':
readinput(sys.stdin)
else:
readinput(open(file, 'r'))
#
warndups()
#
more = (optu + optc + optd > 1)
if optd:
if more:
print '---------------All callees------------------'
printcallee()
if optu:
if more:
print '---------------Undefined callees------------'
printundef()
if optc:
if more:
print '---------------All Callers------------------'
printcaller()
return 0
try:
optlist, args = getopt.getopt(sys.argv[1:], 'cdu')
except getopt.error:
sys.stdout = sys.stderr
print 'Usage:', os.path.basename(sys.argv[0]),
print '[-cdu] [file] ...'
print '-c: print callers per objectfile'
print '-d: print callees per objectfile'
print '-u: print usage of undefined symbols'
print 'If none of -cdu is specified, all are assumed.'
print 'Use "nm -o" to generate the input (on IRIX: "nm -Bo"),'
print 'e.g.: nm -o /lib/libc.a | objgraph'
return 1
optu = optc = optd = 0
for opt, void in optlist:
if opt == '-u':
optu = 1
elif opt == '-c':
optc = 1
elif opt == '-d':
optd = 1
if optu == optc == optd == 0:
optu = optc = optd = 1
if not args:
args = ['-']
for file in args:
if file == '-':
readinput(sys.stdin)
else:
readinput(open(file, 'r'))
#
warndups()
#
more = (optu + optc + optd > 1)
if optd:
if more:
print '---------------All callees------------------'
printcallee()
if optu:
if more:
print '---------------Undefined callees------------'
printundef()
if optc:
if more:
print '---------------All Callers------------------'
printcaller()
return 0
# Call the main program.
# Use its return value as exit status.
# Catch interrupts to avoid stack trace.
#
try:
sys.exit(main())
sys.exit(main())
except KeyboardInterrupt:
sys.exit(1)
sys.exit(1)

View file

@ -8,7 +8,7 @@
Python snippet defining a dictionary "entitydefs" mapping literal
entity name to character or numeric entity.
Marc-Andre Lemburg, mal@lemburg.com, 1999.
Marc-Andre Lemburg, mal@lemburg.com, 1999.
Use as you like. NO WARRANTIES.
"""
@ -62,4 +62,3 @@ if __name__ == '__main__':
text = infile.read()
defs = parse(text)
writefile(outfile,defs)

View file

@ -33,117 +33,117 @@ rep = sys.stdout.write
new_interpreter = None
def main():
global new_interpreter
usage = ('usage: %s -i /interpreter file-or-directory ...\n' %
sys.argv[0])
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:')
except getopt.error, msg:
err(msg + '\n')
err(usage)
sys.exit(2)
for o, a in opts:
if o == '-i':
new_interpreter = a
if not new_interpreter or new_interpreter[0] != '/' or not args:
err('-i option or file-or-directory missing\n')
err(usage)
sys.exit(2)
bad = 0
for arg in args:
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)
global new_interpreter
usage = ('usage: %s -i /interpreter file-or-directory ...\n' %
sys.argv[0])
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:')
except getopt.error, msg:
err(msg + '\n')
err(usage)
sys.exit(2)
for o, a in opts:
if o == '-i':
new_interpreter = a
if not new_interpreter or new_interpreter[0] != '/' or not args:
err('-i option or file-or-directory missing\n')
err(usage)
sys.exit(2)
bad = 0
for arg in args:
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(' + `dirname` + ')\n')
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n')
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(' + `dirname` + ')\n')
bad = 0
try:
names = os.listdir(dirname)
except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n')
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(' + `filename` + ')\n')
try:
f = open(filename, 'r')
except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n')
return 1
line = f.readline()
fixed = fixline(line)
if line == fixed:
rep(filename+': no change\n')
f.close()
return
head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail)
try:
g = open(tempname, 'w')
except IOError, msg:
f.close()
err(tempname+': cannot create: '+`msg`+'\n')
return 1
rep(filename + ': updating\n')
g.write(fixed)
BUFSIZE = 8*1024
while 1:
buf = f.read(BUFSIZE)
if not buf: break
g.write(buf)
g.close()
f.close()
## dbg('fix(' + `filename` + ')\n')
try:
f = open(filename, 'r')
except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n')
return 1
line = f.readline()
fixed = fixline(line)
if line == fixed:
rep(filename+': no change\n')
f.close()
return
head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail)
try:
g = open(tempname, 'w')
except IOError, msg:
f.close()
err(tempname+': cannot create: '+`msg`+'\n')
return 1
rep(filename + ': updating\n')
g.write(fixed)
BUFSIZE = 8*1024
while 1:
buf = f.read(BUFSIZE)
if not buf: break
g.write(buf)
g.close()
f.close()
# Finishing touch -- move files
# 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(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n')
return 1
# Return succes
return 0
# 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(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~
try:
os.rename(filename, filename + '~')
except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file
try:
os.rename(tempname, filename)
except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n')
return 1
# Return succes
return 0
def fixline(line):
if line[:2] != '#!':
return line
if string.find(line, "python") < 0:
return line
return '#! %s\n' % new_interpreter
if line[:2] != '#!':
return line
if string.find(line, "python") < 0:
return line
return '#! %s\n' % new_interpreter
main()

View file

@ -5,7 +5,7 @@
# Find dependencies between a bunch of Python modules.
#
# Usage:
# pdeps file1.py file2.py ...
# pdeps file1.py file2.py ...
#
# Output:
# Four tables separated by lines like '--- Closure ---':
@ -29,31 +29,31 @@ import string
# Main program
#
def main():
args = sys.argv[1:]
if not args:
print 'usage: pdeps file.py file.py ...'
return 2
#
table = {}
for arg in args:
process(arg, table)
#
print '--- Uses ---'
printresults(table)
#
print '--- Used By ---'
inv = inverse(table)
printresults(inv)
#
print '--- Closure of Uses ---'
reach = closure(table)
printresults(reach)
#
print '--- Closure of Used By ---'
invreach = inverse(reach)
printresults(invreach)
#
return 0
args = sys.argv[1:]
if not args:
print 'usage: pdeps file.py file.py ...'
return 2
#
table = {}
for arg in args:
process(arg, table)
#
print '--- Uses ---'
printresults(table)
#
print '--- Used By ---'
inv = inverse(table)
printresults(inv)
#
print '--- Closure of Uses ---'
reach = closure(table)
printresults(reach)
#
print '--- Closure of Used By ---'
invreach = inverse(reach)
printresults(invreach)
#
return 0
# Compiled regular expressions to search for import statements
@ -65,56 +65,56 @@ m_from = regex.compile('^[ \t]*import[ \t]+\([^#]+\)')
# Collect data from one file
#
def process(filename, table):
fp = open(filename, 'r')
mod = os.path.basename(filename)
if mod[-3:] == '.py':
mod = mod[:-3]
table[mod] = list = []
while 1:
line = fp.readline()
if not line: break
while line[-1:] == '\\':
nextline = fp.readline()
if not nextline: break
line = line[:-1] + nextline
if m_import.match(line) >= 0:
(a, b), (a1, b1) = m_import.regs[:2]
elif m_from.match(line) >= 0:
(a, b), (a1, b1) = m_from.regs[:2]
else: continue
words = string.splitfields(line[a1:b1], ',')
# print '#', line, words
for word in words:
word = string.strip(word)
if word not in list:
list.append(word)
fp = open(filename, 'r')
mod = os.path.basename(filename)
if mod[-3:] == '.py':
mod = mod[:-3]
table[mod] = list = []
while 1:
line = fp.readline()
if not line: break
while line[-1:] == '\\':
nextline = fp.readline()
if not nextline: break
line = line[:-1] + nextline
if m_import.match(line) >= 0:
(a, b), (a1, b1) = m_import.regs[:2]
elif m_from.match(line) >= 0:
(a, b), (a1, b1) = m_from.regs[:2]
else: continue
words = string.splitfields(line[a1:b1], ',')
# print '#', line, words
for word in words:
word = string.strip(word)
if word not in list:
list.append(word)
# Compute closure (this is in fact totally general)
#
def closure(table):
modules = table.keys()
#
# Initialize reach with a copy of table
#
reach = {}
for mod in modules:
reach[mod] = table[mod][:]
#
# Iterate until no more change
#
change = 1
while change:
change = 0
for mod in modules:
for mo in reach[mod]:
if mo in modules:
for m in reach[mo]:
if m not in reach[mod]:
reach[mod].append(m)
change = 1
#
return reach
modules = table.keys()
#
# Initialize reach with a copy of table
#
reach = {}
for mod in modules:
reach[mod] = table[mod][:]
#
# Iterate until no more change
#
change = 1
while change:
change = 0
for mod in modules:
for mo in reach[mod]:
if mo in modules:
for m in reach[mo]:
if m not in reach[mod]:
reach[mod].append(m)
change = 1
#
return reach
# Invert a table (this is again totally general).
@ -122,13 +122,13 @@ def closure(table):
# so there may be empty lists in the inverse.
#
def inverse(table):
inv = {}
for key in table.keys():
if not inv.has_key(key):
inv[key] = []
for item in table[key]:
store(inv, item, key)
return inv
inv = {}
for key in table.keys():
if not inv.has_key(key):
inv[key] = []
for item in table[key]:
store(inv, item, key)
return inv
# Store "item" in "dict" under "key".
@ -136,32 +136,32 @@ def inverse(table):
# If there is no list for the key yet, it is created.
#
def store(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]
# Tabulate results neatly
#
def printresults(table):
modules = table.keys()
maxlen = 0
for mod in modules: maxlen = max(maxlen, len(mod))
modules.sort()
for mod in modules:
list = table[mod]
list.sort()
print string.ljust(mod, maxlen), ':',
if mod in list:
print '(*)',
for ref in list:
print ref,
print
modules = table.keys()
maxlen = 0
for mod in modules: maxlen = max(maxlen, len(mod))
modules.sort()
for mod in modules:
list = table[mod]
list.sort()
print string.ljust(mod, maxlen), ':',
if mod in list:
print '(*)',
for ref in list:
print ref,
print
# Call main and honor exit status
try:
sys.exit(main())
sys.exit(main())
except KeyboardInterrupt:
sys.exit(1)
sys.exit(1)

View file

@ -97,236 +97,236 @@ start = 'if', 'while', 'for', 'try', 'def', 'class'
class PythonIndenter:
def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
self.fpi = fpi
self.fpo = fpo
self.indentsize = indentsize
self.tabsize = tabsize
self.lineno = 0
self.expandtabs = expandtabs
self._write = fpo.write
self.kwprog = re.compile(
r'^\s*(?P<kw>[a-z]+)'
r'(\s+(?P<id>[a-zA-Z_]\w*))?'
r'[^\w]')
self.endprog = re.compile(
r'^\s*#?\s*end\s+(?P<kw>[a-z]+)'
r'(\s+(?P<id>[a-zA-Z_]\w*))?'
r'[^\w]')
self.wsprog = re.compile(r'^[ \t]*')
# end def __init__
def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
self.fpi = fpi
self.fpo = fpo
self.indentsize = indentsize
self.tabsize = tabsize
self.lineno = 0
self.expandtabs = expandtabs
self._write = fpo.write
self.kwprog = re.compile(
r'^\s*(?P<kw>[a-z]+)'
r'(\s+(?P<id>[a-zA-Z_]\w*))?'
r'[^\w]')
self.endprog = re.compile(
r'^\s*#?\s*end\s+(?P<kw>[a-z]+)'
r'(\s+(?P<id>[a-zA-Z_]\w*))?'
r'[^\w]')
self.wsprog = re.compile(r'^[ \t]*')
# end def __init__
def write(self, line):
if self.expandtabs:
self._write(string.expandtabs(line, self.tabsize))
else:
self._write(line)
# end if
# end def write
def write(self, line):
if self.expandtabs:
self._write(string.expandtabs(line, self.tabsize))
else:
self._write(line)
# end if
# end def write
def readline(self):
line = self.fpi.readline()
if line: self.lineno = self.lineno + 1
# end if
return line
# end def readline
def readline(self):
line = self.fpi.readline()
if line: self.lineno = self.lineno + 1
# end if
return line
# end def readline
def error(self, fmt, *args):
if args: fmt = fmt % args
# end if
sys.stderr.write('Error at line %d: %s\n' % (self.lineno, fmt))
self.write('### %s ###\n' % fmt)
# end def error
def error(self, fmt, *args):
if args: fmt = fmt % args
# end if
sys.stderr.write('Error at line %d: %s\n' % (self.lineno, fmt))
self.write('### %s ###\n' % fmt)
# end def error
def getline(self):
line = self.readline()
while line[-2:] == '\\\n':
line2 = self.readline()
if not line2: break
# end if
line = line + line2
# end while
return line
# end def getline
def getline(self):
line = self.readline()
while line[-2:] == '\\\n':
line2 = self.readline()
if not line2: break
# end if
line = line + line2
# end while
return line
# end def getline
def putline(self, line, indent = None):
if indent is None:
self.write(line)
return
# end if
tabs, spaces = divmod(indent*self.indentsize, self.tabsize)
i = 0
m = self.wsprog.match(line)
if m: i = m.end()
# end if
self.write('\t'*tabs + ' '*spaces + line[i:])
# end def putline
def putline(self, line, indent = None):
if indent is None:
self.write(line)
return
# end if
tabs, spaces = divmod(indent*self.indentsize, self.tabsize)
i = 0
m = self.wsprog.match(line)
if m: i = m.end()
# end if
self.write('\t'*tabs + ' '*spaces + line[i:])
# end def putline
def reformat(self):
stack = []
while 1:
line = self.getline()
if not line: break # EOF
# end if
m = self.endprog.match(line)
if m:
kw = 'end'
kw2 = m.group('kw')
if not stack:
self.error('unexpected end')
elif stack[-1][0] != kw2:
self.error('unmatched end')
# end if
del stack[-1:]
self.putline(line, len(stack))
continue
# end if
m = self.kwprog.match(line)
if m:
kw = m.group('kw')
if kw in start:
self.putline(line, len(stack))
stack.append((kw, kw))
continue
# end if
if next.has_key(kw) and stack:
self.putline(line, len(stack)-1)
kwa, kwb = stack[-1]
stack[-1] = kwa, kw
continue
# end if
# end if
self.putline(line, len(stack))
# end while
if stack:
self.error('unterminated keywords')
for kwa, kwb in stack:
self.write('\t%s\n' % kwa)
# end for
# end if
# end def reformat
def reformat(self):
stack = []
while 1:
line = self.getline()
if not line: break # EOF
# end if
m = self.endprog.match(line)
if m:
kw = 'end'
kw2 = m.group('kw')
if not stack:
self.error('unexpected end')
elif stack[-1][0] != kw2:
self.error('unmatched end')
# end if
del stack[-1:]
self.putline(line, len(stack))
continue
# end if
m = self.kwprog.match(line)
if m:
kw = m.group('kw')
if kw in start:
self.putline(line, len(stack))
stack.append((kw, kw))
continue
# end if
if next.has_key(kw) and stack:
self.putline(line, len(stack)-1)
kwa, kwb = stack[-1]
stack[-1] = kwa, kw
continue
# end if
# end if
self.putline(line, len(stack))
# end while
if stack:
self.error('unterminated keywords')
for kwa, kwb in stack:
self.write('\t%s\n' % kwa)
# end for
# end if
# end def reformat
def delete(self):
begin_counter = 0
end_counter = 0
while 1:
line = self.getline()
if not line: break # EOF
# end if
m = self.endprog.match(line)
if m:
end_counter = end_counter + 1
continue
# end if
m = self.kwprog.match(line)
if m:
kw = m.group('kw')
if kw in start:
begin_counter = begin_counter + 1
# end if
# end if
self.putline(line)
# end while
if begin_counter - end_counter < 0:
sys.stderr.write('Warning: input contained more end tags than expected\n')
elif begin_counter - end_counter > 0:
sys.stderr.write('Warning: input contained less end tags than expected\n')
# end if
# end def delete
def complete(self):
self.indentsize = 1
stack = []
todo = []
current, firstkw, lastkw, topid = 0, '', '', ''
while 1:
line = self.getline()
i = 0
m = self.wsprog.match(line)
if m: i = m.end()
# end if
m = self.endprog.match(line)
if m:
thiskw = 'end'
endkw = m.group('kw')
thisid = m.group('id')
else:
m = self.kwprog.match(line)
if m:
thiskw = m.group('kw')
if not next.has_key(thiskw):
thiskw = ''
# end if
if thiskw in ('def', 'class'):
thisid = m.group('id')
else:
thisid = ''
# end if
elif line[i:i+1] in ('\n', '#'):
todo.append(line)
continue
else:
thiskw = ''
# end if
# end if
indent = len(string.expandtabs(line[:i], self.tabsize))
while indent < current:
if firstkw:
if topid:
s = '# end %s %s\n' % (
firstkw, topid)
else:
s = '# end %s\n' % firstkw
# end if
self.putline(s, current)
firstkw = lastkw = ''
# end if
current, firstkw, lastkw, topid = stack[-1]
del stack[-1]
# end while
if indent == current and firstkw:
if thiskw == 'end':
if endkw != firstkw:
self.error('mismatched end')
# end if
firstkw = lastkw = ''
elif not thiskw or thiskw in start:
if topid:
s = '# end %s %s\n' % (
firstkw, topid)
else:
s = '# end %s\n' % firstkw
# end if
self.putline(s, current)
firstkw = lastkw = topid = ''
# end if
# end if
if indent > current:
stack.append((current, firstkw, lastkw, topid))
if thiskw and thiskw not in start:
# error
thiskw = ''
# end if
current, firstkw, lastkw, topid = \
indent, thiskw, thiskw, thisid
# end if
if thiskw:
if thiskw in start:
firstkw = lastkw = thiskw
topid = thisid
else:
lastkw = thiskw
# end if
# end if
for l in todo: self.write(l)
# end for
todo = []
if not line: break
# end if
self.write(line)
# end while
# end def complete
def delete(self):
begin_counter = 0
end_counter = 0
while 1:
line = self.getline()
if not line: break # EOF
# end if
m = self.endprog.match(line)
if m:
end_counter = end_counter + 1
continue
# end if
m = self.kwprog.match(line)
if m:
kw = m.group('kw')
if kw in start:
begin_counter = begin_counter + 1
# end if
# end if
self.putline(line)
# end while
if begin_counter - end_counter < 0:
sys.stderr.write('Warning: input contained more end tags than expected\n')
elif begin_counter - end_counter > 0:
sys.stderr.write('Warning: input contained less end tags than expected\n')
# end if
# end def delete
def complete(self):
self.indentsize = 1
stack = []
todo = []
current, firstkw, lastkw, topid = 0, '', '', ''
while 1:
line = self.getline()
i = 0
m = self.wsprog.match(line)
if m: i = m.end()
# end if
m = self.endprog.match(line)
if m:
thiskw = 'end'
endkw = m.group('kw')
thisid = m.group('id')
else:
m = self.kwprog.match(line)
if m:
thiskw = m.group('kw')
if not next.has_key(thiskw):
thiskw = ''
# end if
if thiskw in ('def', 'class'):
thisid = m.group('id')
else:
thisid = ''
# end if
elif line[i:i+1] in ('\n', '#'):
todo.append(line)
continue
else:
thiskw = ''
# end if
# end if
indent = len(string.expandtabs(line[:i], self.tabsize))
while indent < current:
if firstkw:
if topid:
s = '# end %s %s\n' % (
firstkw, topid)
else:
s = '# end %s\n' % firstkw
# end if
self.putline(s, current)
firstkw = lastkw = ''
# end if
current, firstkw, lastkw, topid = stack[-1]
del stack[-1]
# end while
if indent == current and firstkw:
if thiskw == 'end':
if endkw != firstkw:
self.error('mismatched end')
# end if
firstkw = lastkw = ''
elif not thiskw or thiskw in start:
if topid:
s = '# end %s %s\n' % (
firstkw, topid)
else:
s = '# end %s\n' % firstkw
# end if
self.putline(s, current)
firstkw = lastkw = topid = ''
# end if
# end if
if indent > current:
stack.append((current, firstkw, lastkw, topid))
if thiskw and thiskw not in start:
# error
thiskw = ''
# end if
current, firstkw, lastkw, topid = \
indent, thiskw, thiskw, thisid
# end if
if thiskw:
if thiskw in start:
firstkw = lastkw = thiskw
topid = thisid
else:
lastkw = thiskw
# end if
# end if
for l in todo: self.write(l)
# end for
todo = []
if not line: break
# end if
self.write(line)
# end while
# end def complete
# end class PythonIndenter
@ -336,134 +336,134 @@ class PythonIndenter:
# - xxx_file(filename): process file in place, return true iff changed
def complete_filter(input = sys.stdin, output = sys.stdout,
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.complete()
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.complete()
# end def complete_filter
def delete_filter(input= sys.stdin, output = sys.stdout,
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.delete()
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.delete()
# end def delete_filter
def reformat_filter(input = sys.stdin, output = sys.stdout,
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.reformat()
stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.reformat()
# end def reformat_filter
class StringReader:
def __init__(self, buf):
self.buf = buf
self.pos = 0
self.len = len(self.buf)
# end def __init__
def read(self, n = 0):
if n <= 0:
n = self.len - self.pos
else:
n = min(n, self.len - self.pos)
# end if
r = self.buf[self.pos : self.pos + n]
self.pos = self.pos + n
return r
# end def read
def readline(self):
i = string.find(self.buf, '\n', self.pos)
return self.read(i + 1 - self.pos)
# end def readline
def readlines(self):
lines = []
line = self.readline()
while line:
lines.append(line)
line = self.readline()
# end while
return lines
# end def readlines
# seek/tell etc. are left as an exercise for the reader
def __init__(self, buf):
self.buf = buf
self.pos = 0
self.len = len(self.buf)
# end def __init__
def read(self, n = 0):
if n <= 0:
n = self.len - self.pos
else:
n = min(n, self.len - self.pos)
# end if
r = self.buf[self.pos : self.pos + n]
self.pos = self.pos + n
return r
# end def read
def readline(self):
i = string.find(self.buf, '\n', self.pos)
return self.read(i + 1 - self.pos)
# end def readline
def readlines(self):
lines = []
line = self.readline()
while line:
lines.append(line)
line = self.readline()
# end while
return lines
# end def readlines
# seek/tell etc. are left as an exercise for the reader
# end class StringReader
class StringWriter:
def __init__(self):
self.buf = ''
# end def __init__
def write(self, s):
self.buf = self.buf + s
# end def write
def getvalue(self):
return self.buf
# end def getvalue
def __init__(self):
self.buf = ''
# end def __init__
def write(self, s):
self.buf = self.buf + s
# end def write
def getvalue(self):
return self.buf
# end def getvalue
# end class StringWriter
def complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
input = StringReader(source)
output = StringWriter()
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.complete()
return output.getvalue()
input = StringReader(source)
output = StringWriter()
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.complete()
return output.getvalue()
# end def complete_string
def delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
input = StringReader(source)
output = StringWriter()
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.delete()
return output.getvalue()
input = StringReader(source)
output = StringWriter()
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.delete()
return output.getvalue()
# end def delete_string
def reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
input = StringReader(source)
output = StringWriter()
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.reformat()
return output.getvalue()
input = StringReader(source)
output = StringWriter()
pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
pi.reformat()
return output.getvalue()
# end def reformat_string
def complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
source = open(filename, 'r').read()
result = complete_string(source, stepsize, tabsize, expandtabs)
if source == result: return 0
# end if
import os
try: os.rename(filename, filename + '~')
except os.error: pass
# end try
f = open(filename, 'w')
f.write(result)
f.close()
return 1
source = open(filename, 'r').read()
result = complete_string(source, stepsize, tabsize, expandtabs)
if source == result: return 0
# end if
import os
try: os.rename(filename, filename + '~')
except os.error: pass
# end try
f = open(filename, 'w')
f.write(result)
f.close()
return 1
# end def complete_file
def delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
source = open(filename, 'r').read()
result = delete_string(source, stepsize, tabsize, expandtabs)
if source == result: return 0
# end if
import os
try: os.rename(filename, filename + '~')
except os.error: pass
# end try
f = open(filename, 'w')
f.write(result)
f.close()
return 1
source = open(filename, 'r').read()
result = delete_string(source, stepsize, tabsize, expandtabs)
if source == result: return 0
# end if
import os
try: os.rename(filename, filename + '~')
except os.error: pass
# end try
f = open(filename, 'w')
f.write(result)
f.close()
return 1
# end def delete_file
def reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
source = open(filename, 'r').read()
result = reformat_string(source, stepsize, tabsize, expandtabs)
if source == result: return 0
# end if
import os
try: os.rename(filename, filename + '~')
except os.error: pass
# end try
f = open(filename, 'w')
f.write(result)
f.close()
return 1
source = open(filename, 'r').read()
result = reformat_string(source, stepsize, tabsize, expandtabs)
if source == result: return 0
# end if
import os
try: os.rename(filename, filename + '~')
except os.error: pass
# end try
f = open(filename, 'w')
f.write(result)
f.close()
return 1
# end def reformat_file
# Test program when called as a script
@ -482,62 +482,62 @@ the program acts as a filter (reads stdin, writes stdout).
""" % vars()
def error_both(op1, op2):
sys.stderr.write('Error: You can not specify both '+op1+' and -'+op2[0]+' at the same time\n')
sys.stderr.write(usage)
sys.exit(2)
sys.stderr.write('Error: You can not specify both '+op1+' and -'+op2[0]+' at the same time\n')
sys.stderr.write(usage)
sys.exit(2)
# end def error_both
def test():
import getopt
try:
opts, args = getopt.getopt(sys.argv[1:], 'cdrs:t:e')
except getopt.error, msg:
sys.stderr.write('Error: %s\n' % msg)
sys.stderr.write(usage)
sys.exit(2)
# end try
action = None
stepsize = STEPSIZE
tabsize = TABSIZE
expandtabs = EXPANDTABS
for o, a in opts:
if o == '-c':
if action: error_both(o, action)
# end if
action = 'complete'
elif o == '-d':
if action: error_both(o, action)
# end if
action = 'delete'
elif o == '-r':
if action: error_both(o, action)
# end if
action = 'reformat'
elif o == '-s':
stepsize = string.atoi(a)
elif o == '-t':
tabsize = string.atoi(a)
elif o == '-e':
expandtabs = 1
# end if
# end for
if not action:
sys.stderr.write(
'You must specify -c(omplete), -d(elete) or -r(eformat)\n')
sys.stderr.write(usage)
sys.exit(2)
# end if
if not args or args == ['-']:
action = eval(action + '_filter')
action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs)
else:
action = eval(action + '_file')
for file in args:
action(file, stepsize, tabsize, expandtabs)
# end for
# end if
import getopt
try:
opts, args = getopt.getopt(sys.argv[1:], 'cdrs:t:e')
except getopt.error, msg:
sys.stderr.write('Error: %s\n' % msg)
sys.stderr.write(usage)
sys.exit(2)
# end try
action = None
stepsize = STEPSIZE
tabsize = TABSIZE
expandtabs = EXPANDTABS
for o, a in opts:
if o == '-c':
if action: error_both(o, action)
# end if
action = 'complete'
elif o == '-d':
if action: error_both(o, action)
# end if
action = 'delete'
elif o == '-r':
if action: error_both(o, action)
# end if
action = 'reformat'
elif o == '-s':
stepsize = string.atoi(a)
elif o == '-t':
tabsize = string.atoi(a)
elif o == '-e':
expandtabs = 1
# end if
# end for
if not action:
sys.stderr.write(
'You must specify -c(omplete), -d(elete) or -r(eformat)\n')
sys.stderr.write(usage)
sys.exit(2)
# end if
if not args or args == ['-']:
action = eval(action + '_filter')
action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs)
else:
action = eval(action + '_file')
for file in args:
action(file, stepsize, tabsize, expandtabs)
# end for
# end if
# end def test
if __name__ == '__main__':
test()
test()
# end if

View file

@ -39,7 +39,7 @@ def treat_file(file):
tags.append(s)
while 1:
line = fp.readline()
if not line:
if not line:
break
m = matcher.match(line)
if m:

View file

@ -4,159 +4,159 @@ from Tkinter import *
import re
class ReDemo:
def __init__(self, master):
self.master = master
self.promptdisplay = Label(self.master, anchor=W,
text="Enter a Perl-style regular expression:")
self.promptdisplay.pack(side=TOP, fill=X)
def __init__(self, master):
self.master = master
self.regexdisplay = Entry(self.master)
self.regexdisplay.pack(fill=X)
self.regexdisplay.focus_set()
self.promptdisplay = Label(self.master, anchor=W,
text="Enter a Perl-style regular expression:")
self.promptdisplay.pack(side=TOP, fill=X)
self.addoptions()
self.regexdisplay = Entry(self.master)
self.regexdisplay.pack(fill=X)
self.regexdisplay.focus_set()
self.statusdisplay = Label(self.master, text="", anchor=W)
self.statusdisplay.pack(side=TOP, fill=X)
self.addoptions()
self.labeldisplay = Label(self.master, anchor=W,
text="Enter a string to search:")
self.labeldisplay.pack(fill=X)
self.labeldisplay.pack(fill=X)
self.statusdisplay = Label(self.master, text="", anchor=W)
self.statusdisplay.pack(side=TOP, fill=X)
self.showframe = Frame(master)
self.showframe.pack(fill=X, anchor=W)
self.labeldisplay = Label(self.master, anchor=W,
text="Enter a string to search:")
self.labeldisplay.pack(fill=X)
self.labeldisplay.pack(fill=X)
self.showvar = StringVar(master)
self.showvar.set("first")
self.showframe = Frame(master)
self.showframe.pack(fill=X, anchor=W)
self.showfirstradio = Radiobutton(self.showframe,
text="Highlight first match",
variable=self.showvar,
value="first",
command=self.recompile)
self.showfirstradio.pack(side=LEFT)
self.showvar = StringVar(master)
self.showvar.set("first")
self.showallradio = Radiobutton(self.showframe,
text="Highlight all matches",
variable=self.showvar,
value="all",
command=self.recompile)
self.showallradio.pack(side=LEFT)
self.showfirstradio = Radiobutton(self.showframe,
text="Highlight first match",
variable=self.showvar,
value="first",
command=self.recompile)
self.showfirstradio.pack(side=LEFT)
self.stringdisplay = Text(self.master, width=60, height=4)
self.stringdisplay.pack(fill=BOTH, expand=1)
self.stringdisplay.tag_configure("hit", background="yellow")
self.showallradio = Radiobutton(self.showframe,
text="Highlight all matches",
variable=self.showvar,
value="all",
command=self.recompile)
self.showallradio.pack(side=LEFT)
self.grouplabel = Label(self.master, text="Groups:", anchor=W)
self.grouplabel.pack(fill=X)
self.stringdisplay = Text(self.master, width=60, height=4)
self.stringdisplay.pack(fill=BOTH, expand=1)
self.stringdisplay.tag_configure("hit", background="yellow")
self.grouplist = Listbox(self.master)
self.grouplist.pack(expand=1, fill=BOTH)
self.grouplabel = Label(self.master, text="Groups:", anchor=W)
self.grouplabel.pack(fill=X)
self.regexdisplay.bind('<Key>', self.recompile)
self.stringdisplay.bind('<Key>', self.reevaluate)
self.grouplist = Listbox(self.master)
self.grouplist.pack(expand=1, fill=BOTH)
self.compiled = None
self.recompile()
self.regexdisplay.bind('<Key>', self.recompile)
self.stringdisplay.bind('<Key>', self.reevaluate)
btags = self.regexdisplay.bindtags()
self.regexdisplay.bindtags(btags[1:] + btags[:1])
self.compiled = None
self.recompile()
btags = self.stringdisplay.bindtags()
self.stringdisplay.bindtags(btags[1:] + btags[:1])
def addoptions(self):
self.frames = []
self.boxes = []
self.vars = []
for name in ('IGNORECASE',
'LOCALE',
'MULTILINE',
'DOTALL',
'VERBOSE'):
if len(self.boxes) % 3 == 0:
frame = Frame(self.master)
frame.pack(fill=X)
self.frames.append(frame)
val = getattr(re, name)
var = IntVar()
box = Checkbutton(frame,
variable=var, text=name,
offvalue=0, onvalue=val,
command=self.recompile)
box.pack(side=LEFT)
self.boxes.append(box)
self.vars.append(var)
def getflags(self):
flags = 0
for var in self.vars:
flags = flags | var.get()
flags = flags
return flags
def recompile(self, event=None):
try:
self.compiled = re.compile(self.regexdisplay.get(),
self.getflags())
bg = self.promptdisplay['background']
self.statusdisplay.config(text="", background=bg)
except re.error, msg:
self.compiled = None
self.statusdisplay.config(
text="re.error: %s" % str(msg),
background="red")
self.reevaluate()
def reevaluate(self, event=None):
try:
self.stringdisplay.tag_remove("hit", "1.0", END)
except TclError:
pass
try:
self.stringdisplay.tag_remove("hit0", "1.0", END)
except TclError:
pass
self.grouplist.delete(0, END)
if not self.compiled:
return
self.stringdisplay.tag_configure("hit", background="yellow")
self.stringdisplay.tag_configure("hit0", background="orange")
text = self.stringdisplay.get("1.0", END)
last = 0
nmatches = 0
while last <= len(text):
m = self.compiled.search(text, last)
if m is None:
break
first, last = m.span()
if last == first:
last = first+1
tag = "hit0"
else:
tag = "hit"
pfirst = "1.0 + %d chars" % first
plast = "1.0 + %d chars" % last
self.stringdisplay.tag_add(tag, pfirst, plast)
if nmatches == 0:
self.stringdisplay.yview_pickplace(pfirst)
groups = list(m.groups())
groups.insert(0, m.group())
for i in range(len(groups)):
g = "%2d: %s" % (i, `groups[i]`)
self.grouplist.insert(END, g)
nmatches = nmatches + 1
if self.showvar.get() == "first":
break
btags = self.regexdisplay.bindtags()
self.regexdisplay.bindtags(btags[1:] + btags[:1])
if nmatches == 0:
self.statusdisplay.config(text="(no match)",
background="yellow")
else:
self.statusdisplay.config(text="")
btags = self.stringdisplay.bindtags()
self.stringdisplay.bindtags(btags[1:] + btags[:1])
def addoptions(self):
self.frames = []
self.boxes = []
self.vars = []
for name in ('IGNORECASE',
'LOCALE',
'MULTILINE',
'DOTALL',
'VERBOSE'):
if len(self.boxes) % 3 == 0:
frame = Frame(self.master)
frame.pack(fill=X)
self.frames.append(frame)
val = getattr(re, name)
var = IntVar()
box = Checkbutton(frame,
variable=var, text=name,
offvalue=0, onvalue=val,
command=self.recompile)
box.pack(side=LEFT)
self.boxes.append(box)
self.vars.append(var)
def getflags(self):
flags = 0
for var in self.vars:
flags = flags | var.get()
flags = flags
return flags
def recompile(self, event=None):
try:
self.compiled = re.compile(self.regexdisplay.get(),
self.getflags())
bg = self.promptdisplay['background']
self.statusdisplay.config(text="", background=bg)
except re.error, msg:
self.compiled = None
self.statusdisplay.config(
text="re.error: %s" % str(msg),
background="red")
self.reevaluate()
def reevaluate(self, event=None):
try:
self.stringdisplay.tag_remove("hit", "1.0", END)
except TclError:
pass
try:
self.stringdisplay.tag_remove("hit0", "1.0", END)
except TclError:
pass
self.grouplist.delete(0, END)
if not self.compiled:
return
self.stringdisplay.tag_configure("hit", background="yellow")
self.stringdisplay.tag_configure("hit0", background="orange")
text = self.stringdisplay.get("1.0", END)
last = 0
nmatches = 0
while last <= len(text):
m = self.compiled.search(text, last)
if m is None:
break
first, last = m.span()
if last == first:
last = first+1
tag = "hit0"
else:
tag = "hit"
pfirst = "1.0 + %d chars" % first
plast = "1.0 + %d chars" % last
self.stringdisplay.tag_add(tag, pfirst, plast)
if nmatches == 0:
self.stringdisplay.yview_pickplace(pfirst)
groups = list(m.groups())
groups.insert(0, m.group())
for i in range(len(groups)):
g = "%2d: %s" % (i, `groups[i]`)
self.grouplist.insert(END, g)
nmatches = nmatches + 1
if self.showvar.get() == "first":
break
if nmatches == 0:
self.statusdisplay.config(text="(no match)",
background="yellow")
else:
self.statusdisplay.config(text="")
# Main function, run when invoked as a stand-alone Python program.

View file

@ -7,23 +7,23 @@
import sys
def main():
files = sys.argv[1:]
suffixes = {}
for file in files:
suff = getsuffix(file)
if not suffixes.has_key(suff):
suffixes[suff] = []
suffixes[suff].append(file)
keys = suffixes.keys()
keys.sort()
for suff in keys:
print `suff`, len(suffixes[suff])
files = sys.argv[1:]
suffixes = {}
for file in files:
suff = getsuffix(file)
if not suffixes.has_key(suff):
suffixes[suff] = []
suffixes[suff].append(file)
keys = suffixes.keys()
keys.sort()
for suff in keys:
print `suff`, len(suffixes[suff])
def getsuffix(file):
suff = ''
for i in range(len(file)):
if file[i] == '.':
suff = file[i:]
return suff
suff = ''
for i in range(len(file)):
if file[i] == '.':
suff = file[i:]
return suff
main()

View file

@ -25,73 +25,73 @@ StringType = type('')
FileType = type(sys.stdin)
def sum(*files):
sts = 0
if files and type(files[-1]) == FileType:
out, files = files[-1], files[:-1]
else:
out = sys.stdout
if len(files) == 1 and type(files[0]) != StringType:
files = files[0]
for f in files:
if type(f) == StringType:
if f == '-':
sts = printsumfp(sys.stdin, '<stdin>', out) or sts
else:
sts = printsum(f, out) or sts
else:
sts = sum(f, out) or sts
return sts
sts = 0
if files and type(files[-1]) == FileType:
out, files = files[-1], files[:-1]
else:
out = sys.stdout
if len(files) == 1 and type(files[0]) != StringType:
files = files[0]
for f in files:
if type(f) == StringType:
if f == '-':
sts = printsumfp(sys.stdin, '<stdin>', out) or sts
else:
sts = printsum(f, out) or sts
else:
sts = sum(f, out) or sts
return sts
def printsum(file, out = sys.stdout):
try:
fp = open(file, rmode)
except IOError, msg:
sys.stderr.write('%s: Can\'t open: %s\n' % (file, msg))
return 1
if fnfilter:
file = fnfilter(file)
sts = printsumfp(fp, file, out)
fp.close()
return sts
try:
fp = open(file, rmode)
except IOError, msg:
sys.stderr.write('%s: Can\'t open: %s\n' % (file, msg))
return 1
if fnfilter:
file = fnfilter(file)
sts = printsumfp(fp, file, out)
fp.close()
return sts
def printsumfp(fp, file, out = sys.stdout):
m = md5.md5()
try:
while 1:
data = fp.read(bufsize)
if not data: break
m.update(data)
except IOError, msg:
sys.stderr.write('%s: I/O error: %s\n' % (file, msg))
return 1
out.write('%s %s\n' % (hexify(m.digest()), file))
return 0
m = md5.md5()
try:
while 1:
data = fp.read(bufsize)
if not data: break
m.update(data)
except IOError, msg:
sys.stderr.write('%s: I/O error: %s\n' % (file, msg))
return 1
out.write('%s %s\n' % (hexify(m.digest()), file))
return 0
def hexify(s):
res = ''
for c in s:
res = res + '%02x' % ord(c)
return res
res = ''
for c in s:
res = res + '%02x' % ord(c)
return res
def main(args = sys.argv[1:], out = sys.stdout):
global fnfilter, rmode, bufsize
import getopt
try:
opts, args = getopt.getopt(args, 'blts:')
except getopt.error, msg:
sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage))
return 2
for o, a in opts:
if o == '-l':
fnfilter = os.path.basename
if o == '-b':
rmode = 'rb'
if o == '-t':
rmode = 'r'
if o == '-s':
bufsize = string.atoi(a)
if not args: args = ['-']
return sum(args, out)
global fnfilter, rmode, bufsize
import getopt
try:
opts, args = getopt.getopt(args, 'blts:')
except getopt.error, msg:
sys.stderr.write('%s: %s\n%s' % (sys.argv[0], msg, usage))
return 2
for o, a in opts:
if o == '-l':
fnfilter = os.path.basename
if o == '-b':
rmode = 'rb'
if o == '-t':
rmode = 'r'
if o == '-s':
bufsize = string.atoi(a)
if not args: args = ['-']
return sum(args, out)
if __name__ == '__main__' or __name__ == sys.argv[0]:
sys.exit(main(sys.argv[1:], sys.stdout))
sys.exit(main(sys.argv[1:], sys.stdout))

View file

@ -46,13 +46,13 @@ blprog = re.compile('^[ \t]*$') # Blank line
kwprog = re.compile('@[a-z]+') # Keyword (embedded, usually
# with {} args)
spprog = re.compile('[\n@{}&<>]') # Special characters in
# running text
# running text
#
# menu item (Yuck!)
miprog = re.compile('^\* ([^:]*):(:|[ \t]*([^\t,\n.]+)([^ \t\n]*))[ \t\n]*')
class HTMLNode:
"""Some of the parser's functionality is separated into this class.
@ -1568,7 +1568,7 @@ def test():
print_headers = 0
cont = 0
html3 = 0
while sys.argv[1:2] == ['-d']:
debugging = debugging + 1
del sys.argv[1:2]

View file

@ -261,7 +261,7 @@ def find_executable_linenos(filename):
# The only way I know to find line numbers is to look for the
# SET_LINENO instructions. Isn't there some way to get it from
# the AST?
return _find_LINENO(code)
### XXX because os.path.commonprefix seems broken by my way of thinking...
@ -279,7 +279,7 @@ def commonprefix(dirs):
if i == 0: return ''
break
return os.sep.join(prefix)
def create_results_log(results, dirname = ".", show_missing = 1,
save_counts = 0):
import re
@ -297,7 +297,7 @@ def create_results_log(results, dirname = ".", show_missing = 1,
results.update(results.__class__(counts, modules))
except IOError:
pass
# there are many places where this is insufficient, like a blank
# line embedded in a multiline string.
blank = re.compile(r'^\s*(#.*)?$')
@ -307,7 +307,7 @@ def create_results_log(results, dirname = ".", show_missing = 1,
tfdir = tempfile.gettempdir()
for key in per_file.keys():
filename = key
# skip some "files" we don't care about...
if filename == "<string>":
continue
@ -484,7 +484,7 @@ class Trace:
print '%s(%d): ??' % (modulename, lineno)
return self.trace
def _err_exit(msg):
sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))

View file

@ -17,7 +17,7 @@ entry in the master tree are synchronized. This means:
copy the slave to the master
else (the master is newer):
copy the master to the slave
normalizing the slave means replacing CRLF with LF when the master
doesn't use CRLF
@ -59,7 +59,7 @@ def main():
print "slavedir masterdir"
return
process(slave, master)
def process(slave, master):
cvsdir = os.path.join(master, "CVS")
if not os.path.isdir(cvsdir):

View file

@ -11,7 +11,7 @@ import sys, os, string
from stat import *
def msg(str):
sys.stderr.write(str + '\n')
sys.stderr.write(str + '\n')
pathlist = string.splitfields(os.environ['PATH'], ':')
@ -19,38 +19,38 @@ sts = 0
longlist = ''
if sys.argv[1:] and sys.argv[1][:2] == '-l':
longlist = sys.argv[1]
del sys.argv[1]
longlist = sys.argv[1]
del sys.argv[1]
for prog in sys.argv[1:]:
ident = ()
for dir in pathlist:
file = os.path.join(dir, prog)
try:
st = os.stat(file)
except os.error:
continue
if not S_ISREG(st[ST_MODE]):
msg(file + ': not a disk file')
else:
mode = S_IMODE(st[ST_MODE])
if mode & 0111:
if not ident:
print file
ident = st[:3]
else:
if st[:3] == ident:
s = 'same as: '
else:
s = 'also: '
msg(s + file)
else:
msg(file + ': not executable')
if longlist:
sts = os.system('ls ' + longlist + ' ' + file)
if sts: msg('"ls -l" exit status: ' + `sts`)
if not ident:
msg(prog + ': not found')
sts = 1
ident = ()
for dir in pathlist:
file = os.path.join(dir, prog)
try:
st = os.stat(file)
except os.error:
continue
if not S_ISREG(st[ST_MODE]):
msg(file + ': not a disk file')
else:
mode = S_IMODE(st[ST_MODE])
if mode & 0111:
if not ident:
print file
ident = st[:3]
else:
if st[:3] == ident:
s = 'same as: '
else:
s = 'also: '
msg(s + file)
else:
msg(file + ': not executable')
if longlist:
sts = os.system('ls ' + longlist + ' ' + file)
if sts: msg('"ls -l" exit status: ' + `sts`)
if not ident:
msg(prog + ': not found')
sts = 1
sys.exit(sts)

View file

@ -16,102 +16,102 @@ EXECMAGIC = '\001\140\000\010'
MAXSIZE = 200*1024 # Files this big must be binaries and are skipped.
def getargs():
args = sys.argv[1:]
if args:
return args
print 'No arguments, checking almost *, in "ls -t" order'
list = []
for file in os.listdir(os.curdir):
if not skipfile(file):
list.append((getmtime(file), file))
list.sort()
if not list:
print 'Nothing to do -- exit 1'
sys.exit(1)
list.sort()
list.reverse()
for mtime, file in list: args.append(file)
return args
args = sys.argv[1:]
if args:
return args
print 'No arguments, checking almost *, in "ls -t" order'
list = []
for file in os.listdir(os.curdir):
if not skipfile(file):
list.append((getmtime(file), file))
list.sort()
if not list:
print 'Nothing to do -- exit 1'
sys.exit(1)
list.sort()
list.reverse()
for mtime, file in list: args.append(file)
return args
def getmtime(file):
try:
st = os.stat(file)
return st[ST_MTIME]
except os.error:
return -1
try:
st = os.stat(file)
return st[ST_MTIME]
except os.error:
return -1
badnames = ['tags', 'TAGS', 'xyzzy', 'nohup.out', 'core']
badprefixes = ['.', ',', '@', '#', 'o.']
badsuffixes = \
['~', '.a', '.o', '.old', '.bak', '.orig', '.new', '.prev', '.not', \
'.pyc', '.fdc', '.rgb', '.elc', ',v']
['~', '.a', '.o', '.old', '.bak', '.orig', '.new', '.prev', '.not', \
'.pyc', '.fdc', '.rgb', '.elc', ',v']
ignore = []
def setup():
ignore[:] = badnames
for p in badprefixes:
ignore.append(p + '*')
for p in badsuffixes:
ignore.append('*' + p)
try:
f = open('.xxcign', 'r')
except IOError:
return
ignore[:] = ignore + string.split(f.read())
ignore[:] = badnames
for p in badprefixes:
ignore.append(p + '*')
for p in badsuffixes:
ignore.append('*' + p)
try:
f = open('.xxcign', 'r')
except IOError:
return
ignore[:] = ignore + string.split(f.read())
def skipfile(file):
for p in ignore:
if fnmatch.fnmatch(file, p): return 1
try:
st = os.lstat(file)
except os.error:
return 1 # Doesn't exist -- skip it
# Skip non-plain files.
if not S_ISREG(st[ST_MODE]): return 1
# Skip huge files -- probably binaries.
if st[ST_SIZE] >= MAXSIZE: return 1
# Skip executables
try:
data = open(file, 'r').read(len(EXECMAGIC))
if data == EXECMAGIC: return 1
except:
pass
return 0
for p in ignore:
if fnmatch.fnmatch(file, p): return 1
try:
st = os.lstat(file)
except os.error:
return 1 # Doesn't exist -- skip it
# Skip non-plain files.
if not S_ISREG(st[ST_MODE]): return 1
# Skip huge files -- probably binaries.
if st[ST_SIZE] >= MAXSIZE: return 1
# Skip executables
try:
data = open(file, 'r').read(len(EXECMAGIC))
if data == EXECMAGIC: return 1
except:
pass
return 0
def badprefix(file):
for bad in badprefixes:
if file[:len(bad)] == bad: return 1
return 0
for bad in badprefixes:
if file[:len(bad)] == bad: return 1
return 0
def badsuffix(file):
for bad in badsuffixes:
if file[-len(bad):] == bad: return 1
return 0
for bad in badsuffixes:
if file[-len(bad):] == bad: return 1
return 0
def go(args):
for file in args:
print file + ':'
if differing(file):
showdiffs(file)
if askyesno('Check in ' + file + ' ? '):
sts = os.system('rcs -l ' + file) # ignored
sts = os.system('ci -l ' + file)
for file in args:
print file + ':'
if differing(file):
showdiffs(file)
if askyesno('Check in ' + file + ' ? '):
sts = os.system('rcs -l ' + file) # ignored
sts = os.system('ci -l ' + file)
def differing(file):
cmd = 'co -p ' + file + ' 2>/dev/null | cmp -s - ' + file
sts = os.system(cmd)
return sts != 0
cmd = 'co -p ' + file + ' 2>/dev/null | cmp -s - ' + file
sts = os.system(cmd)
return sts != 0
def showdiffs(file):
cmd = 'rcsdiff ' + file + ' 2>&1 | ${PAGER-more}'
sts = os.system(cmd)
cmd = 'rcsdiff ' + file + ' 2>&1 | ${PAGER-more}'
sts = os.system(cmd)
def askyesno(prompt):
s = raw_input(prompt)
return s in ['y', 'yes']
s = raw_input(prompt)
return s in ['y', 'yes']
try:
setup()
go(getargs())
setup()
go(getargs())
except KeyboardInterrupt:
print '[Intr]'
print '[Intr]'