Skip to content

Commit

Permalink
Use actual PO library to check for msgid differences
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Apr 16, 2020
1 parent 6377388 commit 21defbd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ script:
- set -e

# test that everything is well formatted, no missing migrations etc
- ./code_check.py
- ./code_check.py --debug

# run our Javascript tests
- node_modules/karma/bin/karma start karma.conf.coffee --single-run --browsers PhantomJS
Expand Down
37 changes: 30 additions & 7 deletions code_check.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/usr/bin/env python3

import os
import argparse
import subprocess

import colorama
import polib

DEBUG = "TRAVIS" in os.environ
parser = argparse.ArgumentParser(description="Code checks")
parser.add_argument("--debug", action="store_true")
args = parser.parse_args()

DEBUG = args.debug


def cmd(line):
Expand All @@ -26,17 +31,35 @@ def status(line):


def update_po_files():
def get_current_msgids():
pot = polib.pofile("locale/en_US/LC_MESSAGES/django.po")
return {e.msgid for e in pot if not e.fuzzy and not e.obsolete}

# get the current set of msgids
saved_msgids = get_current_msgids()

# re-extract locale files from source code
ignore_paths = ("env/*", "static/bower/*", "static/components/*", "node_modules/*")
ignore_args = " ".join([f'--ignore="{p}"' for p in ignore_paths])

cmd(f"python manage.py makemessages -a -e haml,html,txt,py --no-location --no-wrap {ignore_args}")
cmd(f"python manage.py makemessages -d djangojs -a --no-location --no-wrap {ignore_args}")

modified_pos = cmd("git diff --name-only locale/").split("\n")
for po in modified_pos:
# we only care about changes to msgids, so if we can't find any of those, revert the file
if not cmd(rf'git diff -U0 {po} | grep -e "^[\+\-]msgid" || true').strip():
cmd(f"git checkout -- {po}")
# get the new set of msgids
actual_msgids = get_current_msgids()

added_msgids = actual_msgids.difference(saved_msgids)
removed_msgids = saved_msgids.difference(actual_msgids)

if DEBUG:
for mid in added_msgids:
print(f" + {repr(mid)}")
for mid in removed_msgids:
print(f" - {repr(mid)}")

# if there are no actual changes to msgids, revert
if not added_msgids and not removed_msgids:
cmd(f"git checkout -- locale")


if __name__ == "__main__":
Expand Down

0 comments on commit 21defbd

Please sign in to comment.