Skip to content

Commit

Permalink
feat(i18n): compile .po files during package build
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Jan 23, 2025
1 parent 0c30306 commit fd576e0
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion questionpy_sdk/package/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import shutil
import subprocess
import tempfile
import zipfile
from abc import abstractmethod
from contextlib import AbstractContextManager
Expand All @@ -16,7 +17,10 @@
from tempfile import TemporaryDirectory
from types import TracebackType

import babel.messages.frontend

import questionpy
from questionpy import i18n
from questionpy_common.constants import DIST_DIR, MANIFEST_FILENAME
from questionpy_common.manifest import Manifest, PackageFile
from questionpy_sdk.models import BuildHookName
Expand Down Expand Up @@ -49,6 +53,7 @@ def write_package(self) -> None:
self._install_questionpy()
self._install_requirements()
self._write_package_files()
self._compile_pos()
self._write_manifest()
if self._copy_sources:
self._copy_source_files()
Expand Down Expand Up @@ -92,7 +97,7 @@ def _install_requirements(self) -> None:
# pip doesn't offer a public API, so we have to resort to subprocess (pypa/pip#3121)
try:
with TemporaryDirectory(prefix=f"qpy_{config.short_name}") as tempdir:
subprocess.run(["pip", "install", "--target", tempdir, *pip_args], check=True, capture_output=True) # noqa: S603, S607
subprocess.run(["pip", "install", "--target", tempdir, *pip_args], check=True, capture_output=True)

Check failure on line 100 in questionpy_sdk/package/builder.py

View workflow job for this annotation

GitHub Actions / ci / ruff-lint

Ruff (S603)

questionpy_sdk/package/builder.py:100:17: S603 `subprocess` call: check for execution of untrusted input

Check failure on line 100 in questionpy_sdk/package/builder.py

View workflow job for this annotation

GitHub Actions / ci / ruff-lint

Ruff (S607)

questionpy_sdk/package/builder.py:100:32: S607 Starting a process with a partial executable path
self._write_glob(Path(tempdir), "**/*", Path(DIST_DIR) / "dependencies" / "site-packages")
except subprocess.CalledProcessError as exc:
msg = f"Failed to install requirements: {exc.stderr.decode()}"
Expand Down Expand Up @@ -145,6 +150,22 @@ def _copy_source_files(self) -> None:
log.debug("%s: %s", source_file, path_in_pkg)
self._write_file(source_file, path_in_pkg)

def _compile_pos(self) -> None:
with tempfile.TemporaryDirectory(prefix="qpy_build_locales_") as tempdir_str:
tempdir = Path(tempdir_str)
for locale, domain, po_file in self._source.discover_po_files():
outfile = tempdir / locale / i18n.DEFAULT_CATEGORY / f"{domain}.mo"
outfile.parent.mkdir(parents=True, exist_ok=True)

cmd = babel.messages.frontend.CompileCatalog()
cmd.locale = locale
cmd.input_file = po_file
cmd.output_file = outfile
cmd.ensure_finalized()
cmd.run()

self._write_glob(tempdir, "**/*.mo", f"{DIST_DIR}/locale")

def _write_glob(
self, source_dir: Path, glob: str, prefix: str | Path = "", *, add_to_static_files: bool = False
) -> None:
Expand Down

0 comments on commit fd576e0

Please sign in to comment.