Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#16] remove deprecated pot files #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions locale/autoremove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"""
We must keep old "pot" files in the "./pot/" directory to avoid commit
for only "POT-Creation-Time" line updated.
refs: https://github.com/sphinx-doc/sphinx/issues/3443

So we use the procedure as bellow:

1. update "./pot/" directory by "sphinx-build -b gettext"
2. remove unused pot file from "./pot" directory by this "autoremove.py" script
"""

import argparse
import os
import subprocess
import textwrap
from shutil import which

from pathlib import Path
from sphinx.application import Sphinx

TRANSIFEX_CLI_MINIMUM_VERSION = (1, 2, 1)

CMD_TMPL = (
'tx',
'delete',
'--organization', '%(transifex_organization_name)s',
'--project', '%(transifex_project_name)s',
'--resource', '%(resource_name)s',
'--file-filter', '%(locale_dir)s/<lang>/LC_MESSAGES/%(resource_path)s.po',
'%(pot_dir)s/%(resource_path)s.pot',
)


def main():
check_transifex_cli_installed()

parser = argparse.ArgumentParser()
parser.add_argument('sourcedir',
help=('path to documentation source files'))
parser.add_argument('outputdir',
help=('path to output directory'))
args = parser.parse_args()

proj_slug = os.environ["SPHINXINTL_TRANSIFEX_PROJECT_NAME"]
outdir = Path(args.outputdir)

app = Sphinx(
srcdir=args.sourcedir,
confdir=args.sourcedir,
outdir=outdir,
doctreedir=(outdir / '.doctrees'),
buildername='gettext'
)
new_pots = set((outdir / f"{d}.pot") for d in app.env.all_docs)
if not new_pots:
print("Generated pot files are not exist. ABORT.")
else:
old_pots = set(outdir.glob('**/*.pot'))
to_be_removed = sorted(old_pots - new_pots)
print("Remove unused pot files ... ")
for p in to_be_removed:
resource = str(p.relative_to(outdir).stem).replace("/", "--")
res = f"{proj_slug}.{resource}"
cmd_args = ["tx", "delete", res]
print("RUN:", " ".join(cmd_args))
ret = subprocess.run(cmd_args, encoding="utf-8", capture_output=True)
print(ret.stdout)
if ret.returncode:
breakpoint()
raise RuntimeError(ret.stderr)
# p.unlink()
print("Removed: ", res)


# copy from https://github.com/sphinx-doc/sphinx-intl/blob/c327016e/sphinx_intl/transifex.py#L59
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 RuntimeError(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 RuntimeError(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 RuntimeError(msg)


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion locale/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ set -ex
# pull po files from transifex
cd `dirname $0`
sphinx-intl create-transifexrc
#rm -R pot # skip this line cause "already unused pot files will not removed" but we must keep these files to avoid commit for only "POT-Creation-Time" line updated. see: https://github.com/sphinx-doc/sphinx/issues/3443
sphinx-build -T -b gettext ../sphinx/doc pot
sphinx-intl update-txconfig-resources -p pot -d .
python autoremove.py ../sphinx/doc pot
cat .tx/config
tx push -s --skip
rm -R -f ar ca_ES zh_CN fr de it_IT ja ko pl_PL pt_BR ru sr sr_RS es
Expand Down