@@ -0,0 +1,2088 @@
+#!/usr/bin/python%0A%0A# This file is a part of the OpenSurgSim project.%0A# Copyright 2012-2013, SimQuest Solutions Inc.%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A%0A%22%22%22Remove all trailing whitespace from a file.%0A%0ATypical usage:%0A  Tools/remove-trailing-whitespace.py Foo.h%0Aor:%0A  find SurgSim %5C( -name '*.h' -o -name '*.cpp' %5C) -print %5C%0A    %7C xargs python Tools/remove-trailing-whitespace.py%0A%22%22%22%0A%0Aimport argparse%0Aimport sys%0Aimport re%0A%0Adef slurp_raw_lines(file):%0A  try:%0A    with open(file, 'rb') as f:%0A      return f.readlines()%0A  except IOError as e:%0A    print %3E%3E sys.stderr, e%0A    return None%0A%0Adef spew_raw_lines(file, lines):%0A  try:%0A    with open(file, 'wb') as f:%0A      for line in lines:%0A        f.write(line)%0A      return True%0A  except IOError as e:%0A    print %3E%3E sys.stderr, e%0A    return False%0A%0Adef update(file, lines):%0A  if lines is None:%0A    return None%0A  eol = %22%5Cn%22%0A  if len(lines) and re.search(r'%5Cr%5Cn$', lines%5B0%5D):%0A    eol = %22%5Cr%5Cn%22%0A  result = map(lambda x: x.rstrip() + eol, lines)%0A  if result == lines:%0A    return None%0A  return result%0A%0Aif __name__ == '__main__':%0A  parser = argparse.ArgumentParser(%0A    description=%22Remove all trailing whitespace from a file.%22)%0A  parser.add_argument('files', metavar='FILE', nargs='*',%0A                      help='The file names to modify.')%0A  args = parser.parse_args()%0A%0A  touched = False%0A  for file in args.files:%0A    lines = update(file, slurp_raw_lines(file))%0A    if lines is not None:%0A      spew_raw_lines(file, lines)%0A      print %22Updated%22, file%0A      touched = True%0A%0A  if not touched:%0A    print %22%7B%7D: Nothing to update!%22.format(sys.argv%5B0%5D)%0A

Add a script to remove trailing whitespace from a file.