Bug 1733814 - Add /run/host/local-fonts to sandbox r=gcp
[gecko.git] / mach
blobf46c5c4cf4d863434c3abd7c76ebfb093d0d8a56
1 #!/bin/sh
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 # The beginning of this script is both valid POSIX shell and valid Python,
7 # such that the script starts with the shell and is reexecuted with
8 # the right Python.
10 # Embeds a shell script inside a Python triple quote. This pattern is valid
11 # shell because `''':'`, `':'` and `:` are all equivalent, and `:` is a no-op.
12 ''':'
14 # Commands that are to be run with the system Python 3 instead of the
15 # virtualenv.
16 nativecmds="
17 bootstrap
18 create-mach-environment
19 install-moz-phab
22 run_py() {
23 # Try to run a specific Python interpreter.
24 py_executable="$1"
25 shift
26 if command -v "$py_executable" > /dev/null
27 then
28 exec "$py_executable" $py_profile_command_args "$0" "$@"
29 else
30 echo "This mach command requires $py_executable, which wasn't found on the system!"
31 case "$py_executable" in
32 python3) ;;
34 echo "Consider running 'mach bootstrap' or 'mach create-mach-environment' to create the mach virtualenvs, or set MACH_USE_SYSTEM_PYTHON to use the system Python installation over a virtualenv."
36 esac
37 exit 1
41 get_command() {
42 # Parse the name of the mach command out of the arguments. This is necessary
43 # in the presence of global mach arguments that come before the name of the
44 # command, e.g. `mach -v build`. We dispatch to the correct Python
45 # interpreter depending on the command.
46 while true; do
47 case $1 in
48 -v|--verbose) shift;;
49 -l|--log-file)
50 if [ "$#" -lt 2 ]
51 then
52 echo
53 break
54 else
55 shift 2
58 --no-interactive) shift;;
59 --log-interval) shift;;
60 --log-no-times) shift;;
61 -h) shift;;
62 --debug-command) shift;;
63 --profile-command)
64 py_profile_command="1"
65 shift;;
66 --settings)
67 if [ "$#" -lt 2 ]
68 then
69 echo
70 break
71 else
72 shift 2
75 "") echo; break;;
76 *) echo $1; break;;
77 esac
78 done
79 return ${py_profile_command}
82 state_dir=${MOZBUILD_STATE_PATH:-~/.mozbuild}
83 command=$(get_command "$@")
84 py_profile_command=$?
86 if [ ${py_profile_command} -eq 0 ]
87 then
88 py_profile_command_args=""
89 else
90 # We would prefer to use an array variable here, but we're limited to POSIX.
91 # None of our arguments have quoting or spaces so we can safely interpolate
92 # a string instead.
93 py_profile_command_args="-m cProfile -o mach_profile_${command}.cProfile"
94 echo "Running with --profile-command. To visualize, use snakeviz:"
95 echo "$HOME/.mozbuild/_virtualenvs/mach/bin/python -m pip install snakeviz"
96 echo "$HOME/.mozbuild/_virtualenvs/mach/bin/python -m snakeviz mach_profile_${command}.cProfile"
99 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
100 # python 3 executables and not the virtualenv locations.
101 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
102 then
103 case "$OSTYPE" in
104 cygwin|msys|win32) bin_path=Scripts;;
105 *) bin_path=bin;;
106 esac
107 py3executable=$state_dir/_virtualenvs/mach/$bin_path/python
108 else
109 py3executable=python3
112 # Check whether we need to run with the native Python 3 interpreter.
113 case " $(echo $nativecmds) " in
114 *\ $command\ *)
115 run_py python3 "$@"
117 esac
119 # # Use the mach virtualenv's Python 3 for the rest of the commands.
120 run_py "$py3executable" "$@"
123 from __future__ import absolute_import, print_function, unicode_literals
125 import os
126 import sys
129 def load_mach(dir_path, mach_path):
130 import importlib.util
131 spec = importlib.util.spec_from_file_location('mach_initialize', mach_path)
132 mach_initialize = importlib.util.module_from_spec(spec)
133 spec.loader.exec_module(mach_initialize)
134 return mach_initialize.initialize(dir_path)
137 def check_and_get_mach(dir_path):
138 initialize_paths = (
139 'build/mach_initialize.py',
140 # test package initialize
141 'tools/mach_initialize.py',
143 for initialize_path in initialize_paths:
144 mach_path = os.path.join(dir_path, initialize_path)
145 if os.path.isfile(mach_path):
146 return load_mach(dir_path, mach_path)
147 return None
150 def main(args):
151 # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
152 # used when a python subprocess is created. This is an issue when we want
153 # to run using our virtualenv python executables.
154 # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
155 # application code (mach) is started.
156 # https://github.com/python/cpython/pull/9516
157 os.environ.pop("__PYVENV_LAUNCHER__", None)
159 mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
160 if not mach:
161 print('Could not run mach: No mach source directory found.')
162 sys.exit(1)
163 sys.exit(mach.run(args))
166 if __name__ == '__main__':
167 main(sys.argv[1:])