|
| 1 | +.PHONY: clean data lint requirements sync_data_to_s3 sync_data_from_s3 |
| 2 | + |
| 3 | +################################################################################# |
| 4 | +# GLOBALS # |
| 5 | +################################################################################# |
| 6 | + |
| 7 | +PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) |
| 8 | +BUCKET = [OPTIONAL] your-bucket-for-syncing-data (do not include 's3://') |
| 9 | +PROFILE = default |
| 10 | +PROJECT_NAME = CaReCur |
| 11 | +PYTHON_INTERPRETER = python3 |
| 12 | + |
| 13 | +ifeq (,$(shell which conda)) |
| 14 | +HAS_CONDA=False |
| 15 | +else |
| 16 | +HAS_CONDA=True |
| 17 | +endif |
| 18 | + |
| 19 | +################################################################################# |
| 20 | +# COMMANDS # |
| 21 | +################################################################################# |
| 22 | + |
| 23 | +## Install Python Dependencies |
| 24 | +requirements: test_environment |
| 25 | + $(PYTHON_INTERPRETER) -m pip install -U pip setuptools wheel |
| 26 | + $(PYTHON_INTERPRETER) -m pip install -r requirements.txt |
| 27 | + |
| 28 | +## Make Dataset |
| 29 | +data: requirements |
| 30 | + $(PYTHON_INTERPRETER) src/data/make_dataset.py data/raw data/processed |
| 31 | + |
| 32 | +## Delete all compiled Python files |
| 33 | +clean: |
| 34 | + find . -type f -name "*.py[co]" -delete |
| 35 | + find . -type d -name "__pycache__" -delete |
| 36 | + |
| 37 | +## Lint using flake8 |
| 38 | +lint: |
| 39 | + flake8 src |
| 40 | + |
| 41 | +## Upload Data to S3 |
| 42 | +sync_data_to_s3: |
| 43 | +ifeq (default,$(PROFILE)) |
| 44 | + aws s3 sync data/ s3://$(BUCKET)/data/ |
| 45 | +else |
| 46 | + aws s3 sync data/ s3://$(BUCKET)/data/ --profile $(PROFILE) |
| 47 | +endif |
| 48 | + |
| 49 | +## Download Data from S3 |
| 50 | +sync_data_from_s3: |
| 51 | +ifeq (default,$(PROFILE)) |
| 52 | + aws s3 sync s3://$(BUCKET)/data/ data/ |
| 53 | +else |
| 54 | + aws s3 sync s3://$(BUCKET)/data/ data/ --profile $(PROFILE) |
| 55 | +endif |
| 56 | + |
| 57 | +## Set up python interpreter environment |
| 58 | +create_environment: |
| 59 | +ifeq (True,$(HAS_CONDA)) |
| 60 | + @echo ">>> Detected conda, creating conda environment." |
| 61 | +ifeq (3,$(findstring 3,$(PYTHON_INTERPRETER))) |
| 62 | + conda create --name $(PROJECT_NAME) python=3 |
| 63 | +else |
| 64 | + conda create --name $(PROJECT_NAME) python=2.7 |
| 65 | +endif |
| 66 | + @echo ">>> New conda env created. Activate with:\nsource activate $(PROJECT_NAME)" |
| 67 | +else |
| 68 | + $(PYTHON_INTERPRETER) -m pip install -q virtualenv virtualenvwrapper |
| 69 | + @echo ">>> Installing virtualenvwrapper if not already installed.\nMake sure the following lines are in shell startup file\n\ |
| 70 | + export WORKON_HOME=$$HOME/.virtualenvs\nexport PROJECT_HOME=$$HOME/Devel\nsource /usr/local/bin/virtualenvwrapper.sh\n" |
| 71 | + @bash -c "source `which virtualenvwrapper.sh`;mkvirtualenv $(PROJECT_NAME) --python=$(PYTHON_INTERPRETER)" |
| 72 | + @echo ">>> New virtualenv created. Activate with:\nworkon $(PROJECT_NAME)" |
| 73 | +endif |
| 74 | + |
| 75 | +## Test python environment is setup correctly |
| 76 | +test_environment: |
| 77 | + $(PYTHON_INTERPRETER) test_environment.py |
| 78 | + |
| 79 | +################################################################################# |
| 80 | +# PROJECT RULES # |
| 81 | +################################################################################# |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +################################################################################# |
| 86 | +# Self Documenting Commands # |
| 87 | +################################################################################# |
| 88 | + |
| 89 | +.DEFAULT_GOAL := help |
| 90 | + |
| 91 | +# Inspired by <http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html> |
| 92 | +# sed script explained: |
| 93 | +# /^##/: |
| 94 | +# * save line in hold space |
| 95 | +# * purge line |
| 96 | +# * Loop: |
| 97 | +# * append newline + line to hold space |
| 98 | +# * go to next line |
| 99 | +# * if line starts with doc comment, strip comment character off and loop |
| 100 | +# * remove target prerequisites |
| 101 | +# * append hold space (+ newline) to line |
| 102 | +# * replace newline plus comments by `---` |
| 103 | +# * print line |
| 104 | +# Separate expressions are necessary because labels cannot be delimited by |
| 105 | +# semicolon; see <http://stackoverflow.com/a/11799865/1968> |
| 106 | +.PHONY: help |
| 107 | +help: |
| 108 | + @echo "$$(tput bold)Available rules:$$(tput sgr0)" |
| 109 | + @echo |
| 110 | + @sed -n -e "/^## / { \ |
| 111 | + h; \ |
| 112 | + s/.*//; \ |
| 113 | + :doc" \ |
| 114 | + -e "H; \ |
| 115 | + n; \ |
| 116 | + s/^## //; \ |
| 117 | + t doc" \ |
| 118 | + -e "s/:.*//; \ |
| 119 | + G; \ |
| 120 | + s/\\n## /---/; \ |
| 121 | + s/\\n/ /g; \ |
| 122 | + p; \ |
| 123 | + }" ${MAKEFILE_LIST} \ |
| 124 | + | LC_ALL='C' sort --ignore-case \ |
| 125 | + | awk -F '---' \ |
| 126 | + -v ncol=$$(tput cols) \ |
| 127 | + -v indent=19 \ |
| 128 | + -v col_on="$$(tput setaf 6)" \ |
| 129 | + -v col_off="$$(tput sgr0)" \ |
| 130 | + '{ \ |
| 131 | + printf "%s%*s%s ", col_on, -indent, $$1, col_off; \ |
| 132 | + n = split($$2, words, " "); \ |
| 133 | + line_length = ncol - indent; \ |
| 134 | + for (i = 1; i <= n; i++) { \ |
| 135 | + line_length -= length(words[i]) + 1; \ |
| 136 | + if (line_length <= 0) { \ |
| 137 | + line_length = ncol - indent - length(words[i]) - 1; \ |
| 138 | + printf "\n%*s ", -indent, " "; \ |
| 139 | + } \ |
| 140 | + printf "%s ", words[i]; \ |
| 141 | + } \ |
| 142 | + printf "\n"; \ |
| 143 | + }' \ |
| 144 | + | more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars') |
0 commit comments