tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

repackage.py (5915B)


      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().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                    f"--{arg}",
     92                    os.path.join(dirs["abs_input_dir"], filename),
     93                ])
     94            command.extend([
     95                "--output",
     96                os.path.join(dirs["abs_output_dir"], repack_config["output"]),
     97            ])
     98            self.run_command(
     99                command=command,
    100                cwd=dirs["abs_src_dir"],
    101                halt_on_failure=True,
    102                env=self.query_env(),
    103            )
    104 
    105    def _run_tooltool(self):
    106        config = self.config
    107        dirs = self.query_abs_dirs()
    108        manifest_src = os.environ.get("TOOLTOOL_MANIFEST")
    109        if not manifest_src:
    110            manifest_src = config.get("tooltool_manifest_src")
    111        if not manifest_src:
    112            return
    113 
    114        cmd = [
    115            sys.executable,
    116            "-u",
    117            os.path.join(dirs["abs_src_dir"], "mach"),
    118            "artifact",
    119            "toolchain",
    120            "-v",
    121            "--retry",
    122            "4",
    123            "--artifact-manifest",
    124            os.path.join(dirs["abs_src_dir"], "toolchains.json"),
    125        ]
    126        if manifest_src:
    127            cmd.extend([
    128                "--tooltool-manifest",
    129                os.path.join(dirs["abs_src_dir"], manifest_src),
    130            ])
    131        cache = config.get("tooltool_cache")
    132        if cache:
    133            cmd.extend(["--cache-dir", cache])
    134        self.info(str(cmd))
    135        self.run_command(cmd, cwd=dirs["abs_src_dir"], halt_on_failure=True)
    136 
    137    def _get_mozconfig(self):
    138        """assign mozconfig."""
    139        c = self.config
    140        dirs = self.query_abs_dirs()
    141        abs_mozconfig_path = ""
    142 
    143        # first determine the mozconfig path
    144        if c.get("src_mozconfig"):
    145            self.info("Using in-tree mozconfig")
    146            abs_mozconfig_path = os.path.join(dirs["abs_src_dir"], c["src_mozconfig"])
    147        else:
    148            self.fatal(
    149                "'src_mozconfig' must be in the config "
    150                "in order to determine the mozconfig."
    151            )
    152 
    153        # print its contents
    154        self.read_from_file(abs_mozconfig_path, error_level=FATAL)
    155 
    156        # finally, copy the mozconfig to a path that 'mach build' expects it to be
    157        self.copyfile(
    158            abs_mozconfig_path, os.path.join(dirs["abs_src_dir"], ".mozconfig")
    159        )
    160 
    161    def _run_configure(self):
    162        dirs = self.query_abs_dirs()
    163        command = [sys.executable, "mach", "--log-no-times", "configure"]
    164        return self.run_command(
    165            command=command,
    166            cwd=dirs["abs_src_dir"],
    167            output_timeout=60 * 3,
    168            halt_on_failure=True,
    169        )
    170 
    171 
    172 if __name__ == "__main__":
    173    repack = Repackage()
    174    repack.run_and_exit()