tooltool.py (3804B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 """module for tooltool operations""" 6 7 import os 8 import sys 9 10 from mozharness.base.errors import PythonErrorList 11 from mozharness.base.log import ERROR, FATAL 12 13 TooltoolErrorList = PythonErrorList + [{"substr": "ERROR - ", "level": ERROR}] 14 15 16 _here = os.path.abspath(os.path.dirname(__file__)) 17 _external_tools_path = os.path.normpath( 18 os.path.join(_here, "..", "..", "external_tools") 19 ) 20 _mozbuild_action_path = os.path.normpath( 21 os.path.join( 22 _here, "..", "..", "..", "..", "python", "mozbuild", "mozbuild", "action" 23 ) 24 ) 25 _mozbuild_action_abspath = os.path.join("python", "mozbuild", "mozbuild", "action") 26 27 28 class TooltoolMixin: 29 """Mixin class for handling tooltool manifests. 30 To use a tooltool server other than the Mozilla server, set 31 TOOLTOOL_HOST in the environment. 32 """ 33 34 def tooltool_fetch(self, manifest, output_dir=None, privileged=False, cache=None): 35 """docstring for tooltool_fetch""" 36 if cache is None: 37 cache = os.environ.get("TOOLTOOL_CACHE") 38 39 for d in (output_dir, cache): 40 if d is not None and not os.path.exists(d): 41 self.mkdir_p(d) 42 if self.topsrcdir: 43 cmd = [ 44 sys.executable, 45 "-u", 46 os.path.join(self.topsrcdir, "mach"), 47 "artifact", 48 "toolchain", 49 "-v", 50 ] 51 else: 52 # In CI, tooltool.py is expected in external_tools; in local runs, 53 # tooltool.py is expected in mozbuild, but mozbuild might be hard 54 # to find, expecially if topsrcdir could not be found... 55 locations = [ 56 os.path.join(_external_tools_path, "tooltool.py"), 57 os.path.join(_mozbuild_action_path, "tooltool.py"), 58 os.path.join( 59 os.environ.get("GECKO_PATH", "."), 60 _mozbuild_action_abspath, 61 "tooltool.py", 62 ), 63 ] 64 tooltool_path = [p for p in locations if os.path.exists(p)] 65 if not tooltool_path: 66 raise Exception("unable to find tooltool client file tooltool.py") 67 cmd = [ 68 sys.executable, 69 "-u", 70 tooltool_path[0], 71 ] 72 73 if self.topsrcdir: 74 cmd.extend(["--tooltool-manifest", manifest]) 75 cmd.extend([ 76 "--artifact-manifest", 77 os.path.join(self.topsrcdir, "toolchains.json"), 78 ]) 79 else: 80 cmd.extend(["fetch", "-m", manifest, "-o"]) 81 82 if cache: 83 cmd.extend(["--cache-dir" if self.topsrcdir else "-c", cache]) 84 85 timeout = self.config.get("tooltool_timeout", 10 * 60) 86 87 self.retry( 88 self.run_command, 89 args=(cmd,), 90 kwargs={ 91 "cwd": output_dir, 92 "error_list": TooltoolErrorList, 93 "privileged": privileged, 94 "output_timeout": timeout, 95 }, 96 good_statuses=(0,), 97 error_message="Tooltool %s fetch failed!" % manifest, 98 error_level=FATAL, 99 ) 100 101 def create_tooltool_manifest(self, contents, path=None): 102 """Currently just creates a manifest, given the contents. 103 We may want a template and individual values in the future? 104 """ 105 if path is None: 106 dirs = self.query_abs_dirs() 107 path = os.path.join(dirs["abs_work_dir"], "tooltool.tt") 108 self.write_to_file(path, contents, error_level=FATAL) 109 return path