2 # Copyright (c) 2020 Project CHIP Authors
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # Copyright 2015 The Chromium Authors. All rights reserved.
17 # Use of this source code is governed by a BSD-style license that can be
18 # found in the LICENSE file.
20 """Wrapper script to run javac command as an action with gn."""
30 def IsExecutable(path):
31 """Returns whether file at |path| exists and is executable.
34 path: absolute or relative path to test.
37 True if the file at |path| exists, False otherwise.
39 return os.path.isfile(path) and os.access(path, os.X_OK)
42 def FindCommand(command):
43 """Looks up for |command| in PATH.
46 command: name of the command to lookup, if command is a relative or
47 absolute path (i.e. contains some path separator) then only that
51 Full path to command or None if the command was not found.
53 On Windows, this respects the PATHEXT environment variable when the
54 command name does not have an extension.
56 fpath, _ = os.path.split(command)
58 if IsExecutable(command):
61 if sys.platform == 'win32':
62 # On Windows, if the command does not have an extension, cmd.exe will
63 # try all extensions from PATHEXT when resolving the full path.
64 command, ext = os.path.splitext(command)
66 exts = os.environ['PATHEXT'].split(os.path.pathsep)
72 for path in os.environ['PATH'].split(os.path.pathsep):
74 path = os.path.join(path, command) + ext
75 if IsExecutable(path):
82 java_path = FindCommand('jar')
84 sys.stderr.write('jar: command not found\n')
85 sys.exit(EXIT_FAILURE)
89 sys.stderr.write('usage: %s [jar_args]...\n' % sys.argv[0])
90 sys.exit(EXIT_FAILURE)
92 return subprocess.check_call([java_path] + args)
95 if __name__ == '__main__':