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 shell and valid python,
7 # such that the script starts with the shell and is reexecuted with
9 '''which' python2.7
> /dev
/null
&& exec python2.7
"$0" "$@" ||
exec python
"$0" "$@"
12 from __future__ import print_function, unicode_literals
20 (path, child) = os.path.split(path)
24 def load_mach(dir_path, mach_path):
26 with open(mach_path, 'r
') as fh:
27 imp.load_module('mach_bootstrap
', fh, mach_path,
28 ('.py
', 'r
', imp.PY_SOURCE))
30 return mach_bootstrap.bootstrap(dir_path)
33 def check_and_get_mach(dir_path):
35 'build
/mach_bootstrap.py
',
36 # test package bootstrap
37 'tools
/mach_bootstrap.py
',
39 for bootstrap_path in bootstrap_paths:
40 mach_path = os.path.join(dir_path, bootstrap_path)
41 if os.path.isfile(mach_path):
42 return load_mach(dir_path, mach_path)
47 # Check whether the current directory is within a mach src or obj dir.
48 for dir_path in ancestors(os.getcwd()):
49 # If we find a "mozinfo.json" file, we are in the objdir.
50 mozinfo_path = os.path.join(dir_path, 'mozinfo.json
')
51 if os.path.isfile(mozinfo_path):
53 info = json.load(open(mozinfo_path))
54 if 'mozconfig
' in info and 'MOZCONFIG
' not in os.environ:
55 # If the MOZCONFIG environment variable is not already set, set it
56 # to the value from mozinfo.json. This will tell the build system
57 # to look for a config file at the path in $MOZCONFIG rather than
58 # its default locations.
60 # Note: subprocess requires native strings in os.environ on Windows
61 os.environ[b'MOZCONFIG
'] = str(info['mozconfig
'])
63 if 'topsrcdir
' in info:
64 # Continue searching for mach_bootstrap in the source directory.
65 dir_path = info['topsrcdir
']
67 mach = check_and_get_mach(dir_path)
71 # If we didn't
find a
source path by scanning
for a mozinfo.json
, check
72 # whether the directory containing this script is a source directory.
73 return check_and_get_mach
(os.path.
dirname(__file__
))
78 print
('Could not run mach: No mach source directory found.')
80 sys.
exit(mach.run
(args
))
83 if __name__
== '__main__':
84 if sys.platform
== 'win32':
85 # This is a complete hack to work around the fact that Windows
86 # multiprocessing needs to import the original module (ie: this
87 # file), but only works if it has a .py extension.
89 # We do this by a sort of two-level function interposing. The first
90 # level interposes forking.get_command_line() with our version defined
91 # in my_get_command_line(). Our version of get_command_line will
92 # replace the command string with the contents of the fork_interpose()
93 # function to be used in the subprocess.
95 # The subprocess then gets an interposed imp.find_module(), which we
96 # hack up to find 'mach' without the .py extension, since we already
97 # know where it is (it's us!). If we're not looking for 'mach', then
98 # the original find_module will suffice.
100 # See also: http://bugs.python.org/issue19946
101 # And: https://bugzilla.mozilla.org/show_bug.cgi?id=914563
103 from multiprocessing import forking
104 global orig_command_line
106 def fork_interpose
():
110 orig_find_module
= imp.find_module
111 def my_find_module
(name
, dirs):
113 path
= os.path.
join(dirs[0], 'mach')
115 return (f
, path
, ('', 'r', imp.PY_SOURCE
))
116 return orig_find_module
(name
, dirs)
118 # Don't allow writing bytecode file for mach module.
119 orig_load_module
= imp.load_module
120 def my_load_module
(name
, file, path
, description
):
121 # multiprocess.forking invokes imp.load_module manually and
122 # hard-codes the name __parents_main__ as the module name.
123 if name
== '__parents_main__':
124 old_bytecode
= sys.dont_write_bytecode
125 sys.dont_write_bytecode
= True
127 return orig_load_module
(name
, file, path
, description
)
129 sys.dont_write_bytecode
= old_bytecode
131 return orig_load_module
(name
, file, path
, description
)
133 imp.find_module
= my_find_module
134 imp.load_module
= my_load_module
135 from multiprocessing.forking import main
; main
()
137 def my_get_command_line
():
138 fork_code
, lineno
= inspect.getsourcelines
(fork_interpose
)
139 # Remove the first line (for 'def fork_interpose():') and the three
140 # levels of indentation (12 spaces).
141 fork_string
= ''.
join(x
[12:] for x
in fork_code
[1:])
142 cmdline
= orig_command_line
()
143 cmdline
[2] = fork_string
145 orig_command_line
= forking.get_command_line
146 forking.get_command_line
= my_get_command_line