Repository navigation
400 lines (315 loc) · 12.5 KB
/
Copy pathtransifex-push.yml
File metadata and controls
400 lines (315 loc) · 12.5 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
name: Push Translations to Transifex
on:
push:
branches:
- "3.15"
paths:
- "**/*.po"
workflow_dispatch:
inputs:
push_translations:
description: "Push Punjabi translations to Transifex"
required: true
type: boolean
default: true
po_files:
description: "Optional: space-separated .po paths to push (empty = every .po file)"
required: false
type: string
default: ""
push_sources:
description: "Generate and push CPython source templates"
required: true
type: boolean
default: false
permissions:
contents: read
concurrency:
group: transifex-push-${{ github.ref }}
cancel-in-progress: false
env:
TX_CLI_VERSION: "1.6.16"
TX_PROJECT: python-newest
TX_LANGUAGE: pa
CPYTHON_VERSION: "3.15"
jobs:
push-translations:
name: Push Punjabi translations
if: github.actor != 'github-actions[bot]' && (github.event_name == 'push' || inputs.push_translations)
runs-on: ubuntu-24.04
steps:
- name: Checkout translation repository
uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install configuration dependencies
run: |
sudo apt-get update
sudo apt-get install -y gettext
python -m pip install requests cogapp polib transifex-python sphinx-intl blurb six
- name: Install Transifex CLI
shell: bash
run: |
set -euo pipefail
mkdir -p "$RUNNER_TEMP/txcli"
curl -fsSL \
"https://github.com/transifex/cli/releases/download/v${TX_CLI_VERSION}/tx-linux-amd64.tar.gz" \
-o "$RUNNER_TEMP/txcli/tx.tar.gz"
tar -xzf "$RUNNER_TEMP/txcli/tx.tar.gz" -C "$RUNNER_TEMP/txcli"
TX_BINARY="$(find "$RUNNER_TEMP/txcli" -type f -name tx -print -quit)"
if [ -z "$TX_BINARY" ]; then
echo "ERROR: Transifex CLI binary was not found in the archive."
find "$RUNNER_TEMP/txcli" -maxdepth 3 -type f -print
exit 1
fi
sudo install -m 0755 "$TX_BINARY" /usr/local/bin/tx
echo "/usr/local/bin" >> "$GITHUB_PATH"
/usr/local/bin/tx --version
- name: Generate Transifex configuration
run: |
curl -fsSLO https://raw.githubusercontent.com/python-docs-translations/transifex-automations/main/sample-workflows/transifex-util.py
chmod +x transifex-util.py
./transifex-util.py recreate_tx_config \
--language "$TX_LANGUAGE" \
--project-slug "$TX_PROJECT" \
--version "$CPYTHON_VERSION"
- name: Select translation files to push
env:
EVENT_NAME: ${{ github.event_name }}
BEFORE_SHA: ${{ github.event.before }}
AFTER_SHA: ${{ github.sha }}
PO_FILES_INPUT: ${{ inputs.po_files }}
run: |
set -euo pipefail
out="$RUNNER_TEMP/changed_po.txt"
: > "$out"
if [ "$EVENT_NAME" = "workflow_dispatch" ]; then
if [ -n "${PO_FILES_INPUT//[[:space:]]/}" ]; then
read -ra requested <<< "$PO_FILES_INPUT"
printf '%s\n' "${requested[@]}" > "$out"
else
git ls-files '*.po' > "$out"
fi
elif [ "$BEFORE_SHA" != "0000000000000000000000000000000000000000" ] \
&& git cat-file -e "${BEFORE_SHA}^{commit}" 2>/dev/null; then
git diff --name-only --diff-filter=AMR "$BEFORE_SHA" "$AFTER_SHA" -- '*.po' > "$out"
else
echo "Previous commit unavailable; using only the triggering commit."
git diff-tree --root --no-commit-id --name-only -r --diff-filter=AMR "$AFTER_SHA" -- '*.po' > "$out"
fi
echo "Selected $(wc -l < "$out") .po file(s):"
cat "$out"
- name: Stage selected translations for Transifex
id: stage
run: |
python - <<'PY'
import configparser
import os
import re
import shutil
import sys
from pathlib import Path
language = os.environ["TX_LANGUAGE"]
runner_temp = Path(os.environ["RUNNER_TEMP"])
stage_root = Path("_tx_stage")
config_path = Path(".tx/config")
config = configparser.ConfigParser(interpolation=None)
if not config.read(config_path, encoding="utf-8"):
sys.exit("ERROR: .tx/config was not generated")
# The generated file_filter has no <lang> placeholder, so the CLI
# cannot map it to a language and silently skips the upload.
by_file = {}
for section in config.sections():
if ":r:" not in section:
continue
file_filter = config.get(
section, "file_filter", fallback=""
).strip()
if file_filter:
by_file[os.path.normpath(file_filter)] = section
selected = [
line.strip()
for line in (runner_temp / "changed_po.txt")
.read_text(encoding="utf-8")
.splitlines()
if line.strip()
]
section_re = re.compile(r"^o:([^:]+):p:([^:]+):r:(.+)$")
staged = {}
skipped = []
for name in selected:
rel = os.path.normpath(name)
if (
os.path.isabs(rel)
or rel.startswith("..")
or not rel.endswith(".po")
):
sys.exit(f"ERROR: refusing unexpected path: {name!r}")
section = by_file.get(rel)
source = Path(rel)
if section is None or not source.is_file():
skipped.append(rel)
continue
target = stage_root / language / rel
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, target)
if target.read_bytes() != source.read_bytes():
sys.exit(f"ERROR: staged copy differs from {rel}")
config.set(
section,
"file_filter",
f"{stage_root.as_posix()}/<lang>/{Path(rel).as_posix()}",
)
match = section_re.match(section)
if not match:
sys.exit(f"ERROR: unexpected section name: {section}")
staged[section] = f"{match.group(2)}.{match.group(3)}"
# Keep only the resources being pushed, so `tx push` touches
# nothing else.
for section in list(config.sections()):
if ":r:" in section and section not in staged:
config.remove_section(section)
for rel in skipped:
print(
f"::warning::No Transifex resource or file for {rel}; skipped"
)
if staged:
with config_path.open("w", encoding="utf-8") as file:
config.write(file)
(runner_temp / "staged_resources.txt").write_text(
"\n".join(sorted(staged.values())) + "\n",
encoding="utf-8",
)
print(f"Staged {len(staged)} resource(s):")
for resource in sorted(staged.values()):
print(f" {resource}")
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as file:
file.write(f"count={len(staged)}\n")
PY
- name: Push Punjabi translations
if: steps.stage.outputs.count != '0'
env:
TX_TOKEN: ${{ secrets.TX_TOKEN }}
run: |
set -euo pipefail
log="$RUNNER_TEMP/tx-push.log"
/usr/local/bin/tx push \
--translation \
--languages "$TX_LANGUAGE" 2>&1 | tee "$log"
missing=0
while IFS= read -r resource; do
[ -n "$resource" ] || continue
if ! grep -F "$resource [$TX_LANGUAGE]" "$log" \
| grep -vi 'skipping' > /dev/null; then
echo "::error::No upload confirmation for $resource [$TX_LANGUAGE]"
missing=1
fi
done < "$RUNNER_TEMP/staged_resources.txt"
exit "$missing"
- name: Summarize Transifex push
if: always()
run: |
{
echo "### Transifex push"
if [ -s "$RUNNER_TEMP/staged_resources.txt" ]; then
echo "Pushed [$TX_LANGUAGE] translations for:"
sed 's/^/- /' "$RUNNER_TEMP/staged_resources.txt"
else
echo "No translation files were pushed."
fi
echo
echo "A green run does not prove remote content changed; check the"
echo "resource in Transifex. Pushed strings are not reviewed, and the"
echo "nightly reviewed-only pull ignores unreviewed strings."
} >> "$GITHUB_STEP_SUMMARY"
push-sources:
name: Generate and push source templates
needs: push-translations
if: ${{ always() && github.event_name == 'workflow_dispatch' && inputs.push_sources && (needs.push-translations.result == 'success' || needs.push-translations.result == 'skipped') }}
runs-on: ubuntu-24.04
steps:
- name: Checkout translation repository
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Checkout CPython source
uses: actions/checkout@v4
with:
repository: python/cpython
ref: "3.15"
path: cpython
persist-credentials: false
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install documentation build dependencies
run: |
sudo apt-get update
sudo apt-get install -y gettext
python -m pip install -r cpython/Doc/requirements.txt
python -m pip install requests cogapp polib transifex-python sphinx-intl blurb six
- name: Install Transifex CLI
shell: bash
run: |
set -euo pipefail
mkdir -p "$RUNNER_TEMP/txcli"
curl -fsSL \
"https://github.com/transifex/cli/releases/download/v${TX_CLI_VERSION}/tx-linux-amd64.tar.gz" \
-o "$RUNNER_TEMP/txcli/tx.tar.gz"
tar -xzf "$RUNNER_TEMP/txcli/tx.tar.gz" -C "$RUNNER_TEMP/txcli"
TX_BINARY="$(find "$RUNNER_TEMP/txcli" -type f -name tx -print -quit)"
if [ -z "$TX_BINARY" ]; then
echo "ERROR: Transifex CLI binary was not found in the archive."
find "$RUNNER_TEMP/txcli" -maxdepth 3 -type f -print
exit 1
fi
sudo install -m 0755 "$TX_BINARY" /usr/local/bin/tx
echo "/usr/local/bin" >> "$GITHUB_PATH"
/usr/local/bin/tx --version
- name: Generate gettext source templates
run: |
make -C cpython/Doc gettext
test -d cpython/Doc/build/gettext
mkdir -p gettext
cp -a cpython/Doc/build/gettext/. gettext/
- name: Generate Transifex configuration
run: |
curl -fsSLO https://raw.githubusercontent.com/python-docs-translations/transifex-automations/main/sample-workflows/transifex-util.py
chmod +x transifex-util.py
./transifex-util.py recreate_tx_config \
--language "$TX_LANGUAGE" \
--project-slug "$TX_PROJECT" \
--version "$CPYTHON_VERSION"
- name: Validate configured source templates
run: |
python - <<'PY'
import configparser
from pathlib import Path
path = Path(".tx/config")
config = configparser.ConfigParser(interpolation=None)
if not path.is_file() or not config.read(path):
raise SystemExit("ERROR: .tx/config was not generated")
missing = []
for section in config.sections():
source = config.get(
section, "source_file", fallback=""
).strip()
if source and not Path(source).is_file():
missing.append(f"{section}: {source}")
if missing:
print("Missing source templates:")
print("\n".join(missing))
raise SystemExit(1)
print("All configured source templates exist.")
PY
- name: Push source templates
env:
TX_TOKEN: ${{ secrets.TX_TOKEN }}
run: |
/usr/local/bin/tx push --source --skip