java_action.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. # Copyright 2015 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. """Wrapper script to run java command as action with gn."""
  6. import os
  7. import subprocess
  8. import sys
  9. EXIT_SUCCESS = 0
  10. EXIT_FAILURE = 1
  11. def IsExecutable(path):
  12. """Returns whether file at |path| exists and is executable.
  13. Args:
  14. path: absolute or relative path to test.
  15. Returns:
  16. True if the file at |path| exists, False otherwise.
  17. """
  18. return os.path.isfile(path) and os.access(path, os.X_OK)
  19. def FindCommand(command):
  20. """Looks up for |command| in PATH.
  21. Args:
  22. command: name of the command to lookup, if command is a relative or
  23. absolute path (i.e. contains some path separator) then only that
  24. path will be tested.
  25. Returns:
  26. Full path to command or None if the command was not found.
  27. On Windows, this respects the PATHEXT environment variable when the
  28. command name does not have an extension.
  29. """
  30. fpath, _ = os.path.split(command)
  31. if fpath:
  32. if IsExecutable(command):
  33. return command
  34. if sys.platform == 'win32':
  35. # On Windows, if the command does not have an extension, cmd.exe will
  36. # try all extensions from PATHEXT when resolving the full path.
  37. command, ext = os.path.splitext(command)
  38. if not ext:
  39. exts = os.environ['PATHEXT'].split(os.path.pathsep)
  40. else:
  41. exts = [ext]
  42. else:
  43. exts = ['']
  44. for path in os.environ['PATH'].split(os.path.pathsep):
  45. for ext in exts:
  46. path = os.path.join(path, command) + ext
  47. if IsExecutable(path):
  48. return path
  49. return None
  50. def main():
  51. java_path = FindCommand('java')
  52. if not java_path:
  53. sys.stderr.write('java: command not found\n')
  54. sys.exit(EXIT_FAILURE)
  55. args = sys.argv[1:]
  56. if len(args) < 2 or args[0] != '-jar':
  57. sys.stderr.write('usage: %s -jar JARPATH [java_args]...\n' % sys.argv[0])
  58. sys.exit(EXIT_FAILURE)
  59. return subprocess.check_call([java_path] + args)
  60. if __name__ == '__main__':
  61. sys.exit(main())