Skip to content

Commit

Permalink
Fix recursive globbing in tools/format.py
Browse files Browse the repository at this point in the history
And use third_party/depot_tools/gn.
  • Loading branch information
ry committed Jul 24, 2018
1 parent 1de16af commit 7baf8a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
26 changes: 14 additions & 12 deletions tools/format.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -12,15 +10,19 @@
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 = []
Expand All @@ -29,4 +31,4 @@
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"))
13 changes: 13 additions & 0 deletions tools/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7baf8a0

Please sign in to comment.