Skip to content

Commit df8186d

Browse files
Update to Python 3.12 (#301)
* Update to Python 3.11 * Update python-311.yml * Fix transifex cli position * Fix config generation script * Add 3.11 to workflows * chore: update 3.11 links and badges * chore: Add 3.12 to workflow
1 parent 4dac60f commit df8186d

11 files changed

+150
-126
lines changed

.github/scripts/generate_tx_config.py

+71-64
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,83 @@
1-
#!/usr/bin/env python3
1+
"""Please note that this script requires a Transifex API token to run."""
22
import glob
3-
import json
4-
import os
3+
import subprocess
4+
from functools import partial
5+
from pathlib import Path
56
import re
6-
import sys
7-
import urllib.request
7+
import os
8+
9+
run = partial(subprocess.run, check=True)
10+
811

12+
def init_project():
13+
run(["tx", "init"])
914

10-
def list_resources(token, project):
11-
auth_handler = urllib.request.HTTPBasicAuthHandler()
12-
auth_handler.add_password(
13-
realm="api", uri="https://api.transifex.com/", user="api", passwd=token
15+
16+
def add_files(project_name: str):
17+
run(
18+
[
19+
"tx",
20+
"add",
21+
"remote",
22+
"--file-filter",
23+
"trans/<lang>/<resource_slug>.<ext>",
24+
f"https://www.transifex.com/python-doc/{project_name}/dashboard/",
25+
]
1426
)
15-
opener = urllib.request.build_opener(auth_handler)
16-
urllib.request.install_opener(opener)
17-
next_ = (
18-
"https://api.transifex.com/organizations/python-doc/projects/"
19-
+ project
20-
+ "/resources/"
27+
28+
29+
FILTER_PATTERN = re.compile(
30+
r"^(?P<prefix>file_filter( *)=( *))(?P<resource>.+)$", re.MULTILINE
31+
)
32+
33+
34+
def name_replacer(match: re.Match[str]):
35+
prefix, resource = match.group("prefix", "resource")
36+
override_prefix = prefix.replace("file_filter", "trans.zh_CN")
37+
pattern = (
38+
resource.replace("trans/<lang>/", "")
39+
.replace("glossary_", "glossary")
40+
.replace("--", "/")
41+
.replace("_", "?")
2142
)
22-
resources = []
23-
while True:
24-
resp = urllib.request.urlopen(next_)
25-
result = json.loads(resp.read().decode("utf-8"))
26-
resources.extend([i["slug"] for i in result])
27-
link = re.findall('<([^<]*)>; rel="next"', resp.getheader("Link") or "")
28-
if not link:
29-
break
30-
next_ = link[0]
31-
return resources
32-
33-
34-
def render_config(doc_dir, project, resources):
35-
os.chdir(doc_dir)
36-
tpl = """
37-
38-
[{project}.{resource}]
39-
trans.zh_CN = {filename}
40-
source_lang = en
41-
type = PO"""
42-
conf = """[main]
43-
host = https://www.transifex.com"""
44-
for resource in sorted(resources):
45-
if resource == "glossary_":
46-
filename = "glossary.po"
47-
elif resource == "sphinx":
48-
filename = "sphinx.po"
49-
else:
50-
pattern = resource.replace("--", "/").replace("_", "?")
51-
matches = glob.glob(pattern + ".rst")
52-
if len(matches) == 0:
53-
print("missing", resource, file=sys.stderr)
54-
continue
55-
elif len(matches) == 1:
56-
filename = matches[0].replace(".rst", ".po")
57-
else:
58-
print("multi match", resource, pattern, matches, file=sys.stderr)
59-
conf += tpl.format(project=project, resource=resource, filename=filename)
60-
return conf
43+
matches = list(glob.glob(pattern.replace(".po", ".rst")))
44+
if not matches:
45+
print("missing", pattern)
46+
return f"{prefix}{resource}\n{override_prefix}{pattern.replace('?', '_')}"
47+
elif len(matches) == 1:
48+
filename = matches[0].replace(".rst", ".po").replace("\\", "/")
49+
else:
50+
raise ValueError("multi match", resource, pattern, matches)
51+
return f"{prefix}{resource}\n{override_prefix}{filename}"
52+
53+
54+
def patch_config(path: str):
55+
tx_config_path = Path(".tx", "config")
56+
57+
config_content = tx_config_path.read_text("utf-8")
58+
59+
cwd = os.getcwd()
60+
os.chdir(path)
61+
config_content = FILTER_PATTERN.sub(name_replacer, config_content)
62+
os.chdir(cwd)
63+
64+
tx_config_path.write_text(config_content, "utf-8")
6165

6266

6367
if __name__ == "__main__":
64-
import argparse
68+
from argparse import ArgumentParser
69+
70+
parser = ArgumentParser()
71+
72+
parser.add_argument("--token", default="")
73+
parser.add_argument("--project-name", required=True)
74+
parser.add_argument("--doc-path", required=True)
6575

66-
parser = argparse.ArgumentParser()
67-
parser.add_argument("--token")
68-
parser.add_argument("--project")
69-
parser.add_argument("--doc-dir")
70-
args = parser.parse_args()
76+
params = parser.parse_args()
7177

72-
resources = list_resources(args.token, args.project)
73-
conf = render_config(args.doc_dir, args.project, resources)
74-
print(conf)
78+
if params.token:
79+
os.environ["TX_TOKEN"] = params.token
7580

76-
# vim: set et ts=4 sw=4 sts=4:
81+
init_project()
82+
add_files(params.project_name)
83+
patch_config(params.doc_path)

.github/scripts/prepare.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ git clone --depth=1 --branch="$VERSION" https://github.com/python/cpython cpytho
66
git clone --branch="$VERSION" https://github.com/"$GITHUB_REPOSITORY" docs
77

88
pip3 install --user setuptools
9-
pip3 install --user transifex-client
9+
curl -o- https://raw.githubusercontent.com/transifex/cli/master/install.sh | bash
1010
sudo apt-get update
1111
sudo apt-get install -y python3-venv

.github/scripts/transifex_pull.py

-38
This file was deleted.

.github/scripts/update.sh

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
#!/bin/bash
22

3-
set -ex
4-
5-
script_dir="$(dirname "$(realpath "$0")")"
6-
7-
if [[ -n "$TRANSIFEX_APIKEY" ]]; then
8-
cat > ~/.transifexrc << EOF
9-
[https://www.transifex.com]
10-
api_hostname = https://api.transifex.com
11-
hostname = https://www.transifex.com
12-
password = $TRANSIFEX_APIKEY
13-
username = api
14-
EOF
15-
fi
16-
3+
tx=$(realpath ./tx)
174
cd docs || exit 1
18-
"$script_dir"/transifex_pull.py
5+
$tx pull --languages "$LOCALE" -t --use-git-timestamps

.github/workflows/python-310.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: update
2121
run: .github/scripts/update.sh
2222
env:
23-
TRANSIFEX_APIKEY: ${{ secrets.TRANSIFEX_APIKEY }}
23+
TX_TOKEN: ${{ secrets.TRANSIFEX_APIKEY }}
2424
- name: build
2525
run: .github/scripts/build.sh
2626
- name: commit

.github/workflows/python-311.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: python-311
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
schedule:
8+
- cron: "11 * * * *"
9+
10+
jobs:
11+
sync:
12+
runs-on: ubuntu-latest
13+
env:
14+
LOCALE: zh_CN
15+
VERSION: "3.11"
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: prepare
19+
run: .github/scripts/prepare.sh
20+
- name: update
21+
run: .github/scripts/update.sh
22+
env:
23+
TX_TOKEN: ${{ secrets.TRANSIFEX_APIKEY }}
24+
- name: build
25+
run: .github/scripts/build.sh
26+
- name: commit
27+
run: .github/scripts/commit.sh
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/python-312.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: python-312
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
schedule:
8+
- cron: "42 * * * *"
9+
10+
jobs:
11+
sync:
12+
runs-on: ubuntu-latest
13+
env:
14+
LOCALE: zh_CN
15+
VERSION: "3.12"
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: prepare
19+
run: .github/scripts/prepare.sh
20+
- name: update
21+
run: .github/scripts/update.sh
22+
env:
23+
TX_TOKEN: ${{ secrets.TRANSIFEX_APIKEY }}
24+
- name: build
25+
run: .github/scripts/build.sh
26+
- name: commit
27+
run: .github/scripts/commit.sh
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/python-37.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: update
2121
run: .github/scripts/update.sh
2222
env:
23-
TRANSIFEX_APIKEY: ${{ secrets.TRANSIFEX_APIKEY }}
23+
TX_TOKEN: ${{ secrets.TRANSIFEX_APIKEY }}
2424
- name: build
2525
run: .github/scripts/build.sh
2626
- name: commit

.github/workflows/python-38.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: update
2121
run: .github/scripts/update.sh
2222
env:
23-
TRANSIFEX_APIKEY: ${{ secrets.TRANSIFEX_APIKEY }}
23+
TX_TOKEN: ${{ secrets.TRANSIFEX_APIKEY }}
2424
- name: build
2525
run: .github/scripts/build.sh
2626
- name: commit

.github/workflows/python-39.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: update
2121
run: .github/scripts/update.sh
2222
env:
23-
TRANSIFEX_APIKEY: ${{ secrets.TRANSIFEX_APIKEY }}
23+
TX_TOKEN: ${{ secrets.TRANSIFEX_APIKEY }}
2424
- name: build
2525
run: .github/scripts/build.sh
2626
- name: commit

README.rst

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,36 @@ Maintained versions:
1212
* - Version
1313
- Sync status
1414
- Translation progress
15+
* - `3.12 <https://github.com/python/python-docs-zh-cn/tree/3.12>`_
16+
- .. image:: https://github.com/python/python-docs-zh-cn/workflows/python-312/badge.svg
17+
:target: https://github.com/python/python-docs-zh-cn/actions?workflow=python-312
18+
- .. image:: https://img.shields.io/badge/dynamic/json.svg?label=zh_CN&query=%24.zh_CN&url=http://gce.zhsj.me/python/312
19+
:target: https://app.transifex.com/python-doc/python-newest/
20+
* - `3.11 <https://github.com/python/python-docs-zh-cn/tree/3.11>`_
21+
- .. image:: https://github.com/python/python-docs-zh-cn/workflows/python-311/badge.svg
22+
:target: https://github.com/python/python-docs-zh-cn/actions?workflow=python-311
23+
- .. image:: https://img.shields.io/badge/dynamic/json.svg?label=zh_CN&query=%24.zh_CN&url=http://gce.zhsj.me/python/311
24+
:target: https://app.transifex.com/python-doc/python-311/
1525
* - `3.10 <https://github.com/python/python-docs-zh-cn/tree/3.10>`_
1626
- .. image:: https://github.com/python/python-docs-zh-cn/workflows/python-310/badge.svg
1727
:target: https://github.com/python/python-docs-zh-cn/actions?workflow=python-310
1828
- .. image:: https://img.shields.io/badge/dynamic/json.svg?label=zh_CN&query=%24.zh_CN&url=http://gce.zhsj.me/python/310
19-
:target: https://www.transifex.com/python-doc/python-39/
29+
:target: https://app.transifex.com/python-doc/python-310/
2030
* - `3.9 <https://github.com/python/python-docs-zh-cn/tree/3.9>`_
2131
- .. image:: https://github.com/python/python-docs-zh-cn/workflows/python-39/badge.svg
2232
:target: https://github.com/python/python-docs-zh-cn/actions?workflow=python-39
2333
- .. image:: https://img.shields.io/badge/dynamic/json.svg?label=zh_CN&query=%24.zh_CN&url=http://gce.zhsj.me/python/39
24-
:target: https://www.transifex.com/python-doc/python-39/
34+
:target: https://app.transifex.com/python-doc/python-39/
2535
* - `3.8 <https://github.com/python/python-docs-zh-cn/tree/3.8>`_
2636
- .. image:: https://github.com/python/python-docs-zh-cn/workflows/python-38/badge.svg
2737
:target: https://github.com/python/python-docs-zh-cn/actions?workflow=python-38
2838
- .. image:: https://img.shields.io/badge/dynamic/json.svg?label=zh_CN&query=%24.zh_CN&url=http://gce.zhsj.me/python/38
29-
:target: https://www.transifex.com/python-doc/python-38/
39+
:target: https://app.transifex.com/python-doc/python-38/
3040
* - `3.7 <https://github.com/python/python-docs-zh-cn/tree/3.7>`_
3141
- .. image:: https://github.com/python/python-docs-zh-cn/workflows/python-37/badge.svg
3242
:target: https://github.com/python/python-docs-zh-cn/actions?workflow=python-37
3343
- .. image:: https://img.shields.io/badge/dynamic/json.svg?label=zh_CN&query=%24.zh_CN&url=http://gce.zhsj.me/python/37
34-
:target: https://www.transifex.com/python-doc/python-37/
44+
:target: https://app.transifex.com/python-doc/python-37/
3545

3646
Documentation Contribution Agreement
3747
------------------------------------

0 commit comments

Comments
 (0)