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

Add locale checker to CI #456

Merged
merged 8 commits into from
Feb 28, 2022
Merged
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
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: [email protected]
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()