Debian Codegraph @

Makefile

raw
1 # Makefile for packaging and testing django-pgbulk
2 #
3 # This Makefile has the following targets:
4 #
5 # setup - Sets up the development environment
6 # dependencies - Installs dependencies
7 # docs - Build documentation
8 # docs-serve - Serve documentation
9 # lint - Run code linting and static checks
10 # lint-fix - Fix common linting errors
11 # type-check - Run Pyright type-checking
12 # test - Run tests using pytest
13 # full-test-suite - Run full test suite using tox
14 # shell - Run a shell in a virtualenv
15 # docker-teardown - Spin down docker resources
16
17 OS = $(shell uname -s)
18
19 PACKAGE_NAME=django-pgbulk
20 MODULE_NAME=pgbulk
21 SHELL=bash
22
23 ifeq (${OS}, Linux)
24 DOCKER_CMD?=sudo docker
25 DOCKER_RUN_ARGS?=-v /home:/home -v $(shell pwd):/code -e EXEC_WRAPPER="" -u "$(shell id -u):$(shell id -g)" -v /etc/passwd:/etc/passwd
26 # The user can be passed to docker exec commands in Linux.
27 # For example, "make shell user=root" for access to apt-get commands
28 user?=$(shell id -u)
29 group?=$(shell id ${user} -u)
30 EXEC_WRAPPER?=$(DOCKER_CMD) exec --user="$(user):$(group)" -it $(PACKAGE_NAME)
31 else ifeq (${OS}, Darwin)
32 DOCKER_CMD?=docker
33 DOCKER_RUN_ARGS?=-v ~/:/home/circleci -v $(shell pwd):/code -e EXEC_WRAPPER=""
34 EXEC_WRAPPER?=$(DOCKER_CMD) exec -it $(PACKAGE_NAME)
35 endif
36
37 # Docker run mounts the local code directory, SSH (for git), and global git config information
38 DOCKER_RUN_CMD?=$(DOCKER_CMD) compose run --name $(PACKAGE_NAME) $(DOCKER_RUN_ARGS) -d app
39
40 # Print usage of main targets when user types "make" or "make help"
41 .PHONY: help
42 help:
43 ifndef run
44 @echo "Please choose one of the following targets: \n"\
45 " docker-setup: Setup Docker development environment\n"\
46 " conda-setup: Setup Conda development environment\n"\
47 " lock: Lock dependencies\n"\
48 " dependencies: Install dependencies\n"\
49 " shell: Start a shell\n"\
50 " test: Run tests\n"\
51 " tox: Run tests against all versions of Python\n"\
52 " lint: Run code linting and static checks\n"\
53 " lint-fix: Fix common linting errors\n"\
54 " type-check: Run Pyright type-checking\n"\
55 " docs: Build documentation\n"\
56 " docs-serve: Serve documentation\n"\
57 " docker-teardown: Spin down docker resources\n"\
58 "\n"\
59 "View the Makefile for more documentation"
60 @exit 2
61 else
62 $(EXEC_WRAPPER) $(run)
63 endif
64
65
66 # Pull the latest container and start a detached run
67 .PHONY: docker-start
68 docker-start:
69 $(DOCKER_CMD) compose pull
70 $(DOCKER_RUN_CMD)
71
72
73 # Lock dependencies
74 .PHONY: lock
75 lock:
76 $(EXEC_WRAPPER) poetry lock
77 $(EXEC_WRAPPER) poetry export --with dev --without-hashes -f requirements.txt > docs/requirements.txt
78
79
80 # Install dependencies
81 .PHONY: dependencies
82 dependencies:
83 $(EXEC_WRAPPER) poetry install --no-ansi
84
85
86 # Sets up the local database
87 .PHONY: db-setup
88 db-setup:
89 -psql postgres -c "CREATE USER postgres;"
90 -psql postgres -c "ALTER USER postgres SUPERUSER;"
91 -psql postgres -c "CREATE DATABASE ${MODULE_NAME}_local OWNER postgres;"
92 -psql postgres -c "GRANT ALL PRIVILEGES ON DATABASE ${MODULE_NAME}_local to postgres;"
93 $(EXEC_WRAPPER) python manage.py migrate
94
95
96 # Sets up a conda development environment
97 .PHONY: conda-create
98 conda-create:
99 -conda env create -f environment.yml -y
100 $(EXEC_WRAPPER) poetry config virtualenvs.create false --local
101
102
103 # Sets up a Conda development environment
104 .PHONY: conda-setup
105 conda-setup: EXEC_WRAPPER=conda run -n ${PACKAGE_NAME} --no-capture-output
106 conda-setup: conda-create lock dependencies db-setup
107
108
109 # Sets up a Docker development environment
110 .PHONY: docker-setup
111 docker-setup: docker-teardown docker-start lock dependencies
112
113
114 # Spin down docker resources
115 .PHONY: docker-teardown
116 docker-teardown:
117 $(DOCKER_CMD) compose down --remove-orphans
118
119
120 # Run a shell
121 .PHONY: shell
122 shell:
123 $(EXEC_WRAPPER) /bin/bash
124
125
126 # Run pytest
127 .PHONY: test
128 test:
129 $(EXEC_WRAPPER) pytest
130
131
132 # Run full test suite
133 .PHONY: full-test-suite
134 full-test-suite:
135 $(EXEC_WRAPPER) tox
136
137
138 # Build documentation
139 .PHONY: docs
140 docs:
141 $(EXEC_WRAPPER) mkdocs build -s
142
143
144 # Serve documentation
145 .PHONY: docs-serve
146 docs-serve:
147 $(EXEC_WRAPPER) mkdocs serve
148
149
150 # Run code linting and static analysis. Ensure docs can be built
151 .PHONY: lint
152 lint:
153 $(EXEC_WRAPPER) ruff format . --check
154 $(EXEC_WRAPPER) ruff check ${MODULE_NAME}
155 $(EXEC_WRAPPER) bash -c 'make docs'
156 $(EXEC_WRAPPER) diff <(poetry export --with dev --without-hashes -f requirements.txt) docs/requirements.txt >/dev/null 2>&1 || exit 1
157
158
159 # Fix common linting errors
160 .PHONY: lint-fix
161 lint-fix:
162 $(EXEC_WRAPPER) ruff format .
163 $(EXEC_WRAPPER) ruff check ${MODULE_NAME} --fix
164
165
166 # Run Pyright type-checking
167 .PHONY: type-check
168 type-check:
169 $(EXEC_WRAPPER) pyright $(MODULE_NAME)