Skip to content

Commit 624dc71

Browse files
author
akuchling
committed
[Patch #1005491 ] use __name__ == '__main__' in scripts
git-svn-id: http://svn.python.org/projects/python/trunk@36938 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 4cf2976 commit 624dc71

34 files changed

Lines changed: 268 additions & 211 deletions

Tools/scripts/byteyears.py

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,53 @@
99
import sys, os, time
1010
from stat import *
1111

12-
# Use lstat() to stat files if it exists, else stat()
13-
try:
14-
statfunc = os.lstat
15-
except AttributeError:
16-
statfunc = os.stat
17-
18-
# Parse options
19-
if sys.argv[1] == '-m':
20-
itime = ST_MTIME
21-
del sys.argv[1]
22-
elif sys.argv[1] == '-c':
23-
itime = ST_CTIME
24-
del sys.argv[1]
25-
elif sys.argv[1] == '-a':
26-
itime = ST_CTIME
27-
del sys.argv[1]
28-
else:
29-
itime = ST_MTIME
30-
31-
secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
32-
now = time.time() # Current time, for age computations
33-
status = 0 # Exit status, set to 1 on errors
34-
35-
# Compute max file name length
36-
maxlen = 1
37-
for filename in sys.argv[1:]:
38-
maxlen = max(maxlen, len(filename))
39-
40-
# Process each argument in turn
41-
for filename in sys.argv[1:]:
12+
def main():
13+
14+
# Use lstat() to stat files if it exists, else stat()
4215
try:
43-
st = statfunc(filename)
44-
except os.error, msg:
45-
sys.stderr.write("can't stat %r: %r\n" % (filename, msg))
46-
status = 1
47-
st = ()
48-
if st:
49-
anytime = st[itime]
50-
size = st[ST_SIZE]
51-
age = now - anytime
52-
byteyears = float(size) * float(age) / secs_per_year
53-
print filename.ljust(maxlen),
54-
print repr(int(byteyears)).rjust(8)
55-
56-
sys.exit(status)
16+
statfunc = os.lstat
17+
except AttributeError:
18+
statfunc = os.stat
19+
20+
# Parse options
21+
if sys.argv[1] == '-m':
22+
itime = ST_MTIME
23+
del sys.argv[1]
24+
elif sys.argv[1] == '-c':
25+
itime = ST_CTIME
26+
del sys.argv[1]
27+
elif sys.argv[1] == '-a':
28+
itime = ST_CTIME
29+
del sys.argv[1]
30+
else:
31+
itime = ST_MTIME
32+
33+
secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
34+
now = time.time() # Current time, for age computations
35+
status = 0 # Exit status, set to 1 on errors
36+
37+
# Compute max file name length
38+
maxlen = 1
39+
for filename in sys.argv[1:]:
40+
maxlen = max(maxlen, len(filename))
41+
42+
# Process each argument in turn
43+
for filename in sys.argv[1:]:
44+
try:
45+
st = statfunc(filename)
46+
except os.error, msg:
47+
sys.stderr.write("can't stat %r: %r\n" % (filename, msg))
48+
status = 1
49+
st = ()
50+
if st:
51+
anytime = st[itime]
52+
size = st[ST_SIZE]
53+
age = now - anytime
54+
byteyears = float(size) * float(age) / secs_per_year
55+
print filename.ljust(maxlen),
56+
print repr(int(byteyears)).rjust(8)
57+
58+
sys.exit(status)
59+
60+
if __name__ == '__main__':
61+
main()

Tools/scripts/checkpyc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ def get_long(s):
6262
return -1
6363
return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
6464

65-
main()
65+
if __name__ == '__main__':
66+
main()

Tools/scripts/classfix.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,5 @@ def fixline(line):
186186

187187
return head + '(' + basepart + '):' + tail
188188

189-
main()
189+
if __name__ == '__main__':
190+
main()

Tools/scripts/copytime.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ def main():
2222
sys.stderr.write(file2 + ': cannot change time\n')
2323
sys.exit(2)
2424

25-
main()
25+
if __name__ == '__main__':
26+
main()

Tools/scripts/crlf.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
#! /usr/bin/env python
2-
32
"Replace CRLF with LF in argument files. Print names of changed files."
43

54
import sys, os
6-
for filename in sys.argv[1:]:
7-
if os.path.isdir(filename):
8-
print filename, "Directory!"
9-
continue
10-
data = open(filename, "rb").read()
11-
if '\0' in data:
12-
print filename, "Binary!"
13-
continue
14-
newdata = data.replace("\r\n", "\n")
15-
if newdata != data:
16-
print filename
17-
f = open(filename, "wb")
18-
f.write(newdata)
19-
f.close()
5+
6+
def main():
7+
for filename in sys.argv[1:]:
8+
if os.path.isdir(filename):
9+
print filename, "Directory!"
10+
continue
11+
data = open(filename, "rb").read()
12+
if '\0' in data:
13+
print filename, "Binary!"
14+
continue
15+
newdata = data.replace("\r\n", "\n")
16+
if newdata != data:
17+
print filename
18+
f = open(filename, "wb")
19+
f.write(newdata)
20+
f.close()
21+
22+
if __name__ == '__main__':
23+
main()
24+

Tools/scripts/cvsfiles.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ def getmtime(filename):
6868
return 0
6969
return st[stat.ST_MTIME]
7070

71-
sys.exit(main())
71+
if __name__ == '__main__':
72+
main()

Tools/scripts/diff.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,38 @@
88

99
import sys, os, time, difflib, optparse
1010

11-
usage = "usage: %prog [options] fromfile tofile"
12-
parser = optparse.OptionParser(usage)
13-
parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
14-
parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
15-
parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
16-
parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
17-
(options, args) = parser.parse_args()
18-
19-
if len(args) == 0:
20-
parser.print_help()
21-
sys.exit(1)
22-
if len(args) != 2:
23-
parser.error("need to specify both a fromfile and tofile")
24-
25-
n = options.lines
26-
fromfile, tofile = args
27-
28-
fromdate = time.ctime(os.stat(fromfile).st_mtime)
29-
todate = time.ctime(os.stat(tofile).st_mtime)
30-
fromlines = open(fromfile).readlines()
31-
tolines = open(tofile).readlines()
32-
33-
if options.u:
34-
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
35-
elif options.n:
36-
diff = difflib.ndiff(fromlines, tolines)
37-
else:
38-
diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
39-
40-
sys.stdout.writelines(diff)
11+
def main():
12+
13+
usage = "usage: %prog [options] fromfile tofile"
14+
parser = optparse.OptionParser(usage)
15+
parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
16+
parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
17+
parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
18+
parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
19+
(options, args) = parser.parse_args()
20+
21+
if len(args) == 0:
22+
parser.print_help()
23+
sys.exit(1)
24+
if len(args) != 2:
25+
parser.error("need to specify both a fromfile and tofile")
26+
27+
n = options.lines
28+
fromfile, tofile = args
29+
30+
fromdate = time.ctime(os.stat(fromfile).st_mtime)
31+
todate = time.ctime(os.stat(tofile).st_mtime)
32+
fromlines = open(fromfile).readlines()
33+
tolines = open(tofile).readlines()
34+
35+
if options.u:
36+
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
37+
elif options.n:
38+
diff = difflib.ndiff(fromlines, tolines)
39+
else:
40+
diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
41+
42+
sys.stdout.writelines(diff)
43+
44+
if __name__ == '__main__':
45+
main()

Tools/scripts/dutree.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ def show(total, d, prefix):
5656
if d.has_key(key):
5757
show(tsub, d[key][1], psub)
5858

59-
main()
59+
if __name__ == '__main__':
60+
main()

Tools/scripts/findlinksto.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ def visit(prog, dirname, names):
3939
except os.error:
4040
pass
4141

42-
main()
42+
if __name__ == '__main__':
43+
main()

Tools/scripts/fixcid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,5 @@ def addsubst(substfile):
310310
Dict[key] = value
311311
fp.close()
312312

313-
main()
313+
if __name__ == '__main__':
314+
main()

0 commit comments

Comments
 (0)