| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,48 +9,53 @@ | |||
| 9 | 9 | import sys, os, time | |
| 10 | 10 | from stat import * | |
| 11 | 11 | ||
| 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() | ||
| 42 | 15 | 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() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,4 +62,5 @@ def get_long(s): | |||
| 62 | 62 | return -1 | |
| 63 | 63 | return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24) | |
| 64 | 64 | ||
| 65 | - main() | ||
| 65 | + if __name__ == '__main__': | ||
| 66 | + main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -186,4 +186,5 @@ def fixline(line): | |||
| 186 | 186 | ||
| 187 | 187 | return head + '(' + basepart + '):' + tail | |
| 188 | 188 | ||
| 189 | - main() | ||
| 189 | + if __name__ == '__main__': | ||
| 190 | + main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,4 +22,5 @@ def main(): | |||
| 22 | 22 | sys.stderr.write(file2 + ': cannot change time\n') | |
| 23 | 23 | sys.exit(2) | |
| 24 | 24 | ||
| 25 | - main() | ||
| 25 | + if __name__ == '__main__': | ||
| 26 | + main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,19 +1,24 @@ | |||
| 1 | 1 | #! /usr/bin/env python | |
| 2 | - | ||
| 3 | 2 | "Replace CRLF with LF in argument files. Print names of changed files." | |
| 4 | 3 | ||
| 5 | 4 | 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 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -68,4 +68,5 @@ def getmtime(filename): | |||
| 68 | 68 | return 0 | |
| 69 | 69 | return st[stat.ST_MTIME] | |
| 70 | 70 | ||
| 71 | - sys.exit(main()) | ||
| 71 | + if __name__ == '__main__': | ||
| 72 | + main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,33 +8,38 @@ | |||
| 8 | 8 | ||
| 9 | 9 | import sys, os, time, difflib, optparse | |
| 10 | 10 | ||
| 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() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,4 +56,5 @@ def show(total, d, prefix): | |||
| 56 | 56 | if d.has_key(key): | |
| 57 | 57 | show(tsub, d[key][1], psub) | |
| 58 | 58 | ||
| 59 | - main() | ||
| 59 | + if __name__ == '__main__': | ||
| 60 | + main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,4 +39,5 @@ def visit(prog, dirname, names): | |||
| 39 | 39 | except os.error: | |
| 40 | 40 | pass | |
| 41 | 41 | ||
| 42 | - main() | ||
| 42 | + if __name__ == '__main__': | ||
| 43 | + main() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -310,4 +310,5 @@ def addsubst(substfile): | |||
| 310 | 310 | Dict[key] = value | |
| 311 | 311 | fp.close() | |
| 312 | 312 | ||
| 313 | - main() | ||
| 313 | + if __name__ == '__main__': | ||
| 314 | + main() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments