7"""Module that sanitizes source files with specified modifiers."""
10from __future__
import print_function
16_FILE_EXTENSIONS_TO_SANITIZE = [
'cpp',
'h',
'c']
18_SUBDIRS_TO_IGNORE = [
'.git',
'.svn',
'third_party']
22 """Sanitizes source files with the specified file and line modifiers.
25 directory: string - The directory which will be recursively traversed to
26 find source files to apply modifiers to.
27 file_modifiers: list - file-modification methods which should be applied to
28 the complete file content (Eg: EOFOneAndOnlyOneNewlineAdder).
29 line_modifiers: list - line-modification methods which should be applied to
30 lines in a file (Eg: TabReplacer).
32 for item
in os.listdir(directory):
34 full_item_path = os.path.join(directory, item)
36 if os.path.isfile(full_item_path):
39 if (
len(full_item_path.split(
'.')) > 1
and
40 full_item_path.split(
'.')[-1]
in _FILE_EXTENSIONS_TO_SANITIZE):
41 f =
file(full_item_path)
56 for modifier
in line_modifiers:
57 line = modifier(line, full_item_path, line_number)
58 if original_line != line:
60 new_lines.append(line)
63 old_content =
''.
join(lines)
64 new_content =
''.
join(new_lines)
65 for modifier
in file_modifiers:
66 new_content = modifier(new_content, full_item_path)
67 if new_content != old_content:
72 f =
file(full_item_path,
'w')
77 print(
'Made changes to %s' % full_item_path)
79 elif item
not in _SUBDIRS_TO_IGNORE:
88 """Strips out trailing whitespaces from the specified line."""
89 stripped_line = line.rstrip() +
'\n'
90 if line != stripped_line:
91 print(
'Removing trailing whitespace in %s:%s' % (file_path, line_number))
96 """Replaces CRLF with LF."""
98 print(
'Replacing CRLF with LF in %s:%s' % (file_path, line_number))
99 return line.replace(
'\r\n',
'\n')
103 """Replaces Tabs with 4 whitespaces."""
105 print(
'Replacing Tab with whitespace in %s:%s' % (file_path, line_number))
106 return line.replace(
'\t',
' ')
113 """Ensures that the copywrite information is correct."""
120 """Adds one and only one LF at the end of the file."""
121 if file_content
and (file_content[-1] !=
'\n' or file_content[-2:-1] ==
'\n'):
122 file_content = file_content.rstrip()
124 print(
'Added exactly one newline to %s' % file_path)
129 """Sets svn:eol-style property to LF."""
130 output = commands.getoutput(
131 'svn propget svn:eol-style %s' % file_path)
133 print(
'Setting svn:eol-style property to LF in %s' % file_path)
134 os.system(
'svn ps svn:eol-style LF %s' % file_path)
141if '__main__' == __name__:
146 EOFOneAndOnlyOneNewlineAdder,
152 TrailingWhitespaceRemover,
def print(*args, **kwargs)
static SkString join(const CommandLineFlags::StringArray &)