gecko-dev/mach
Mitchell Hentges 07f875d674 Bug 1725895: Port --profile-command to pure-Python r=nalexander,glandium
As part of this, the shell-script part of `./mach` can be removed,
making it pure Python.

There's a change in `--profile-command` behaviour, though: it now only
profiles the specific command, rather than all of Mach.
This is because _so much of Mach_ has already been run before
CLI arguments are parsed in the Python process.

If a developer wants to profile Mach itself, they can manually run
`python3 -m cProfile -o <file> ./mach ...`

Differential Revision: https://phabricator.services.mozilla.com/D133928
2022-01-04 21:07:32 +00:00

53 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, print_function, unicode_literals
import os
import sys
def load_mach(dir_path, mach_path):
import importlib.util
spec = importlib.util.spec_from_file_location('mach_initialize', mach_path)
mach_initialize = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mach_initialize)
return mach_initialize.initialize(dir_path)
def check_and_get_mach(dir_path):
initialize_paths = (
# Run Thunderbird's mach_initialize.py if it exists
'comm/build/mach_initialize.py',
'build/mach_initialize.py',
# test package initialize
'tools/mach_initialize.py',
)
for initialize_path in initialize_paths:
mach_path = os.path.join(dir_path, initialize_path)
if os.path.isfile(mach_path):
return load_mach(dir_path, mach_path)
return None
def main(args):
# XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
# used when a python subprocess is created. This is an issue when we want
# to run using our virtualenv python executables.
# In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
# application code (mach) is started.
# https://github.com/python/cpython/pull/9516
os.environ.pop("__PYVENV_LAUNCHER__", None)
mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
if not mach:
print('Could not run mach: No mach source directory found.')
sys.exit(1)
sys.exit(mach.run(args))
if __name__ == '__main__':
main(sys.argv[1:])