Skip to content

Instantly share code, notes, and snippets.

@diegobz
Last active April 26, 2017 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegobz/925bb8e40f984a9351d981a22c77232b to your computer and use it in GitHub Desktop.
Save diegobz/925bb8e40f984a9351d981a22c77232b to your computer and use it in GitHub Desktop.
Quick script to setup transifex-client when multiple source files are involved.

Setup transifex-client for hundreds of source files with a single command

Quick start

Given the following directory structure:

└── locale
    ├── es
    │   └── intro
    │       ├── get-started.html
    │       └── index.html
    └── intro
        ├── get-started.html
        └── index.html

The command below will show the tx commands needed to setup multiple resource (source) entries with a single command ignoring the es directory:

$ python txsetup.py -p my_project -e .html -t HTML -s locale -i es -x 'locale/<lang>/{filepath}/{filename}{extension}'

Output of above command:

tx set --auto-local --resource my_project.locale_intro_get-started --source-lang en --type HTML --source-file locale/intro/get-started.html 'locale/<lang>/intro/get-started.html' --execute
tx set --auto-local --resource my_project.locale_intro_index --source-lang en --type HTML --source-file locale/intro/index.html 'locale/<lang>/intro/index.html' --execute

IMPORTANT: To actually execute the tx commands and generate the .tx/config file the option --execute needs to be present in the command.

All options of script:

$ python txsetup.py --help
usage: txsetup.py [-h] -p PROJECT -e FILE_EXTENSION -t I18N_TYPE -s
                  SOURCE_FILE_DIR [-l SOURCE_LANGUAGE] [-x EXPRESSION]
                  [-i IGNORE_DIRS] [--execute]

Setup transifex-client config file (.tx/config) to have a configuration entry
for every source file found under a given directory. This script should be
executed in the root directory of a repo.

optional arguments:
  -h, --help            show this help message and exit
  -p PROJECT, --project PROJECT
                        Project slug where files will live in Transifex.
  -e FILE_EXTENSION, --file-extension FILE_EXTENSION
                        File extension of files to be mapped.
  -t I18N_TYPE, --i18n-type I18N_TYPE
                        File format type in Transifex. More info
                        http://docs.transifex.com/formats/.
  -s SOURCE_FILE_DIR, --source-file-dir SOURCE_FILE_DIR
                        Directory where to find source files to be mapped.
                        Example: locale/en/
  -l SOURCE_LANGUAGE, --source-language SOURCE_LANGUAGE
                        Source language in Transifex for the source files.
                        Default: en
  -x EXPRESSION, --expression EXPRESSION
                        Expression defining where translation files should be
                        save. Default value is:
                        'locale/<lang>/{filepath}/{filename}{extension}'
  -i IGNORE_DIRS, --ignore-dir IGNORE_DIRS
                        Directory to ignore while looking for source files.
                        Can be called multiple times. Example: `-i es -i fr`.
  --execute             Call `tx` command to actually write the configuration
                        file
#!/usr/bin/env python
import argparse
import os
import sys
import subprocess
SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
TX_SET_CMD = ("tx set --auto-local --resource {project}.{resource} "
"--source-lang {source_language} --type {type} --source-file "
"{source_file} '{expr}' --execute")
def tx_exists():
return subprocess.call("type tx", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
def parse_args():
parser = argparse.ArgumentParser(
description='Setup transifex-client config file (.tx/config) to have '
'a configuration entry for every source file found under '
'a given directory. This script should be executed in the '
'root directory of a repo.')
parser.add_argument(
'-p', '--project', action='store', dest='project', required=True,
help='Project slug where files will live in Transifex.')
parser.add_argument(
'-e', '--file-extension', action='store', dest='file_extension',
required=True, help='File extension of files to be mapped.')
parser.add_argument(
'-t', '--i18n-type', action='store', dest='i18n_type', required=True,
help='File format type in Transifex. More info '
'http://docs.transifex.com/formats/.')
parser.add_argument(
'-s', '--source-file-dir', action='store', dest='source_file_dir',
required=True,
help='Directory to find source files to be mapped. '
'Example: locale/en/')
parser.add_argument(
'-l', '--source-language', action='store', dest='source_language',
default='en',
help='Source language for the source files. Default: en')
parser.add_argument(
'-x', '--expression', action='store', dest='expression',
default='locale/<lang>/{filepath}/{filename}{extension}',
help='Expression defining where translation files should be save once '
'they are downloaded from Transifex. Default value is: '
'\'locale/<lang>/{filepath}/{filename}{extension}\'')
parser.add_argument(
'-i', '--ignore-dir', action='append', dest='ignore_dirs',
default=[],
help='Directory to ignore while looking for source files. Can be '
'called multiple times. Example: `-i es -i fr`.')
parser.add_argument(
'--execute', default=False, action='store_true',
help='Call `tx` command to actually write the configuration file.')
return parser.parse_args()
def main():
args = parse_args()
if not args.file_extension.startswith('.'):
file_extension = '.{}'.format(args.file_extension)
else:
file_extension = args.file_extension
DOCS_ROOT = os.path.join(SOURCE_ROOT, args.source_file_dir)
os.chdir(SOURCE_ROOT)
for root, dirs, files in os.walk(DOCS_ROOT):
for file_name in files:
if file_name.endswith(file_extension):
source_file = os.path.join(
root, file_name).split(SOURCE_ROOT)[1][1:]
resource = source_file.split('.')[0].replace('/', '_')
relpath = root.split(args.source_file_dir)[1]
should_continue = True
for directory in args.ignore_dirs:
if relpath.strip('/').startswith(directory):
should_continue = False
break
if not should_continue:
break
expr = args.expression.format(
filepath=relpath, filename=file_name.split('.')[0],
extension=args.file_extension
).replace('//', '/')
cmd = TX_SET_CMD.format(
project=args.project,
resource=resource,
source_language=args.source_language,
type=args.i18n_type,
source_file=os.path.join(source_file),
expr=expr
)
if args.execute:
if tx_exists:
subprocess.call(cmd.split())
else:
error = ('Transifex-client not installed. Please run '
'`pip install transifex-client` first\n')
sys.stderr.write(error)
sys.stderr.flush()
return 1
else:
print cmd
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment