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

Unified Diff: Tools/scripts/reindent.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Tools/scripts/patchcheck.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Tools/scripts/reindent.py Tue Jul 26 09:37:46 2011 +0300
+++ b/Tools/scripts/reindent.py Mon Jul 25 09:47:18 2011 -0400
@@ -8,6 +8,8 @@
-r (--recurse) Recurse. Search for all .py files in subdirectories too.
-n (--nobackup) No backup. Does not make a ".bak" file before reindenting.
-v (--verbose) Verbose. Print informative msgs; else no output.
+ (--newline) Newline. Specify the newline character to use (CRLF, LF).
+ Default is the same as the original file.
-h (--help) Help. Print this usage information and exit.
Change Python (.py) files to use 4-space indents and no hard tab characters.
@@ -35,7 +37,7 @@
The backup file is a copy of the one that is being reindented. The ".bak"
file is generated with shutil.copy(), but some corner cases regarding
-user/group and permissions could leave the backup file more readable that
+user/group and permissions could leave the backup file more readable than
you'd prefer. You can always use the --nobackup option to prevent this.
"""
@@ -65,10 +67,11 @@
def main():
import getopt
- global verbose, recurse, dryrun, makebackup
+ global verbose, recurse, dryrun, makebackup, spec_newline
+ spec_newline = None
try:
opts, args = getopt.getopt(sys.argv[1:], "drnvh",
- ["dryrun", "recurse", "nobackup", "verbose", "help"])
+ ["dryrun", "recurse", "nobackup", "verbose", "newline=", "help"])
except getopt.error as msg:
usage(msg)
return
@@ -81,6 +84,11 @@
makebackup = False
elif o in ('-v', '--verbose'):
verbose = True
+ elif o in ('--newline',):
+ if not a.upper() in ('CRLF', 'LF'):
+ usage()
+ return
+ spec_newline = dict(CRLF='\r\n', LF='\n')[a.upper()]
elif o in ('-h', '--help'):
usage()
return
@@ -109,7 +117,7 @@
if verbose:
print("checking", file, "...", end=' ')
- with open(file, 'rb') as f:
+ with open(file, 'rb') as f:
encoding, _ = tokenize.detect_encoding(f.readline)
try:
with open(file, encoding=encoding) as f:
@@ -118,6 +126,11 @@
errprint("%s: I/O Error: %s" % (file, str(msg)))
return
+ newline = spec_newline if spec_newline else r.newlines
+ if isinstance(newline, tuple):
+ errprint("%s: mixed newlines detected; cannot continue without --newline" % file)
+ return
+
if r.run():
if verbose:
print("changed.")
@@ -129,7 +142,7 @@
shutil.copyfile(file, bak)
if verbose:
print("backed up", file, "to", bak)
- with open(file, "w", encoding=encoding) as f:
+ with open(file, "w", encoding=encoding, newline=newline) as f:
r.write(f)
if verbose:
print("wrote new", file)
@@ -177,6 +190,10 @@
# indeed, they're our headache!
self.stats = []
+ # Save the newlines found in the file so they can be used to
+ # create output without mutating the newlines.
+ self.newlines = f.newlines
+
def run(self):
tokens = tokenize.generate_tokens(self.getline)
for _token in tokens:
« no previous file with comments | « Tools/scripts/patchcheck.py ('k') | no next file » | no next file with comments »

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