PYTHON_MODULES := {{python-pkg-name}}
PYTHONPATH := .
VENV := .venv
PYTEST := env PYTHONPATH=$(PYTHONPATH) PYTEST=1 $(VENV)/bin/py.test
PYLINT := env PYTHONPATH=$(PYTHONPATH) $(VENV)/bin/pylint --disable=I0011 --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}"
PEP8 := env PYTHONPATH=$(PYTHONPATH) $(VENV)/bin/pep8 --repeat --ignore=E202,E501,E402
PYTHON := env PYTHONPATH=$(PYTHONPATH) $(VENV)/bin/python
PIP := $(VENV)/bin/pip

DEFAULT_PYTHON := $(shell ./utils/choose_default_python.py)
VIRTUALENV := ./utils/virtualenv-15.1.0/virtualenv.py

REQUIREMENTS := -r requirements.txt
DEV_REQUIREMENTS := -r requirements-dev.txt

default: check-coding-style
build:

debug:
	env DEBUG=1 $(PYTHON) {{python-pkg-name}}/main.py
run:
	$(PYTHON) {{python-pkg-name}}/main.py
uwsgi:
	$(VENV)/bin/uwsgi --processes=2 --threads=4 --wsgi-file={{python-pkg-name}}/main.py --env=PYTHONPATH=. --http=localhost:8082 --disable-logging
shell:
	$(PYTHON) -i

venv:
	test -d $(VENV) || $(DEFAULT_PYTHON) $(VIRTUALENV) --no-download -q $(VENV)
requirements:
	@if [ -d wheelhouse ]; then \
		$(PIP) install -q --no-index --find-links=wheelhouse $(REQUIREMENTS); \
	else \
		$(PIP) install -q $(REQUIREMENTS); \
	fi
logdir:
	@if [ ! -d /var/log/{{name}} ]; then \
		sudo mkdir /var/log/{{name}}; \
		sudo chown -R ${USER} /var/log/{{name}}; \
	fi
bootstrap: venv requirements
bootstrap-dev: REQUIREMENTS += $(DEV_REQUIREMENTS)
bootstrap-dev: venv requirements
install: bootstrap
	$(PIP) install -q --no-index .
uninstall: bootstrap
	$(PIP) uninstall -q -y {{python-pkg-name}}
deb:
	utils/build-deb
pipfreeze:
	$(PIP) freeze
wheel: bootstrap-dev
	@echo "building wheelhouse..."
	$(PIP) install wheel
	$(PIP) wheel -w wheelhouse $(REQUIREMENTS)

check: just-test
sanity-check: bootstrap
	$(PYTHON) {{python-pkg-name}}/sanity_check.py
check-coding-style: bootstrap-dev
	$(PEP8) $(PYTHON_MODULES)
	$(PYLINT) -E $(PYTHON_MODULES)
	$(PYTHON) {{python-pkg-name}}/config.py
pylint-full: check-coding-style
	$(PYLINT) $(PYTHON_MODULES)
test: check-coding-style
	$(PYTEST) $(PYTHON_MODULES)
just-test:
	$(PYTEST) $(PYTHON_MODULES)

install-dev-no-wheel:
	$(PIP) install -q -i https://pypi.tuna.tsinghua.edu.cn/simple $(DEV_REQUIREMENTS)
test-prod: install-dev-no-wheel test

clean:
	find . -name "__pycache__" -type d -exec rm -rf {} \; || true
	find . -name "*.pyc" -type f -exec rm -rf {} \; || true
full-clean:
	rm -rf $(VENV) build/ dist/ wheelhouse/ {{python-pkg-name}}.egg-info/ distribute-*.tar.gz
	find . -name "__pycache__" -type d -exec rm -rf {} \; || true
	find . -name "*.pyc" -type f -exec rm -rf {} \; || true
todo:
	@rgrep -I -n --exclude=Makefile --exclude=TAGS --exclude-dir=$(VENV) -E '(TODO|FIXME|XXX|not_implemented)' $(PYTHON_MODULES)
TAGS:
	etags -R --exclude=static $(PYTHON_MODULES)
update-git-hooks: install-git-hooks-force
install-git-hooks:
	./utils/install-git-hooks
install-git-hooks-force:
	./utils/install-git-hooks -f
loc:
	find . -regex '.*\.pyw?$$' -exec wc -l {} \+ | tail -n 1
	which cloc >/dev/null && cloc {{python-pkg-name}}/
.PHONY: default build debug run uwsgi shell venv bootstrap bootstrap-dev install uninstall deb pipfreeze wheel check check-coding-style pylint-full test just-test test-prod clean full-clean todo TAGS update-git-hooks install-git-hooks install-git-hooks-force loc
