jar_runner.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # Copyright (c) 2020 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Copyright 2015 The Chromium Authors. All rights reserved.
  16. # Use of this source code is governed by a BSD-style license that can be
  17. # found in the LICENSE file.
  18. """Wrapper script to run javac command as an action with gn."""
  19. import os
  20. import subprocess
  21. import sys
  22. EXIT_SUCCESS = 0
  23. EXIT_FAILURE = 1
  24. def IsExecutable(path):
  25. """Returns whether file at |path| exists and is executable.
  26. Args:
  27. path: absolute or relative path to test.
  28. Returns:
  29. True if the file at |path| exists, False otherwise.
  30. """
  31. return os.path.isfile(path) and os.access(path, os.X_OK)
  32. def FindCommand(command):
  33. """Looks up for |command| in PATH.
  34. Args:
  35. command: name of the command to lookup, if command is a relative or
  36. absolute path (i.e. contains some path separator) then only that
  37. path will be tested.
  38. Returns:
  39. Full path to command or None if the command was not found.
  40. On Windows, this respects the PATHEXT environment variable when the
  41. command name does not have an extension.
  42. """
  43. fpath, _ = os.path.split(command)
  44. if fpath:
  45. if IsExecutable(command):
  46. return command
  47. if sys.platform == 'win32':
  48. # On Windows, if the command does not have an extension, cmd.exe will
  49. # try all extensions from PATHEXT when resolving the full path.
  50. command, ext = os.path.splitext(command)
  51. if not ext:
  52. exts = os.environ['PATHEXT'].split(os.path.pathsep)
  53. else:
  54. exts = [ext]
  55. else:
  56. exts = ['']
  57. for path in os.environ['PATH'].split(os.path.pathsep):
  58. for ext in exts:
  59. path = os.path.join(path, command) + ext
  60. if IsExecutable(path):
  61. return path
  62. return None
  63. def main():
  64. java_path = FindCommand('jar')
  65. if not java_path:
  66. sys.stderr.write('jar: command not found\n')
  67. sys.exit(EXIT_FAILURE)
  68. args = sys.argv[1:]
  69. if len(args) < 1:
  70. sys.stderr.write('usage: %s [jar_args]...\n' % sys.argv[0])
  71. sys.exit(EXIT_FAILURE)
  72. return subprocess.check_call([java_path] + args)
  73. if __name__ == '__main__':
  74. sys.exit(main())