commandrunner.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright 2014 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. """This program is a wrapper to run external commands. It determines
  12. what to run, sets up the environment and executes the command."""
  13. import sys, os, subprocess, shutil, shlex
  14. import re
  15. def run_command(source_dir, build_dir, subdir, meson_command, command, arguments):
  16. env = {'MESON_SOURCE_ROOT': source_dir,
  17. 'MESON_BUILD_ROOT': build_dir,
  18. 'MESON_SUBDIR': subdir,
  19. 'MESONINTROSPECT': ' '.join([shlex.quote(x) for x in meson_command + ['introspect']]),
  20. }
  21. cwd = os.path.join(source_dir, subdir)
  22. child_env = os.environ.copy()
  23. child_env.update(env)
  24. # Is the command an executable in path?
  25. exe = shutil.which(command)
  26. if exe is not None:
  27. command_array = [exe] + arguments
  28. else:# No? Maybe it is a script in the source tree.
  29. fullpath = os.path.join(source_dir, subdir, command)
  30. command_array = [fullpath] + arguments
  31. try:
  32. return subprocess.Popen(command_array, env=child_env, cwd=cwd)
  33. except FileNotFoundError:
  34. print('Could not execute command "%s". File not found.' % command)
  35. sys.exit(1)
  36. except PermissionError:
  37. print('Could not execute command "%s". File not executable.' % command)
  38. sys.exit(1)
  39. except OSError as err:
  40. print('Could not execute command "{}": {}'.format(command, err))
  41. sys.exit(1)
  42. except subprocess.SubprocessError as err:
  43. print('Could not execute command "{}": {}'.format(command, err))
  44. sys.exit(1)
  45. def is_python_command(cmdname):
  46. end_py_regex = r'python(3|3\.\d+)?(\.exe)?$'
  47. return re.search(end_py_regex, cmdname) is not None
  48. def run(args):
  49. if len(args) < 4:
  50. print('commandrunner.py <source dir> <build dir> <subdir> <command> [arguments]')
  51. return 1
  52. src_dir = args[0]
  53. build_dir = args[1]
  54. subdir = args[2]
  55. meson_command = args[3]
  56. if is_python_command(meson_command):
  57. meson_command = [meson_command, args[4]]
  58. command = args[5]
  59. arguments = args[6:]
  60. else:
  61. meson_command = [meson_command]
  62. command = args[4]
  63. arguments = args[5:]
  64. pc = run_command(src_dir, build_dir, subdir, meson_command, command, arguments)
  65. while True:
  66. try:
  67. pc.wait()
  68. break
  69. except KeyboardInterrupt:
  70. pass
  71. return pc.returncode
  72. if __name__ == '__main__':
  73. sys.exit(run(sys.argv[1:]))