From 9d21d5577f92d3db36877ca96641ed7e879c8795 Mon Sep 17 00:00:00 2001 From: Rafael Fontenelle Date: Fri, 26 May 2023 10:31:34 -0300 Subject: [PATCH 1/2] Create manage_tx.py --- scripts/manage_tx.py | 97 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 scripts/manage_tx.py diff --git a/scripts/manage_tx.py b/scripts/manage_tx.py new file mode 100644 index 00000000000..66f8c8e54bf --- /dev/null +++ b/scripts/manage_tx.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python + +import json +import os +import re +from argparse import ArgumentParser +from pathlib import Path + +from transifex.api import transifex_api + +# Version number of a new versioned project to create +new_project_version = '3.11' + +transifex_api.setup(auth=os.getenv('TX_TOKEN')) + +ORG_SLUG = "python-doc" +PROJ_SLUG = "python-newest" +ORGANIZATION = transifex_api.Organization.get(slug=ORG_SLUG) +PROJECT = ORGANIZATION.fetch('projects').get(slug=PROJ_SLUG) +RESOURCES = transifex_api.Resource.filter(project=PROJECT).all() + + +def __allow_translations_status(status): + for resource in RESOURCES: + print(f'{resource.name} ...') + resource.attributes['accept_translations'] = status + resource.save('accept_translations') + + +def lock_resources(): + __allow_translations_status(status=False) + + +def unlock_resources(): + __allow_translations_status(status=True) + + +def fetch_translations(): + """Fetch translations from Transifex, remove source lines.""" + pull_return_code = os.system(f'tx pull -all --force --skip') + if pull_return_code != 0: + exit(pull_return_code) + + +def create_project(): + """ + Creates a new Transifex project with versioned name based on python-newest + vers_proj_* = version project being created + vers_res_* = resource of version project being created + p.* = python-newest project + r.* = resource of python-newest project + + """ + p = PROJECT + organization = ORGANIZATION + vers_proj_slug = 'python-' + new_project_version.replace('.', '') + vers_proj_name = f'Python {new_project_version}' + print(f'Creating project: {vers_proj_name}') + versioned_project = transifex_api.Project.create( + description=vers_proj_name, + homepage_url=p.attributes.get('homepage_url'), + instructions_url=p.attributes.get('instructions_url'), + license=p.attributes.get('license'), + long_description=p.attributes.get('long_description'), + machine_translation_fillup=p.attributes.get('machine_translation_fillup'), + name=vers_proj_name, + private=p.attributes.get('private'), + repository_url=p.attributes.get('repository_url'), + slug=vers_proj_slug, + tags=p.attributes.get('tags'), + team=p.fetch('team'), + organization=organization, + source_language=p.fetch('source_language'), + translation_memory_fillup=p.attributes.get('translation_memory_fillup'), + ) + + print(f'Creating {vers_proj_name}\'s resources:') + for r in RESOURCES: + print(f'r.name ...') + transifex_api.Resource.create( + project=versioned_project, + i18n_format=r.i18n_format, + slug=r.slug, + name=r.name, + priority=r.priority, + accept_translations=True + ) + + +if __name__ == "__main__": + RUNNABLE_SCRIPTS = ('lock_resources', 'unlock_resources', 'fetch_translations', 'create_project') + + parser = ArgumentParser() + parser.add_argument('cmd', nargs=1, choices=RUNNABLE_SCRIPTS) + options = parser.parse_args() + + eval(options.cmd[0])() From 68da1c650785ff3cddc6e63ab2fed53748a76517 Mon Sep 17 00:00:00 2001 From: Rafael Fontenelle Date: Thu, 8 May 2025 02:11:32 -0300 Subject: [PATCH 2/2] Remove un/lock and resources push from manage_tx.py --- scripts/manage_tx.py | 109 +++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 61 deletions(-) diff --git a/scripts/manage_tx.py b/scripts/manage_tx.py index 66f8c8e54bf..74c5d8ac57a 100644 --- a/scripts/manage_tx.py +++ b/scripts/manage_tx.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import configparser import json import os import re @@ -8,90 +9,76 @@ from transifex.api import transifex_api -# Version number of a new versioned project to create -new_project_version = '3.11' - -transifex_api.setup(auth=os.getenv('TX_TOKEN')) - ORG_SLUG = "python-doc" PROJ_SLUG = "python-newest" -ORGANIZATION = transifex_api.Organization.get(slug=ORG_SLUG) -PROJECT = ORGANIZATION.fetch('projects').get(slug=PROJ_SLUG) -RESOURCES = transifex_api.Resource.filter(project=PROJECT).all() - - -def __allow_translations_status(status): - for resource in RESOURCES: - print(f'{resource.name} ...') - resource.attributes['accept_translations'] = status - resource.save('accept_translations') - - -def lock_resources(): - __allow_translations_status(status=False) - - -def unlock_resources(): - __allow_translations_status(status=True) - - -def fetch_translations(): - """Fetch translations from Transifex, remove source lines.""" - pull_return_code = os.system(f'tx pull -all --force --skip') - if pull_return_code != 0: - exit(pull_return_code) -def create_project(): +def _get_tx_token() -> str: + """ + Try to get Transifex API token from some sources. + First try TX_TOKEN environment variable, then ~/.transifexrc file. + """ + token = "" + if 'TX_TOKEN' in os.environ: + token = os.environ['TX_TOKEN'] + else: + config = configparser.ConfigParser() + config.read(f"{os.path.expanduser('~')}/.transifexrc") + for section in config.sections(): + if section in ["https://www.transifex.com", "https://app.transifex.com"]: + token = config[section]["token"] + break + return token + + +def create_translation_project(new_project_version: str) -> str: """ Creates a new Transifex project with versioned name based on python-newest - vers_proj_* = version project being created - vers_res_* = resource of version project being created - p.* = python-newest project - r.* = resource of python-newest project - + project_slug = slug of the translation project being created + project_name = name of the translation project being created + p.* = Reference to the current python-newest project + r.* = Reference to a resource from from python-newest project """ p = PROJECT - organization = ORGANIZATION - vers_proj_slug = 'python-' + new_project_version.replace('.', '') - vers_proj_name = f'Python {new_project_version}' - print(f'Creating project: {vers_proj_name}') + + project_slug = 'python-' + new_project_version.replace('.', '') + project_name = f'Python {new_project_version}' + print(f'Creating project: {project_name}') + versioned_project = transifex_api.Project.create( - description=vers_proj_name, + description=project_name, homepage_url=p.attributes.get('homepage_url'), instructions_url=p.attributes.get('instructions_url'), license=p.attributes.get('license'), long_description=p.attributes.get('long_description'), machine_translation_fillup=p.attributes.get('machine_translation_fillup'), - name=vers_proj_name, + name=project_name, private=p.attributes.get('private'), repository_url=p.attributes.get('repository_url'), - slug=vers_proj_slug, + slug=project_slug, tags=p.attributes.get('tags'), team=p.fetch('team'), - organization=organization, + organization=ORGANIZATION, source_language=p.fetch('source_language'), translation_memory_fillup=p.attributes.get('translation_memory_fillup'), ) - - print(f'Creating {vers_proj_name}\'s resources:') - for r in RESOURCES: - print(f'r.name ...') - transifex_api.Resource.create( - project=versioned_project, - i18n_format=r.i18n_format, - slug=r.slug, - name=r.name, - priority=r.priority, - accept_translations=True - ) + print(f'{project_slug} created') -if __name__ == "__main__": - RUNNABLE_SCRIPTS = ('lock_resources', 'unlock_resources', 'fetch_translations', 'create_project') +if __name__ == "__main__": parser = ArgumentParser() - parser.add_argument('cmd', nargs=1, choices=RUNNABLE_SCRIPTS) - options = parser.parse_args() + parser.add_argument( + 'version', + help='Python version currently assigned to python-newest (only major and minor, X.Y)' + ) + args = parser.parse_args() + + transifex_api.setup(auth=_get_tx_token()) + ORGANIZATION = transifex_api.Organization.get(slug=ORG_SLUG) + PROJECT = ORGANIZATION.fetch('projects').get(slug=PROJ_SLUG) + RESOURCES = transifex_api.Resource.filter(project=PROJECT).all() + + project_version = args.version - eval(options.cmd[0])() + create_translation_project(project_version)