tor-browser

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

split_by_locale.py (3212B)


      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 This transform splits the jobs it receives into per-locale tasks. Locales are
      6 provided by the `locales-file`.
      7 """
      8 
      9 from pprint import pprint
     10 
     11 from taskgraph.transforms.base import TransformSequence
     12 from taskgraph.util.copy import deepcopy
     13 from taskgraph.util.schema import Schema
     14 from voluptuous import Extra, Optional, Required
     15 
     16 from gecko_taskgraph.transforms.l10n import parse_locales_file
     17 
     18 transforms = TransformSequence()
     19 
     20 split_by_locale_schema = Schema({
     21    # The file to pull locale information from. This should be a json file
     22    # such as browser/locales/l10n-changesets.json.
     23    Required("locales-file"): str,
     24    # The platform name in the form used by the locales files. Defaults to
     25    # attributes.build_platform if not provided.
     26    Optional("locale-file-platform"): str,
     27    # A list of properties elsewhere in the job that need to have the locale
     28    # name substituted into them. The referenced properties may be strings
     29    # or lists. In the case of the latter, all list values will have
     30    # substitutions performed.
     31    Optional("properties-with-locale"): [str],
     32    Extra: object,
     33 })
     34 
     35 
     36 transforms.add_validate(split_by_locale_schema)
     37 
     38 
     39 @transforms.add
     40 def add_command(config, jobs):
     41    for job in jobs:
     42        locales_file = job.pop("locales-file")
     43        properties_with_locale = job.pop("properties-with-locale")
     44        build_platform = job.pop(
     45            "locale-file-platform", job["attributes"]["build_platform"]
     46        )
     47 
     48        for locale in parse_locales_file(locales_file, build_platform):
     49            locale_job = deepcopy(job)
     50            locale_job["attributes"]["locale"] = locale
     51            for prop in properties_with_locale:
     52                container, subfield = locale_job, prop
     53                while "." in subfield:
     54                    f, subfield = subfield.split(".", 1)
     55                    if f not in container:
     56                        raise Exception(
     57                            f"Unable to find property {prop} to perform locale substitution on. Job is:\n{pprint(job)}"
     58                        )
     59                    container = container[f]
     60                    if not isinstance(container, dict):
     61                        raise Exception(
     62                            f"{container} is not a dict, cannot perform locale substitution. Job is:\n{pprint(job)}"
     63                        )
     64 
     65                if isinstance(container[subfield], str):
     66                    container[subfield] = container[subfield].format(locale=locale)
     67                elif isinstance(container[subfield], list):
     68                    for i in range(len(container[subfield])):
     69                        container[subfield][i] = container[subfield][i].format(
     70                            locale=locale
     71                        )
     72                else:
     73                    raise Exception(
     74                        f"Don't know how to subtitute locale for value of type: {type(container[subfield])}; value is: {container[subfield]}"
     75                    )
     76 
     77            yield locale_job