From 1d95dd69b9936175ff01e5e4bfff5efc93639ee1 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Sun, 28 May 2017 15:14:27 +0200 Subject: Update tooling to use yapf for Python file formatting (issue #272) --- tools/fix_style.py | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'tools/fix_style.py') diff --git a/tools/fix_style.py b/tools/fix_style.py index 4c066ad..b610c0d 100644 --- a/tools/fix_style.py +++ b/tools/fix_style.py @@ -5,15 +5,21 @@ import os, re, sys from clang_util import clang_format -from file_util import * +from file_util import eval_file, get_files, read_file, write_file from git_util import get_changed_files +from yapf_util import yapf_format -# Valid extensions for files we want to clang-format. -DEFAULT_LINT_WHITELIST_REGEX = r"(.*\.cpp|.*\.cc|.*\.h|.*\.java|.*\.mm)$" +# File extensions that can be formatted. +DEFAULT_LINT_WHITELIST_REGEX = r"(.*\.cpp|.*\.cc|.*\.h|.*\.java|.*\.mm|.*\.py)$" DEFAULT_LINT_BLACKLIST_REGEX = r"$^" -IGNORE_DIRECTORIES = ["binary_distrib", "jcef_build", "out", "third_party", - "tools"] +# Directories containing these path components will be ignored. +IGNORE_DIRECTORIES = [] + +# Script directory. +script_dir = os.path.dirname(__file__) +root_dir = os.path.join(script_dir, os.pardir) + def msg(filename, status): if sys.platform == 'win32': @@ -30,14 +36,32 @@ def msg(filename, status): print "%-60s %s" % (filename, status) + updatect = 0 + + +def read_config(): + style_cfg = os.path.join(root_dir, ".style.cfg") + if os.path.exists(style_cfg): + config = eval_file(style_cfg) + if 'ignore_directories' in config: + global IGNORE_DIRECTORIES + IGNORE_DIRECTORIES = config['ignore_directories'] + + def update_file(filename): oldcontents = read_file(filename) if len(oldcontents) == 0: msg(filename, "empty") - return; + return + + if os.path.splitext(filename)[1] == ".py": + # Format Python files using YAPF. + newcontents = yapf_format(filename, oldcontents) + else: + # Format C/C++/ObjC/Java files using clang-format. + newcontents = clang_format(filename, oldcontents) - newcontents = clang_format(filename, oldcontents) if newcontents is None: raise Exception("Failed to process %s" % filename) @@ -50,7 +74,8 @@ def update_file(filename): msg(filename, "ok") return -def fix_style(filenames, white_list = None, black_list = None): + +def fix_style(filenames, white_list=None, black_list=None): """ Execute clang-format with the specified arguments. """ if not white_list: white_list = DEFAULT_LINT_WHITELIST_REGEX @@ -96,6 +121,7 @@ def fix_style(filenames, white_list = None, black_list = None): else: msg(filename, "skipped") + if __name__ == "__main__": if len(sys.argv) == 1: print "Usage: %s [file-path|git-hash|unstaged|staged] ..." % sys.argv[0] @@ -109,6 +135,9 @@ if __name__ == "__main__": print " staged\t\tProcess all staged files in the Git repo." sys.exit(1) + # Read the configuration file. + read_config() + # Process anything passed on the command-line. fix_style(sys.argv[1:]) print 'Done - Wrote %d files.' % updatect -- cgit v1.2.3