Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(9)

Delta Between Two Patch Sets: Tools/scripts/reindent.py

Issue 10639: reindent.py converts newlines to platform default
Left Patch Set: Created 9 years, 1 month ago
Right Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Tools/scripts/patchcheck.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #! /usr/bin/env python3 1 #! /usr/bin/env python3
2 2
3 # Released to the public domain, by Tim Peters, 03 October 2000. 3 # Released to the public domain, by Tim Peters, 03 October 2000.
4 4
5 """reindent [-d][-r][-v] [ path ... ] 5 """reindent [-d][-r][-v] [ path ... ]
6 6
7 -d (--dryrun) Dry run. Analyze, but don't make any changes to, files. 7 -d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
8 -r (--recurse) Recurse. Search for all .py files in subdirectories too. 8 -r (--recurse) Recurse. Search for all .py files in subdirectories too.
9 -n (--nobackup) No backup. Does not make a ".bak" file before reindenting. 9 -n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
10 -v (--verbose) Verbose. Print informative msgs; else no output. 10 -v (--verbose) Verbose. Print informative msgs; else no output.
11 (--newline) Newline. Specify the newline character to use (CRLF, LF).
12 Default is the same as the original file.
11 -h (--help) Help. Print this usage information and exit. 13 -h (--help) Help. Print this usage information and exit.
12 14
13 Change Python (.py) files to use 4-space indents and no hard tab characters. 15 Change Python (.py) files to use 4-space indents and no hard tab characters.
14 Also trim excess spaces and tabs from ends of lines, and remove empty lines 16 Also trim excess spaces and tabs from ends of lines, and remove empty lines
15 at the end of files. Also ensure the last line ends with a newline. 17 at the end of files. Also ensure the last line ends with a newline.
16 18
17 If no paths are given on the command line, reindent operates as a filter, 19 If no paths are given on the command line, reindent operates as a filter,
18 reading a single source file from standard input and writing the transformed 20 reading a single source file from standard input and writing the transformed
19 source to standard output. In this case, the -d, -r and -v flags are 21 source to standard output. In this case, the -d, -r and -v flags are
20 ignored. 22 ignored.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 print(msg, file=sys.stderr) 60 print(msg, file=sys.stderr)
59 61
60 62
61 def errprint(*args): 63 def errprint(*args):
62 sys.stderr.write(" ".join(str(arg) for arg in args)) 64 sys.stderr.write(" ".join(str(arg) for arg in args))
63 sys.stderr.write("\n") 65 sys.stderr.write("\n")
64 66
65 67
66 def main(): 68 def main():
67 import getopt 69 import getopt
68 global verbose, recurse, dryrun, makebackup 70 global verbose, recurse, dryrun, makebackup, spec_newline
71 spec_newline = None
69 try: 72 try:
70 opts, args = getopt.getopt(sys.argv[1:], "drnvh", 73 opts, args = getopt.getopt(sys.argv[1:], "drnvh",
71 ["dryrun", "recurse", "nobackup", "verbose", "help"]) 74 ["dryrun", "recurse", "nobackup", "verbose", "newline=", "help"])
72 except getopt.error as msg: 75 except getopt.error as msg:
73 usage(msg) 76 usage(msg)
74 return 77 return
75 for o, a in opts: 78 for o, a in opts:
76 if o in ('-d', '--dryrun'): 79 if o in ('-d', '--dryrun'):
77 dryrun = True 80 dryrun = True
78 elif o in ('-r', '--recurse'): 81 elif o in ('-r', '--recurse'):
79 recurse = True 82 recurse = True
80 elif o in ('-n', '--nobackup'): 83 elif o in ('-n', '--nobackup'):
81 makebackup = False 84 makebackup = False
82 elif o in ('-v', '--verbose'): 85 elif o in ('-v', '--verbose'):
83 verbose = True 86 verbose = True
87 elif o in ('--newline',):
88 if not a.upper() in ('CRLF', 'LF'):
89 usage()
90 return
91 spec_newline = dict(CRLF='\r\n', LF='\n')[a.upper()]
84 elif o in ('-h', '--help'): 92 elif o in ('-h', '--help'):
85 usage() 93 usage()
86 return 94 return
87 if not args: 95 if not args:
88 r = Reindenter(sys.stdin) 96 r = Reindenter(sys.stdin)
89 r.run() 97 r.run()
90 r.write(sys.stdout) 98 r.write(sys.stdout)
91 return 99 return
92 for arg in args: 100 for arg in args:
93 check(arg) 101 check(arg)
94 102
95 103
96 def check(file): 104 def check(file):
97 if os.path.isdir(file) and not os.path.islink(file): 105 if os.path.isdir(file) and not os.path.islink(file):
98 if verbose: 106 if verbose:
99 print("listing directory", file) 107 print("listing directory", file)
100 names = os.listdir(file) 108 names = os.listdir(file)
101 for name in names: 109 for name in names:
102 fullname = os.path.join(file, name) 110 fullname = os.path.join(file, name)
103 if ((recurse and os.path.isdir(fullname) and 111 if ((recurse and os.path.isdir(fullname) and
104 not os.path.islink(fullname) and 112 not os.path.islink(fullname) and
105 not os.path.split(fullname)[1].startswith(".")) 113 not os.path.split(fullname)[1].startswith("."))
106 or name.lower().endswith(".py")): 114 or name.lower().endswith(".py")):
107 check(fullname) 115 check(fullname)
108 return 116 return
109 117
110 if verbose: 118 if verbose:
111 print("checking", file, "...", end=' ') 119 print("checking", file, "...", end=' ')
112 with open(file, 'rb') as f: 120 with open(file, 'rb') as f:
113 encoding, _ = tokenize.detect_encoding(f.readline) 121 encoding, _ = tokenize.detect_encoding(f.readline)
114 try: 122 try:
115 with open(file, encoding=encoding) as f: 123 with open(file, encoding=encoding) as f:
116 r = Reindenter(f) 124 r = Reindenter(f)
117 except IOError as msg: 125 except IOError as msg:
118 errprint("%s: I/O Error: %s" % (file, str(msg))) 126 errprint("%s: I/O Error: %s" % (file, str(msg)))
127 return
128
129 newline = spec_newline if spec_newline else r.newlines
130 if isinstance(newline, tuple):
131 errprint("%s: mixed newlines detected; cannot continue without --newline " % file)
119 return 132 return
120 133
121 if r.run(): 134 if r.run():
122 if verbose: 135 if verbose:
123 print("changed.") 136 print("changed.")
124 if dryrun: 137 if dryrun:
125 print("But this is a dry run, so leaving it alone.") 138 print("But this is a dry run, so leaving it alone.")
126 if not dryrun: 139 if not dryrun:
127 bak = file + ".bak" 140 bak = file + ".bak"
128 if makebackup: 141 if makebackup:
129 shutil.copyfile(file, bak) 142 shutil.copyfile(file, bak)
130 if verbose: 143 if verbose:
131 print("backed up", file, "to", bak) 144 print("backed up", file, "to", bak)
132 with open(file, "w", encoding=encoding, newline=r.newlines) as f: 145 with open(file, "w", encoding=encoding, newline=newline) as f:
133 r.write(f) 146 r.write(f)
134 if verbose: 147 if verbose:
135 print("wrote new", file) 148 print("wrote new", file)
136 return True 149 return True
137 else: 150 else:
138 if verbose: 151 if verbose:
139 print("unchanged.") 152 print("unchanged.")
140 return False 153 return False
141 154
142 155
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 # Count number of leading blanks. 320 # Count number of leading blanks.
308 def getlspace(line): 321 def getlspace(line):
309 i, n = 0, len(line) 322 i, n = 0, len(line)
310 while i < n and line[i] == " ": 323 while i < n and line[i] == " ":
311 i += 1 324 i += 1
312 return i 325 return i
313 326
314 327
315 if __name__ == '__main__': 328 if __name__ == '__main__':
316 main() 329 main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+