forked from datopian/ckanext-blob-storage
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
284 lines (240 loc) · 10.1 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Makefile for ckanext-blob-storage
PACKAGE_DIR := ckanext/blob_storage
PACKAGE_NAME := ckanext.blob_storage
SHELL := bash
PYTHON := python
PIP := pip
PIP_COMPILE := pip-compile
PYTEST := pytest
PASTER := paster
DOCKER_COMPOSE := docker-compose
PSQL := psql
GIT := git
# Find GNU sed in path (on OS X gsed should be preferred)
SED := $(shell which gsed sed | head -n1)
# The `ckan` command line only exists in newer versions of CKAN
CKAN_CLI := $(shell which ckan | head -n1)
TEST_INI_PATH := ./test.ini
TEST_PATH :=
SENTINELS := .make-status
PYTHON_VERSION := $(shell $(PYTHON) -c 'import sys; print(sys.version_info[0])')
PACKAGE_TAG_PREFIX := "v"
PACKAGE_TAG_SUFFIX := ""
PACKAGE_VERSION := $(shell $(PYTHON) -c 'import $(PACKAGE_NAME) as p; print(p.__version__)')
# CKAN environment variables
CKAN_PATH := ckan
CKAN_REPO_URL := https://github.com/ckan/ckan.git
CKAN_VERSION := ckan-2.8.3
CKAN_CONFIG_FILE := $(CKAN_PATH)/development.ini
CKAN_SITE_URL := http://localhost:5000
POSTGRES_USER := ckan
POSTGRES_PASSWORD := ckan
POSTGRES_DB := ckan
POSTGRES_HOST := 127.0.0.1
CKAN_SOLR_PASSWORD := ckan
DATASTORE_DB_NAME := datastore
DATASTORE_DB_RO_USER := datastore_ro
DATASTORE_DB_RO_PASSWORD := datastore_ro
CKAN_LOAD_PLUGINS := stats text_view image_view recline_view datastore authz_service blob_storage
CKAN_CONFIG_VALUES := \
ckan.site_url=$(CKAN_SITE_URL) \
sqlalchemy.url=postgresql://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST)/$(POSTGRES_DB) \
ckan.datastore.write_url=postgresql://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST)/$(DATASTORE_DB_NAME) \
ckan.datastore.read_url=postgresql://$(DATASTORE_DB_RO_USER):$(DATASTORE_DB_RO_PASSWORD)@$(POSTGRES_HOST)/$(DATASTORE_DB_NAME) \
ckan.plugins='$(CKAN_LOAD_PLUGINS)' \
ckan.storage_path='%(here)s/storage' \
solr_url=http://127.0.0.1:8983/solr/ckan \
ckanext.blob_storage.storage_service_url=http://localhost:9419 \
ckanext.blob_storage.storage_namespace=my-ckan-ns \
ckanext.authz_service.jwt_algorithm=HS256 \
ckanext.authz_service.jwt_private_key=this-is-a-test-only-key \
ckanext.authz_service.jwt_include_user_email=true
CKAN_TEST_CONFIG_VALUES := \
sqlalchemy.url=postgresql://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST)/$(POSTGRES_DB)_test \
ckan.datastore.write_url=postgresql://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST)/$(DATASTORE_DB_NAME)_test \
ckan.datastore.read_url=postgresql://$(DATASTORE_DB_RO_USER):$(DATASTORE_DB_RO_PASSWORD)@$(POSTGRES_HOST)/$(DATASTORE_DB_NAME)_test
ifdef WITH_COVERAGE
COVERAGE_ARG := --cov=$(PACKAGE_NAME)
else
COVERAGE_ARG :=
endif
dev-requirements.%.txt: dev-requirements.in
$(PIP_COMPILE) --no-index dev-requirements.in -o $@
requirements.%.txt: requirements.in
$(PIP_COMPILE) --no-index requirements.in -o $@
## Update requirements files for the current Python version
requirements: $(SENTINELS)/requirements
.PHONEY: requirements
## Install this extension to the current Python environment
install: $(SENTINELS)/install
.PHONY: install
## Set up the extension for development in the current Python environment
develop: $(SENTINELS)/develop
.PHONEY: develop
## Run all tests
test: $(SENTINELS)/tests-passed
.PHONY: test
## Install the right version of CKAN into the virtual environment
ckan-install: $(SENTINELS)/ckan-installed
@echo "Current CKAN version: $(shell cat $(SENTINELS)/ckan-version)"
.PHONY: ckan-install
## Run CKAN in the local virtual environment
ckan-start: $(SENTINELS)/ckan-installed $(SENTINELS)/install-dev $(CKAN_CONFIG_FILE) | _check_virtualenv
ifdef CKAN_CLI
$(CKAN_CLI) -c $(CKAN_CONFIG_FILE) db init
$(CKAN_CLI) -c $(CKAN_CONFIG_FILE) server -r
else
$(PASTER) --plugin=ckan db init -c $(CKAN_CONFIG_FILE)
$(PASTER) --plugin=ckan serve --reload --monitor-restart $(CKAN_CONFIG_FILE)
endif
.PHONY: ckan-start
## Create a version tag
version-tag:
@echo "Creating tag: $(PACKAGE_TAG_PREFIX)$(PACKAGE_VERSION)$(PACKAGE_TAG_SUFFIX)"
$(GIT) tag "$(PACKAGE_TAG_PREFIX)$(PACKAGE_VERSION)$(PACKAGE_TAG_SUFFIX)"
$(GIT) push --tags
.PHONY: version-tag
$(CKAN_PATH):
$(GIT) clone $(CKAN_REPO_URL) $@
$(CKAN_CONFIG_FILE): $(SENTINELS)/ckan-installed $(SENTINELS)/develop | _check_virtualenv
ifdef CKAN_CLI
$(CKAN_CLI) generate config $(CKAN_CONFIG_FILE)
$(CKAN_CLI) config-tool $(CKAN_CONFIG_FILE) -s DEFAULT debug=true
$(CKAN_CLI) config-tool $(CKAN_CONFIG_FILE) $(CKAN_CONFIG_VALUES)
else
$(PASTER) make-config --no-interactive ckan $(CKAN_CONFIG_FILE)
$(PASTER) --plugin=ckan config-tool $(CKAN_CONFIG_FILE) -s DEFAULT debug=true
$(PASTER) --plugin=ckan config-tool $(CKAN_CONFIG_FILE) $(CKAN_CONFIG_VALUES)
endif
.env:
@___POSTGRES_USER=$(POSTGRES_USER) \
___POSTGRES_PASSWORD=$(POSTGRES_PASSWORD) \
___POSTGRES_DB=$(POSTGRES_DB) \
___CKAN_SOLR_PASSWORD=$(CKAN_SOLR_PASSWORD) \
___DATASTORE_DB_NAME=$(DATASTORE_DB_NAME) \
___DATASTORE_DB_USER=$(POSTGRES_USER) \
___DATASTORE_DB_RO_USER=$(DATASTORE_DB_RO_USER) \
___DATASTORE_DB_RO_PASSWORD=$(DATASTORE_DB_RO_PASSWORD) \
env | grep ^___ | $(SED) 's/^___//' > .env
@cat .env
## Create the database for test running
create-test-db:
@echo " \
CREATE ROLE $(DATASTORE_DB_RO_USER) NOSUPERUSER NOCREATEDB NOCREATEROLE LOGIN PASSWORD '$(DATASTORE_DB_RO_PASSWORD)'; \
CREATE DATABASE $(DATASTORE_DB_NAME)_test OWNER $(POSTGRES_USER) ENCODING 'utf-8'; \
CREATE DATABASE $(POSTGRES_DB)_test OWNER $(POSTGRES_USER) ENCODING 'utf-8'; \
GRANT ALL PRIVILEGES ON DATABASE $(DATASTORE_DB_NAME)_test TO $(POSTGRES_USER); \
GRANT ALL PRIVILEGES ON DATABASE $(POSTGRES_DB)_test TO $(POSTGRES_USER); \
" | PGPASSWORD=$(POSTGRES_PASSWORD) $(PSQL) -h $(POSTGRES_HOST) --username "$(POSTGRES_USER)"
.PHONY: create-test-db
## Start all Docker services
docker-up: .env
$(DOCKER_COMPOSE) up -d
@until $(DOCKER_COMPOSE) exec db pg_isready -U $(POSTGRES_USER); do sleep 1; done
@sleep 2
@echo " \
CREATE ROLE $(DATASTORE_DB_RO_USER) NOSUPERUSER NOCREATEDB NOCREATEROLE LOGIN PASSWORD '$(DATASTORE_DB_RO_PASSWORD)'; \
CREATE DATABASE $(DATASTORE_DB_NAME) OWNER $(POSTGRES_USER) ENCODING 'utf-8'; \
CREATE DATABASE $(DATASTORE_DB_NAME)_test OWNER $(POSTGRES_USER) ENCODING 'utf-8'; \
CREATE DATABASE $(POSTGRES_DB)_test OWNER $(POSTGRES_USER) ENCODING 'utf-8'; \
GRANT ALL PRIVILEGES ON DATABASE $(DATASTORE_DB_NAME) TO $(POSTGRES_USER); \
GRANT ALL PRIVILEGES ON DATABASE $(DATASTORE_DB_NAME)_test TO $(POSTGRES_USER); \
GRANT ALL PRIVILEGES ON DATABASE $(POSTGRES_DB)_test TO $(POSTGRES_USER); \
" | $(DOCKER_COMPOSE) exec -T db psql --username "$(POSTGRES_USER)"
.PHONY: docker-up
## Stop all Docker services
docker-down: .env
$(DOCKER_COMPOSE) down
.PHONY: docker-down
## Initialize the development environment
dev-setup: _check_virtualenv $(SENTINELS)/ckan-installed $(CKAN_PATH)/who.ini $(CKAN_CONFIG_FILE) $(SENTINELS)/develop
.PHONY: setup
## Start a full development environment
dev-start: dev-setup docker-up ckan-start
.PHONY: start-dev
# Private targets
_check_virtualenv:
@if [ -z "$(VIRTUAL_ENV)" ]; then \
echo "You are not in a virtual environment - activate your virtual environment first"; \
exit 1; \
fi
.PHONY: _check_virtualenv
$(SENTINELS):
mkdir -p $@
$(SENTINELS)/ckan-version: $(CKAN_PATH) | _check_virtualenv $(SENTINELS)
$(GIT) -C $(CKAN_PATH) remote update
$(GIT) -C $(CKAN_PATH) checkout $(CKAN_VERSION)
if [ -e $(CKAN_PATH)/requirement-setuptools.txt ]; then $(PIP) install -r $(CKAN_PATH)/requirement-setuptools.txt; fi
if [[ "$(PYTHON_VERSION)" == "2" && -e $(CKAN_PATH)/requirements-py2.txt ]]; then \
$(PIP) install -r $(CKAN_PATH)/requirements-py2.txt; \
else \
$(PIP) install -r $(CKAN_PATH)/requirements.txt; \
fi
$(PIP) install -r $(CKAN_PATH)/dev-requirements.txt
$(PIP) install -e $(CKAN_PATH)
echo "$(CKAN_VERSION)" > $@
$(SENTINELS)/ckan-installed: $(SENTINELS)/ckan-version | $(SENTINELS)
@if [ "$(shell cat $(SENTINELS)/ckan-version)" != "$(CKAN_VERSION)" ]; then \
echo "Switching to CKAN $(CKAN_VERSION)"; \
rm $(SENTINELS)/ckan-version; \
$(MAKE) $(SENTINELS)/ckan-version; \
fi
@touch $@
$(SENTINELS)/test.ini: $(TEST_INI_PATH) $(CKAN_PATH) $(CKAN_PATH)/test-core.ini | $(SENTINELS)
$(SED) "s@use = config:.*@use = config:$(CKAN_PATH)/test-core.ini@" -i $(TEST_INI_PATH)
ifdef CKAN_CLI
$(CKAN_CLI) config-tool $(CKAN_PATH)/test-core.ini $(CKAN_CONFIG_VALUES) $(CKAN_TEST_CONFIG_VALUES)
else
$(PASTER) --plugin=ckan config-tool $(CKAN_PATH)/test-core.ini $(CKAN_CONFIG_VALUES) $(CKAN_TEST_CONFIG_VALUES)
endif
@touch $@
$(SENTINELS)/requirements: requirements.py$(PYTHON_VERSION).txt dev-requirements.py$(PYTHON_VERSION).txt | $(SENTINELS)
@touch $@
$(SENTINELS)/install: requirements.py$(PYTHON_VERSION).txt | $(SENTINELS)
$(PIP) install -r requirements.py$(PYTHON_VERSION).txt
@touch $@
$(SENTINELS)/install-dev: requirements.py$(PYTHON_VERSION).txt | $(SENTINELS)
$(PIP) install -r dev-requirements.py$(PYTHON_VERSION).txt
$(PIP) install -e .
@touch $@
$(SENTINELS)/develop: $(SENTINELS)/requirements $(SENTINELS)/install $(SENTINELS)/install-dev setup.py | $(SENTINELS)
@touch $@
$(SENTINELS)/test-setup: $(SENTINELS)/develop $(SENTINELS)/test.ini
ifdef CKAN_CLI
$(CKAN_CLI) -c $(TEST_INI_PATH) db init
else
$(PASTER) --plugin=ckan db init -c $(TEST_INI_PATH)
endif
@touch $@
$(SENTINELS)/tests-passed: $(SENTINELS)/test-setup $(shell find $(PACKAGE_DIR) -type f) .flake8 .isort.cfg | $(SENTINELS)
$(PYTEST) $(COVERAGE_ARG) \
--flake8 \
--isort \
--ckan-ini=$(TEST_INI_PATH) \
--doctest-modules \
--ignore $(PACKAGE_DIR)/cli.py \
-s \
$(PACKAGE_DIR)/$(TEST_PATH)
@touch $@
# Help related variables and targets
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
TARGET_MAX_CHAR_NUM := 15
## Show help
help:
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)