forked from zhmcclient/zhmc-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
318 lines (283 loc) · 11.9 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
# Makefile for zhmc-prometheus-exporter project
#
# Use this to get information on the targets:
# make - or - make help
#
# It is recommended to run this Makefile in a virtual Python environment,
# because Python packages will be installed automatically.
#
# Supported OS platforms:
# Windows (native)
# Linux (any)
# macOS/OS-X
#
# OS-level commands used by this Makefile (to be provided manually):
# On native Windows:
# cmd (providing: del, copy, rmdir, set)
# where
# On Linux and macOS:
# rm, find, cp, env, sort, which, uname
#
# Environment variables:
# PYTHON_CMD: Python command to use (OS-X needs to distinguish Python 2/3)
# PIP_CMD: Pip command to use (OS-X needs to distinguish Python 2/3)
# PACKAGE_LEVEL: minimum/latest - Level of Python dependent packages to use
# No built-in rules needed:
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:
# Python / Pip commands
ifndef PYTHON_CMD
PYTHON_CMD := python
endif
ifndef PIP_CMD
PIP_CMD := pip
endif
# Package level
ifndef PACKAGE_LEVEL
PACKAGE_LEVEL := latest
endif
# Determine OS platform make runs on.
ifeq ($(OS),Windows_NT)
ifdef PWD
PLATFORM := Windows_UNIX
else
PLATFORM := Windows_native
ifndef COMSPEC
# Make variables are case sensitive and some native Windows environments have
# ComSpec set instead of COMSPEC.
ifdef ComSpec
COMSPEC = $(ComSpec)
endif
endif
ifdef COMSPEC
SHELL := $(subst \,/,$(COMSPEC))
else
SHELL := cmd.exe
endif
.SHELLFLAGS := /c
endif
else
# Values: Linux, Darwin
PLATFORM := $(shell uname -s)
endif
ifeq ($(PLATFORM),Windows_native)
# Note: The substituted backslashes must be doubled.
# Remove files (blank-separated list of wildcard path specs)
RM_FUNC = del /f /q $(subst /,\\,$(1))
# Remove files recursively (single wildcard path spec)
RM_R_FUNC = del /f /q /s $(subst /,\\,$(1))
# Remove directories (blank-separated list of wildcard path specs)
RMDIR_FUNC = rmdir /q /s $(subst /,\\,$(1))
# Remove directories recursively (single wildcard path spec)
RMDIR_R_FUNC = rmdir /q /s $(subst /,\\,$(1))
# Copy a file, preserving the modified date
CP_FUNC = copy /y $(subst /,\\,$(1)) $(subst /,\\,$(2))
ENV = set
WHICH = where
else
RM_FUNC = rm -f $(1)
RM_R_FUNC = find . -type f -name '$(1)' -delete
RMDIR_FUNC = rm -rf $(1)
RMDIR_R_FUNC = find . -type d -name '$(1)' | xargs -n 1 rm -rf
CP_FUNC = cp -r $(1) $(2)
ENV = env | sort
WHICH = which
endif
package_name := zhmc_prometheus_exporter
package_version := $(shell $(PYTHON_CMD) setup.py --version)
python_version := $(shell $(PYTHON_CMD) -c "import sys; sys.stdout.write('{}.{}'.format(sys.version_info[0], sys.version_info[1]))")
pymn := $(shell $(PYTHON_CMD) -c "import sys; sys.stdout.write('py{}{}'.format(sys.version_info[0], sys.version_info[1]))")
package_dir := $(package_name)
package_py_files := \
$(wildcard $(package_dir)/*.py) \
$(wildcard $(package_dir)/*/*.py) \
test_dir := tests
test_py_files := \
$(wildcard $(test_dir)/*.py) \
$(wildcard $(test_dir)/*/*.py) \
dist_dir := dist
bdist_file := $(dist_dir)/$(package_name)-$(package_version)-py2.py3-none-any.whl
sdist_file := $(dist_dir)/$(package_name)-$(package_version).tar.gz
doc_dir := docs
doc_build_dir := build_docs
doc_build_file := $(doc_build_dir)/index.html
doc_dependent_files := \
$(wildcard $(doc_dir)/*.*) \
$(wildcard $(doc_dir)/*/*.*) \
examples/metrics.yaml \
examples/hmccreds.yaml \
$(package_py_files) \
pytest_cov_opts := --cov $(package_name) --cov-config .coveragerc --cov-report=html:htmlcov
ifeq ($(PACKAGE_LEVEL),minimum)
pip_level_opts := -c minimum-constraints.txt
else
ifeq ($(PACKAGE_LEVEL),latest)
pip_level_opts := --upgrade --upgrade-strategy eager
else
$(error Invalid value for PACKAGE_LEVEL variable: $(PACKAGE_LEVEL))
endif
endif
.PHONY: help
help:
@echo "Makefile for project $(package_name)"
@echo "Package version: $(package_version)"
@echo "Python version: $(python_version)"
@echo "Targets:"
@echo " install - Install package and its prerequisites"
@echo " develop - Install prerequisites for development"
@echo " check - Perform flake8 checks"
@echo " pylint - Perform pylint checks"
@echo " test - Perform unit tests including coverage checker"
@echo " build - Build the distribution files in $(dist_dir)"
@echo " builddoc - Build the documentation in $(doc_build_dir)"
@echo " check_reqs - Perform missing dependency checks"
@echo " all - Do all of the above"
@echo " upload - Upload the package to Pypi"
@echo " clean - Remove any temporary files"
@echo " clobber - Remove any build products"
@echo " platform - Display the information about the platform as seen by make"
@echo " env - Display the environment as seen by make"
@echo 'Environment variables:'
@echo " PACKAGE_LEVEL - Package level to be used for installing dependent Python"
@echo " packages in 'install' and 'develop' targets:"
@echo " latest - Latest package versions available on Pypi"
@echo " minimum - A minimum version as defined in minimum-constraints.txt"
@echo " Optional, defaults to 'latest'."
@echo ' PYTHON_CMD=... - Name of python command. Default: python'
@echo ' PIP_CMD=... - Name of pip command. Default: pip'
.PHONY: platform
platform:
@echo "Makefile: Platform information as seen by make:"
@echo "Platform: $(PLATFORM)"
@echo "Shell used for commands: $(SHELL)"
@echo "Shell flags: $(.SHELLFLAGS)"
@echo "Make version: $(MAKE_VERSION)"
@echo "Python command name: $(PYTHON_CMD)"
@echo "Python command location: $(shell $(WHICH) $(PYTHON_CMD))"
@echo "Python version: $(python_version)"
@echo "Pip command name: $(PIP_CMD)"
@echo "Pip command location: $(shell $(WHICH) $(PIP_CMD))"
@echo "Pip version: $(shell $(PIP_CMD) --version)"
@echo "$(package_name) package version: $(package_version)"
.PHONY: env
env:
@echo "Makefile: Environment variables as seen by make:"
$(ENV)
.PHONY: _check_version
_check_version:
ifeq (,$(package_version))
$(error Package version could not be determined)
endif
.PHONY: install
install: install_$(pymn).done
@echo "Makefile: $@ done."
.PHONY: develop
develop: develop_$(pymn).done
@echo "Makefile: $@ done."
.PHONY: check
check: develop_$(pymn).done
@echo "Makefile: Performing flake8 checks with PACKAGE_LEVEL=$(PACKAGE_LEVEL)"
flake8 --config .flake8 $(package_py_files) $(test_py_files) setup.py $(doc_dir)/conf.py
@echo "Makefile: Done performing flake8 checks"
@echo "Makefile: $@ done."
.PHONY: pylint
pylint: develop_$(pymn).done
@echo "Makefile: Performing pylint checks with PACKAGE_LEVEL=$(PACKAGE_LEVEL)"
pylint --rcfile=.pylintrc --disable=fixme $(package_py_files) $(test_py_files) setup.py $(doc_dir)/conf.py
@echo "Makefile: Done performing pylint checks"
@echo "Makefile: $@ done."
.PHONY: check_reqs
check_reqs: develop_$(pymn).done
@echo "Makefile: Checking missing dependencies of the package"
pip-missing-reqs $(package_dir) --requirements-file=requirements.txt
pip-missing-reqs $(package_dir) --requirements-file=minimum-constraints.txt
@echo "Makefile: Done checking missing dependencies of the package"
ifeq ($(PLATFORM),Windows_native)
# Reason for skipping on Windows is https://github.com/r1chardj0n3s/pip-check-reqs/issues/67
@echo "Makefile: Warning: Skipping the checking of missing dependencies of site-packages directory on native Windows" >&2
else
@echo "Makefile: Checking missing dependencies of some development packages"
pip-missing-reqs $(shell $(PYTHON_CMD) -c "import pytest as m,os; print(m.__file__)") --requirements-file=minimum-constraints.txt
pip-missing-reqs $(shell $(PYTHON_CMD) -c "import coverage as m,os; print(os.path.dirname(m.__file__))") --requirements-file=minimum-constraints.txt
pip-missing-reqs $(shell $(PYTHON_CMD) -c "import coveralls as m,os; print(os.path.dirname(m.__file__))") --requirements-file=minimum-constraints.txt
pip-missing-reqs $(shell $(PYTHON_CMD) -c "import flake8 as m,os; print(os.path.dirname(m.__file__))") --requirements-file=minimum-constraints.txt
pip-missing-reqs $(shell $(PYTHON_CMD) -c "import pylint as m,os; print(os.path.dirname(m.__file__))") --requirements-file=minimum-constraints.txt
pip-missing-reqs $(shell $(PYTHON_CMD) -c "import sphinx as m,os; print(os.path.dirname(m.__file__))") --requirements-file=minimum-constraints.txt
pip-missing-reqs $(shell $(PYTHON_CMD) -c "import twine as m,os; print(os.path.dirname(m.__file__))") --requirements-file=minimum-constraints.txt
@echo "Makefile: Done checking missing dependencies of some development packages"
endif
@echo "Makefile: $@ done."
.PHONY: test
test: develop_$(pymn).done
@echo "Makefile: Performing unit tests and coverage with PACKAGE_LEVEL=$(PACKAGE_LEVEL)"
@echo "Makefile: Note that the warning about an unknown metric is part of the tests"
pytest $(pytest_cov_opts) -s $(test_dir)
@echo "Makefile: Done performing unit tests and coverage"
@echo "Makefile: $@ done."
.PHONY: build
build: _check_version $(bdist_file) $(sdist_file)
@echo "Makefile: $@ done."
.PHONY: builddoc
builddoc: _check_version $(doc_build_file)
@echo "Makefile: $@ done."
.PHONY: all
all: install develop check pylint test build builddoc check_reqs
@echo "Makefile: $@ done."
.PHONY: upload
upload: _check_version $(bdist_file) $(sdist_file)
ifeq (,$(findstring .dev,$(package_version)))
@echo "==> This will upload $(package_name) version $(package_version) to PyPI!"
@echo -n "==> Continue? [yN] "
@bash -c 'read answer; if [ "$$answer" != "y" ]; then echo "Aborted."; false; fi'
twine upload $(bdist_file) $(sdist_file)
@echo "Makefile: Done: Uploaded $(package_name) version to PyPI: $(package_version)"
else
@echo "Error: A development version $(package_version) of $(package_name) cannot be uploaded to PyPI!"
@false
endif
.PHONY: clean
clean:
-$(call RM_R_FUNC,*.pyc)
-$(call RM_R_FUNC,*.tmp)
-$(call RM_R_FUNC,tmp_*)
-$(call RM_FUNC,.coverage AUTHORS ChangeLog)
-$(call RMDIR_R_FUNC,__pycache__)
-$(call RMDIR_FUNC,build $(package_name).egg-info .pytest_cache)
@echo "Makefile: $@ done."
.PHONY: clobber
clobber: clean
-$(call RMDIR_FUNC,$(doc_build_dir) htmlcov)
-$(call RM_FUNC,*.done)
@echo "Makefile: $@ done."
install_base_$(pymn).done:
@echo "Makefile: Installing base packages with PACKAGE_LEVEL=$(PACKAGE_LEVEL)"
-$(call RM_FUNC,$@)
bash -c 'pv=$$($(PYTHON_CMD) -m pip --version); if [[ $$pv =~ (^pip [1-8]\..*) ]]; then $(PYTHON_CMD) -m pip install pip==9.0.1; fi'
$(PYTHON_CMD) -m pip install $(pip_level_opts) pip setuptools wheel
@echo "Makefile: Done installing base packages"
echo "done" >$@
install_$(pymn).done: install_base_$(pymn).done requirements.txt setup.py
@echo "Makefile: Installing package and its prerequisites with PACKAGE_LEVEL=$(PACKAGE_LEVEL)"
-$(call RM_FUNC,$@)
$(PYTHON_CMD) -m pip install $(pip_level_opts) -r requirements.txt
$(PIP_CMD) install -e .
@echo "Makefile: Done installing package and its prerequisites"
echo "done" >$@
develop_$(pymn).done: install_$(pymn).done dev-requirements.txt
@echo "Makefile: Installing prerequisites for development with PACKAGE_LEVEL=$(PACKAGE_LEVEL)"
-$(call RM_FUNC,$@)
$(PYTHON_CMD) -m pip install $(pip_level_opts) -r dev-requirements.txt
@echo "Makefile: Done installing prerequisites for development"
echo "done" >$@
$(doc_build_file): develop_$(pymn).done $(doc_dependent_files)
@echo "Makefile: Generating HTML documentation with main file: $@"
sphinx-build -b html -v $(doc_dir) $(doc_build_dir)
@echo "Makefile: Done generating HTML documentation"
$(bdist_file): _check_version develop_$(pymn).done
@echo "Makefile: Creating binary distribution archive: $@"
$(PYTHON_CMD) setup.py bdist_wheel -d $(dist_dir) --universal
@echo "Makefile: Done creating binary distribution archive"
$(sdist_file): _check_version develop_$(pymn).done
@echo "Makefile: Creating source distribution archive: $@"
$(PYTHON_CMD) setup.py sdist -d $(dist_dir)
@echo "Makefile: Done creating source distribution archive"