From 0c9dbf1c47aae2a2cdd950bf47de40cd91391ddc Mon Sep 17 00:00:00 2001 From: Andrew Chen Wang <60190294+Andrew-Chen-Wang@users.noreply.github.com> Date: Fri, 17 Sep 2021 17:12:31 -0400 Subject: [PATCH 1/8] Add locale checker to CI --- .github/workflows/test.yml | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 619298400..c968cbb4e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -63,3 +63,42 @@ jobs: uses: codecov/codecov-action@v1 with: name: Python ${{ matrix.python-version }} + + locale-test: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v2 + with: + fetch-head: 2 + + - name: Set up Python 3.6 + uses: actions/setup-python@v2 + with: + python-version: '3.6' + + - 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.6-v1-${{ hashFiles('**/setup.py') }} + restore-keys: | + 3.6-v1- + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e .[dev] + + - name: Run locale + working-directory: rest_framework_simplejwt/locale + run: | + find . -maxdepth 1 -type d -exec django-admin makemessages -l {} \; + git diff --exit-code + From fa9ba58c0d1eff59e9f2fe9394064f8b963dbd6a Mon Sep 17 00:00:00 2001 From: Andrew Chen Wang <60190294+Andrew-Chen-Wang@users.noreply.github.com> Date: Sun, 19 Sep 2021 14:21:01 -0400 Subject: [PATCH 2/8] Just pip install Django --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c968cbb4e..9c46e4265 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -94,7 +94,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -e .[dev] + pip install django - name: Run locale working-directory: rest_framework_simplejwt/locale From ea389aa603a97b8fd4c181bf14f30d78a2d3610d Mon Sep 17 00:00:00 2001 From: Andrew Chen Wang <60190294+Andrew-Chen-Wang@users.noreply.github.com> Date: Sun, 19 Sep 2021 14:37:34 -0400 Subject: [PATCH 3/8] Add gettext package to OS --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9c46e4265..26f175e0f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -93,6 +93,7 @@ jobs: - name: Install dependencies run: | + apt-get install -y gettext python -m pip install --upgrade pip pip install django From 47f69a17eae05b448be72d61d4e6dd1f9b283b23 Mon Sep 17 00:00:00 2001 From: Andrew Chen Wang <60190294+Andrew-Chen-Wang@users.noreply.github.com> Date: Sun, 19 Sep 2021 14:41:46 -0400 Subject: [PATCH 4/8] Add sudo to apt-get --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 26f175e0f..d2bbdee17 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -93,7 +93,7 @@ jobs: - name: Install dependencies run: | - apt-get install -y gettext + sudo apt-get install -y gettext python -m pip install --upgrade pip pip install django From 26f0ac3a848e144f6cbdc4d6e317b9cb99a1f328 Mon Sep 17 00:00:00 2001 From: Andrew-Chen-Wang Date: Thu, 24 Feb 2022 15:31:40 -0500 Subject: [PATCH 5/8] Use @2ykwang 's updated script --- .github/workflows/release.yml | 10 +++++- .github/workflows/test.yml | 42 +++++++++++++++++++------ scripts/i18n_updater.py | 58 +++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 scripts/i18n_updater.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 152109f4b..caf375ffd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: | diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d2bbdee17..392a544d7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -70,12 +70,12 @@ jobs: - name: Checkout repo uses: actions/checkout@v2 with: - fetch-head: 2 + ref: ${{ github.head_ref }} - - name: Set up Python 3.6 + - name: Set up Python 3.9 uses: actions/setup-python@v2 with: - python-version: '3.6' + python-version: '3.9' - name: Get pip cache dir id: pip-cache @@ -87,19 +87,43 @@ jobs: with: path: ${{ steps.pip-cache.outputs.dir }} key: - 3.6-v1-${{ hashFiles('**/setup.py') }} + 3.9-v1-${{ hashFiles('**/setup.py') }} restore-keys: | - 3.6-v1- + 3.9-v1- - name: Install dependencies run: | sudo apt-get install -y gettext python -m pip install --upgrade pip - pip install django + pip install -e .[dev] - name: Run locale - working-directory: rest_framework_simplejwt/locale + working-directory: rest_framework_simplejwt run: | - find . -maxdepth 1 -type d -exec django-admin makemessages -l {} \; - git diff --exit-code + 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/** + + - name: Tell user about locale update + if: steps.auto-commit-action.outputs.changes_detected == 'true' + run: echo "Locale files updated" + + - name: Tell user about locale update + if: | + steps.auto-commit-action.outputs.changes_detected == 'true' + && github.event_name == 'pull_request' + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: "👋 Hey! We've automatically updated the locale files for you." + }) diff --git a/scripts/i18n_updater.py b/scripts/i18n_updater.py new file mode 100644 index 000000000..060217da6 --- /dev/null +++ b/scripts/i18n_updater.py @@ -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, "r") 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() From bab26a410e9ad504f5d906fa34ca55a8f359b78b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 24 Feb 2022 20:31:54 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- scripts/i18n_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/i18n_updater.py b/scripts/i18n_updater.py index 060217da6..bea42c762 100644 --- a/scripts/i18n_updater.py +++ b/scripts/i18n_updater.py @@ -26,7 +26,7 @@ def cache_creation(): if os.path.isdir(file): continue - with open(file, "r") as f: + with open(file) as f: for line in f.readlines(): if line.startswith('"POT-Creation-Date: '): cache[file] = line From f57edf48793244eb418b86972aafea00699d8d04 Mon Sep 17 00:00:00 2001 From: Andrew-Chen-Wang Date: Thu, 24 Feb 2022 17:00:23 -0500 Subject: [PATCH 7/8] 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 --- .github/workflows/test.yml | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 392a544d7..aa322e493 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -94,7 +94,7 @@ jobs: - name: Install dependencies run: | sudo apt-get install -y gettext - python -m pip install --upgrade pip + python -m pip install --upgrade pip wheel setuptools pip install -e .[dev] - name: Run locale @@ -108,22 +108,10 @@ jobs: 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 user about locale update + - name: Tell whether locale updated if: steps.auto-commit-action.outputs.changes_detected == 'true' run: echo "Locale files updated" - - - name: Tell user about locale update - if: | - steps.auto-commit-action.outputs.changes_detected == 'true' - && github.event_name == 'pull_request' - uses: actions/github-script@v5 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body: "👋 Hey! We've automatically updated the locale files for you." - }) From 03cea7f8d05df58b772f4c3d57087f8bd668a962 Mon Sep 17 00:00:00 2001 From: Andrew-Chen-Wang Date: Thu, 24 Feb 2022 17:04:10 -0500 Subject: [PATCH 8/8] Use separate workflow file --- .github/workflows/i18n.yml | 61 ++++++++++++++++++++++++++++++++++++++ .github/workflows/test.yml | 52 -------------------------------- 2 files changed, 61 insertions(+), 52 deletions(-) create mode 100644 .github/workflows/i18n.yml diff --git a/.github/workflows/i18n.yml b/.github/workflows/i18n.yml new file mode 100644 index 000000000..671de4807 --- /dev/null +++ b/.github/workflows/i18n.yml @@ -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" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aa322e493..619298400 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -63,55 +63,3 @@ jobs: uses: codecov/codecov-action@v1 with: name: Python ${{ matrix.python-version }} - - locale-test: - 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"