| 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 |
import os |
| 6 |
import sys |
| 7 |
|
| 8 |
sys.path.insert(1, os.path.dirname(sys.path[0])) # noqa - don't warn about imports |
| 9 |
|
| 10 |
from mozharness.base.log import FATAL |
| 11 |
from mozharness.base.script import BaseScript |
| 12 |
|
| 13 |
|
| 14 |
class Repackage(BaseScript): |
| 15 |
def __init__(self, require_config_file=False): |
| 16 |
script_kwargs = { |
| 17 |
"all_actions": [ |
| 18 |
"setup", |
| 19 |
"repackage", |
| 20 |
], |
| 21 |
} |
| 22 |
BaseScript.__init__( |
| 23 |
self, require_config_file=require_config_file, **script_kwargs |
| 24 |
) |
| 25 |
|
| 26 |
def setup(self): |
| 27 |
dirs = self.query_abs_dirs() |
| 28 |
|
| 29 |
self._run_tooltool() |
| 30 |
|
| 31 |
mar_path = os.path.join(dirs["abs_input_dir"], "mar") |
| 32 |
if self._is_windows(): |
| 33 |
mar_path += ".exe" |
| 34 |
if mar_path and os.path.exists(mar_path): |
| 35 |
self.chmod(mar_path, 0o755) |
| 36 |
if self.config.get("run_configure", True): |
| 37 |
self._get_mozconfig() |
| 38 |
self._run_configure() |
| 39 |
|
| 40 |
def query_abs_dirs(self): |
| 41 |
if self.abs_dirs: |
| 42 |
return self.abs_dirs |
| 43 |
abs_dirs = super(Repackage, self).query_abs_dirs() |
| 44 |
config = self.config |
| 45 |
|
| 46 |
dirs = {} |
| 47 |
dirs["abs_input_dir"] = os.path.join(abs_dirs["base_work_dir"], "fetches") |
| 48 |
output_dir_suffix = [] |
| 49 |
if config.get("locale"): |
| 50 |
output_dir_suffix.append(config["locale"]) |
| 51 |
if config.get("repack_id"): |
| 52 |
output_dir_suffix.append(config["repack_id"]) |
| 53 |
dirs["abs_output_dir"] = os.path.join( |
| 54 |
abs_dirs["abs_work_dir"], "outputs", *output_dir_suffix |
| 55 |
) |
| 56 |
for key in dirs.keys(): |
| 57 |
if key not in abs_dirs: |
| 58 |
abs_dirs[key] = dirs[key] |
| 59 |
self.abs_dirs = abs_dirs |
| 60 |
return self.abs_dirs |
| 61 |
|
| 62 |
def repackage(self): |
| 63 |
config = self.config |
| 64 |
dirs = self.query_abs_dirs() |
| 65 |
|
| 66 |
subst = { |
| 67 |
"package-name": config["package-name"], |
| 68 |
# sfx-stub is only defined for Windows targets |
| 69 |
"sfx-stub": config.get("sfx-stub"), |
| 70 |
"installer-tag": config["installer-tag"], |
| 71 |
"stub-installer-tag": config["stub-installer-tag"], |
| 72 |
"deb-templates": config["deb-templates"], |
| 73 |
"rpm-templates": config["rpm-templates"], |
| 74 |
"deb-l10n-templates": config["deb-l10n-templates"], |
| 75 |
"flatpak-templates": config.get("flatpak-templates"), |
| 76 |
"wsx-stub": config["wsx-stub"], |
| 77 |
"extensions-dir": config["extensions-dir"], |
| 78 |
} |
| 79 |
subst.update(dirs) |
| 80 |
if config.get("fetch-dir"): |
| 81 |
subst.update({"fetch-dir": os.path.abspath(config["fetch-dir"])}) |
| 82 |
|
| 83 |
# Make sure the upload dir is around. |
| 84 |
self.mkdir_p(dirs["abs_output_dir"]) |
| 85 |
|
| 86 |
for repack_config in config["repackage_config"]: |
| 87 |
command = [sys.executable, "mach", "--log-no-times", "repackage"] |
| 88 |
command.extend([arg.format(**subst) for arg in repack_config["args"]]) |
| 89 |
for arg, filename in repack_config["inputs"].items(): |
| 90 |
command.extend( |
| 91 |
[ |
| 92 |
f"--{arg}", |
| 93 |
os.path.join(dirs["abs_input_dir"], filename), |
| 94 |
] |
| 95 |
) |
| 96 |
command.extend( |
| 97 |
[ |
| 98 |
"--output", |
| 99 |
os.path.join(dirs["abs_output_dir"], repack_config["output"]), |
| 100 |
] |
| 101 |
) |
| 102 |
self.run_command( |
| 103 |
command=command, |
| 104 |
cwd=dirs["abs_src_dir"], |
| 105 |
halt_on_failure=True, |
| 106 |
env=self.query_env(), |
| 107 |
) |
| 108 |
|
| 109 |
def _run_tooltool(self): |
| 110 |
config = self.config |
| 111 |
dirs = self.query_abs_dirs() |
| 112 |
manifest_src = os.environ.get("TOOLTOOL_MANIFEST") |
| 113 |
if not manifest_src: |
| 114 |
manifest_src = config.get("tooltool_manifest_src") |
| 115 |
if not manifest_src: |
| 116 |
return |
| 117 |
|
| 118 |
cmd = [ |
| 119 |
sys.executable, |
| 120 |
"-u", |
| 121 |
os.path.join(dirs["abs_src_dir"], "mach"), |
| 122 |
"artifact", |
| 123 |
"toolchain", |
| 124 |
"-v", |
| 125 |
"--retry", |
| 126 |
"4", |
| 127 |
"--artifact-manifest", |
| 128 |
os.path.join(dirs["abs_src_dir"], "toolchains.json"), |
| 129 |
] |
| 130 |
if manifest_src: |
| 131 |
cmd.extend( |
| 132 |
[ |
| 133 |
"--tooltool-manifest", |
| 134 |
os.path.join(dirs["abs_src_dir"], manifest_src), |
| 135 |
] |
| 136 |
) |
| 137 |
cache = config.get("tooltool_cache") |
| 138 |
if cache: |
| 139 |
cmd.extend(["--cache-dir", cache]) |
| 140 |
self.info(str(cmd)) |
| 141 |
self.run_command(cmd, cwd=dirs["abs_src_dir"], halt_on_failure=True) |
| 142 |
|
| 143 |
def _get_mozconfig(self): |
| 144 |
"""assign mozconfig.""" |
| 145 |
c = self.config |
| 146 |
dirs = self.query_abs_dirs() |
| 147 |
abs_mozconfig_path = "" |
| 148 |
|
| 149 |
# first determine the mozconfig path |
| 150 |
if c.get("src_mozconfig"): |
| 151 |
self.info("Using in-tree mozconfig") |
| 152 |
abs_mozconfig_path = os.path.join(dirs["abs_src_dir"], c["src_mozconfig"]) |
| 153 |
else: |
| 154 |
self.fatal( |
| 155 |
"'src_mozconfig' must be in the config " |
| 156 |
"in order to determine the mozconfig." |
| 157 |
) |
| 158 |
|
| 159 |
# print its contents |
| 160 |
self.read_from_file(abs_mozconfig_path, error_level=FATAL) |
| 161 |
|
| 162 |
# finally, copy the mozconfig to a path that 'mach build' expects it to be |
| 163 |
self.copyfile( |
| 164 |
abs_mozconfig_path, os.path.join(dirs["abs_src_dir"], ".mozconfig") |
| 165 |
) |
| 166 |
|
| 167 |
def _run_configure(self): |
| 168 |
dirs = self.query_abs_dirs() |
| 169 |
command = [sys.executable, "mach", "--log-no-times", "configure"] |
| 170 |
return self.run_command( |
| 171 |
command=command, |
| 172 |
cwd=dirs["abs_src_dir"], |
| 173 |
output_timeout=60 * 3, |
| 174 |
halt_on_failure=True, |
| 175 |
) |
| 176 |
|
| 177 |
|
| 178 |
if __name__ == "__main__": |
| 179 |
repack = Repackage() |
| 180 |
repack.run_and_exit() |