Makefile 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. ifeq (,$(shell which conda))
  14. HAS_CONDA=False
  15. else
  16. HAS_CONDA=True
  17. endif
  18. #################################################################################
  19. # COMMANDS #
  20. #################################################################################
  21. ## Install Python Dependencies
  22. requirements: test_environment
  23. $(PYTHON_INTERPRETER) -m pip install -U pip setuptools wheel
  24. $(PYTHON_INTERPRETER) -m pip install -r requirements.txt
  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. ## Make Dataset
  33. data: requirements
  34. $(PYTHON_INTERPRETER) $(PROJECT_NAME)/utils/make_dataset.py
  35. ## Upload Data to S3
  36. sync_data_to_s3:
  37. ifeq (default,$(S3PROFILE))
  38. aws s3 sync data/ s3://$(S3BUCKET)/data/
  39. aws s3 sync models/ s3://$(S3BUCKET)/models/
  40. else
  41. aws s3 sync data/ s3://$(S3BUCKET)/data/ --profile $(S3PROFILE)
  42. aws s3 sync models/ s3://$(S3BUCKET)/models/ --profile $(S3PROFILE)
  43. endif
  44. ## Download Data from S3
  45. sync_data_from_s3:
  46. ifeq (default,$(S3PROFILE))
  47. aws s3 sync s3://$(S3BUCKET)/data/ data/
  48. aws s3 sync s3://$(S3BUCKET)/models/ models/
  49. else
  50. aws s3 sync s3://$(S3BUCKET)/data/ data/ --profile $(S3PROFILE)
  51. aws s3 sync s3://$(S3BUCKET)/models/ models/ --profile $(S3PROFILE)
  52. endif
  53. ## Upload to minio
  54. sync_data_to_minio:
  55. ifeq (default,$(MINIOPROFILE))
  56. rclone sync data/ $(MINIO):$(PROJECT_NAME)/data/ --stats-one-line -P --stats 2s
  57. rclone sync models/ $(MINIO):$(PROJECT_NAME)/models/ --stats-one-line -P --stats 2s
  58. ## Download from minio
  59. sync_data_from_minio:
  60. ifeq (default,$(MINIOPROFILE))
  61. rclone sync $(MINIO):$(PROJECT_NAME)/data/ data/ --stats-one-line -P --stats 2s
  62. rclone sync $(MINIO):$(PROJECT_NAME)/models/ models/ --stats-one-line -P --stats 2s
  63. ## Initial set up of python interpreter environment, version control and pre-commit hooks
  64. initial_setup:
  65. ifeq (True,$(HAS_CONDA))
  66. @echo ">>> Detected conda, creating conda environment."
  67. ifeq (3,$(findstring 3,$(PYTHON_INTERPRETER)))
  68. conda create --name $(PROJECT_NAME) python=3
  69. endif
  70. @bash -c "git init"
  71. @bash -c "source activate $(PROJECT_NAME); pip install dvc[all]; dvc init"
  72. @bash -c "echo '[core]' >> $(PROJECT_DIR)/.dvc/config"
  73. @bash -c "echo 'analytics = false' >> $(PROJECT_DIR)/.dvc/config"
  74. @bash -c "source activate $(PROJECT_NAME); pip install pre-commit; pre-commit install; pre-commit autoupdate"
  75. @bash -c "source activate $(PROJECT_NAME);; pip install -r $(PROJECT_DIR)/requirements.txt"
  76. @bash -c "echo 'CWD=$(PROJECT_DIR)' >> $(PROJECT_DIR)/.env"
  77. @bash -c "echo '.env' >> $(PROJECT_DIR)/.gitignore"
  78. @bash -c "git add . ; git commit -am 'INITIAL COMMIT'"
  79. @bash -c "git remote add origin https://github.com/$(USERNAME)/$(PROJECT_NAME).git"
  80. # @bash -c "git remote add origin $(GIT)"
  81. @bash -c "git push -u origin" && echo "upload repo to the remote" || echo "repository $(PROJECT_NAME) doesn't exist yet, please create it."
  82. @echo ">>> New virtualenv created. Activate with:\nsource activate $(PROJECT_NAME)"
  83. @echo ">>> git and dvc are ready to go"
  84. else
  85. $(PYTHON_INTERPRETER) -m pip install -q virtualenv virtualenvwrapper
  86. @echo ">>> Installing virtualenvwrapper if not already installed.\nMake sure the following lines are in shell startup file\n\
  87. export WORKON_HOME=$$HOME/.virtualenvs\nexport PROJECT_HOME=$$HOME/Devel\nsource /usr/local/bin/virtualenvwrapper.sh\n"
  88. @bash -c "source `which virtualenvwrapper.sh`; mkvirtualenv $(PROJECT_NAME) --python=$(PYTHON_INTERPRETER)"
  89. @echo ">>> Initializing git repo"
  90. @bash -c "git init"
  91. @bash -c "workon $(PROJECT_NAME); pip install dvc[all]; dvc init"
  92. @bash -c "echo '[core]' >> $(PROJECT_DIR)/.dvc/config"
  93. @bash -c "echo 'analytics = false' >> $(PROJECT_DIR)/.dvc/config"
  94. @bash -c "workon $(PROJECT_NAME); pip install pre-commit; pre-commit install; pre-commit autoupdate"
  95. @bash -c "workon $(PROJECT_NAME);; pip install -r $(PROJECT_DIR)/requirements.txt"
  96. @bash -c "echo 'CWD=$(PROJECT_DIR)' >> $(PROJECT_DIR)/.env"
  97. @bash -c "echo '.env' >> $(PROJECT_DIR)/.gitignore"
  98. @bash -c "git add . ; git commit -am 'INITIAL COMMIT'"
  99. @bash -c "git remote add origin https://github.com/$(USERNAME)/$(PROJECT_NAME).git"
  100. # @bash -c "git remote add origin $(GIT)"
  101. @bash -c "git push -u origin" && echo "upload repo to the remote" || echo "repository $(PROJECT_NAME) doesn't exist yet, please create it."
  102. @echo ">>> New virtualenv created. Activate with:\nworkon $(PROJECT_NAME)"
  103. @echo ">>> git and dvc are ready to go"
  104. ## Following set up of python interpreter environment, version control and pre-commit hooks
  105. following_setup:
  106. @bash -c "virtualenv -p $(PYTHON_INTERPRETER) $(HOME)/envs/$(PROJECT_NAME)"
  107. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install dvc[all]"
  108. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install pre-commit; pre-commit install"
  109. @bash -c "source $(HOME)/envs/$(PROJECT_NAME)/bin/activate; pip install -r $(PROJECT_DIR)/requirements.txt"
  110. @echo ">>> New virtualenv created. Activate with:\nsource $(HOME)/envs/$(PROJECT_NAME)/bin/activate"
  111. @echo ">>> git and dvc are ready to go"
  112. requirements:
  113. ifeq (True,$(HAS_CONDA))
  114. @bash -c "workon $(PROJECT_NAME)/bin/activate; pip install -r $(PROJECT_DIR)/requirements.txt"
  115. else
  116. @bash -c "source activate $(PROJECT_NAME)/bin/activate; pip install -r $(PROJECT_DIR)/requirements.txt"
  117. environment:
  118. ifeq (True,$(HAS_CONDA))
  119. @echo ">>> Activate with:\nsource activate $(PROJECT_NAME)
  120. else
  121. @echo ">>> Activate with:\nworkon $(PROJECT_NAME)
  122. commit:
  123. $(eval EXP_FILE := "$(PROJECT_DIR)/$(PROJECT_NAME)/study_name.txt")
  124. $(eval EXP_NAME := $(shell cat ${EXP_FILE}))
  125. @bash -c "git commit -am \"completed experiment $(EXP_NAME)\"; git tag -a $(EXP_NAME) -m \"$(EXP_NAME)\""
  126. push:
  127. @bash -c "git push origin --follow-tags"
  128. ifeq (True,$(HAS_CONDA))
  129. @bash -c "source activate $(PROJECT_NAME); dvc push"
  130. else
  131. @bash -c "workon $(PROJECT_NAME); dvc push"
  132. #################################################################################
  133. # PROJECT RULES #
  134. #################################################################################
  135. #################################################################################
  136. # Self Documenting Commands #
  137. #################################################################################
  138. .DEFAULT_GOAL := help
  139. # Inspired by <http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html>
  140. # sed script explained:
  141. # /^##/:
  142. # * save line in hold space
  143. # * purge line
  144. # * Loop:
  145. # * append newline + line to hold space
  146. # * go to next line
  147. # * if line starts with doc comment, strip comment character off and loop
  148. # * remove target prerequisites
  149. # * append hold space (+ newline) to line
  150. # * replace newline plus comments by `---`
  151. # * print line
  152. # Separate expressions are necessary because labels cannot be delimited by
  153. # semicolon; see <http://stackoverflow.com/a/11799865/1968>
  154. .PHONY: help
  155. help:
  156. @echo "$$(tput bold)Available rules:$$(tput sgr0)"
  157. @echo
  158. @sed -n -e "/^## / { \
  159. h; \
  160. s/.*//; \
  161. :doc" \
  162. -e "H; \
  163. n; \
  164. s/^## //; \
  165. t doc" \
  166. -e "s/:.*//; \
  167. G; \
  168. s/\\n## /---/; \
  169. s/\\n/ /g; \
  170. p; \
  171. }" ${MAKEFILE_LIST} \
  172. | LC_ALL='C' sort --ignore-case \
  173. | awk -F '---' \
  174. -v ncol=$$(tput cols) \
  175. -v indent=19 \
  176. -v col_on="$$(tput setaf 6)" \
  177. -v col_off="$$(tput sgr0)" \
  178. '{ \
  179. printf "%s%*s%s ", col_on, -indent, $$1, col_off; \
  180. n = split($$2, words, " "); \
  181. line_length = ncol - indent; \
  182. for (i = 1; i <= n; i++) { \
  183. line_length -= length(words[i]) + 1; \
  184. if (line_length <= 0) { \
  185. line_length = ncol - indent - length(words[i]) - 1; \
  186. printf "\n%*s ", -indent, " "; \
  187. } \
  188. printf "%s ", words[i]; \
  189. } \
  190. printf "\n"; \
  191. }' \
  192. | more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars')