-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
166 lines (138 loc) · 5.82 KB
/
Makefile
File metadata and controls
166 lines (138 loc) · 5.82 KB
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
# DO NOT EDIT - All project-specific values belong in config.mk!
# NOTE: From the GNU Make manual: A phony target should not be a
# prerequisite of a real target file; if it is, its recipe will be
# run every time make considers that file.
# https://www.gnu.org/software/make/manual/make.html#Phony-Targets
.PHONY: all autopep8 build build-test clean lint static python-version wheels check_template
ifdef SOAR_TEST_APP
include config-test.mk
else
include config.mk
endif
SOAR_PYTHON_VERSION:=$(shell PYTHONPATH=tests python -c 'from test_python_version import SOAR_PYTHON_VERSION as V; print(f"{V[0]}.{V[1]}")')
ifeq (src/app/readme.html, $(wildcard src/app/readme.html))
DIST_OPT:=readme.html
endif
DIST_SRCS:=$(addprefix dist/app/, app.json app.py app.png $(DIST_OPT))
SRCS:=$(shell find src/app -name '*.py')
TSCS:=$(shell find tests -name '*.py')
BUILD_TIME:=$(shell date -u +%FT%X.%6NZ)
VENV_PYTHON:=.venv/bin/python
BORG:=.venv/bin/borg
VENV_REQS:=.requirements.venv
UNAME:=$(shell uname -s)
ifeq (tag, $(GITHUB_REF_TYPE))
TAG?=$(GITHUB_REF_NAME)
else
TAG?=$(shell printf "0.0.%d" 0x$(shell git rev-parse --short=6 HEAD))
endif
GITHUB_SHA?=$(shell git rev-parse HEAD)
all: build
build: app.tar
deps: deps-deploy
deps-deploy: # Install deps for deploy.py on Github
pip install requests
dist: $(DIST_SRCS)
# NOTE: Make uses modification time (mtime) to determine if a target
# should be rebuilt. For a file this simply means whenever the contents
# are changed the mtime is updated. For a directory the mtime is
# updated whenever files or subdirectories are added, removed, or
# renamed within it.
#
# dist/app is a dependency for all the files that reside within it.
# This is necessary to ensure the directory is created before we add
# files. Unfortunately, this has the side effect that the timestamp
# for dist/app is always ahead of the timestamps of its contents
# because each time a file is created dist/app's mtime is updated by
# the operating system. Therefore, on subsequent runs Make will
# ALWAYS rebuild every file residing within dist/app.
#
# To avoid these unnecessary rebuilds we can use order-only prerequisites.
# An order-only prerequisite NEVER causes a target to rebuild, the
# prerequisite will be built if it does not exist but when updated
# will NOT cause a rebuild of the target. This is exactly the behavior
# we want in this case. The directory dist/app must exist before
# building a file, but changes to dist/app's mtime do not warrant a
# rebuild of one of dist/app's files. To specify a list of order-only
# prerequisites simply add a pipe | before them and after any normal
# prerequisites like so:
#
# targets : normal-prerequisites | order-only-prerequisites
dist/app:
mkdir -p $@
dist/app/app.py: src/app/app.py | dist/app
sed "s/GITHUB_TAG/$(TAG)/;s/GITHUB_SHA/$(GITHUB_SHA)/;s/BUILD_TIME/$(BUILD_TIME)/" $< > $@
dist/app/app.json: src/app/app.json .venv dist/app/wheels | dist/app
# LC_ALL=C is needed on macOS to avoid illegal byte sequence error
LC_ALL=C sed "s/APP_ID/$(APP_ID)/;s/APP_NAME/$(APP_NAME)/;s/GITHUB_TAG/$(TAG)/;s/BUILD_TIME/$(BUILD_TIME)/" $< |\
$(VENV_PYTHON) -m phtoolbox deps -o $@ -C dist/app wheels
dist/app/%: src/app/% dist/app
cp -r $< $@
app.tar: $(DIST_SRCS)
tar cvf $@ -C dist app
deploy: app.tar .venv
$(VENV_PYTHON) -m phtoolbox deploy $<
python-version:
@echo $(SOAR_PYTHON_VERSION)
.python-version: tests/test_python_version.py
uv python install $(SOAR_PYTHON_VERSION)
uv python pin $(SOAR_PYTHON_VERSION)
.gitattributes: .venv
$(BORG) gen $@
venv: .venv
.venv: requirements-test.txt .python-version
rm -rf $@
uv venv
uv pip install -r $<
wheels: dist/app/wheels
dist/app/wheels: requirements.in
pip wheel --no-deps --wheel-dir=$@ -r $^
requirements-test.txt: export PYTEST_SOAR_REPO=git+https://github.com/splunk/pytest-splunk-soar-connectors.git
requirements-test.txt: requirements-test.in requirements.in .python-version
rm -rf $(VENV_REQS)
$(shell uv python find) -m venv $(VENV_REQS)
$(VENV_REQS)/bin/python -m pip install -r requirements.in
$(VENV_REQS)/bin/python -m pip install -r requirements-test.in
$(VENV_REQS)/bin/python -m pip freeze -qqq | \
sed "s;^pytest-splunk-soar-connectors==.*;$(PYTEST_SOAR_REPO);" > $@
# REMOVE sed line above once pytest-splunk-soar-connectors is on pypi
lint: .venv .lint
.lint: $(SRCS) $(TSCS)
$(VENV_PYTHON) -m flake8 $?
touch $@
static: .venv .static
.static: $(SRCS) $(TSCS)
echo "Code: $(SRCS)"
echo "Test: $(TSCS)"
$(VENV_PYTHON) -m mypy $^
touch $@
unit: .venv
$(VENV_PYTHON) -m pytest
autopep8: .autopep8
.autopep8: $(SRCS) $(TSCS)
autopep8 --in-place $?
touch $@
# .check_template.d is generated by soar_template
-include .check_template.d
check_template: .venv .check_template check_template_contents
.check_template:
$(BORG) compare --make-target $@
touch $@
check_template_contents:
@ ! grep -l PROD_ config.mk || echo "config.mk should not contain PROD_"
@ ! grep -l TEST_ config-test.mk || echo config-test.mk should not contain TEST_
@ grep -ql '^APP_NAME:=Test ' config-test.mk || echo APP_NAME in config-test.mk should start 'Test '
@ ! grep -ql MODULE src/app/app.json || echo "app.json should not contain MODULE. Change MODULE to app."
@ python -c "import json, sys; d=json.load(open('src/app/app.json')); sys.exit(0) if d.get('python_version') == '3.13' else print('app.json: python_version must be set to 3.13.') or sys.exit(1)"
@ ! grep -ql '"pip[0-9]*_dependencies"' src/app/app.json || echo "app.json should not contain pip dependencies. phantom_toolbox will manage dependencies."
@ (grep -c '"type": "test"' src/app/app.json | grep -q 1) || echo "app.json should not contain multiple test actions"
test: lint static check_template unit
clean:
rm -rf .venv $(VENV_REQS)
rm -rf .lint .static
rm -rf .mypy_cache
rm -rf dist
rm -f app.tar
force-clean: clean
rm -f requirements-test.txt .python-version
rm .check_template.d