diff options
author | Andrew M. Kuchling <amk@amk.ca> | 2004-08-09 17:27:55 (GMT) |
---|---|---|
committer | Andrew M. Kuchling <amk@amk.ca> | 2004-08-09 17:27:55 (GMT) |
commit | e236b38731823aeb87bb6d101062f54a72044954 (patch) | |
tree | 6283b4999bb585c11ae1dfeb87dfb6e083f78e6c | |
parent | 6c542b731cfe3e17991643bfc41bb785331e7e5c (diff) | |
download | cpython-e236b38731823aeb87bb6d101062f54a72044954.zip cpython-e236b38731823aeb87bb6d101062f54a72044954.tar.gz cpython-e236b38731823aeb87bb6d101062f54a72044954.tar.bz2 |
[Patch #1005491 ] use __name__ == '__main__' in scripts
34 files changed, 268 insertions, 211 deletions
diff --git a/Tools/scripts/byteyears.py b/Tools/scripts/byteyears.py index b2a114f..ba38caf 100755 --- a/Tools/scripts/byteyears.py +++ b/Tools/scripts/byteyears.py @@ -9,48 +9,53 @@ import sys, os, time from stat import * -# Use lstat() to stat files if it exists, else stat() -try: - statfunc = os.lstat -except AttributeError: - statfunc = os.stat - -# Parse options -if sys.argv[1] == '-m': - itime = ST_MTIME - del sys.argv[1] -elif sys.argv[1] == '-c': - itime = ST_CTIME - del sys.argv[1] -elif sys.argv[1] == '-a': - itime = ST_CTIME - del sys.argv[1] -else: - itime = ST_MTIME - -secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor -now = time.time() # Current time, for age computations -status = 0 # Exit status, set to 1 on errors - -# Compute max file name length -maxlen = 1 -for filename in sys.argv[1:]: - maxlen = max(maxlen, len(filename)) - -# Process each argument in turn -for filename in sys.argv[1:]: +def main(): + + # Use lstat() to stat files if it exists, else stat() try: - st = statfunc(filename) - except os.error, msg: - sys.stderr.write("can't stat %r: %r\n" % (filename, msg)) - status = 1 - st = () - if st: - anytime = st[itime] - size = st[ST_SIZE] - age = now - anytime - byteyears = float(size) * float(age) / secs_per_year - print filename.ljust(maxlen), - print repr(int(byteyears)).rjust(8) - -sys.exit(status) + statfunc = os.lstat + except AttributeError: + statfunc = os.stat + + # Parse options + if sys.argv[1] == '-m': + itime = ST_MTIME + del sys.argv[1] + elif sys.argv[1] == '-c': + itime = ST_CTIME + del sys.argv[1] + elif sys.argv[1] == '-a': + itime = ST_CTIME + del sys.argv[1] + else: + itime = ST_MTIME + + secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor + now = time.time() # Current time, for age computations + status = 0 # Exit status, set to 1 on errors + + # Compute max file name length + maxlen = 1 + for filename in sys.argv[1:]: + maxlen = max(maxlen, len(filename)) + + # Process each argument in turn + for filename in sys.argv[1:]: + try: + st = statfunc(filename) + except os.error, msg: + sys.stderr.write("can't stat %r: %r\n" % (filename, msg)) + status = 1 + st = () + if st: + anytime = st[itime] + size = st[ST_SIZE] + age = now - anytime + byteyears = float(size) * float(age) / secs_per_year + print filename.ljust(maxlen), + print repr(int(byteyears)).rjust(8) + + sys.exit(status) + +if __name__ == '__main__': + main() diff --git a/Tools/scripts/checkpyc.py b/Tools/scripts/checkpyc.py index d5f3c7a..8af7672 100755 --- a/Tools/scripts/checkpyc.py +++ b/Tools/scripts/checkpyc.py @@ -62,4 +62,5 @@ def get_long(s): return -1 return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/classfix.py b/Tools/scripts/classfix.py index 520b352..cdf006a 100755 --- a/Tools/scripts/classfix.py +++ b/Tools/scripts/classfix.py @@ -186,4 +186,5 @@ def fixline(line): return head + '(' + basepart + '):' + tail -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/copytime.py b/Tools/scripts/copytime.py index 74c29c9..209c492 100755 --- a/Tools/scripts/copytime.py +++ b/Tools/scripts/copytime.py @@ -22,4 +22,5 @@ def main(): sys.stderr.write(file2 + ': cannot change time\n') sys.exit(2) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/crlf.py b/Tools/scripts/crlf.py index 4fb8be3..03908b1 100755 --- a/Tools/scripts/crlf.py +++ b/Tools/scripts/crlf.py @@ -1,19 +1,24 @@ #! /usr/bin/env python - "Replace CRLF with LF in argument files. Print names of changed files." import sys, os -for filename in sys.argv[1:]: - if os.path.isdir(filename): - print filename, "Directory!" - continue - data = open(filename, "rb").read() - if '\0' in data: - print filename, "Binary!" - continue - newdata = data.replace("\r\n", "\n") - if newdata != data: - print filename - f = open(filename, "wb") - f.write(newdata) - f.close() + +def main(): + for filename in sys.argv[1:]: + if os.path.isdir(filename): + print filename, "Directory!" + continue + data = open(filename, "rb").read() + if '\0' in data: + print filename, "Binary!" + continue + newdata = data.replace("\r\n", "\n") + if newdata != data: + print filename + f = open(filename, "wb") + f.write(newdata) + f.close() + +if __name__ == '__main__': + main() + diff --git a/Tools/scripts/cvsfiles.py b/Tools/scripts/cvsfiles.py index 2168556..53b4294 100755 --- a/Tools/scripts/cvsfiles.py +++ b/Tools/scripts/cvsfiles.py @@ -68,4 +68,5 @@ def getmtime(filename): return 0 return st[stat.ST_MTIME] -sys.exit(main()) +if __name__ == '__main__': + main() diff --git a/Tools/scripts/diff.py b/Tools/scripts/diff.py index d452668..ecbff61 100644 --- a/Tools/scripts/diff.py +++ b/Tools/scripts/diff.py @@ -8,33 +8,38 @@ import sys, os, time, difflib, optparse -usage = "usage: %prog [options] fromfile tofile" -parser = optparse.OptionParser(usage) -parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') -parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff') -parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') -parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') -(options, args) = parser.parse_args() - -if len(args) == 0: - parser.print_help() - sys.exit(1) -if len(args) != 2: - parser.error("need to specify both a fromfile and tofile") - -n = options.lines -fromfile, tofile = args - -fromdate = time.ctime(os.stat(fromfile).st_mtime) -todate = time.ctime(os.stat(tofile).st_mtime) -fromlines = open(fromfile).readlines() -tolines = open(tofile).readlines() - -if options.u: - diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) -elif options.n: - diff = difflib.ndiff(fromlines, tolines) -else: - diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) - -sys.stdout.writelines(diff) +def main(): + + usage = "usage: %prog [options] fromfile tofile" + parser = optparse.OptionParser(usage) + parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') + parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff') + parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') + parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') + (options, args) = parser.parse_args() + + if len(args) == 0: + parser.print_help() + sys.exit(1) + if len(args) != 2: + parser.error("need to specify both a fromfile and tofile") + + n = options.lines + fromfile, tofile = args + + fromdate = time.ctime(os.stat(fromfile).st_mtime) + todate = time.ctime(os.stat(tofile).st_mtime) + fromlines = open(fromfile).readlines() + tolines = open(tofile).readlines() + + if options.u: + diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) + elif options.n: + diff = difflib.ndiff(fromlines, tolines) + else: + diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) + + sys.stdout.writelines(diff) + +if __name__ == '__main__': + main() diff --git a/Tools/scripts/dutree.py b/Tools/scripts/dutree.py index a49d80a..d938ae1 100755 --- a/Tools/scripts/dutree.py +++ b/Tools/scripts/dutree.py @@ -56,4 +56,5 @@ def show(total, d, prefix): if d.has_key(key): show(tsub, d[key][1], psub) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/findlinksto.py b/Tools/scripts/findlinksto.py index 00d4a18..8dd4bfb 100755 --- a/Tools/scripts/findlinksto.py +++ b/Tools/scripts/findlinksto.py @@ -39,4 +39,5 @@ def visit(prog, dirname, names): except os.error: pass -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/fixcid.py b/Tools/scripts/fixcid.py index 76841fe..42aa835 100755 --- a/Tools/scripts/fixcid.py +++ b/Tools/scripts/fixcid.py @@ -310,4 +310,5 @@ def addsubst(substfile): Dict[key] = value fp.close() -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/fixheader.py b/Tools/scripts/fixheader.py index 02b4f3b..1bf5c52 100755 --- a/Tools/scripts/fixheader.py +++ b/Tools/scripts/fixheader.py @@ -45,4 +45,5 @@ def process(filename): print '#endif' print '#endif /*', '!'+magic, '*/' -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/fixps.py b/Tools/scripts/fixps.py index 12e9f43..2ff15f4 100755 --- a/Tools/scripts/fixps.py +++ b/Tools/scripts/fixps.py @@ -29,5 +29,5 @@ def main(): f.write(rest) f.close() - -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/google.py b/Tools/scripts/google.py index d9fe0ac..b2ab1a4 100755 --- a/Tools/scripts/google.py +++ b/Tools/scripts/google.py @@ -19,4 +19,5 @@ def main(): url = "http://www.google.com/search?q=%s" % s webbrowser.open(url) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/gprof2html.py b/Tools/scripts/gprof2html.py index aa13d52..cb01c2c 100755 --- a/Tools/scripts/gprof2html.py +++ b/Tools/scripts/gprof2html.py @@ -75,4 +75,5 @@ def main(): output.close() webbrowser.open("file:" + os.path.abspath(outputfilename)) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/h2py.py b/Tools/scripts/h2py.py index e2f6054..63e7336 100755 --- a/Tools/scripts/h2py.py +++ b/Tools/scripts/h2py.py @@ -171,4 +171,5 @@ def process(fp, outfp, env = {}): sys.stderr.write('Warning - could not find file %s\n' % filename) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/idle b/Tools/scripts/idle index a209d56..1ba2a2c 100644 --- a/Tools/scripts/idle +++ b/Tools/scripts/idle @@ -1,4 +1,5 @@ #! /usr/bin/env python from idlelib.PyShell import main -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/ifdef.py b/Tools/scripts/ifdef.py index 15777f4..7e7b5cc 100755 --- a/Tools/scripts/ifdef.py +++ b/Tools/scripts/ifdef.py @@ -109,4 +109,5 @@ def process(fpi, fpo): if stack: sys.stderr.write('stack: %s\n' % stack) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/lfcr.py b/Tools/scripts/lfcr.py index 3baac97..babafae 100755 --- a/Tools/scripts/lfcr.py +++ b/Tools/scripts/lfcr.py @@ -3,17 +3,22 @@ "Replace LF with CRLF in argument files. Print names of changed files." import sys, re, os -for filename in sys.argv[1:]: - if os.path.isdir(filename): - print filename, "Directory!" - continue - data = open(filename, "rb").read() - if '\0' in data: - print filename, "Binary!" - continue - newdata = re.sub("\r?\n", "\r\n", data) - if newdata != data: - print filename - f = open(filename, "wb") - f.write(newdata) - f.close() + +def main(): + for filename in sys.argv[1:]: + if os.path.isdir(filename): + print filename, "Directory!" + continue + data = open(filename, "rb").read() + if '\0' in data: + print filename, "Binary!" + continue + newdata = re.sub("\r?\n", "\r\n", data) + if newdata != data: + print filename + f = open(filename, "wb") + f.write(newdata) + f.close() + +if __name__ == '__main__': + main() diff --git a/Tools/scripts/linktree.py b/Tools/scripts/linktree.py index 502873e..995f2ef 100755 --- a/Tools/scripts/linktree.py +++ b/Tools/scripts/linktree.py @@ -76,4 +76,5 @@ def linknames(old, new, link): else: os.symlink(linkname, newname) -sys.exit(main()) +if __name__ == '__main__': + sys.exit(main()) diff --git a/Tools/scripts/lll.py b/Tools/scripts/lll.py index cbcd80a..dc21cf7 100755 --- a/Tools/scripts/lll.py +++ b/Tools/scripts/lll.py @@ -13,13 +13,17 @@ def lll(dirname): full = os.path.join(dirname, name) if os.path.islink(full): print name, '->', os.readlink(full) - -args = sys.argv[1:] -if not args: args = [os.curdir] -first = 1 -for arg in args: - if len(args) > 1: - if not first: print - first = 0 - print arg + ':' +def main(): + args = sys.argv[1:] + if not args: args = [os.curdir] + first = 1 + for arg in args: + if len(args) > 1: + if not first: print + first = 0 + print arg + ':' lll(arg) + +if __name__ == '__main__': + main() + diff --git a/Tools/scripts/logmerge.py b/Tools/scripts/logmerge.py index 54d1676..edfec2c 100755 --- a/Tools/scripts/logmerge.py +++ b/Tools/scripts/logmerge.py @@ -177,8 +177,9 @@ def format_output(database): prev.append((date, working_file, rev, author)) prevtext = text -try: - main() -except IOError, e: - if e.errno != errno.EPIPE: - raise +if __name__ == '__main__': + try: + main() + except IOError, e: + if e.errno != errno.EPIPE: + raise diff --git a/Tools/scripts/methfix.py b/Tools/scripts/methfix.py index 9200d265..a872ab7 100755 --- a/Tools/scripts/methfix.py +++ b/Tools/scripts/methfix.py @@ -167,5 +167,5 @@ def fixline(line): line = line[:a] + line[c:d] + line[b:] return line - -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/mkreal.py b/Tools/scripts/mkreal.py index a890ac7..fb9e9a5 100755 --- a/Tools/scripts/mkreal.py +++ b/Tools/scripts/mkreal.py @@ -62,4 +62,5 @@ def main(): mkrealfile(name) sys.exit(status) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/objgraph.py b/Tools/scripts/objgraph.py index c48a425..01060f9 100755 --- a/Tools/scripts/objgraph.py +++ b/Tools/scripts/objgraph.py @@ -208,7 +208,8 @@ def main(): # Use its return value as exit status. # Catch interrupts to avoid stack trace. # -try: - sys.exit(main()) -except KeyboardInterrupt: - sys.exit(1) +if __name__ == '__main__': + try: + sys.exit(main()) + except KeyboardInterrupt: + sys.exit(1) diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index 47ae464..5798d9b 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -145,4 +145,6 @@ def fixline(line): return line return '#! %s\n' % new_interpreter -main() +if __name__ == '__main__': + main() + diff --git a/Tools/scripts/pdeps.py b/Tools/scripts/pdeps.py index 1589709..e835f84 100755 --- a/Tools/scripts/pdeps.py +++ b/Tools/scripts/pdeps.py @@ -160,7 +160,8 @@ def printresults(table): # Call main and honor exit status -try: - sys.exit(main()) -except KeyboardInterrupt: - sys.exit(1) +if __name__ == '__main__': + try: + sys.exit(main()) + except KeyboardInterrupt: + sys.exit(1) diff --git a/Tools/scripts/ptags.py b/Tools/scripts/ptags.py index de6e901..ac01356 100755 --- a/Tools/scripts/ptags.py +++ b/Tools/scripts/ptags.py @@ -49,4 +49,5 @@ def treat_file(filename): s = name + '\t' + filename + '\t/^' + content + '/\n' tags.append(s) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/pydoc b/Tools/scripts/pydoc index 0964929..0879a42 100755 --- a/Tools/scripts/pydoc +++ b/Tools/scripts/pydoc @@ -1,4 +1,5 @@ #!/usr/bin/env python import pydoc -pydoc.cli() +if __name__ == '__main__': + pydoc.cli() diff --git a/Tools/scripts/pydocgui.pyw b/Tools/scripts/pydocgui.pyw index 61a8b89..8e9a3d6 100644 --- a/Tools/scripts/pydocgui.pyw +++ b/Tools/scripts/pydocgui.pyw @@ -2,4 +2,6 @@ # importing itself (Python began allowing import of .pyw files # between 2.2a1 and 2.2a2). import pydoc -pydoc.gui() + +if __name__ == '__main__': + pydoc.gui() diff --git a/Tools/scripts/setup.py b/Tools/scripts/setup.py index b36febc..23bf247 100644 --- a/Tools/scripts/setup.py +++ b/Tools/scripts/setup.py @@ -1,18 +1,19 @@ from distutils.core import setup -setup( - scripts=[ - 'byteyears.py', - 'checkpyc.py', - 'copytime.py', - 'crlf.py', - 'dutree.py', - 'ftpmirror.py', - 'h2py.py', - 'lfcr.py', - 'logmerge.py', - '../../Lib/tabnanny.py', - '../../Lib/timeit.py', - 'untabify.py', - ], - ) +if __name__ == '__main__': + setup( + scripts=[ + 'byteyears.py', + 'checkpyc.py', + 'copytime.py', + 'crlf.py', + 'dutree.py', + 'ftpmirror.py', + 'h2py.py', + 'lfcr.py', + 'logmerge.py', + '../../Lib/tabnanny.py', + '../../Lib/timeit.py', + 'untabify.py', + ], + ) diff --git a/Tools/scripts/suff.py b/Tools/scripts/suff.py index dfb3da0..c026201 100755 --- a/Tools/scripts/suff.py +++ b/Tools/scripts/suff.py @@ -26,4 +26,5 @@ def getsuffix(filename): suff = filename[i:] return suff -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/treesync.py b/Tools/scripts/treesync.py index 1064ab7..ab9324b 100755 --- a/Tools/scripts/treesync.py +++ b/Tools/scripts/treesync.py @@ -201,4 +201,5 @@ def okay(prompt, answer='ask'): print "Yes or No please -- try again:" return okay(prompt) -main() +if __name__ == '__main__': + main() diff --git a/Tools/scripts/which.py b/Tools/scripts/which.py index 7b3d2e0..f9b2e61 100755 --- a/Tools/scripts/which.py +++ b/Tools/scripts/which.py @@ -13,44 +13,49 @@ from stat import * def msg(str): sys.stderr.write(str + '\n') -pathlist = os.environ['PATH'].split(os.pathsep) - -sts = 0 -longlist = '' - -if sys.argv[1:] and sys.argv[1][:2] == '-l': - longlist = sys.argv[1] - del sys.argv[1] - -for prog in sys.argv[1:]: - ident = () - for dir in pathlist: - filename = os.path.join(dir, prog) - try: - st = os.stat(filename) - except os.error: - continue - if not S_ISREG(st[ST_MODE]): - msg(filename + ': not a disk file') - else: - mode = S_IMODE(st[ST_MODE]) - if mode & 0111: - if not ident: - print filename - ident = st[:3] - else: - if st[:3] == ident: - s = 'same as: ' - else: - s = 'also: ' - msg(s + filename) +def main(): + pathlist = os.environ['PATH'].split(os.pathsep) + + sts = 0 + longlist = '' + + if sys.argv[1:] and sys.argv[1][:2] == '-l': + longlist = sys.argv[1] + del sys.argv[1] + + for prog in sys.argv[1:]: + ident = () + for dir in pathlist: + filename = os.path.join(dir, prog) + try: + st = os.stat(filename) + except os.error: + continue + if not S_ISREG(st[ST_MODE]): + msg(filename + ': not a disk file') else: - msg(filename + ': not executable') - if longlist: - sts = os.system('ls ' + longlist + ' ' + filename) - if sts: msg('"ls -l" exit status: ' + repr(sts)) - if not ident: - msg(prog + ': not found') - sts = 1 - -sys.exit(sts) + mode = S_IMODE(st[ST_MODE]) + if mode & 0111: + if not ident: + print filename + ident = st[:3] + else: + if st[:3] == ident: + s = 'same as: ' + else: + s = 'also: ' + msg(s + filename) + else: + msg(filename + ': not executable') + if longlist: + sts = os.system('ls ' + longlist + ' ' + filename) + if sts: msg('"ls -l" exit status: ' + repr(sts)) + if not ident: + msg(prog + ': not found') + sts = 1 + + sys.exit(sts) + +if __name__ == '__main__': + main() + diff --git a/Tools/scripts/xxci.py b/Tools/scripts/xxci.py index 681ce0b..c6a7d08 100755 --- a/Tools/scripts/xxci.py +++ b/Tools/scripts/xxci.py @@ -109,8 +109,9 @@ def askyesno(prompt): s = raw_input(prompt) return s in ['y', 'yes'] -try: - setup() - go(getargs()) -except KeyboardInterrupt: - print '[Intr]' +if __name__ == '__main__': + try: + setup() + go(getargs()) + except KeyboardInterrupt: + print '[Intr]' |