Makefile 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #################################################################################
  2. # GLOBALS #
  3. #################################################################################
  4. PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
  5. S3BUCKET = {{ cookiecutter.s3_bucket }}
  6. S3PROFILE = {{ cookiecutter.aws_profile }}
  7. MINIO = {{ cookiecutter.minio }}
  8. MINIOPROFILE = {{ cookiecutter.minio_profile }}
  9. PROJECT_NAME = {{ cookiecutter.repo_name }}
  10. PYTHON_INTERPRETER = {{ cookiecutter.python_interpreter }}
  11. # GIT = {{ cookiecutter.git_remote_repo }}
  12. USERNAME = {{ cookiecutter.author_name }}
  13. #################################################################################
  14. # COMMANDS #
  15. #################################################################################
  16. ## Install Python Dependencies
  17. requirements: test_environment
  18. $(PYTHON_INTERPRETER) -m pip install -U pip setuptools wheel
  19. $(PYTHON_INTERPRETER) -m pip install -r requirements.txt
  20. ## Delete all compiled Python files
  21. clean:
  22. find . -type f -name "*.py[co]" -delete
  23. find . -type d -name "__pycache__" -delete
  24. ## Lint using flake8
  25. lint:
  26. flake8 src
  27. ## Make Dataset
  28. data: requirements
  29. $(PYTHON_INTERPRETER) src/data/make_dataset.py data/raw data/processed
  30. ## Upload Data to S3
  31. sync_data_to_s3:
  32. ifeq (default,$(S3PROFILE))
  33. aws s3 sync data/ s3://$(S3BUCKET)/data/
  34. else
  35. aws s3 sync data/ s3://$(S3BUCKET)/data/ --profile $(S3PROFILE)
  36. endif
  37. ## Download Data from S3
  38. sync_data_from_s3:
  39. ifeq (default,$(S3PROFILE))
  40. aws s3 sync s3://$(S3BUCKET)/data/ data/
  41. else
  42. aws s3 sync s3://$(S3BUCKET)/data/ data/ --profile $(S3PROFILE)
  43. endif
  44. ## Upload to minio
  45. sync_data_to_minio:
  46. ifeq (default,$(MINIOPROFILE))
  47. rclone --size-only sync data/ $(MINIO)/data/ --stats-one-line -P --stats 2s
  48. ## Download from minio
  49. sync_data_to_minio:
  50. ifeq (default,$(MINIOPROFILE))
  51. rclone --size-only sync $(MINIO)/data/ data/ --stats-one-line -P --stats 2s
  52. ## Initial set up of python interpreter environment, version control and pre-commit hooks
  53. initial_setup:
  54. @bash -c "virtualenv -p $(PYTHON_INTERPRETER) $(HOME)/envs/$(PROJECT_NAME)"
  55. @bash -c "git init"
  56. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install dvc[all]; dvc init"
  57. @bash -c "echo '[core]' >> $(PROJECT_DIR)/.dvc/config"
  58. @bash -c "echo 'analytics = false' >> $(PROJECT_DIR)/.dvc/config"
  59. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install pre-commit; pre-commit install; pre-commit autoupdate"
  60. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install -r $(PROJECT_DIR)/requirements.txt"
  61. @bash -c "echo 'CWD=$(PROJECT_DIR)' >> $(PROJECT_DIR)/.env"
  62. @bash -c "git add . ; git commit -am 'INITIAL COMMIT'"
  63. @bash -c "git remote add origin https://github.com/$(USERNAME)/$(PROJECT_NAME).git"
  64. # @bash -c "git remote add origin $(GIT)"
  65. @echo ">>> New virtualenv created. Activate with:\nsource $(HOME)/envs/$(PROJECT_NAME)/bin/activate"
  66. @echo ">>> git and dvc are ready to go"
  67. ## Following set up of python interpreter environment, version control and pre-commit hooks
  68. following_setup:
  69. @bash -c "virtualenv -p $(PYTHON_INTERPRETER) $(HOME)/envs/$(PROJECT_NAME)"
  70. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install dvc[all]"
  71. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install pre-commit; pre-commit install"
  72. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install -r $(PROJECT_DIR)/requirements.txt"
  73. @echo ">>> New virtualenv created. Activate with:\nsource $(HOME)/envs/$(PROJECT_NAME)/bin/activate"
  74. @echo ">>> git and dvc are ready to go"
  75. requirements:
  76. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install -r $(PROJECT_DIR)/requirements.txt"
  77. environment:
  78. @echo ">>> Activate with:\nsource $(HOME)/envs/$(PROJECT_NAME)/bin/activate"
  79. commit:
  80. $(eval EXP_FILE := "$(PROJECT_DIR)/$(PROJECT_NAME)/study_name.txt")
  81. $(eval EXP_NAME := $(shell cat ${EXP_FILE}))
  82. @bash -c "git commit -am \"completed experiment $(EXP_NAME)\"; git tag -a $(EXP_NAME) -m \"$(EXP_NAME)\""
  83. push:
  84. @bash -c "git push origin --follow-tags"
  85. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; dvc push"
  86. #################################################################################
  87. # PROJECT RULES #
  88. #################################################################################
  89. #################################################################################
  90. # Self Documenting Commands #
  91. #################################################################################
  92. .DEFAULT_GOAL := help
  93. # Inspired by <http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html>
  94. # sed script explained:
  95. # /^##/:
  96. # * save line in hold space
  97. # * purge line
  98. # * Loop:
  99. # * append newline + line to hold space
  100. # * go to next line
  101. # * if line starts with doc comment, strip comment character off and loop
  102. # * remove target prerequisites
  103. # * append hold space (+ newline) to line
  104. # * replace newline plus comments by `---`
  105. # * print line
  106. # Separate expressions are necessary because labels cannot be delimited by
  107. # semicolon; see <http://stackoverflow.com/a/11799865/1968>
  108. .PHONY: help
  109. help:
  110. @echo "$$(tput bold)Available rules:$$(tput sgr0)"
  111. @echo
  112. @sed -n -e "/^## / { \
  113. h; \
  114. s/.*//; \
  115. :doc" \
  116. -e "H; \
  117. n; \
  118. s/^## //; \
  119. t doc" \
  120. -e "s/:.*//; \
  121. G; \
  122. s/\\n## /---/; \
  123. s/\\n/ /g; \
  124. p; \
  125. }" ${MAKEFILE_LIST} \
  126. | LC_ALL='C' sort --ignore-case \
  127. | awk -F '---' \
  128. -v ncol=$$(tput cols) \
  129. -v indent=19 \
  130. -v col_on="$$(tput setaf 6)" \
  131. -v col_off="$$(tput sgr0)" \
  132. '{ \
  133. printf "%s%*s%s ", col_on, -indent, $$1, col_off; \
  134. n = split($$2, words, " "); \
  135. line_length = ncol - indent; \
  136. for (i = 1; i <= n; i++) { \
  137. line_length -= length(words[i]) + 1; \
  138. if (line_length <= 0) { \
  139. line_length = ncol - indent - length(words[i]) - 1; \
  140. printf "\n%*s ", -indent, " "; \
  141. } \
  142. printf "%s ", words[i]; \
  143. } \
  144. printf "\n"; \
  145. }' \
  146. | more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars')