author Andreea Pavel <>
Fri, 21 Jan 2022 23:41:00 +0200
changeset 605190 00753e705770d35e46096f2894203b92910e98ce
parent 587960 a15dc32c4c2686e3ca85e1560b47c9a67dd90264
permissions -rw-r--r--
Merge autoland to mozilla-central a=merge

# 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

# script to pull and import Firefox Translations's extension source code

import os.path
from zipfile import ZipFile
import subprocess
import shutil
import sys

# the version number is the only part of this script that needs to be updated
# in order to update the in-tree version with the latest xpi version
extension_version = input(
    "Type the extension's version (tag) on Github you want to import: "
print("Importing version:", extension_version)
if not extension_version:
    sys.exit("Value can't be empty.")

extension_folder = "firefox-translations-src"

if not os.path.exists(""):
    sys.exit("This script is intended to be executed from its local folder")

have_xpi = "N"
local_xpi_file = (
    "firefox-infobar-ui/firefox-translations-" + extension_version + ".xpi"
if os.path.isfile(local_xpi_file):
    have_xpi = input(
        "Extension xpi exists. Press Y to use it or any other key to rebuild it."

if have_xpi.lower() != "y":
    # deleting old files if any
    shutil.rmtree(extension_folder, ignore_errors=True)
    # cloning the extension
            "git clone -b v" + extension_version + " "
            " "
            + extension_folder
            + " "
    # setting up the repo"yarn install".split(), cwd=extension_folder)
    # pulling bergamot-translator submodule, the repo containing the port of the
    # neural machine translation engine to wasm
        "git submodule update --init --recursive".split(),
    # build the wasm nmt module
    # import the generated wasm module to the extension
        "./ ./bergamot-translator/build-wasm/".split(),
    # build the final xpi
    env = {
        "MC": str(1),
        "yarn build:firefox-infobar-ui".split(), cwd=extension_folder, env=env

shutil.rmtree("extension", ignore_errors=True)
file_exceptions = [

def isValidFile(filename):
    for exception in file_exceptions:
        if exception in filename:
            return False
    return True

file_set = set()
# read xpi files
with ZipFile(local_xpi_file, "r") as zip:
    namelist = zip.namelist()
    cleared_namelist = []
    for filename in namelist:
        if isValidFile(filename):
            full_file_path = zip.extract(filename, "extension")
            if filename.endswith(".js"):
                filename = "browser/extensions/translations/{}".format(full_file_path)
                        "./mach lint --linter license {} --fix".format(filename)

# patching BrowserGlue.jsm
with open("../../components/BrowserGlue.jsm") as fp:
    count = 0
    Lines = fp.readlines()
    for line in Lines:
        if "resource://builtin-addons/translations/" in line:
            Lines[count - 1] = '            "{}",\n'.format(extension_version)
            with open("../../components/BrowserGlue.jsm", "w") as outfile:
        count += 1

print("Import finalized successfully")