google_java_format.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. # Copyright 2014 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Redirects to the version of google-java-format checked into the Chrome tree.
  6. google-java-format executable is pulled down from the cipd storage whenever
  7. you sync Chrome. This script finds and runs the executable.
  8. """
  9. import gclient_paths
  10. import os
  11. import subprocess
  12. import sys
  13. def FindGoogleJavaFormat():
  14. """Returns the path to the google-java-format executable."""
  15. # Allow non-chromium projects to use a custom location.
  16. primary_solution_path = gclient_paths.GetPrimarySolutionPath()
  17. if primary_solution_path:
  18. override = os.environ.get('GOOGLE_JAVA_FORMAT_PATH')
  19. if override:
  20. # Make relative to solution root if not an absolute path.
  21. return os.path.join(primary_solution_path, override)
  22. path = os.path.join(primary_solution_path, 'third_party',
  23. 'google-java-format', 'google-java-format')
  24. # Check that the .jar exists, since it is conditionally downloaded via
  25. # DEPS conditions.
  26. if os.path.exists(path) and os.path.exists(path + '.jar'):
  27. return path
  28. return None
  29. def main(args):
  30. google_java_format = FindGoogleJavaFormat()
  31. if google_java_format is None:
  32. # Fail silently. It could be we are on an old chromium revision,
  33. # or that it is a non-chromium project. https://crbug.com/1491627.
  34. print('google-java-format not found, skipping java formatting.')
  35. return 0
  36. # Add some visibility to --help showing where the tool lives, since this
  37. # redirection can be a little opaque.
  38. help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list')
  39. if any(match in args for match in help_syntax):
  40. print('\nDepot tools redirects you to the google-java-format at:\n' +
  41. ' %s\n' % google_java_format)
  42. return subprocess.call([google_java_format] + args)
  43. if __name__ == '__main__':
  44. try:
  45. sys.exit(main(sys.argv[1:]))
  46. except KeyboardInterrupt:
  47. sys.stderr.write('interrupted\n')
  48. sys.exit(1)