Ejemplo n.º 1
0
    def save(self, locale):
        if not self.source_resource:
            raise SyncError(
                'Cannot save resource {0}: No source resource given.'
                .format(self.path)
            )

        # A dictionary of new translations
        new_l10n = {
            key: entity.strings[None] if entity.strings else None
            for key, entity in self.entities.items()
        }

        # Create parent folders if necessary
        create_parent_directory(self.path)

        with open(self.path, 'w') as output_file:
            log.debug('Saving file: %s', self.path)
            output_file.write(
                serializer.serialize(
                    self.path,
                    self.source_resource.parsed_objects,
                    self.parsed_objects,
                    new_l10n,
                )
            )
Ejemplo n.º 2
0
    def save(self, locale):
        if not self.source_resource:
            raise SyncError(
                f"Cannot save resource {self.path}: No source resource given."
            )

        # A dictionary of new translations
        new_l10n = {
            key: entity.strings[None] if entity.strings else None
            for key, entity in self.entities.items()
        }

        # Create parent folders if necessary
        create_parent_directory(self.path)

        with open(self.path, "wb") as output_file:
            log.debug("Saving file: %s", self.path)
            output_file.write(
                serializer.serialize(
                    self.path,
                    self.source_resource.parsed_objects,
                    self.parsed_objects,
                    new_l10n,
                )
            )
Ejemplo n.º 3
0
 def _test(self, old_content, new_data, expected):
     """Test with old content, new data, and the reference data
     against the expected unicode output.
     """
     self.parser.readUnicode(old_content)
     old_l10n = list(self.parser.walk())
     output = serialize(self.name, self.reference, old_l10n, new_data)
     self.assertMultiLineEqual(output.decode(self.parser.encoding),
                               expected)
Ejemplo n.º 4
0
 def _test(self, old_content, new_data, expected):
     """Test with old content, new data, and the reference data
     against the expected unicode output.
     """
     self.parser.readUnicode(old_content)
     old_l10n = list(self.parser.walk())
     output = serialize(self.name, self.reference, old_l10n, new_data)
     self.assertMultiLineEqual(
         output.decode(self.parser.encoding),
         expected
     )
Ejemplo n.º 5
0
def main():
    p = argparse.ArgumentParser(
        description="Remove obsolete strings and reformat files in localized repositories"
    )

    p.add_argument("--noupdates", help="Do not pull from remote", action="store_true")
    p.add_argument(
        "--wetrun",
        help="Commit local changes and push them to remote",
        action="store_true",
    )
    p.add_argument(
        "--locale", help="Run on a specific locale", action="store", default=""
    )
    args = p.parse_args()

    # Read paths from config file
    [l10n_clones_path, quarantine_path] = local_config.read_config(
        ["l10n_clones_path", "quarantine_path"]
    )

    if args.locale:
        locales = [args.locale]
    else:
        locales = [x for x in os.listdir(l10n_clones_path) if not x.startswith(".")]
        # Exclude locales still working on Mercurial directly
        excluded_locales = [
            "ja",
            "ja-JP-mac",
        ]
        locales = sorted([x for x in locales if x not in excluded_locales])

    # Get a list of supported files in the source repository
    source_file_list = extractFileList(quarantine_path)

    for locale in locales:
        print("Locale: {}".format(locale))
        locale_path = os.path.join(l10n_clones_path, locale)

        # Update locale repository, unless --noupdates was called explicitly
        if not args.noupdates:
            print("Updating repository...")
            subprocess.run(["hg", "-R", locale_path, "pull", "-u"])

        # Create list of target files
        target_file_list = extractFileList(locale_path)

        # Ignore files in locale that are not available in the source
        target_file_list = [f for f in target_file_list if f in source_file_list]

        # Read source and target and write the output overwriting the existing
        # localized file
        for filename in target_file_list:
            source_filename = os.path.join(quarantine_path, filename)
            target_filename = os.path.join(locale_path, filename)
            with open(source_filename) as f:
                source_content = f.read()
                source_parser = getParser(filename)
                source_parser.readUnicode(source_content)
                reference = list(source_parser.walk())
            with open(target_filename) as f:
                target_content = f.read()
                target_parser = getParser(filename)
                target_parser.readUnicode(target_content)
                target = list(target_parser.walk())

            output = serialize(filename, reference, target, {})

            # zh-CN has an extra string that needs to be kept (start page)
            if (
                locale == "zh-CN"
                and filename == "browser/chrome/browser/browser.properties"
            ):
                output += b"\n# DO NOT REMOVE: this string is used to set up the home page for zh-CN\n"
                output += b"browser.startup.homepage = https://start.firefoxchina.cn\n"

            with open(target_filename, "wb") as f:
                f.write(output)

        if args.wetrun:
            # Commit changes
            subprocess.run(["hg", "-R", locale_path, "addremove"])
            subprocess.run(
                [
                    "hg",
                    "-R",
                    locale_path,
                    "commit",
                    "-m",
                    "Remove obsolete strings and reformat files",
                ]
            )
            subprocess.run(["hg", "-R", locale_path, "push"])
        else:
            print("(dry run)")
Ejemplo n.º 6
0
 def test_nothing_new_or_old(self):
     output = serialize(self.name, self.reference, [], {})
     self.assertMultiLineEqual(output.decode(self.parser.encoding), '\n\n')