summaryrefslogtreecommitdiff
path: root/deps/v8/build/util/java_action.py
blob: ed9bb601de7b7b5c5f013fdc40ffd3522c7e7c20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Wrapper script to run java command as action with gn."""

import os
import subprocess
import sys

EXIT_SUCCESS = 0
EXIT_FAILURE = 1


def IsExecutable(path):
  """Returns whether file at |path| exists and is executable.

  Args:
    path: absolute or relative path to test.

  Returns:
    True if the file at |path| exists, False otherwise.
  """
  return os.path.isfile(path) and os.access(path, os.X_OK)


def FindCommand(command):
  """Looks up for |command| in PATH.

  Args:
    command: name of the command to lookup, if command is a relative or
      absolute path (i.e. contains some path separator) then only that
      path will be tested.

  Returns:
    Full path to command or None if the command was not found.

    On Windows, this respects the PATHEXT environment variable when the
    command name does not have an extension.
  """
  fpath, _ = os.path.split(command)
  if fpath:
    if IsExecutable(command):
      return command

  if sys.platform == 'win32':
    # On Windows, if the command does not have an extension, cmd.exe will
    # try all extensions from PATHEXT when resolving the full path.
    command, ext = os.path.splitext(command)
    if not ext:
      exts = os.environ['PATHEXT'].split(os.path.pathsep)
    else:
      exts = [ext]
  else:
    exts = ['']

  for path in os.environ['PATH'].split(os.path.pathsep):
    for ext in exts:
      path = os.path.join(path, command) + ext
      if IsExecutable(path):
        return path

  return None


def main():
  java_path = FindCommand('java')
  if not java_path:
    sys.stderr.write('java: command not found\n')
    sys.exit(EXIT_FAILURE)

  args = sys.argv[1:]
  if len(args) < 2 or args[0] != '-jar':
    sys.stderr.write('usage: %s -jar JARPATH [java_args]...\n' % sys.argv[0])
    sys.exit(EXIT_FAILURE)

  return subprocess.check_call([java_path] + args)


if __name__ == '__main__':
  sys.exit(main())