aboutsummaryrefslogtreecommitdiff
path: root/tools/fix_style.py
diff options
context:
space:
mode:
authorMarshall Greenblatt <magreenblatt@gmail.com>2017-05-28 15:14:27 +0200
committerMarshall Greenblatt <magreenblatt@gmail.com>2017-05-28 15:15:10 +0200
commit1d95dd69b9936175ff01e5e4bfff5efc93639ee1 (patch)
tree16415c99ee7ebdf31f77bd5e5d22b7cd9c6f4dc2 /tools/fix_style.py
parentd85ab5c40f88049495cbf2e0c5c11b5ee27f1422 (diff)
downloadjcef-1d95dd69b9936175ff01e5e4bfff5efc93639ee1.tar.gz
Update tooling to use yapf for Python file formatting (issue #272)
Diffstat (limited to 'tools/fix_style.py')
-rw-r--r--tools/fix_style.py45
1 files changed, 37 insertions, 8 deletions
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