-
Notifications
You must be signed in to change notification settings - Fork 75
/
Makefile
344 lines (279 loc) · 9.88 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
COSMOS_SDK_URL := https://github.com/fetchai/cosmos-sdk
COSMOS_SDK_VERSION := v0.18.0
COSMOS_SDK_DIR := build/cosmos-sdk-proto-schema
WASMD_URL := https://github.com/CosmWasm/wasmd
WASMD_VERSION := v0.27.0
WASMD_DIR := build/wasm-proto-shema
IBCGO_URL := https://github.com/cosmos/ibc-go
IBCGO_VERSION := v2.2.0
IBCGO_DIR := build/ibcgo-proto-schema
COSMPY_PROTOS_DIR := cosmpy/protos
COSMPY_SRC_DIR := cosmpy
COSMPY_TESTS_DIR := tests
COSMPY_EXAMPLES_DIR := examples
COSMPY_SCRIPTS_DIR := scripts
PYTHON_CODE_DIRS := $(COSMPY_SRC_DIR) $(COSMPY_TESTS_DIR) $(COSMPY_EXAMPLES_DIR) $(COSMPY_SCRIPTS_DIR)
########################################
### Initialise dev environment
########################################
# Create a new poetry virtual environment with all the necessary dependencies installed.
# Once finished, `poetry shell` to enter the virtual environment
v := $(shell pip -V | grep virtualenvs)
.PHONY: new-env
new-env: clean
if [ -z "$v" ];\
then\
poetry install --with main,dev,test,docs;\
echo "Enter virtual environment with all development dependencies now: 'poetry shell'.";\
else\
echo "In a virtual environment! Exit first: 'exit'.";\
fi
########################################
### Tests
########################################
# Run all tests
.PHONY: test
test:
coverage run -m pytest $(COSMPY_TESTS_DIR) --doctest-modules
$(MAKE) coverage-report
# Run all unit tests
.PHONY: unit-test
unit-test:
coverage run -m pytest $(COSMPY_TESTS_DIR) --doctest-modules -m "not integration"
# Run all integration tests
.PHONY: integration-test
integration-test:
coverage run -m pytest $(COSMPY_TESTS_DIR) --doctest-modules -m "integration"
# Run all third party tests
.PHONY: third-party-test
third-party-test:
coverage run -m pytest $(COSMPY_TESTS_DIR) --doctest-modules -m "thirdparty"
# Produce the coverage report. Can see a report summary on the terminal.
# Detailed report on all modules are placed under /coverage-report
.PHONY: coverage-report
coverage-report:
coverage report -m
coverage html
########################################
### Code Styling
########################################
# Automatically run black and isort to format the code, and run flake8 and vulture checks
.PHONY: lint
lint: black isort flake8 vulture
# Automatically format the code using black
.PHONY: black
black:
black $(PYTHON_CODE_DIRS) --exclude $(COSMPY_PROTOS_DIR)
# Automatically sort the imports
.PHONY: isort
isort:
isort $(PYTHON_CODE_DIRS)
# Check the code format
.PHONY: black-check
black-check:
black --check --verbose $(PYTHON_CODE_DIRS) --exclude $(COSMPY_PROTOS_DIR)
# Check the imports are sorted
.PHONY: isort-check
isort-check:
isort --check-only --verbose $(PYTHON_CODE_DIRS)
# Run flake8 linter
.PHONY: flake8
flake8:
flake8 $(PYTHON_CODE_DIRS)
# Check for unused code
.PHONY: vulture
vulture:
vulture $(PYTHON_CODE_DIRS) scripts/whitelist.py --exclude '*_pb2.py,*_pb2_grpc.py' --min-confidence 100
########################################
### Security & safety checks
########################################
# Run bandit and safety
.PHONY: security
security: bandit safety
# Check the security of the code
.PHONY: bandit
bandit:
bandit -r $(COSMPY_SRC_DIR) $(COSMPY_TESTS_DIR) -s B101
bandit -r $(COSMPY_EXAMPLES_DIR) -s B101,B105
# Check the security of the code for known vulnerabilities
.PHONY: safety
safety:
safety check -i 41002
########################################
### Linters
########################################
# Check types (statically) using mypy
.PHONY: mypy
mypy:
mypy $(PYTHON_CODE_DIRS) --exclude $(COSMPY_PROTOS_DIR)
# Lint the code using pylint
.PHONY: pylint
pylint:
pylint -j 0 $(PYTHON_CODE_DIRS)
########################################
### License and copyright checks
########################################
# Check dependency licenses
.PHONY: liccheck
liccheck:
poetry export > tmp-requirements.txt
liccheck -s strategy.ini -r tmp-requirements.txt -l PARANOID
rm -frv tmp-requirements.txt
# Check that the relevant files have appropriate Copyright header
.PHONY: copyright-check
copyright-check:
python scripts/check_copyright.py --directory .
########################################
### Docs
########################################
# Build documentation
.PHONY: docs
docs:
mkdocs build --clean
# Live documentation server
.PHONY: docs-live
docs-live:
mkdocs serve
# Generate API documentation (ensure you add the new pages created into /mkdocs.yml --> nav)
.PHONY: generate-api-docs
generate-api-docs:
python scripts/generate_api_docs.py
########################################
### Poetry Lock
########################################
# Updates the poetry lock
poetry.lock: pyproject.toml
poetry lock
########################################
### Clear the caches and temporary files
########################################
# clean the caches and temporary files and directories
.PHONY: clean
clean: clean-build clean-pyc clean-test clean-docs
.PHONY: clean-build
clean-build:
rm -fr build/
rm -fr dist/
rm -fr .eggs/
rm -fr pip-wheel-metadata
find . -name '*.egg-info' -exec rm -fr {} +
find . -name '*.egg' -exec rm -fr {} +
.PHONY: clean-docs
clean-docs:
rm -fr site/
.PHONY: clean-pyc
clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +
find . -name '.DS_Store' -exec rm -fr {} +
.PHONY: clean-test
clean-test:
rm -fr .tox/
rm -f .coverage
find . -name ".coverage*" -not -name ".coveragerc" -exec rm -fr "{}" \;
rm -fr coverage.xml
rm -fr htmlcov/
rm -fr .hypothesis
rm -fr .pytest_cache
rm -fr .mypy_cache/
find . -name 'log.txt' -exec rm -fr {} +
find . -name 'log.*.txt' -exec rm -fr {} +
########################################
### Build
########################################
# Build the project
.PHONY: dist
dist: clean
poetry build
########################################
### Generate protos and grpc files
########################################
ifeq ($(OS),Windows_NT)
$(error "Please use the WSL (Windows Subsystem for Linux) on Windows platform.")
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
OPEN_CMD := xdg-open
endif
ifeq ($(UNAME_S),Darwin)
OPEN_CMD := open
endif
endif
define unique
$(eval seen :=)
$(foreach _,$1,$(if $(filter $_,${seen}),,$(eval seen += $_)))
${seen}
endef
unique = $(if $1,$(firstword $1) $(call unique,$(filter-out $(firstword $1),$1)))
proto: fetch_proto_schema_source generate_proto_types generate_init_py_files
generate_proto_types: $(COSMOS_SDK_DIR) $(WASMD_DIR) $(IBCGO_DIR)
rm -frv $(COSMPY_PROTOS_DIR)/*
python -m grpc_tools.protoc --proto_path=$(WASMD_DIR)/proto --proto_path=$(WASMD_DIR)/third_party/proto --python_out=$(COSMPY_PROTOS_DIR) --grpc_python_out=$(COSMPY_PROTOS_DIR) $(shell find $(WASMD_DIR) \( -path */proto/* -or -path */third_party/proto/* \) -type f -name *.proto)
python -m grpc_tools.protoc --proto_path=$(IBCGO_DIR)/proto --proto_path=$(IBCGO_DIR)/third_party/proto --python_out=$(COSMPY_PROTOS_DIR) --grpc_python_out=$(COSMPY_PROTOS_DIR) $(shell find $(IBCGO_DIR) \( -path */proto/* -or -path */third_party/proto/* \) -type f -name *.proto)
# ensure cosmos-sdk is last as previous modules may have duplicated proto models which are now outdated
python -m grpc_tools.protoc --proto_path=$(COSMOS_SDK_DIR)/proto --proto_path=$(COSMOS_SDK_DIR)/third_party/proto --python_out=$(COSMPY_PROTOS_DIR) --grpc_python_out=$(COSMPY_PROTOS_DIR) $(shell find $(COSMOS_SDK_DIR) \( -path */proto/* -or -path */third_party/proto/* \) -type f -name *.proto)
fetch_proto_schema_source: $(COSMOS_SDK_DIR) $(WASMD_DIR) $(IBCGO_DIR)
.PHONY: generate_init_py_files
generate_init_py_files: generate_proto_types
find $(COSMPY_PROTOS_DIR)/ -type d -exec touch {}/__init__.py \;
# restore root __init__.py as it contains code to have the proto files module available
git restore $(COSMPY_PROTOS_DIR)/__init__.py
$(SOURCE): $(COSMOS_SDK_DIR)
$(GENERATED): $(SOURCE)
$(COMPILE_PROTOBUFS_COMMAND)
$(INIT_PY_FILES_TO_CREATE): $(GENERATED_DIRS)
touch $(INIT_PY_FILES_TO_CREATE)
$(GENERATED_DIRS): $(COSMOS_SDK_DIR) $(WASMD_DIR) $(IBCGO_DIR)
$(COSMOS_SDK_DIR): Makefile
rm -rfv $(COSMOS_SDK_DIR)
git clone --branch $(COSMOS_SDK_VERSION) --depth 1 --quiet --no-checkout --filter=blob:none $(COSMOS_SDK_URL) $(COSMOS_SDK_DIR)
cd $(COSMOS_SDK_DIR) && git checkout $(COSMOS_SDK_VERSION) -- $(COSMOS_PROTO_RELATIVE_DIRS)
$(WASMD_DIR): Makefile
rm -rfv $(WASMD_DIR)
git clone --branch $(WASMD_VERSION) --depth 1 --quiet --no-checkout --filter=blob:none $(WASMD_URL) $(WASMD_DIR)
cd $(WASMD_DIR) && git checkout $(WASMD_VERSION) -- $(WASMD_PROTO_RELATIVE_DIRS)
$(IBCGO_DIR): Makefile
rm -rfv $(IBCGO_DIR)
git clone --branch $(IBCGO_VERSION) --depth 1 --quiet --no-checkout --filter=blob:none $(IBCGO_URL) $(IBCGO_DIR)
cd $(IBCGO_DIR) && git checkout $(IBCGO_VERSION) -- $(IBCGO_PROTO_RELATIVE_DIRS)
debug:
$(info SOURCES_REGEX_TO_EXCLUDE: $(SOURCES_REGEX_TO_EXCLUDE))
$(info )
$(info GENERATED_DIRS: $(GENERATED_DIRS))
$(info )
$(info INIT_PY_FILES_TO_CREATE: $(INIT_PY_FILES_TO_CREATE))
$(info )
$(info SOURCE: $(SOURCE))
$(info )
$(info RELATIVE_SOURCE: $(RELATIVE_SOURCE))
$(info )
$(info GENERATED: $(GENERATED))
$(info )
$(info UNROOTED_SOURCE: $(UNROOTED_SOURCE))
$(info )
$(info PROTO_ROOT_DIRS: $(PROTO_ROOT_DIRS))
$(info )
$(info FIND_CMD: $(FIND_CMD))
$(info )
$(info COMPILE_PROTOBUFS_COMMAND: $(COMPILE_PROTOBUFS_COMMAND))
########################################
### Commands used by CI workflows
########################################
# Live documentation server
.PHONY: docs-live-ci
docs-live-ci:
mkdocs serve -a localhost:8080
# Produce the coverage report
.PHONY: coverage-report-ci
coverage-report-ci:
coverage report -m -i
# Check MANIFEST.in file for completeness
.PHONY: check-manifest-ci
check-manifest-ci:
check-manifest
# Check API documentation is up-to-date
.PHONY: check-api-docs-ci
check-api-docs-ci:
python scripts/generate_api_docs.py --check-clean