Skip to content

Commit

Permalink
Rename script, add check for migrations and use in travis
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Feb 12, 2020
1 parent a24b133 commit 7c1cdfc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 80 deletions.
36 changes: 9 additions & 27 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ services:
- redis-server

env:
- PGDB=11 PGIS=2.5
- PGDB=10 PGIS=2.4
global:
- DJANGO_SETTINGS_MODULE=temba.settings_travis
jobs:
- PGDB=11 PGIS=2.5
- PGDB=10 PGIS=2.4

cache:
directories:
Expand Down Expand Up @@ -85,29 +88,8 @@ script:
# any non-zero error code should be treated as an immediate failure
- set -e

# test that everything is well formatted
- black --line-length=119 --target-version=py36 temba --check --quiet || (echo 'The source code could use a bit more black.' && exit 1)

# test PEP8 compliance on both Python 2 and 3
- flake8

# compile messages
- cd temba && python ../manage.py compilemessages && cd ..

# fail if there are changes
- git diff --numstat
- git diff --numstat | awk '$3 ~ /locale.*mo$/ { print "compilemessages needs to be rerun, see .travis.yml for command"; exit 1 }'

# generate our locale messages
- python manage.py makemessages -a -e haml,html,txt,py --ignore="env/*" --no-location --no-wrap | awk '/warning/ { print "makemessages contains warnings"; exit 1 }'
- python manage.py makemessages -d djangojs -a --ignore="env/*" --ignore="static/bower/*" --ignore="static/components/*" --ignore="node_modules/*" --no-location --no-wrap
- python manage.py makemessages -d djangojs -a --ignore="env/*" --ignore="static/bower/*" --ignore="static/components/*" --ignore="node_modules/*" --no-location --no-wrap | awk '/warning/ { print "makemessages for djangojs contains warnings"; exit 1 }'

# fail if there are locale changes (we only care about changes to msgids)
- (! git diff -U0 locale/ | grep -e "^[+\-]msgid") || (echo 'Make messages needs to be rerun, see travis.yml for command.' && exit 1)

# check for model changes not reflected in a migration
- python manage.py makemigrations --settings=temba.settings_travis --dry-run | grep 'No changes detected' || (echo 'There are changes which require migrations.' && exit 1)
# test that everything is well formatted, no missing migrations etc
- ./code_check.py

# run our Javascript tests
- node_modules/karma/bin/karma start karma.conf.coffee --single-run --browsers PhantomJS
Expand All @@ -117,14 +99,14 @@ script:
- (! python manage.py compress --extension=".haml" --settings=temba.settings_compress | grep 'Error') || exit 1

# install database schema
- python manage.py migrate --settings=temba.settings_travis
- python manage.py migrate

# fire up mailroom
- ./mailroom -db=postgres://temba:temba@localhost:5432/temba -redis=redis://localhost:6379/10 -log-level=info &
- jobs

# run our Python unit tests on same database
- coverage run manage.py test --settings=temba.settings_travis --keepdb --noinput --verbosity=2
- coverage run manage.py test --keepdb --noinput --verbosity=2

- set +e

Expand Down
63 changes: 63 additions & 0 deletions code_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3

import os
import subprocess

import colorama

DEBUG = "TRAVIS" in os.environ


def cmd(line):
if DEBUG:
print(colorama.Style.DIM + "% " + line + colorama.Style.RESET_ALL)
try:
output = subprocess.check_output(line, shell=True).decode("utf-8")
if DEBUG:
print(colorama.Style.DIM + output + colorama.Style.RESET_ALL)
return output
except subprocess.CalledProcessError as e:
print(colorama.Fore.RED + e.stdout.decode("utf-8") + colorama.Style.RESET_ALL)
exit(1)


def status(line):
print(colorama.Fore.GREEN + f">>> {line}..." + colorama.Style.RESET_ALL)


def update_po_files():
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}")


if __name__ == "__main__":
colorama.init()

status("Running black")
cmd("black --line-length=119 --target-version=py36 temba")
status("Running flake8")
cmd("flake8")
status("Running isort")
cmd("isort -rc temba")
status("Updating locale PO files")
update_po_files()
status("Recompiling locale MO files")
cmd("python manage.py compilemessages")
status("Make any missing migrations")
cmd("python manage.py makemigrations")

# if any code changes were made, exit with error
if cmd("git diff temba locale"):
print("👎 " + colorama.Fore.RED + "Changes to be committed")
exit(1)
else:
print("👍 " + colorama.Fore.GREEN + "Code looks good. Make that PR!")
53 changes: 0 additions & 53 deletions pre-commit.py

This file was deleted.

0 comments on commit 7c1cdfc

Please sign in to comment.