"""Redirects to the version of google-java-format checked into the Chrome tree.
google-java-format executable is pulled down from the cipd storage whenever
you sync Chrome. This script finds and runs the executable.
"""
import gclient_paths
import os
import subprocess
import sys
def FindGoogleJavaFormat():
"""Returns the path to the google-java-format executable."""
primary_solution_path = gclient_paths.GetPrimarySolutionPath()
if primary_solution_path:
override = os.environ.get("GOOGLE_JAVA_FORMAT_PATH")
if override:
return os.path.join(primary_solution_path, override)
bin_path = os.path.join(
primary_solution_path,
"third_party",
"google-java-format",
"google-java-format",
)
cipd_path = os.path.join(
primary_solution_path,
"third_party",
"google-java-format",
"cipd",
"google-java-format.jar",
)
if os.path.exists(bin_path) and (
os.path.exists(bin_path + ".jar") or os.path.exists(cipd_path)
):
return bin_path
return None
def main(args):
google_java_format = FindGoogleJavaFormat()
if google_java_format is None:
print(
'google-java-format not found. Please run "gclient sync" to '
"download build tools.",
file=sys.stderr,
)
return 2
help_syntax = ("-h", "--help", "-help", "-help-list", "--help-list")
if any(match in args for match in help_syntax):
print(
"\nDepot tools redirects you to the google-java-format at:\n"
+ " %s\n" % google_java_format
)
return subprocess.call([google_java_format] + args)
if __name__ == "__main__":
try:
sys.exit(main(sys.argv[1:]))
except KeyboardInterrupt:
sys.stderr.write("interrupted\n")
sys.exit(1)