Skip to content

Commit 0789ce3

Browse files
committed
Convert Makefile to use Python's Invoke project
1 parent 8d85c05 commit 0789ce3

10 files changed

Lines changed: 421 additions & 239 deletions

File tree

.circle/Makefile

Lines changed: 21 additions & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -1,249 +1,33 @@
11
ROOT_DIR ?= $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
2-
ifndef FORCE_CHECK_ALL_FILES
3-
CHANGED_FILES := $(shell $(CI_DIR)/utils/git-changes files)
4-
CHANGED_PY := $(shell ${CI_DIR}/utils/git-changes py)
5-
CHANGED_YAML := $(shell $(CI_DIR)/utils/git-changes yaml)
6-
CHANGED_JSON := $(shell $(CI_DIR)/utils/git-changes json)
7-
endif
8-
CHANGED_DIRECTORIES := $(shell $(CI_DIR)/utils/git-changes directories)
92
VIRTUALENV_DIR ?= virtualenv
103
ST2_REPO_PATH ?= /tmp/st2
114
ST2_REPO_BRANCH ?= master
12-
FORCE_CHECK_ALL_FILES ?= false
13-
FORCE_CHECK_PACK ?= false
145

15-
export ST2_REPO_PATH ROOT_DIR FORCE_CHECK_ALL_FILES FORCE_CHECK_PACK
6+
export ST2_REPO_PATH ST2_REPO_BRANCH ROOT_DIR
167

17-
# All components are prefixed by st2
18-
COMPONENTS := $(wildcard /tmp/st2/st2*)
19-
COMPONENTS_RUNNERS := $(wildcard /tmp/st2/contrib/runners/*)
20-
21-
.PHONY: all
22-
all: requirements lint packs-resource-register packs-tests
23-
24-
.PHONY: all-ci
25-
all-ci: compile .flake8 .pylint .copy-pack-to-subdirectory .configs-check .metadata-check .packs-resource-register .packs-tests
26-
27-
.PHONY: lint
28-
lint: requirements flake8 pylint configs-check metadata-check
29-
30-
.PHONY: flake8
31-
flake8: requirements .flake8
32-
33-
.PHONY: pylint
34-
pylint: requirements .clone_st2_repo .pylint
35-
36-
.PHONY: configs-check
37-
configs-check: requirements .clone_st2_repo .copy-pack-to-subdirectory .configs-check
38-
39-
.PHONY: metadata-check
40-
metadata-check: requirements .metadata-check
41-
42-
# Task which copies pack to temporary sub-directory so we can use old-style check scripts which
43-
# # require pack to be in a sub-directory
44-
.PHONY: .copy-pack-to-subdirectory
45-
.copy-pack-to-subdirectory:
46-
rm -rf /tmp/packs/$(PACK_NAME)
47-
mkdir -p /tmp/packs/$(PACK_NAME)
48-
cp -r ./* /tmp/packs/$(PACK_NAME)
49-
50-
.PHONY: packs-resource-register
51-
packs-resource-register: requirements .clone_st2_repo .copy-pack-to-subdirectory .packs-resource-register
52-
53-
.PHONY: packs-missing-tests
54-
packs-missing-tests: requirements .packs-missing-tests
55-
56-
.PHONY: packs-tests
57-
packs-tests: requirements .clone_st2_repo .packs-tests
58-
59-
.PHONY: compile
60-
compile:
61-
@echo "======================= compile ========================"
62-
@echo "------- Compile all .py files (syntax check test) ------"
63-
if python -c 'import compileall,re; compileall.compile_dir(".", rx=re.compile(r"/virtualenv|virtualenv-osx|virtualenv-py3|.tox|.git|.venv-st2devbox"), quiet=True)' | grep .; then exit 1; else exit 0; fi
64-
65-
.PHONY: .flake8
66-
.flake8:
67-
@echo
68-
@echo "==================== flake8 ===================="
69-
@echo
70-
. $(VIRTUALENV_DIR)/bin/activate; \
71-
if [ "$${FORCE_CHECK_ALL_FILES}" = "true" ]; then \
72-
find ./* -name "*.py" | while read py_file; do \
73-
flake8 --config=$(CI_DIR)/lint-configs/python/.flake8 $$py_file || exit 1; \
74-
done; \
75-
elif [ -n "${CHANGED_PY}" ]; then \
76-
for file in ${CHANGED_PY}; do \
77-
if [ -n "$$file" ]; then \
78-
flake8 --config=$(CI_DIR)/lint-configs/python/.flake8 $$file || exit 1; \
79-
fi; \
80-
done; \
81-
else \
82-
echo "No files have changed, skipping run..."; \
83-
fi;
84-
85-
.PHONY: .pylint
86-
.pylint:
87-
@echo
88-
@echo "==================== pylint ===================="
89-
@echo
90-
. $(VIRTUALENV_DIR)/bin/activate; \
91-
if [ "$${FORCE_CHECK_ALL_FILES}" = "true" ] || [ -n "${CHANGED_PY}" ]; then \
92-
REQUIREMENTS_DIR=$(CI_DIR)/.circle/ \
93-
CONFIG_DIR=$(CI_DIR)/lint-configs/ \
94-
st2-check-pylint-pack $(ROOT_DIR) || exit 1; \
95-
else \
96-
echo "No files have changed, skipping run..."; \
97-
fi;
98-
99-
.PHONY: .configs-check
100-
.configs-check:
101-
@echo
102-
@echo "==================== configs-check ===================="
103-
@echo
104-
@# The number of changed files in the AWS pack exceeds the limits of Bash,
105-
@# leading to CI failures like this:
106-
@# https://circleci.com/gh/StackStorm-Exchange/stackstorm-aws/320
107-
@# Instead of passing the entire list into a Bash for loop, we convert the
108-
@# make variable to a Bash string, convert that to a Bash array, and then
109-
@# iterate through each element of the array
110-
. $(VIRTUALENV_DIR)/bin/activate; \
111-
if [ "$${FORCE_CHECK_ALL_FILES}" = "true" ]; then \
112-
find $(CI_DIR)/* -name "*.yaml" -o -name "*.yml" | while read yaml_file; do \
113-
st2-check-validate-yaml-file "$$yaml_file" || exit 1 ; \
114-
done; \
115-
elif [ -n "${CHANGED_YAML}" ]; then \
116-
for file in $(CHANGED_YAML); do \
117-
if [ -n "$$file" ]; then \
118-
st2-check-validate-yaml-file $$file || exit 1 ; \
119-
fi; \
120-
done; \
121-
else \
122-
echo "No files have changed, skipping run..."; \
123-
fi
124-
@#
125-
. $(VIRTUALENV_DIR)/bin/activate; \
126-
if [ "$${FORCE_CHECK_ALL_FILES}" = "true" ]; then \
127-
find $(CI_DIR)/* -name "*.json" | while read json_file; do \
128-
st2-check-validate-json-file "$$json_file" || exit 1 ; \
129-
done; \
130-
elif [ -n "${CHANGED_JSON}" ]; then \
131-
for file in $(CHANGED_JSON); do \
132-
if [ -n "$$file" ]; then \
133-
echo "file: $$file"; \
134-
st2-check-validate-json-file $$file || exit 1 ; \
135-
fi; \
136-
done; \
137-
else \
138-
echo "No files have changed, skipping run..."; \
139-
fi
140-
@#
141-
@echo
142-
@echo "==================== example config check ===================="
143-
@echo
144-
. $(VIRTUALENV_DIR)/bin/activate; \
145-
if [ "$${FORCE_CHECK_ALL_FILES}" = "true" ] || [ -n "${CHANGED_FILES}" ]; then \
146-
st2-check-validate-pack-example-config /tmp/packs/$(PACK_NAME) || exit 1; \
147-
else \
148-
echo "No files have changed, skipping run..."; \
149-
fi;
150-
151-
.PHONY: .metadata-check
152-
.metadata-check:
153-
@echo
154-
@echo "==================== metadata-check ===================="
155-
@echo
156-
. $(VIRTUALENV_DIR)/bin/activate; \
157-
if [ "$${FORCE_CHECK_ALL_FILES}" = "true" ] || [ -n "${CHANGED_YAML}" ]; then \
158-
st2-check-validate-pack-metadata-exists $(ROOT_DIR) || exit 1; \
159-
else \
160-
echo "No files have changed, skipping run..."; \
161-
fi;
162-
163-
.PHONY: .packs-resource-register
164-
.packs-resource-register:
165-
@echo
166-
@echo "==================== packs-resource-register ===================="
167-
@echo
168-
. $(VIRTUALENV_DIR)/bin/activate; \
169-
if [ -z "${CHANGED_FILES}" ]; then \
170-
echo "No files have changed, skipping run..."; \
171-
else \
172-
st2-check-register-pack-resources /tmp/packs/$(PACK_NAME) || exit 1; \
173-
fi;
174-
175-
.PHONY: .packs-tests
176-
.packs-tests:
177-
@echo
178-
@echo "==================== packs-tests ===================="
179-
@echo
180-
. $(VIRTUALENV_DIR)/bin/activate; \
181-
if [ -z "${CHANGED_FILES}" ]; then \
182-
echo "No files have changed, skipping run..."; \
183-
else \
184-
$(ST2_REPO_PATH)/st2common/bin/st2-run-pack-tests -c -t -x -j -p $(ROOT_DIR) || exit 1; \
185-
fi;
186-
187-
.PHONY: .packs-missing-tests
188-
.packs-missing-tests:
189-
@echo
190-
@echo "==================== pack-missing-tests ===================="
191-
@echo
192-
if [ -z "${CHANGED_FILES}" ]; then \
193-
echo "No files have changed, skipping run..."; \
194-
else \
195-
st2-check-print-pack-tests-coverage $(ROOT_DIR) || exit 1; \
196-
fi;
197-
198-
.PHONY: .clone_st2_repo
199-
.clone_st2_repo: /tmp/st2
200-
/tmp/st2:
201-
@echo
202-
@echo "==================== cloning st2 repo ===================="
203-
@echo
204-
@rm -rf /tmp/st2
205-
@git clone https://github.com/StackStorm/st2.git --depth 1 --single-branch --branch $(ST2_REPO_BRANCH) /tmp/st2
206-
207-
.PHONY: .install-runners
208-
.install-runners:
209-
@echo ""
210-
@echo "================== install runners ===================="
211-
@echo ""
212-
@for component in $(COMPONENTS_RUNNERS); do \
213-
echo "==========================================================="; \
214-
echo "Installing runner:" $$component; \
215-
echo "==========================================================="; \
216-
(. $(VIRTUALENV_DIR)/bin/activate; cd $$component; python setup.py develop); \
217-
done
218-
@echo ""
219-
@echo "================== register metrics drivers ======================"
220-
@echo ""
221-
222-
# Install st2common to register metrics drivers
223-
(. $(VIRTUALENV_DIR)/bin/activate; cd $(ST2_REPO_PATH)/st2common; python setup.py develop)
224-
225-
.PHONY: requirements
226-
requirements: virtualenv .clone_st2_repo .install-runners
227-
@echo
228-
@echo "==================== requirements ===================="
229-
@echo
230-
. $(VIRTUALENV_DIR)/bin/activate && $(VIRTUALENV_DIR)/bin/pip install --upgrade "pip>=9.0,<9.1"
231-
. $(VIRTUALENV_DIR)/bin/activate && $(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(CI_DIR)/.circle/requirements-dev.txt
232-
. $(VIRTUALENV_DIR)/bin/activate && $(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(CI_DIR)/.circle/requirements-pack-tests.txt
233-
234-
.PHONY: requirements-ci
235-
requirements-ci:
236-
@echo
237-
@echo "==================== requirements-ci ===================="
238-
@echo
239-
. $(VIRTUALENV_DIR)/bin/activate && $(VIRTUALENV_DIR)/bin/pip install --upgrade "pip>=9.0,<9.1"
240-
. $(VIRTUALENV_DIR)/bin/activate && $(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(CI_DIR)/.circle/requirements-dev.txt
241-
. $(VIRTUALENV_DIR)/bin/activate && $(VIRTUALENV_DIR)/bin/pip install --cache-dir $(HOME)/.pip-cache -q -r $(CI_DIR)/.circle/requirements-pack-tests.txt
242-
243-
.PHONY: virtualenv
2448
virtualenv: $(VIRTUALENV_DIR)/bin/activate
2459
$(VIRTUALENV_DIR)/bin/activate:
24610
@echo
24711
@echo "==================== virtualenv ===================="
24812
@echo
24913
test -d $(VIRTUALENV_DIR) || virtualenv --no-site-packages $(VIRTUALENV_DIR)
14+
15+
$(VIRTUALENV_DIR)/bin/invoke: $(VIRTUALENV_DIR)
16+
. $(VIRTUALENV_DIR)/bin/activate && pip install invoke
17+
18+
.PHONY: invoke
19+
invoke: virtualenv $(VIRTUALENV_DIR)/bin/invoke
20+
$(VIRTUALENV_DIR)/bin/pip install invoke
21+
22+
# https://stackoverflow.com/a/33018558
23+
# Workaround to support all previous make targets
24+
# This default target simply passes all targets on to invoke
25+
# We can't add invoke as a make dependency for the .DEFAULT target since the
26+
# dependency will get overridden by whatever target is passed in
27+
.DEFAULT:
28+
@# Manually make virtualenv target
29+
if [ ! -d $(VIRTUALENV_DIR) ]; then make virtualenv; fi
30+
@# Manually make invoke target
31+
if [ ! -e $(VIRTUALENV_DIR)/bin/invoke ]; then $(VIRTUALENV_DIR)/bin/pip install invoke; fi
32+
. $(VIRTUALENV_DIR)/bin/activate && invoke --search-root=$(CI_DIR) $@
33+
@#. $(VIRTUALENV_DIR)/bin/activate && echo $$PYTHONPATH

.circle/dependencies

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ if [ ! -z "${ROOT_DIR}" ]; then
4949

5050
echo "Copying Makefile to ${ROOT_DIR}"
5151
cp ~/ci/.circle/Makefile ${ROOT_DIR}
52-
make -C requirements-ci .install-runners
52+
make -C requirements install-runners
5353
else
5454
PACK_REQUIREMENTS_FILE="$(pwd)/requirements.txt"
5555
PACK_TESTS_REQUIREMENTS_FILE="$(pwd)/requirements-tests.txt"
5656

5757
echo "Copying Makefile to $(pwd)"
5858
cp ~/ci/.circle/Makefile .
59-
make requirements-ci .install-runners
59+
make requirements install-runners
6060
fi
6161

6262
# Install pack requirements

tasks/__init__.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from invoke import Collection, task, run
2+
3+
from . import check
4+
from . import copy
5+
from . import git_tasks
6+
from . import lint
7+
from . import requirements
8+
from . import tests
9+
10+
11+
# All tasks are implemented in submodules of this package
12+
# All tasks in this module are only for reverse compatibility with the original
13+
# Makefile
14+
15+
# This task aliases a Python built-in
16+
@task(requirements.install, lint.lint, tests.packs_resource_register, tests.packs_tests)
17+
def all_(ctx):
18+
pass
19+
20+
21+
@task(check.compile_, lint.flake8, lint.pylint, copy.copy_pack_to_subdirectory,
22+
check.configs, check.metadata, tests.packs_resource_register, tests.packs_tests)
23+
def all_ci(ctx):
24+
pass
25+
26+
27+
@task(lint.lint)
28+
def lint_(ctx):
29+
pass
30+
31+
32+
@task(lint.flake8)
33+
def flake8(ctx):
34+
pass
35+
36+
37+
@task(lint.pylint)
38+
def pylint(ctx):
39+
pass
40+
41+
42+
@task(check.configs)
43+
def configs_check(ctx):
44+
pass
45+
46+
47+
@task(check.metadata)
48+
def metadata_check(ctx):
49+
pass
50+
51+
52+
@task(tests.packs_resource_register)
53+
def packs_resource_register(ctx):
54+
pass
55+
56+
57+
@task(tests.packs_missing_tests)
58+
def packs_missing_tests(ctx):
59+
pass
60+
61+
62+
@task(tests.packs_tests)
63+
def packs_tests(ctx):
64+
pass
65+
66+
67+
@task(check.compile_)
68+
def compile_(ctx):
69+
pass
70+
71+
72+
@task(requirements.runners)
73+
def install_runners(ctx):
74+
pass
75+
76+
77+
@task(requirements.install)
78+
def requirements(ctx):
79+
pass
80+
81+
82+
namespace = Collection()
83+
84+
namespace.add_task(all_, name='all')
85+
namespace.add_task(all_ci, name='all-ci')
86+
namespace.add_task(lint_, name='lint')
87+
namespace.add_task(flake8, name='flake8')
88+
namespace.add_task(pylint, name='pylint')
89+
namespace.add_task(configs_check, name='configs-check')
90+
namespace.add_task(metadata_check, name='metadata-check')
91+
namespace.add_task(packs_resource_register, name='packs-resource-register')
92+
namespace.add_task(packs_missing_tests, name='packs-missing-tests')
93+
namespace.add_task(packs_tests, name='packs-tests')
94+
namespace.add_task(compile_, name='compile')
95+
namespace.add_task(install_runners, name='install-runners')
96+
namespace.add_task(requirements, name='requirements')

0 commit comments

Comments
 (0)