OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Find and fix files with inconsistent line endings. | 6 """Find and fix files with inconsistent line endings. |
7 | 7 |
8 This script requires 'dos2unix.exe' and 'unix2dos.exe' from Cygwin; they | 8 This script requires 'dos2unix.exe' and 'unix2dos.exe' from Cygwin; they |
9 must be in the user's PATH. | 9 must be in the user's PATH. |
10 | 10 |
11 Arg: Either one or more files to examine, or (with --file-list) one or more | 11 Arg: Either one or more files to examine, or (with --file-list) one or more |
12 files that themselves contain lists of files. The argument(s) passed to | 12 files that themselves contain lists of files. The argument(s) passed to |
(...skipping 18 matching lines...) Expand all Loading... |
31 """Local exception class.""" | 31 """Local exception class.""" |
32 pass | 32 pass |
33 | 33 |
34 | 34 |
35 def CountChars(text, str): | 35 def CountChars(text, str): |
36 """Count the number of instances of the given string in the text.""" | 36 """Count the number of instances of the given string in the text.""" |
37 split = text.split(str) | 37 split = text.split(str) |
38 logging.debug(len(split) - 1) | 38 logging.debug(len(split) - 1) |
39 return len(split) - 1 | 39 return len(split) - 1 |
40 | 40 |
| 41 |
41 def PrevailingEOLName(crlf, cr, lf): | 42 def PrevailingEOLName(crlf, cr, lf): |
42 """Describe the most common line ending. | 43 """Describe the most common line ending. |
43 | 44 |
44 Args: | 45 Args: |
45 crlf: How many CRLF (\r\n) sequences are in the file. | 46 crlf: How many CRLF (\r\n) sequences are in the file. |
46 cr: How many CR (\r) characters are in the file, excluding CRLF sequences. | 47 cr: How many CR (\r) characters are in the file, excluding CRLF sequences. |
47 lf: How many LF (\n) characters are in the file, excluding CRLF sequences. | 48 lf: How many LF (\n) characters are in the file, excluding CRLF sequences. |
48 | 49 |
49 Returns: | 50 Returns: |
50 A string describing the most common of the three line endings. | 51 A string describing the most common of the three line endings. |
51 """ | 52 """ |
52 most = max(crlf, cr, lf) | 53 most = max(crlf, cr, lf) |
53 if most == cr: | 54 if most == cr: |
54 return 'cr' | 55 return 'cr' |
55 if most == crlf: | 56 if most == crlf: |
56 return 'crlf' | 57 return 'crlf' |
57 return 'lf' | 58 return 'lf' |
58 | 59 |
| 60 |
59 def FixEndings(file, crlf, cr, lf): | 61 def FixEndings(file, crlf, cr, lf): |
60 """Change the file's line endings to CRLF or LF, whichever is more common.""" | 62 """Change the file's line endings to CRLF or LF, whichever is more common.""" |
61 most = max(crlf, cr, lf) | 63 most = max(crlf, cr, lf) |
62 if most == crlf: | 64 if most == crlf: |
63 result = subprocess.call('unix2dos.exe %s' % file, shell=True) | 65 result = subprocess.call('unix2dos.exe %s' % file, shell=True) |
64 if result: | 66 if result: |
65 raise Error('Error running unix2dos.exe %s' % file) | 67 raise Error('Error running unix2dos.exe %s' % file) |
66 else: | 68 else: |
67 result = subprocess.call('dos2unix.exe %s' % file, shell=True) | 69 result = subprocess.call('dos2unix.exe %s' % file, shell=True) |
68 if result: | 70 if result: |
(...skipping 23 matching lines...) Expand all Loading... |
92 print '%s: forcing to LF' % filename | 94 print '%s: forcing to LF' % filename |
93 # Fudge the counts to force switching to LF. | 95 # Fudge the counts to force switching to LF. |
94 FixEndings(filename, 0, 0, 1) | 96 FixEndings(filename, 0, 0, 1) |
95 else: | 97 else: |
96 if ((crlf > 0 and cr > 0) or | 98 if ((crlf > 0 and cr > 0) or |
97 (crlf > 0 and lf > 0) or | 99 (crlf > 0 and lf > 0) or |
98 ( lf > 0 and cr > 0)): | 100 ( lf > 0 and cr > 0)): |
99 print '%s: mostly %s' % (filename, PrevailingEOLName(crlf, cr, lf)) | 101 print '%s: mostly %s' % (filename, PrevailingEOLName(crlf, cr, lf)) |
100 FixEndings(filename, crlf, cr, lf) | 102 FixEndings(filename, crlf, cr, lf) |
101 | 103 |
102 def main(options, args): | 104 |
| 105 def process(options, args): |
103 """Process the files.""" | 106 """Process the files.""" |
104 if not args or len(args) < 1: | 107 if not args or len(args) < 1: |
105 raise Error('No files given.') | 108 raise Error('No files given.') |
106 | 109 |
107 if options.file_list: | 110 if options.file_list: |
108 for arg in args: | 111 for arg in args: |
109 filelist = open(arg, 'r').readlines() | 112 filelist = open(arg, 'r').readlines() |
110 ProcessFiles(filelist) | 113 ProcessFiles(filelist) |
111 else: | 114 else: |
112 filelist = args | 115 filelist = args |
113 ProcessFiles(filelist) | 116 ProcessFiles(filelist) |
| 117 return 0 |
114 | 118 |
115 if '__main__' == __name__: | 119 |
| 120 def main(): |
116 if DEBUGGING: | 121 if DEBUGGING: |
117 debug_level = logging.DEBUG | 122 debug_level = logging.DEBUG |
118 else: | 123 else: |
119 debug_level = logging.INFO | 124 debug_level = logging.INFO |
120 logging.basicConfig(level=debug_level, | 125 logging.basicConfig(level=debug_level, |
121 format='%(asctime)s %(levelname)-7s: %(message)s', | 126 format='%(asctime)s %(levelname)-7s: %(message)s', |
122 datefmt='%H:%M:%S') | 127 datefmt='%H:%M:%S') |
123 | 128 |
124 option_parser = optparse.OptionParser() | 129 option_parser = optparse.OptionParser() |
125 option_parser.add_option("", "--file-list", action="store_true", | 130 option_parser.add_option("", "--file-list", action="store_true", |
126 default=False, | 131 default=False, |
127 help="Treat the arguments as files containing " | 132 help="Treat the arguments as files containing " |
128 "lists of files to examine, rather than as " | 133 "lists of files to examine, rather than as " |
129 "the files to be checked.") | 134 "the files to be checked.") |
130 option_parser.add_option("", "--force-lf", action="store_true", | 135 option_parser.add_option("", "--force-lf", action="store_true", |
131 default=False, | 136 default=False, |
132 help="Force any files with CRLF to LF instead.") | 137 help="Force any files with CRLF to LF instead.") |
133 options, args = option_parser.parse_args() | 138 options, args = option_parser.parse_args() |
| 139 return process(options, args) |
134 | 140 |
135 sys.exit(main(options, args)) | 141 |
| 142 if '__main__' == __name__: |
| 143 sys.exit(main()) |
OLD | NEW |