diff --git a/tools/format.py b/tools/format.py index cb3b15a7e4..b994535f7c 100755 --- a/tools/format.py +++ b/tools/format.py @@ -1,8 +1,6 @@ #!/usr/bin/env python - import os -from glob import glob -from util import run +from util import run, find_exts root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) third_party_path = os.path.join(root_path, "third_party") @@ -12,15 +10,19 @@ tools_path = os.path.join(root_path, "tools") rustfmt_config = os.path.join(tools_path, "rustfmt.toml") os.chdir(root_path) -# TODO(ry) Install clang-format in third_party. -run(["clang-format", "-i", "-style", "Google"] + glob("src/*.cc") + - glob("src/*.h")) -for fn in ["BUILD.gn", ".gn"] + glob("build_extra/**/*.gn*"): - run(["gn", "format", fn]) + +# TODO(ry) Use third_party/depot_tools/clang-format. +run(["clang-format", "-i", "-style", "Google"] + find_exts("src", ".cc", ".h")) + +for fn in ["BUILD.gn", ".gn"] + find_exts("build_extra", ".gn", ".gni"): + run(["third_party/depot_tools/gn", "format", fn]) + # TODO(ry) Install yapf in third_party. -run(["yapf", "-i"] + glob("tools/*.py") + glob("build_extra/**/*.py")) -run(["node", prettier, "--write"] + glob("js/*.js") + glob("js/*.ts") + - ["tsconfig.json"] + ["tslint.json"]) +run(["yapf", "-i"] + find_exts("tools/", ".py") + + find_exts("build_extra", ".py")) + +run(["node", prettier, "--write"] + find_exts("js/", ".js", ".ts") + + ["tsconfig.json", "tslint.json"]) # Set RUSTFMT_FLAGS for extra flags. rustfmt_extra_args = [] @@ -29,4 +31,4 @@ if 'RUSTFMT_FLAGS' in os.environ: run([ "rustfmt", "--config-path", rustfmt_config, "--error-on-unformatted", "--write-mode", "overwrite" -] + rustfmt_extra_args + glob("src/*.rs")) +] + rustfmt_extra_args + find_exts("src/", ".rs")) diff --git a/tools/util.py b/tools/util.py index 4141396df0..4938c51579 100644 --- a/tools/util.py +++ b/tools/util.py @@ -55,3 +55,16 @@ def touch(fname): os.utime(fname, None) else: open(fname, 'a').close() + + +# Recursive search for files of certain extensions. +# (Recursive glob doesn't exist in python 2.7.) +def find_exts(directory, *extensions): + matches = [] + for root, dirnames, filenames in os.walk(directory): + for filename in filenames: + for ext in extensions: + if filename.endswith(ext): + matches.append(os.path.join(root, filename)) + break + return matches