From 690241a8a926ae646fdc45ed59ce72f29e6d0bad Mon Sep 17 00:00:00 2001 From: Colin Sullivan Date: Wed, 5 Oct 2016 13:38:34 -0400 Subject: [PATCH 1/4] Added self-documenting comments and default Rule to list commands with descriptions --- {{ cookiecutter.repo_name }}/Makefile | 69 +++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/{{ cookiecutter.repo_name }}/Makefile b/{{ cookiecutter.repo_name }}/Makefile index df94b11..b99ac61 100644 --- a/{{ cookiecutter.repo_name }}/Makefile +++ b/{{ cookiecutter.repo_name }}/Makefile @@ -10,24 +10,93 @@ BUCKET = {{ cookiecutter.s3_bucket }} # COMMANDS # ################################################################################# +## Install Python Dependencies requirements: pip install -q -r requirements.txt +## Make Dataset data: requirements python src/data/make_dataset.py +## Delete all compiled Python files clean: find . -name "*.pyc" -exec rm {} \; +## Lint using flake8 lint: flake8 --exclude=lib/,bin/,docs/conf.py . +## Upload Data to S3 sync_data_to_s3: aws s3 sync data/ s3://$(BUCKET)/data/ +## Download Data from S3 sync_data_from_s3: aws s3 sync s3://$(BUCKET)/data/ data/ ################################################################################# # PROJECT RULES # ################################################################################# + + + +################################################################################# +# Self Documenting Commands # +################################################################################# + +.DEFAULT_GOAL := show-help + +# Inspired by +# sed script explained: +# /^##/: +# * save line in hold space +# * purge line +# * Loop: +# * append newline + line to hold space +# * go to next line +# * if line starts with doc comment, strip comment character off and loop +# * remove target prerequisites +# * append hold space (+ newline) to line +# * replace newline plus comments by `---` +# * print line +# Separate expressions are necessary because labels cannot be delimited by +# semicolon; see +.PHONY: show-help +show-help: + @echo "$$(tput bold)Available rules:$$(tput sgr0)" + @echo + @sed -n -e "/^## / { \ + h; \ + s/.*//; \ + :doc" \ + -e "H; \ + n; \ + s/^## //; \ + t doc" \ + -e "s/:.*//; \ + G; \ + s/\\n## /---/; \ + s/\\n/ /g; \ + p; \ + }" ${MAKEFILE_LIST} \ + | LC_ALL='C' sort --ignore-case \ + | awk -F '---' \ + -v ncol=$$(tput cols) \ + -v indent=19 \ + -v col_on="$$(tput setaf 6)" \ + -v col_off="$$(tput sgr0)" \ + '{ \ + printf "%s%*s%s ", col_on, -indent, $$1, col_off; \ + n = split($$2, words, " "); \ + line_length = ncol - indent; \ + for (i = 1; i <= n; i++) { \ + line_length -= length(words[i]) + 1; \ + if (line_length <= 0) { \ + line_length = ncol - indent - length(words[i]) - 1; \ + printf "\n%*s ", -indent, " "; \ + } \ + printf "%s ", words[i]; \ + } \ + printf "\n"; \ + }' \ + | more $(shell test $(shell uname) == Darwin && echo '--no-init --raw-control-chars') \ No newline at end of file From d13bc88ab0398195db466f99e2f58e31f1e241fb Mon Sep 17 00:00:00 2001 From: Colin Sullivan Date: Wed, 5 Oct 2016 15:16:42 -0400 Subject: [PATCH 2/4] add parameter to specify python interpreter and commands to create and test virtual environments --- cookiecutter.json | 3 ++- {{ cookiecutter.repo_name }}/Makefile | 13 +++++++++- .../test_environment.py | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 {{ cookiecutter.repo_name }}/test_environment.py diff --git a/cookiecutter.json b/cookiecutter.json index 22302ac..cf3ad39 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -5,5 +5,6 @@ "description": "A short description of the project.", "year": "2016", "open_source_license": ["MIT", "BSD", "Not open source"], - "s3_bucket": "[OPTIONAL] your-bucket-for-syncing-data (do not include 's3://')" + "s3_bucket": "[OPTIONAL] your-bucket-for-syncing-data (do not include 's3://')", + "python_interpreter": ["python", "python3"] } diff --git a/{{ cookiecutter.repo_name }}/Makefile b/{{ cookiecutter.repo_name }}/Makefile index b99ac61..c3ef900 100644 --- a/{{ cookiecutter.repo_name }}/Makefile +++ b/{{ cookiecutter.repo_name }}/Makefile @@ -5,13 +5,15 @@ ################################################################################# BUCKET = {{ cookiecutter.s3_bucket }} +PROJECT_NAME = {{ cookiecutter.repo_name }} +PYTHON_INTERPRETER = {{ cookiecutter.python_interpreter }} ################################################################################# # COMMANDS # ################################################################################# ## Install Python Dependencies -requirements: +requirements: test_environment pip install -q -r requirements.txt ## Make Dataset @@ -34,6 +36,15 @@ sync_data_to_s3: sync_data_from_s3: aws s3 sync s3://$(BUCKET)/data/ data/ +## Set up python interpreter environment +setup_environment: + source /usr/local/bin/virtualenvwrapper.sh; mkvirtualenv $(PROJECT_NAME) --python=$(PYTHON_INTERPRETER) + @echo ">>> Activate virtualenv with:\nworkon $(PROJECT_NAME)" + +## Test python environment is setup correctly +test_environment: + python test_environment.py + ################################################################################# # PROJECT RULES # ################################################################################# diff --git a/{{ cookiecutter.repo_name }}/test_environment.py b/{{ cookiecutter.repo_name }}/test_environment.py new file mode 100644 index 0000000..ed8c3a9 --- /dev/null +++ b/{{ cookiecutter.repo_name }}/test_environment.py @@ -0,0 +1,24 @@ +import sys + +REQUIRED_PYTHON = "{{ cookiecutter.python_interpreter }}" + + +def main(): + system_major = sys.version_info.major + if REQUIRED_PYTHON == "python": + required_major = 2 + elif REQUIRED_PYTHON == "python3": + required_major = 3 + else: + raise ValueError("Unrecognized python interpreter: {}".format( + REQUIRED_PYTHON)) + + if system_major != required_major: + raise TypeError("This project requires Python {}. Found: {}".format( + required_major, sys.version)) + else: + print(">>> Development environment passes all tests!") + + +if __name__ == '__main__': + main() \ No newline at end of file From a3785c9180ea97bcec807b9167790c19eddb5937 Mon Sep 17 00:00:00 2001 From: Colin Sullivan Date: Wed, 5 Oct 2016 16:03:25 -0400 Subject: [PATCH 3/4] small naming fix --- {{ cookiecutter.repo_name }}/Makefile | 4 ++-- {{ cookiecutter.repo_name }}/test_environment.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/{{ cookiecutter.repo_name }}/Makefile b/{{ cookiecutter.repo_name }}/Makefile index c3ef900..04a6738 100644 --- a/{{ cookiecutter.repo_name }}/Makefile +++ b/{{ cookiecutter.repo_name }}/Makefile @@ -37,9 +37,9 @@ sync_data_from_s3: aws s3 sync s3://$(BUCKET)/data/ data/ ## Set up python interpreter environment -setup_environment: +create_environment: source /usr/local/bin/virtualenvwrapper.sh; mkvirtualenv $(PROJECT_NAME) --python=$(PYTHON_INTERPRETER) - @echo ">>> Activate virtualenv with:\nworkon $(PROJECT_NAME)" + @echo ">>> New virtualenv created. Activate with:\nworkon $(PROJECT_NAME)" ## Test python environment is setup correctly test_environment: diff --git a/{{ cookiecutter.repo_name }}/test_environment.py b/{{ cookiecutter.repo_name }}/test_environment.py index ed8c3a9..752c2dc 100644 --- a/{{ cookiecutter.repo_name }}/test_environment.py +++ b/{{ cookiecutter.repo_name }}/test_environment.py @@ -14,8 +14,9 @@ def main(): REQUIRED_PYTHON)) if system_major != required_major: - raise TypeError("This project requires Python {}. Found: {}".format( - required_major, sys.version)) + raise TypeError( + "This project requires Python {}. Found: Python {}".format( + required_major, sys.version)) else: print(">>> Development environment passes all tests!") From ae2b54e738a5563d633f93e6f35a99aaa3a4b652 Mon Sep 17 00:00:00 2001 From: Colin Sullivan Date: Mon, 10 Oct 2016 17:28:44 -0400 Subject: [PATCH 4/4] install virtualenvwrapper by default, and use project defined python interpreter. Also make requirements command output verbose --- {{ cookiecutter.repo_name }}/Makefile | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/{{ cookiecutter.repo_name }}/Makefile b/{{ cookiecutter.repo_name }}/Makefile index 04a6738..bef8958 100644 --- a/{{ cookiecutter.repo_name }}/Makefile +++ b/{{ cookiecutter.repo_name }}/Makefile @@ -14,11 +14,11 @@ PYTHON_INTERPRETER = {{ cookiecutter.python_interpreter }} ## Install Python Dependencies requirements: test_environment - pip install -q -r requirements.txt + pip install -r requirements.txt ## Make Dataset data: requirements - python src/data/make_dataset.py + $(PYTHON_INTERPRETER) src/data/make_dataset.py ## Delete all compiled Python files clean: @@ -38,12 +38,16 @@ sync_data_from_s3: ## Set up python interpreter environment create_environment: + @pip install -q virtualenvwrapper + @echo ">>> Installing virtualenvwrapper if not already intalled.\nMake sure the following lines are in shell startup file\n\ + export WORKON_HOME=$HOME/.virtualenvs\nexport PROJECT_HOME=$HOME/Devel\nsource /usr/local/bin/virtualenvwrapper.sh\n" + source /usr/local/bin/virtualenvwrapper.sh; mkvirtualenv $(PROJECT_NAME) --python=$(PYTHON_INTERPRETER) @echo ">>> New virtualenv created. Activate with:\nworkon $(PROJECT_NAME)" ## Test python environment is setup correctly test_environment: - python test_environment.py + $(PYTHON_INTERPRETER) test_environment.py ################################################################################# # PROJECT RULES #