Example #1
0
    def run(self, migration):
        print('\nRunning migration {} for {}'.format(
            migration.__name__, self.locale))

        # For each migration create a new context.
        ctx = MigrationContext(
            self.locale, self.reference_dir, self.localization_dir
        )

        try:
            # Add the migration spec.
            migration.migrate(ctx)
        except MigrationError as e:
            print('  Skipping migration {} for {}:\n    {}'.format(
                migration.__name__, self.locale, e))
            return

        # Keep track of how many changesets we're committing.
        index = 0
        description_template = migration.migrate.__doc__

        # Annotate localization files used as sources by this migration
        # to preserve attribution of translations.
        files = ctx.localization_resources.keys()
        blame = Blame(self.client).attribution(files)
        changesets = convert_blame_to_changesets(blame)
        known_legacy_translations = set()

        for changeset in changesets:
            snapshot = self.snapshot(
                ctx, changeset['changes'], known_legacy_translations
            )
            if not snapshot:
                continue
            self.serialize_changeset(snapshot)
            index += 1
            self.commit_changeset(
                description_template, changeset['author'], index
            )
Example #2
0
def main(lang, reference_dir, localization_dir, migrations, dry_run):
    """Run migrations and commit files with the result."""
    client = hglib.open(localization_dir)

    for migration in migrations:

        print('\nRunning migration {} for {}'.format(migration.__name__, lang))

        # For each migration create a new context.
        ctx = MergeContext(lang, reference_dir, localization_dir)

        try:
            # Add the migration spec.
            migration.migrate(ctx)
        except MigrationError:
            print('  Skipping migration {} for {}'.format(
                migration.__name__, lang))
            continue

        # Keep track of how many changesets we're committing.
        index = 0

        # Annotate legacy localization files used as sources by this migration
        # to preserve attribution of translations.
        files = (path for path in ctx.localization_resources.keys()
                 if not path.endswith('.ftl'))
        blame = Blame(client).attribution(files)
        changesets = convert_blame_to_changesets(blame)
        known_legacy_translations = set()

        for changeset in changesets:
            changes_in_changeset = changeset['changes']
            known_legacy_translations.update(changes_in_changeset)
            # Run the migration for the changeset, with the set of
            # this and all prior legacy translations.
            snapshot = ctx.serialize_changeset(changes_in_changeset,
                                               known_legacy_translations)

            # Did it change any files?
            if not snapshot:
                continue

            # Write serialized FTL files to disk.
            for path, content in snapshot.iteritems():
                fullpath = os.path.join(localization_dir, path)
                print('  Writing to {}'.format(fullpath))
                if not dry_run:
                    fulldir = os.path.dirname(fullpath)
                    if not os.path.isdir(fulldir):
                        os.makedirs(fulldir)
                    with open(fullpath, 'w') as f:
                        f.write(content.encode('utf8'))
                        f.close()

            index += 1
            author = changeset['author'].encode('utf8')
            message = migration.migrate.__doc__.format(index=index,
                                                       author=author)

            print('    Committing changeset: {}'.format(message))
            if not dry_run:
                client.commit(b(message), user=b(author), addremove=True)