diff --git a/tools/refactoring/webrtc_reformat.py b/tools/refactoring/webrtc_reformat.py new file mode 100755 index 0000000000..db0de03766 --- /dev/null +++ b/tools/refactoring/webrtc_reformat.py @@ -0,0 +1,269 @@ +#!/usr/bin/python +# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. +# +# Use of this source code is governed by a BSD-style license +# that can be found in the LICENSE file in the root of the source +# tree. An additional intellectual property rights grant can be found +# in the file PATENTS. All contributing project authors may +# be found in the AUTHORS file in the root of the source tree. + +"""WebRTC reformat script. + +This script is used to reformat WebRTC code from the old code style to Google +C++ code style. + +You need to have astyle (http://astyle.sourceforge.net/) in your path. You also +need to put the following contents into ~/.astylerc: + +# =======================COPY============================================== +# Google C++ style guide settings. +indent=spaces=2 # Indentation uses two spaces. +style=attach # Attach braces. + +indent-switches +indent-preprocessor # Indent preprocessor continuation lines. +min-conditional-indent=0 # Align conditional continuation with "(". + # e.g. if (foo && + # bar +max-instatement-indent=80 # Try not to mess with current alignment. + +pad-oper # Padding around operators. +pad-header # Padding after if, for etc. +unpad-paren # No padding around parentheses. +align-pointer=type # e.g. int* foo + +convert-tabs # Convert non-indentation tabs as well. + +# The following are available in the unreleased svn repo. +# Behvaiour isn't quite what we'd like; more testing needed. +#max-code-length=80 +#break-after-logical + +lineend=linux +# ========================================================================= +""" + +# TODO(mflodman) +# x s/type *var/type* var/g +# x : list indention -> 4 spaces. + +__author__ = 'mflodman@webrtc.org (Magnus Flodman)' + +import fnmatch +import os +import re +import subprocess +import sys + + +def LowerWord(obj): + """Helper for DeCamelCase.""" + return obj.group(1) + '_' + obj.group(2).lower() + obj.group(3) + + +def DeCamelCase(text): + """De-camelize variable names.""" + pattern = re.compile(r'(?<=[ _*\(\&\!])([a-z]+)(?' % sys.argv[0] + sys.exit(1) + + for filename in args: + f = open(filename) + text = f.read() + f.close() + + text = DeCamelCase(text) + text = MoveUnderScore(text) + text = CPPComments(text) + text = AddHeaderPath(text) + text = SortIncludeHeaders(text, filename) + text = RemoveMultipleEmptyLines(text) + text = TrimLineEndings(text) + + # Remove the original file and re-create it with the reformatted content. + SaveFile(filename, text) + + # Fix tabs, indentation and '{' using astyle. + astyle_cmd = 'astyle' + if sys.platform == 'win32': + astyle_cmd += '.exe' + subprocess.call([astyle_cmd, '-n', '-q', filename]) + + if filename.endswith('.h'): + f = open(filename) + text = f.read() + f.close() + text = IndentLabels(text) + text = FixIncludeGuards(text, filename) + SaveFile(filename, text) + + print filename + ' done.' + + +if __name__ == '__main__': + main()