Makefile 4.6 KB

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