Commit f62f6874 authored by Guido van Rossum's avatar Guido van Rossum

Various changes, andded -c option

parent 4e8aef84
...@@ -47,19 +47,23 @@ dbg = err ...@@ -47,19 +47,23 @@ dbg = err
rep = sys.stdout.write rep = sys.stdout.write
def usage(): def usage():
err('Usage: ' + sys.argv[0] + \ progname = sys.argv[0]
' [-r] [-s file] ... file-or-directory ...\n') err('Usage: ' + progname +
' [-c] [-r] [-s file] ... file-or-directory ...\n')
err('\n') err('\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('inside a comment even when -c is specified.\n')
def main(): def main():
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'rs:') 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()
...@@ -69,6 +73,8 @@ def main(): ...@@ -69,6 +73,8 @@ def main():
usage() usage()
sys.exit(2) sys.exit(2)
for opt, arg in opts: for opt, arg in opts:
if opt == '-c':
setdocomments()
if opt == '-r': if opt == '-r':
setreverse() setreverse()
if opt == '-s': if opt == '-s':
...@@ -146,7 +152,7 @@ def fix(filename): ...@@ -146,7 +152,7 @@ def fix(filename):
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)
...@@ -239,13 +245,15 @@ def fixline(line): ...@@ -239,13 +245,15 @@ def fixline(line):
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:
print 'Found in comment:', found
continue
if NotInComment.has_key(found): if NotInComment.has_key(found):
pass
## 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,
...@@ -254,6 +262,11 @@ def fixline(line): ...@@ -254,6 +262,11 @@ def fixline(line):
i = i + n i = i + n
return line return line
Docomments = 0
def setdocomments():
global Docomments
Docomments = 1
Reverse = 0 Reverse = 0
def setreverse(): def setreverse():
global Reverse global Reverse
...@@ -279,7 +292,7 @@ def addsubst(substfile): ...@@ -279,7 +292,7 @@ def addsubst(substfile):
words = string.split(line[:i]) words = string.split(line[:i])
if not words: continue if not words: continue
if len(words) <> 2: if len(words) <> 2:
err(substfile + ':' + `lineno` + \ err(substfile + ':' + `lineno` +
': warning: bad line: ' + line) ': warning: bad line: ' + line)
continue continue
if Reverse: if Reverse:
...@@ -292,10 +305,10 @@ def addsubst(substfile): ...@@ -292,10 +305,10 @@ def addsubst(substfile):
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()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment