Skip to content

Commit

Permalink
Add locale checker to CI (#456)
Browse files Browse the repository at this point in the history
* Add locale checker to CI

* Just pip install Django

* Add gettext package to OS

* Add sudo to apt-get

* Use @2ykwang 's updated script

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Only update on push to master
* To avoid pain points of PRs and histories being split
* Trying to use Andrew's username for pushing to see if that works

* Use separate workflow file

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Andrew-Chen-Wang and pre-commit-ci[bot] committed Feb 28, 2022
1 parent 1ea72de commit 6587c2a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
61 changes: 61 additions & 0 deletions .github/workflows/i18n.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Locale Updater

on:
push:
branches:
- master
- main

jobs:
locale-updater:
name: Locale updater
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: '3.9'

- name: Get pip cache dir
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
- name: Cache
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
3.9-v1-${{ hashFiles('**/setup.py') }}
restore-keys: |
3.9-v1-
- name: Install dependencies
run: |
sudo apt-get install -y gettext
python -m pip install --upgrade pip wheel setuptools
pip install -e .[dev]
- name: Run locale
working-directory: rest_framework_simplejwt
run: |
python ../scripts/i18n_updater.py
- name: Commit locale changes
uses: stefanzweifel/git-auto-commit-action@v4
id: auto-commit-action
with:
commit_message: "Update locale files"
file_pattern: rest_framework_simplejwt/locale/**
commit_user_name: Andrew-Chen-Wang
commit_user_email: acwangpython@gmail.com
commit_author: Andrew-Chen-Wang

- name: Tell whether locale updated
if: steps.auto-commit-action.outputs.changes_detected == 'true'
run: echo "Locale files updated"
10 changes: 9 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.9

- name: Install dependencies
run: |
sudo apt-get install -y gettext
python -m pip install -U pip
python -m pip install -U setuptools twine wheel
pip install -e .[dev]
- name: Check locale
run: |
echo "Checking if locale files need updating. If they do, cd rest_framework_simplejwt && run python ../scripts/i18n_updater.py"
python ../scripts/i18n_updater.py
git diff --exit-code
- name: Build package
run: |
Expand Down
58 changes: 58 additions & 0 deletions scripts/i18n_updater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import contextlib
import os
import subprocess


def get_list_of_files(dir_name: str, extension: str):
file_list = os.listdir(dir_name)

result = []
for entry in file_list:
full_path = os.path.join(dir_name, entry)
if os.path.isdir(full_path):
result = result + get_list_of_files(full_path, extension)
else:
if entry[-len(extension) : len(entry)] == extension:
result.append(full_path)

return result


@contextlib.contextmanager
def cache_creation():
# DO NOT cache the line number; the file may change
cache: dict[str, str] = {}
for file in get_list_of_files("./", ".po"):
if os.path.isdir(file):
continue

with open(file) as f:
for line in f.readlines():
if line.startswith('"POT-Creation-Date: '):
cache[file] = line
break
yield
for file, line_cache in cache.items():
with open(file, "r+") as f:
lines = f.readlines()
# clear file
f.seek(0)
f.truncate()

# find line
index = [
lines.index(x) for x in lines if x.startswith('"POT-Creation-Date: ')
][0]

lines[index] = line_cache
f.writelines(lines)


def main():
with cache_creation():
subprocess.run(["django-admin", "makemessages", "-a"])
subprocess.run(["django-admin", "compilemessages"])


if __name__ == "__main__":
main()

0 comments on commit 6587c2a

Please sign in to comment.