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

@ -136,14 +136,14 @@ class shlex:
if nextchar == self.state: if nextchar == self.state:
self.state = ' ' self.state = ' '
break break
elif not nextchar: # end of file elif not nextchar: # end of file
if self.debug >= 2: if self.debug >= 2:
print "shlex: I see EOF in quotes state" print "shlex: I see EOF in quotes state"
# XXX what error should be raised here? # XXX what error should be raised here?
raise ValueError, "No closing quotation" raise ValueError, "No closing quotation"
elif self.state == 'a': elif self.state == 'a':
if not nextchar: if not nextchar:
self.state = None # end of file self.state = None # end of file
break break
elif nextchar in self.whitespace: elif nextchar in self.whitespace:
if self.debug >= 2: if self.debug >= 2:

View file

@ -91,9 +91,9 @@ class CallTips:
namespace = sys.modules.copy() namespace = sys.modules.copy()
namespace.update(__main__.__dict__) namespace.update(__main__.__dict__)
try: try:
return eval(word, namespace) return eval(word, namespace)
except: except:
pass pass
return None # Can't find an object. return None # Can't find an object.
def _find_constructor(class_ob): def _find_constructor(class_ob):

View file

@ -299,10 +299,10 @@ class Debugger(bdb.Bdb):
import linecache # Import as late as possible import linecache # Import as late as possible
line = linecache.getline(filename, lineno) line = linecache.getline(filename, lineno)
if not line: if not line:
return 'That line does not exist!' return 'That line does not exist!'
if not self.breaks.has_key(filename): if not self.breaks.has_key(filename):
self.breaks[filename] = [] self.breaks[filename] = []
list = self.breaks[filename] list = self.breaks[filename]
if not lineno in list: if not lineno in list:
list.append(lineno) list.append(lineno)
bp = bdb.Breakpoint(filename, lineno, temporary, cond) bp = bdb.Breakpoint(filename, lineno, temporary, cond)

View file

@ -1,4 +1,3 @@
class Delegator: class Delegator:
# The cache is only used to be able to change delegates! # The cache is only used to be able to change delegates!

View file

@ -26,12 +26,12 @@ class FileList:
def goodname(self, filename): def goodname(self, filename):
filename = self.canonize(filename) filename = self.canonize(filename)
key = os.path.normcase(filename) key = os.path.normcase(filename)
if self.dict.has_key(key): if self.dict.has_key(key):
edit = self.dict[key] edit = self.dict[key]
filename = edit.io.filename or filename filename = edit.io.filename or filename
return filename return filename
def open(self, filename): def open(self, filename):
assert filename assert filename

View file

@ -17,17 +17,17 @@ class IdleConfParser(ConfigParser):
The return value is appropriate for passing to Tkinter in, e.g., The return value is appropriate for passing to Tkinter in, e.g.,
a tag_config call. a tag_config call.
""" """
fore = self.getdef(sec, name + "-foreground") fore = self.getdef(sec, name + "-foreground")
back = self.getdef(sec, name + "-background") back = self.getdef(sec, name + "-background")
return {"foreground": fore, return {"foreground": fore,
"background": back} "background": back}
def getdef(self, sec, options, raw=0, vars=None, default=None): def getdef(self, sec, options, raw=0, vars=None, default=None):
"""Get an option value for given section or return default""" """Get an option value for given section or return default"""
try: try:
return self.get(sec, options, raw, vars) return self.get(sec, options, raw, vars)
except (NoSectionError, NoOptionError): except (NoSectionError, NoOptionError):
return default return default
def getsection(self, section): def getsection(self, section):
"""Return a SectionConfigParser object""" """Return a SectionConfigParser object"""
@ -37,10 +37,10 @@ class IdleConfParser(ConfigParser):
exts = [] exts = []
for sec in self.sections(): for sec in self.sections():
if self.builtin_sections.has_key(sec): if self.builtin_sections.has_key(sec):
continue continue
# enable is a bool, but it may not be defined # enable is a bool, but it may not be defined
if self.getdef(sec, 'enable') != '0': if self.getdef(sec, 'enable') != '0':
exts.append(sec) exts.append(sec)
return exts return exts
def reload(self): def reload(self):
@ -110,4 +110,3 @@ def load(dir):
os.path.join(homedir, ".idle"))) os.path.join(homedir, ".idle")))
idleconf = IdleConfParser() idleconf = IdleConfParser()

View file

@ -86,4 +86,3 @@ class History:
self.text.mark_set("insert", "end-1c") self.text.mark_set("insert", "end-1c")
self.text.insert("insert", s) self.text.insert("insert", s)
self.text.see("insert") self.text.see("insert")

View file

@ -2,31 +2,31 @@ from Tkinter import *
class MultiStatusBar(Frame): class MultiStatusBar(Frame):
def __init__(self, master=None, **kw): def __init__(self, master=None, **kw):
if master is None: if master is None:
master = Tk() master = Tk()
apply(Frame.__init__, (self, master), kw) apply(Frame.__init__, (self, master), kw)
self.labels = {} self.labels = {}
def set_label(self, name, text='', side=LEFT): def set_label(self, name, text='', side=LEFT):
if not self.labels.has_key(name): if not self.labels.has_key(name):
label = Label(self, bd=1, relief=SUNKEN, anchor=W) label = Label(self, bd=1, relief=SUNKEN, anchor=W)
label.pack(side=side) label.pack(side=side)
self.labels[name] = label self.labels[name] = label
else: else:
label = self.labels[name] label = self.labels[name]
label.config(text=text) label.config(text=text)
def _test(): def _test():
b = Frame() b = Frame()
c = Text(b) c = Text(b)
c.pack(side=TOP) c.pack(side=TOP)
a = MultiStatusBar(b) a = MultiStatusBar(b)
a.set_label("one", "hello") a.set_label("one", "hello")
a.set_label("two", "world") a.set_label("two", "world")
a.pack(side=BOTTOM, fill=X) a.pack(side=BOTTOM, fill=X)
b.pack() b.pack()
b.mainloop() b.mainloop()
if __name__ == '__main__': if __name__ == '__main__':
_test() _test()

View file

@ -158,7 +158,7 @@ class LastOpenBracketFinder:
startatindex = `startat` + ".0" startatindex = `startat` + ".0"
# rawtext needs to contain everything up to the last # rawtext needs to contain everything up to the last
# character, which was the close paren. the parser also # character, which was the close paren. the parser also
# requires that the last line ends with "\n" # requires that the last line ends with "\n"
rawtext = self.text.get(startatindex, "insert")[:-1] + "\n" rawtext = self.text.get(startatindex, "insert")[:-1] + "\n"
y.set_str(rawtext) y.set_str(rawtext)
bod = y.find_good_parse_start( bod = y.find_good_parse_start(
@ -175,7 +175,7 @@ class LastOpenBracketFinder:
lno = index2line(self.text.index("insert")) lno = index2line(self.text.index("insert"))
i, buf = self._find_offset_in_buf(lno) i, buf = self._find_offset_in_buf(lno)
if i is None \ if i is None \
or keysym_type(buf[i]) != right_keysym_type: or keysym_type(buf[i]) != right_keysym_type:
return None return None
lines_back = string.count(buf[i:], "\n") - 1 lines_back = string.count(buf[i:], "\n") - 1
# subtract one for the "\n" added to please the parser # subtract one for the "\n" added to please the parser
@ -189,4 +189,3 @@ class LastOpenBracketFinder:
icis=self.editwin.is_char_in_string): icis=self.editwin.is_char_in_string):
return icis(startindex + "%dc" % offset) return icis(startindex + "%dc" % offset)
return inner return inner

View file

@ -124,7 +124,7 @@ class ModifiedColorDelegator(ColorDelegator):
"stderr": cconf.getcolor("stderr"), "stderr": cconf.getcolor("stderr"),
"console": cconf.getcolor("console"), "console": cconf.getcolor("console"),
"ERROR": cconf.getcolor("ERROR"), "ERROR": cconf.getcolor("ERROR"),
None: cconf.getcolor("normal"), None: cconf.getcolor("normal"),
}) })

View file

@ -340,4 +340,3 @@ if __name__ == "__main__":
else: else:
file = args[1] file = args[1]
riExec(id, file) riExec(id, file)

View file

@ -10,14 +10,14 @@ class Separator:
self.dim = "width" self.dim = "width"
self.dir = "x" self.dir = "x"
self.cursor = "sb_h_double_arrow" self.cursor = "sb_h_double_arrow"
elif orient in ("v", "vertical"): elif orient in ("v", "vertical"):
self.side = "top" self.side = "top"
self.dim = "height" self.dim = "height"
self.dir = "y" self.dir = "y"
self.cursor = "sb_v_double_arrow" self.cursor = "sb_v_double_arrow"
else: else:
raise ValueError, "Separator: orient should be h or v" raise ValueError, "Separator: orient should be h or v"
self.winfo_dim = "winfo_" + self.dim self.winfo_dim = "winfo_" + self.dim
self.master = master = Frame(master) self.master = master = Frame(master)
master.pack(expand=1, fill="both") master.pack(expand=1, fill="both")
self.f1 = Frame(master) self.f1 = Frame(master)

View file

@ -39,7 +39,7 @@ class WindowList:
def unregister_callback(self, callback): def unregister_callback(self, callback):
try: try:
self.callbacks.remove(callback) self.callbacks.remove(callback)
except ValueError: except ValueError:
pass pass

View file

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

View file

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

View file

@ -4,9 +4,9 @@
# #
# Fix Python source files to use the new class definition syntax, i.e., # Fix Python source files to use the new class definition syntax, i.e.,
# the syntax used in Python versions before 0.9.8: # the syntax used in Python versions before 0.9.8:
# class C() = base(), base(), ...: ... # class C() = base(), base(), ...: ...
# is changed to the current syntax: # is changed to the current syntax:
# class C(base, base, ...): ... # class C(base, base, ...): ...
# #
# The script uses heuristics to find class definitions that usually # The script uses heuristics to find class definitions that usually
# work but occasionally can fail; carefully check the output! # work but occasionally can fail; carefully check the output!
@ -39,113 +39,113 @@ dbg = err
rep = sys.stdout.write rep = sys.stdout.write
def main(): def main():
bad = 0 bad = 0
if not sys.argv[1:]: # No arguments if not sys.argv[1:]: # No arguments
err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')
sys.exit(2) sys.exit(2)
for arg in sys.argv[1:]: for arg in sys.argv[1:]:
if os.path.isdir(arg): if os.path.isdir(arg):
if recursedown(arg): bad = 1 if recursedown(arg): bad = 1
elif os.path.islink(arg): elif os.path.islink(arg):
err(arg + ': will not process symbolic links\n') err(arg + ': will not process symbolic links\n')
bad = 1 bad = 1
else: else:
if fix(arg): bad = 1 if fix(arg): bad = 1
sys.exit(bad) sys.exit(bad)
ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$') ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
def ispython(name): def ispython(name):
return ispythonprog.match(name) >= 0 return ispythonprog.match(name) >= 0
def recursedown(dirname): def recursedown(dirname):
dbg('recursedown(' + `dirname` + ')\n') dbg('recursedown(' + `dirname` + ')\n')
bad = 0 bad = 0
try: try:
names = os.listdir(dirname) names = os.listdir(dirname)
except os.error, msg: except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n') err(dirname + ': cannot list directory: ' + `msg` + '\n')
return 1 return 1
names.sort() names.sort()
subdirs = [] subdirs = []
for name in names: for name in names:
if name in (os.curdir, os.pardir): continue if name in (os.curdir, os.pardir): continue
fullname = os.path.join(dirname, name) fullname = os.path.join(dirname, name)
if os.path.islink(fullname): pass if os.path.islink(fullname): pass
elif os.path.isdir(fullname): elif os.path.isdir(fullname):
subdirs.append(fullname) subdirs.append(fullname)
elif ispython(name): elif ispython(name):
if fix(fullname): bad = 1 if fix(fullname): bad = 1
for fullname in subdirs: for fullname in subdirs:
if recursedown(fullname): bad = 1 if recursedown(fullname): bad = 1
return bad return bad
def fix(filename): def fix(filename):
## dbg('fix(' + `filename` + ')\n') ## dbg('fix(' + `filename` + ')\n')
try: try:
f = open(filename, 'r') f = open(filename, 'r')
except IOError, msg: except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n') err(filename + ': cannot open: ' + `msg` + '\n')
return 1 return 1
head, tail = os.path.split(filename) head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail) tempname = os.path.join(head, '@' + tail)
g = None g = None
# If we find a match, we rewind the file and start over but # If we find a match, we rewind the file and start over but
# now copy everything to a temp file. # now copy everything to a temp file.
lineno = 0 lineno = 0
while 1: while 1:
line = f.readline() line = f.readline()
if not line: break if not line: break
lineno = lineno + 1 lineno = lineno + 1
while line[-2:] == '\\\n': while line[-2:] == '\\\n':
nextline = f.readline() nextline = f.readline()
if not nextline: break if not nextline: break
line = line + nextline line = line + nextline
lineno = lineno + 1 lineno = lineno + 1
newline = fixline(line) newline = fixline(line)
if newline != line: if newline != line:
if g is None: if g is None:
try: try:
g = open(tempname, 'w') g = open(tempname, 'w')
except IOError, msg: except IOError, msg:
f.close() f.close()
err(tempname+': cannot create: '+\ err(tempname+': cannot create: '+\
`msg`+'\n') `msg`+'\n')
return 1 return 1
f.seek(0) f.seek(0)
lineno = 0 lineno = 0
rep(filename + ':\n') rep(filename + ':\n')
continue # restart from the beginning continue # restart from the beginning
rep(`lineno` + '\n') rep(`lineno` + '\n')
rep('< ' + line) rep('< ' + line)
rep('> ' + newline) rep('> ' + newline)
if g is not None: if g is not None:
g.write(newline) g.write(newline)
# End of file # End of file
f.close() f.close()
if not g: return 0 # No changes 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 # First copy the file's mode to the temp file
try: try:
statbuf = os.stat(filename) statbuf = os.stat(filename)
os.chmod(tempname, statbuf[ST_MODE] & 07777) os.chmod(tempname, statbuf[ST_MODE] & 07777)
except os.error, msg: except os.error, msg:
err(tempname + ': warning: chmod failed (' + `msg` + ')\n') err(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~ # Then make a backup of the original file as filename~
try: try:
os.rename(filename, filename + '~') os.rename(filename, filename + '~')
except os.error, msg: except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n') err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file # Now move the temp file to the original file
try: try:
os.rename(tempname, filename) os.rename(tempname, filename)
except os.error, msg: except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n') err(filename + ': rename failed (' + `msg` + ')\n')
return 1 return 1
# Return succes # Return succes
return 0 return 0
# This expression doesn't catch *all* class definition headers, # This expression doesn't catch *all* class definition headers,
# but it's pretty darn close. # but it's pretty darn close.
@ -159,34 +159,34 @@ baseprog = regex.compile(baseexpr)
import string import string
def fixline(line): def fixline(line):
if classprog.match(line) < 0: # No 'class' keyword -- no change if classprog.match(line) < 0: # No 'class' keyword -- no change
return line return line
(a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3] (a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]
# a0, b0 = Whole match (up to ':') # a0, b0 = Whole match (up to ':')
# a1, b1 = First subexpression (up to classname) # a1, b1 = First subexpression (up to classname)
# a2, b2 = Second subexpression (=.*) # a2, b2 = Second subexpression (=.*)
head = line[:b1] head = line[:b1]
tail = line[b0:] # Unmatched rest of line tail = line[b0:] # Unmatched rest of line
if a2 == b2: # No base classes -- easy case if a2 == b2: # No base classes -- easy case
return head + ':' + tail return head + ':' + tail
# Get rid of leading '=' # Get rid of leading '='
basepart = line[a2+1:b2] basepart = line[a2+1:b2]
# Extract list of base expressions # Extract list of base expressions
bases = string.splitfields(basepart, ',') bases = string.splitfields(basepart, ',')
# Strip trailing '()' from each base expression # Strip trailing '()' from each base expression
for i in range(len(bases)): for i in range(len(bases)):
if baseprog.match(bases[i]) >= 0: if baseprog.match(bases[i]) >= 0:
x1, y1 = baseprog.regs[1] x1, y1 = baseprog.regs[1]
bases[i] = bases[i][x1:y1] bases[i] = bases[i][x1:y1]
# Join the bases back again and build the new line # Join the bases back again and build the new line
basepart = string.joinfields(bases, ', ') basepart = string.joinfields(bases, ', ')
return head + '(' + basepart + '):' + tail return head + '(' + basepart + '):' + tail
main() main()

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -35,79 +35,79 @@ defs = []
undefs = [] undefs = []
def main(): def main():
opts, args = getopt.getopt(sys.argv[1:], 'D:U:') opts, args = getopt.getopt(sys.argv[1:], 'D:U:')
for o, a in opts: for o, a in opts:
if o == '-D': if o == '-D':
defs.append(a) defs.append(a)
if o == '-U': if o == '-U':
undefs.append(a) undefs.append(a)
if not args: if not args:
args = ['-'] args = ['-']
for file in args: for file in args:
if file == '-': if file == '-':
process(sys.stdin, sys.stdout) process(sys.stdin, sys.stdout)
else: else:
f = open(file, 'r') f = open(file, 'r')
process(f, sys.stdout) process(f, sys.stdout)
f.close() f.close()
def process(fpi, fpo): def process(fpi, fpo):
keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif') keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif')
ok = 1 ok = 1
stack = [] stack = []
while 1: while 1:
line = fpi.readline() line = fpi.readline()
if not line: break if not line: break
while line[-2:] == '\\\n': while line[-2:] == '\\\n':
nextline = fpi.readline() nextline = fpi.readline()
if not nextline: break if not nextline: break
line = line + nextline line = line + nextline
tmp = string.strip(line) tmp = string.strip(line)
if tmp[:1] != '#': if tmp[:1] != '#':
if ok: fpo.write(line) if ok: fpo.write(line)
continue continue
tmp = string.strip(tmp[1:]) tmp = string.strip(tmp[1:])
words = string.split(tmp) words = string.split(tmp)
keyword = words[0] keyword = words[0]
if keyword not in keywords: if keyword not in keywords:
if ok: fpo.write(line) if ok: fpo.write(line)
continue continue
if keyword in ('ifdef', 'ifndef') and len(words) == 2: if keyword in ('ifdef', 'ifndef') and len(words) == 2:
if keyword == 'ifdef': if keyword == 'ifdef':
ko = 1 ko = 1
else: else:
ko = 0 ko = 0
word = words[1] word = words[1]
if word in defs: if word in defs:
stack.append((ok, ko, word)) stack.append((ok, ko, word))
if not ko: ok = 0 if not ko: ok = 0
elif word in undefs: elif word in undefs:
stack.append((ok, not ko, word)) stack.append((ok, not ko, word))
if ko: ok = 0 if ko: ok = 0
else: else:
stack.append((ok, -1, word)) stack.append((ok, -1, word))
if ok: fpo.write(line) if ok: fpo.write(line)
elif keyword == 'if': elif keyword == 'if':
stack.append((ok, -1, '')) stack.append((ok, -1, ''))
if ok: fpo.write(line) if ok: fpo.write(line)
elif keyword == 'else' and stack: elif keyword == 'else' and stack:
s_ok, s_ko, s_word = stack[-1] s_ok, s_ko, s_word = stack[-1]
if s_ko < 0: if s_ko < 0:
if ok: fpo.write(line) if ok: fpo.write(line)
else: else:
s_ko = not s_ko s_ko = not s_ko
ok = s_ok ok = s_ok
if not s_ko: ok = 0 if not s_ko: ok = 0
stack[-1] = s_ok, s_ko, s_word stack[-1] = s_ok, s_ko, s_word
elif keyword == 'endif' and stack: elif keyword == 'endif' and stack:
s_ok, s_ko, s_word = stack[-1] s_ok, s_ko, s_word = stack[-1]
if s_ko < 0: if s_ko < 0:
if ok: fpo.write(line) if ok: fpo.write(line)
del stack[-1] del stack[-1]
ok = s_ok ok = s_ok
else: else:
sys.stderr.write('Unknown keyword %s\n' % keyword) sys.stderr.write('Unknown keyword %s\n' % keyword)
if stack: if stack:
sys.stderr.write('stack: %s\n' % stack) sys.stderr.write('stack: %s\n' % stack)
main() main()

View file

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

View file

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

View file

@ -1,9 +1,9 @@
#! /usr/bin/env python #! /usr/bin/env python
# Fix Python source files to avoid using # Fix Python source files to avoid using
# def method(self, (arg1, ..., argn)): # def method(self, (arg1, ..., argn)):
# instead of the more rational # 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. # Command line arguments are files or directories to be processed.
# Directories are searched recursively for files whose name looks # Directories are searched recursively for files whose name looks
@ -37,137 +37,137 @@ dbg = err
rep = sys.stdout.write rep = sys.stdout.write
def main(): def main():
bad = 0 bad = 0
if not sys.argv[1:]: # No arguments if not sys.argv[1:]: # No arguments
err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')
sys.exit(2) sys.exit(2)
for arg in sys.argv[1:]: for arg in sys.argv[1:]:
if os.path.isdir(arg): if os.path.isdir(arg):
if recursedown(arg): bad = 1 if recursedown(arg): bad = 1
elif os.path.islink(arg): elif os.path.islink(arg):
err(arg + ': will not process symbolic links\n') err(arg + ': will not process symbolic links\n')
bad = 1 bad = 1
else: else:
if fix(arg): bad = 1 if fix(arg): bad = 1
sys.exit(bad) sys.exit(bad)
ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$') ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
def ispython(name): def ispython(name):
return ispythonprog.match(name) >= 0 return ispythonprog.match(name) >= 0
def recursedown(dirname): def recursedown(dirname):
dbg('recursedown(' + `dirname` + ')\n') dbg('recursedown(' + `dirname` + ')\n')
bad = 0 bad = 0
try: try:
names = os.listdir(dirname) names = os.listdir(dirname)
except os.error, msg: except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n') err(dirname + ': cannot list directory: ' + `msg` + '\n')
return 1 return 1
names.sort() names.sort()
subdirs = [] subdirs = []
for name in names: for name in names:
if name in (os.curdir, os.pardir): continue if name in (os.curdir, os.pardir): continue
fullname = os.path.join(dirname, name) fullname = os.path.join(dirname, name)
if os.path.islink(fullname): pass if os.path.islink(fullname): pass
elif os.path.isdir(fullname): elif os.path.isdir(fullname):
subdirs.append(fullname) subdirs.append(fullname)
elif ispython(name): elif ispython(name):
if fix(fullname): bad = 1 if fix(fullname): bad = 1
for fullname in subdirs: for fullname in subdirs:
if recursedown(fullname): bad = 1 if recursedown(fullname): bad = 1
return bad return bad
def fix(filename): def fix(filename):
## dbg('fix(' + `filename` + ')\n') ## dbg('fix(' + `filename` + ')\n')
try: try:
f = open(filename, 'r') f = open(filename, 'r')
except IOError, msg: except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n') err(filename + ': cannot open: ' + `msg` + '\n')
return 1 return 1
head, tail = os.path.split(filename) head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail) tempname = os.path.join(head, '@' + tail)
g = None g = None
# If we find a match, we rewind the file and start over but # If we find a match, we rewind the file and start over but
# now copy everything to a temp file. # now copy everything to a temp file.
lineno = 0 lineno = 0
while 1: while 1:
line = f.readline() line = f.readline()
if not line: break if not line: break
lineno = lineno + 1 lineno = lineno + 1
if g is None and '\0' in line: if g is None and '\0' in line:
# Check for binary files # Check for binary files
err(filename + ': contains null bytes; not fixed\n') err(filename + ': contains null bytes; not fixed\n')
f.close() f.close()
return 1 return 1
if lineno == 1 and g is None and line[:2] == '#!': if lineno == 1 and g is None and line[:2] == '#!':
# Check for non-Python scripts # Check for non-Python scripts
words = string.split(line[2:]) words = string.split(line[2:])
if words and regex.search('[pP]ython', words[0]) < 0: if words and regex.search('[pP]ython', words[0]) < 0:
msg = filename + ': ' + words[0] msg = filename + ': ' + words[0]
msg = msg + ' script; not fixed\n' msg = msg + ' script; not fixed\n'
err(msg) err(msg)
f.close() f.close()
return 1 return 1
while line[-2:] == '\\\n': while line[-2:] == '\\\n':
nextline = f.readline() nextline = f.readline()
if not nextline: break if not nextline: break
line = line + nextline line = line + nextline
lineno = lineno + 1 lineno = lineno + 1
newline = fixline(line) newline = fixline(line)
if newline != line: if newline != line:
if g is None: if g is None:
try: try:
g = open(tempname, 'w') g = open(tempname, 'w')
except IOError, msg: except IOError, msg:
f.close() f.close()
err(tempname+': cannot create: '+\ err(tempname+': cannot create: '+\
`msg`+'\n') `msg`+'\n')
return 1 return 1
f.seek(0) f.seek(0)
lineno = 0 lineno = 0
rep(filename + ':\n') rep(filename + ':\n')
continue # restart from the beginning continue # restart from the beginning
rep(`lineno` + '\n') rep(`lineno` + '\n')
rep('< ' + line) rep('< ' + line)
rep('> ' + newline) rep('> ' + newline)
if g is not None: if g is not None:
g.write(newline) g.write(newline)
# End of file # End of file
f.close() f.close()
if not g: return 0 # No changes 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 # First copy the file's mode to the temp file
try: try:
statbuf = os.stat(filename) statbuf = os.stat(filename)
os.chmod(tempname, statbuf[ST_MODE] & 07777) os.chmod(tempname, statbuf[ST_MODE] & 07777)
except os.error, msg: except os.error, msg:
err(tempname + ': warning: chmod failed (' + `msg` + ')\n') err(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~ # Then make a backup of the original file as filename~
try: try:
os.rename(filename, filename + '~') os.rename(filename, filename + '~')
except os.error, msg: except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n') err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file # Now move the temp file to the original file
try: try:
os.rename(tempname, filename) os.rename(tempname, filename)
except os.error, msg: except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n') err(filename + ': rename failed (' + `msg` + ')\n')
return 1 return 1
# Return succes # Return succes
return 0 return 0
fixpat = '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *\(( *\(.*\) *)\) *) *:' fixpat = '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *\(( *\(.*\) *)\) *) *:'
fixprog = regex.compile(fixpat) fixprog = regex.compile(fixpat)
def fixline(line): def fixline(line):
if fixprog.match(line) >= 0: if fixprog.match(line) >= 0:
(a, b), (c, d) = fixprog.regs[1:3] (a, b), (c, d) = fixprog.regs[1:3]
line = line[:a] + line[c:d] + line[b:] line = line[:a] + line[c:d] + line[b:]
return line return line
main() main()

View file

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

View file

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

View file

@ -62,4 +62,3 @@ if __name__ == '__main__':
text = infile.read() text = infile.read()
defs = parse(text) defs = parse(text)
writefile(outfile,defs) writefile(outfile,defs)

View file

@ -33,117 +33,117 @@ rep = sys.stdout.write
new_interpreter = None new_interpreter = None
def main(): def main():
global new_interpreter global new_interpreter
usage = ('usage: %s -i /interpreter file-or-directory ...\n' % usage = ('usage: %s -i /interpreter file-or-directory ...\n' %
sys.argv[0]) sys.argv[0])
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'i:') opts, args = getopt.getopt(sys.argv[1:], 'i:')
except getopt.error, msg: except getopt.error, msg:
err(msg + '\n') err(msg + '\n')
err(usage) err(usage)
sys.exit(2) sys.exit(2)
for o, a in opts: for o, a in opts:
if o == '-i': if o == '-i':
new_interpreter = a new_interpreter = a
if not new_interpreter or new_interpreter[0] != '/' or not args: if not new_interpreter or new_interpreter[0] != '/' or not args:
err('-i option or file-or-directory missing\n') err('-i option or file-or-directory missing\n')
err(usage) err(usage)
sys.exit(2) sys.exit(2)
bad = 0 bad = 0
for arg in args: for arg in args:
if os.path.isdir(arg): if os.path.isdir(arg):
if recursedown(arg): bad = 1 if recursedown(arg): bad = 1
elif os.path.islink(arg): elif os.path.islink(arg):
err(arg + ': will not process symbolic links\n') err(arg + ': will not process symbolic links\n')
bad = 1 bad = 1
else: else:
if fix(arg): bad = 1 if fix(arg): bad = 1
sys.exit(bad) sys.exit(bad)
ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$') ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
def ispython(name): def ispython(name):
return ispythonprog.match(name) >= 0 return ispythonprog.match(name) >= 0
def recursedown(dirname): def recursedown(dirname):
dbg('recursedown(' + `dirname` + ')\n') dbg('recursedown(' + `dirname` + ')\n')
bad = 0 bad = 0
try: try:
names = os.listdir(dirname) names = os.listdir(dirname)
except os.error, msg: except os.error, msg:
err(dirname + ': cannot list directory: ' + `msg` + '\n') err(dirname + ': cannot list directory: ' + `msg` + '\n')
return 1 return 1
names.sort() names.sort()
subdirs = [] subdirs = []
for name in names: for name in names:
if name in (os.curdir, os.pardir): continue if name in (os.curdir, os.pardir): continue
fullname = os.path.join(dirname, name) fullname = os.path.join(dirname, name)
if os.path.islink(fullname): pass if os.path.islink(fullname): pass
elif os.path.isdir(fullname): elif os.path.isdir(fullname):
subdirs.append(fullname) subdirs.append(fullname)
elif ispython(name): elif ispython(name):
if fix(fullname): bad = 1 if fix(fullname): bad = 1
for fullname in subdirs: for fullname in subdirs:
if recursedown(fullname): bad = 1 if recursedown(fullname): bad = 1
return bad return bad
def fix(filename): def fix(filename):
## dbg('fix(' + `filename` + ')\n') ## dbg('fix(' + `filename` + ')\n')
try: try:
f = open(filename, 'r') f = open(filename, 'r')
except IOError, msg: except IOError, msg:
err(filename + ': cannot open: ' + `msg` + '\n') err(filename + ': cannot open: ' + `msg` + '\n')
return 1 return 1
line = f.readline() line = f.readline()
fixed = fixline(line) fixed = fixline(line)
if line == fixed: if line == fixed:
rep(filename+': no change\n') rep(filename+': no change\n')
f.close() f.close()
return return
head, tail = os.path.split(filename) head, tail = os.path.split(filename)
tempname = os.path.join(head, '@' + tail) tempname = os.path.join(head, '@' + tail)
try: try:
g = open(tempname, 'w') g = open(tempname, 'w')
except IOError, msg: except IOError, msg:
f.close() f.close()
err(tempname+': cannot create: '+`msg`+'\n') err(tempname+': cannot create: '+`msg`+'\n')
return 1 return 1
rep(filename + ': updating\n') rep(filename + ': updating\n')
g.write(fixed) g.write(fixed)
BUFSIZE = 8*1024 BUFSIZE = 8*1024
while 1: while 1:
buf = f.read(BUFSIZE) buf = f.read(BUFSIZE)
if not buf: break if not buf: break
g.write(buf) g.write(buf)
g.close() g.close()
f.close() f.close()
# Finishing touch -- move files # Finishing touch -- move files
# First copy the file's mode to the temp file # First copy the file's mode to the temp file
try: try:
statbuf = os.stat(filename) statbuf = os.stat(filename)
os.chmod(tempname, statbuf[ST_MODE] & 07777) os.chmod(tempname, statbuf[ST_MODE] & 07777)
except os.error, msg: except os.error, msg:
err(tempname + ': warning: chmod failed (' + `msg` + ')\n') err(tempname + ': warning: chmod failed (' + `msg` + ')\n')
# Then make a backup of the original file as filename~ # Then make a backup of the original file as filename~
try: try:
os.rename(filename, filename + '~') os.rename(filename, filename + '~')
except os.error, msg: except os.error, msg:
err(filename + ': warning: backup failed (' + `msg` + ')\n') err(filename + ': warning: backup failed (' + `msg` + ')\n')
# Now move the temp file to the original file # Now move the temp file to the original file
try: try:
os.rename(tempname, filename) os.rename(tempname, filename)
except os.error, msg: except os.error, msg:
err(filename + ': rename failed (' + `msg` + ')\n') err(filename + ': rename failed (' + `msg` + ')\n')
return 1 return 1
# Return succes # Return succes
return 0 return 0
def fixline(line): def fixline(line):
if line[:2] != '#!': if line[:2] != '#!':
return line return line
if string.find(line, "python") < 0: if string.find(line, "python") < 0:
return line return line
return '#! %s\n' % new_interpreter return '#! %s\n' % new_interpreter
main() main()

View file

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

View file

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

View file

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

View file

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

View file

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

@ -52,7 +52,7 @@ spprog = re.compile('[\n@{}&<>]') # Special characters in
miprog = re.compile('^\* ([^:]*):(:|[ \t]*([^\t,\n.]+)([^ \t\n]*))[ \t\n]*') miprog = re.compile('^\* ([^:]*):(:|[ \t]*([^\t,\n.]+)([^ \t\n]*))[ \t\n]*')
class HTMLNode: class HTMLNode:
"""Some of the parser's functionality is separated into this class. """Some of the parser's functionality is separated into this class.

View file

@ -11,7 +11,7 @@ import sys, os, string
from stat import * from stat import *
def msg(str): def msg(str):
sys.stderr.write(str + '\n') sys.stderr.write(str + '\n')
pathlist = string.splitfields(os.environ['PATH'], ':') pathlist = string.splitfields(os.environ['PATH'], ':')
@ -19,38 +19,38 @@ sts = 0
longlist = '' longlist = ''
if sys.argv[1:] and sys.argv[1][:2] == '-l': if sys.argv[1:] and sys.argv[1][:2] == '-l':
longlist = sys.argv[1] longlist = sys.argv[1]
del sys.argv[1] del sys.argv[1]
for prog in sys.argv[1:]: for prog in sys.argv[1:]:
ident = () ident = ()
for dir in pathlist: for dir in pathlist:
file = os.path.join(dir, prog) file = os.path.join(dir, prog)
try: try:
st = os.stat(file) st = os.stat(file)
except os.error: except os.error:
continue continue
if not S_ISREG(st[ST_MODE]): if not S_ISREG(st[ST_MODE]):
msg(file + ': not a disk file') msg(file + ': not a disk file')
else: else:
mode = S_IMODE(st[ST_MODE]) mode = S_IMODE(st[ST_MODE])
if mode & 0111: if mode & 0111:
if not ident: if not ident:
print file print file
ident = st[:3] ident = st[:3]
else: else:
if st[:3] == ident: if st[:3] == ident:
s = 'same as: ' s = 'same as: '
else: else:
s = 'also: ' s = 'also: '
msg(s + file) msg(s + file)
else: else:
msg(file + ': not executable') msg(file + ': not executable')
if longlist: if longlist:
sts = os.system('ls ' + longlist + ' ' + file) sts = os.system('ls ' + longlist + ' ' + file)
if sts: msg('"ls -l" exit status: ' + `sts`) if sts: msg('"ls -l" exit status: ' + `sts`)
if not ident: if not ident:
msg(prog + ': not found') msg(prog + ': not found')
sts = 1 sts = 1
sys.exit(sts) 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. MAXSIZE = 200*1024 # Files this big must be binaries and are skipped.
def getargs(): def getargs():
args = sys.argv[1:] args = sys.argv[1:]
if args: if args:
return args return args
print 'No arguments, checking almost *, in "ls -t" order' print 'No arguments, checking almost *, in "ls -t" order'
list = [] list = []
for file in os.listdir(os.curdir): for file in os.listdir(os.curdir):
if not skipfile(file): if not skipfile(file):
list.append((getmtime(file), file)) list.append((getmtime(file), file))
list.sort() list.sort()
if not list: if not list:
print 'Nothing to do -- exit 1' print 'Nothing to do -- exit 1'
sys.exit(1) sys.exit(1)
list.sort() list.sort()
list.reverse() list.reverse()
for mtime, file in list: args.append(file) for mtime, file in list: args.append(file)
return args return args
def getmtime(file): def getmtime(file):
try: try:
st = os.stat(file) st = os.stat(file)
return st[ST_MTIME] return st[ST_MTIME]
except os.error: except os.error:
return -1 return -1
badnames = ['tags', 'TAGS', 'xyzzy', 'nohup.out', 'core'] badnames = ['tags', 'TAGS', 'xyzzy', 'nohup.out', 'core']
badprefixes = ['.', ',', '@', '#', 'o.'] badprefixes = ['.', ',', '@', '#', 'o.']
badsuffixes = \ badsuffixes = \
['~', '.a', '.o', '.old', '.bak', '.orig', '.new', '.prev', '.not', \ ['~', '.a', '.o', '.old', '.bak', '.orig', '.new', '.prev', '.not', \
'.pyc', '.fdc', '.rgb', '.elc', ',v'] '.pyc', '.fdc', '.rgb', '.elc', ',v']
ignore = [] ignore = []
def setup(): def setup():
ignore[:] = badnames ignore[:] = badnames
for p in badprefixes: for p in badprefixes:
ignore.append(p + '*') ignore.append(p + '*')
for p in badsuffixes: for p in badsuffixes:
ignore.append('*' + p) ignore.append('*' + p)
try: try:
f = open('.xxcign', 'r') f = open('.xxcign', 'r')
except IOError: except IOError:
return return
ignore[:] = ignore + string.split(f.read()) ignore[:] = ignore + string.split(f.read())
def skipfile(file): def skipfile(file):
for p in ignore: for p in ignore:
if fnmatch.fnmatch(file, p): return 1 if fnmatch.fnmatch(file, p): return 1
try: try:
st = os.lstat(file) st = os.lstat(file)
except os.error: except os.error:
return 1 # Doesn't exist -- skip it return 1 # Doesn't exist -- skip it
# Skip non-plain files. # Skip non-plain files.
if not S_ISREG(st[ST_MODE]): return 1 if not S_ISREG(st[ST_MODE]): return 1
# Skip huge files -- probably binaries. # Skip huge files -- probably binaries.
if st[ST_SIZE] >= MAXSIZE: return 1 if st[ST_SIZE] >= MAXSIZE: return 1
# Skip executables # Skip executables
try: try:
data = open(file, 'r').read(len(EXECMAGIC)) data = open(file, 'r').read(len(EXECMAGIC))
if data == EXECMAGIC: return 1 if data == EXECMAGIC: return 1
except: except:
pass pass
return 0 return 0
def badprefix(file): def badprefix(file):
for bad in badprefixes: for bad in badprefixes:
if file[:len(bad)] == bad: return 1 if file[:len(bad)] == bad: return 1
return 0 return 0
def badsuffix(file): def badsuffix(file):
for bad in badsuffixes: for bad in badsuffixes:
if file[-len(bad):] == bad: return 1 if file[-len(bad):] == bad: return 1
return 0 return 0
def go(args): def go(args):
for file in args: for file in args:
print file + ':' print file + ':'
if differing(file): if differing(file):
showdiffs(file) showdiffs(file)
if askyesno('Check in ' + file + ' ? '): if askyesno('Check in ' + file + ' ? '):
sts = os.system('rcs -l ' + file) # ignored sts = os.system('rcs -l ' + file) # ignored
sts = os.system('ci -l ' + file) sts = os.system('ci -l ' + file)
def differing(file): def differing(file):
cmd = 'co -p ' + file + ' 2>/dev/null | cmp -s - ' + file cmd = 'co -p ' + file + ' 2>/dev/null | cmp -s - ' + file
sts = os.system(cmd) sts = os.system(cmd)
return sts != 0 return sts != 0
def showdiffs(file): def showdiffs(file):
cmd = 'rcsdiff ' + file + ' 2>&1 | ${PAGER-more}' cmd = 'rcsdiff ' + file + ' 2>&1 | ${PAGER-more}'
sts = os.system(cmd) sts = os.system(cmd)
def askyesno(prompt): def askyesno(prompt):
s = raw_input(prompt) s = raw_input(prompt)
return s in ['y', 'yes'] return s in ['y', 'yes']
try: try:
setup() setup()
go(getargs()) go(getargs())
except KeyboardInterrupt: except KeyboardInterrupt:
print '[Intr]' print '[Intr]'