1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
import os
import re
import subprocess
import textwrap
from pathlib import Path
from shutil import which
import click
from .catalog import load_po
# ==================================
# settings
TRANSIFEX_CLI_MINIMUM_VERSION = (1, 2, 1)
# To avoid using invalid resource name, append underscore to such names.
# As a limitation, append `_` doesn't care about collision to other resources.
# e.g. 'glossary' and 'glossary_' are pushed as a 'glossary_'. The following
# resource names are reserved slugs, Transifex will reply with an error on these
# resource names.
IGNORED_RESOURCE_NAMES = (
"glossary",
"settings",
)
TRANSIFEXRC_TEMPLATE = """\
[https://www.transifex.com]
rest_hostname = https://rest.api.transifex.com
token = %(transifex_token)s
"""
TXCONFIG_TEMPLATE = """\
[main]
host = https://www.transifex.com
"""
# ==================================
# utility functions
def normalize_resource_name(name):
# replace path separator with '--'
name = re.sub(r"[\\/]", "--", name)
# replace unusable characters (not: -, _ ascii, digit) with '_'
name = re.sub(r"[^\-\w]", "_", name)
# append `_` for ignored resource names
while name in IGNORED_RESOURCE_NAMES:
name += "_"
return name
def check_transifex_cli_installed():
tx_cli_url = "https://raw.githubusercontent.com/transifex/cli/master/install.sh"
if not which("tx"):
msg = textwrap.dedent(f"""\
Could not run "tx".
You need to install the Transifex CLI external library.
Please install the below command and restart your terminal \
if you want to use this action:
$ curl -o- {tx_cli_url} | bash
""")
raise click.BadParameter(msg)
version_msg = subprocess.check_output("tx --version", shell=True, encoding="utf-8")
if not version_msg.startswith("TX Client"):
msg = textwrap.dedent(f"""\
The old transifex_client library was found.
You need to install the Transifex CLI external library.
Please install the below command and restart your terminal \
if you want to use this action:
$ curl -o- {tx_cli_url} | bash
""")
raise click.BadParameter(msg)
version = tuple(int(x) for x in version_msg.split("=")[-1].strip().split("."))
if not version >= TRANSIFEX_CLI_MINIMUM_VERSION:
msg = textwrap.dedent(f"""\
An unsupported version of the Transifex CLI was found.
Version {TRANSIFEX_CLI_MINIMUM_VERSION} or greater is required.
Please run the below command if you want to use this action:
$ tx update
""")
raise click.BadParameter(msg)
# ==================================
# commands
def create_transifexrc(transifex_token):
"""
Create `$HOME/.transifexrc`
"""
target = os.path.normpath(os.path.expanduser("~/.transifexrc"))
if os.path.exists(target):
click.echo(f"{target} already exists, skipped.")
return
if not transifex_token:
msg = textwrap.dedent("""\
You need a transifex token by command option or environment.
command option: --transifex-token
""")
raise click.BadParameter(msg, param_hint="transifex_token")
with open(target, "w") as rc:
rc.write(TRANSIFEXRC_TEMPLATE % locals())
click.echo(f"Create: {target}")
def create_txconfig():
"""
Create `./.tx/config`
"""
target = os.path.normpath(".tx/config")
if os.path.exists(target):
click.echo(f"{target} already exists, skipped.")
return
if not os.path.exists(".tx"):
os.mkdir(".tx")
with open(target, "w") as f:
f.write(TXCONFIG_TEMPLATE)
click.echo(f"Create: {target}")
def update_txconfig_resources(
transifex_organization_name, transifex_project_name, locale_dir, pot_dir
):
"""
Update resource sections of `./.tx/config`.
"""
check_transifex_cli_installed()
cmd_tmpl = (
"tx",
"add",
"--organization",
"%(transifex_organization_name)s",
"--project",
"%(transifex_project_name)s",
"--resource",
"%(resource_slug)s",
"--resource-name",
"%(resource_name)s",
"--file-filter",
"%(locale_dir)s/<lang>/LC_MESSAGES/%(resource_path)s.po",
"--type",
"PO",
"%(pot_dir)s/%(resource_path)s.pot",
)
# convert transifex_project_name to internal name
transifex_project_name = transifex_project_name.replace(" ", "-")
transifex_project_name = re.sub(r"[^\-_\w]", "", transifex_project_name)
pot_dir = Path(pot_dir)
pot_paths = sorted(pot_dir.glob("**/*.pot"))
with click.progressbar(
pot_paths,
length=len(pot_paths),
color="green",
label="adding pots...",
item_show_func=lambda p: str(p),
) as progress_bar:
for pot_path in progress_bar:
resource_path = str(pot_path.relative_to(pot_dir).with_suffix(""))
resource_slug = resource_name = normalize_resource_name(resource_path)
pot = load_po(str(pot_path))
if len(pot):
lv = locals()
cmd = [arg % lv for arg in cmd_tmpl]
subprocess.check_output(cmd, shell=False)
else:
click.echo(f"{pot_path} is empty, skipped")
|