forked from nhairs/nserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.sh
executable file
Β·367 lines (300 loc) Β· 10.8 KB
/
dev.sh
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/bin/bash
# NOTICE: dev.sh and it's related files are
# Copyright (c) 2020 Nicholas Hairs
# Licenced under The MIT Licence
# Source: https://github.com/nhairs/python-package-template
# Notes:
# - use shellcheck for bash linter (https://github.com/koalaman/shellcheck)
### SETUP
### ============================================================================
set -e # Bail at the first sign of trouble
# Notation Reference: https://unix.stackexchange.com/questions/122845/using-a-b-for-variable-assignment-in-scripts#comment685330_122848
: ${DEBUG:=0}
: ${CI:=0} # Flag for if we are in CI - default to not.
if ! command -v toml &> /dev/null; then
pip install --user toml-cli
fi
if ! command -v uv &> /dev/null; then
pip install --user uv
fi
### CONTANTS
### ============================================================================
SOURCE_UID=$(id -u)
SOURCE_GID=$(id -g)
SOURCE_UID_GID="${SOURCE_UID}:${SOURCE_GID}"
GIT_REPOSITORY=$(git remote get-url origin 2>/dev/null | cut -d '/' -f 2 | cut -d '.' -f 1 | grep . || echo -n 'none')
GIT_COMMIT_SHORT=$(git rev-parse --short HEAD 2>/dev/null || echo -n 'none')
GIT_COMMIT=$(git rev-parse HEAD 2>/dev/null || echo -n 'none')
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo -n 'none')
PACKAGE_NAME=$(toml get --toml-path pyproject.toml project.name)
PACKAGE_PYTHON_NAME=$(echo -n "$PACKAGE_NAME" | tr '-' '_')
PACKAGE_VERSION=$(toml get --toml-path pyproject.toml project.version)
: ${AUTOCLEAN_LIMIT:=10}
: ${PYTHON_PACKAGE_REPOSITORY:="pypi"}
: ${TESTPYPI_USERNAME="$USER-test"}
## Python Project Related
## -----------------------------------------------------------------------------
# You may want to customise these for your project
# TODO: this potentially should be moved to manifest.env so that projects can easily
# customise the main dev.sh
PYTHON_MIN_VERSION="py38"
## Build related
## -----------------------------------------------------------------------------
BUILD_TIMESTAMP=$(date +%s)
if [[ "$GIT_BRANCH" == "master" || "$GIT_BRANCH" == "main" ]]; then
BUILD_VERSION="${PACKAGE_VERSION}"
else
# Tox doesn't like version labels like
# python_template-0.0.0+3f2d02f.1667158525-py3-none-any.whl
#BUILD_VERSION="${PACKAGE_VERSION}+${GIT_COMMIT_SHORT}.${BUILD_TIMESTAMP}"
# Use PEP 440 non-compliant versions since we know it works
#BUILD_VERSION="${PACKAGE_VERSION}.${GIT_COMMIT_SHORT}"
# PEP 440 compliant `.devM` versioning using timestamps
# Multiple users on the same branch may interfere with eachother
# But this is fine for now...
BUILD_VERSION="${PACKAGE_VERSION}.dev${BUILD_TIMESTAMP}"
fi
BUILT_WHEEL="${PACKAGE_PYTHON_NAME}-${BUILD_VERSION}-py3-none-any.whl"
## Load manifests
## -----------------------------------------------------------------------------
if [[ -f "lib/manifest.env" ]]; then
echo "βοΈ loading lib/manifest.env"
source lib/manifest.env
fi
if [[ -f "locals.env" ]]; then
echo "βοΈ loading locals.env"
source locals.env
fi
## Generate remaining vars
## Insert into .tmp/env
## -----------------------------------------------------------------------------
if [ ! -d .tmp ]; then
mkdir .tmp
fi
if [[ "$DEBUG" -gt 0 ]]; then
echo "βοΈ writing .tmp/env"
fi
cat > .tmp/env <<EOF
PACKAGE_NAME=${PACKAGE_NAME}
PACKAGE_PYTHON_NAME=${PACKAGE_PYTHON_NAME}
PACKAGE_VERSION=${PACKAGE_VERSION}
GIT_REPOSITORY=${GIT_REPOSITORY}
GIT_COMMIT_SHORT=${GIT_COMMIT_SHORT}
GIT_COMMIT=${GIT_COMMIT}
GIT_BRANCH=${GIT_BRANCH}
PYTHON_PACKAGE_REPOSITORY=${PYTHON_PACKAGE_REPOSITORY}
TESTPYPI_USERNAME=${TESTPYPI_USERNAME}
SOURCE_UID=${SOURCE_UID}
SOURCE_GID=${SOURCE_GID}
SOURCE_UID_GID=${SOURCE_UID_GID}
BUILD_TIMESTAMP=${BUILD_TIMESTAMP}
BUILD_VERSION=${BUILD_VERSION}
BUILD_DIR=.tmp/dist # default
BUILT_WHEEL=${BUILT_WHEEL}
EOF
# workaround for old docker-compose versions
cp .tmp/env .env
### FUNCTIONS
### ============================================================================
## Utility
## -----------------------------------------------------------------------------
function heading {
# Print a pretty heading
# https://en.wikipedia.org/wiki/Box-drawing_character#Unicode
echo "ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ"
echo "β $@"
echo "ββββββββββββββββββββ"
echo
}
function heading2 {
# Print a pretty heading-2
# https://en.wikipedia.org/wiki/Box-drawing_character#Unicode
echo "β $@"
echo "ββββββββββββββββββββ"
}
## Debug Functions
## -----------------------------------------------------------------------------
function check_file {
# Pretty print if a file exists or not.
if [[ -f "$1" ]]; then
echo -e "$1 \e[1;32m EXISTS\e[0m"
else
echo -e "$1 \e[1;31m MISSING\e[0m"
fi
}
function check_pyproject_toml {
# Pretty print if the given key exists in pyproject.toml.
if toml get --toml-path pyproject.toml $1 1>/dev/null 2>/dev/null; then
echo -e "$1 \e[1;32m EXISTS\e[0m"
else
echo -e "$1 \e[1;31m MISSING\e[0m"
fi
}
## Command Functions
## -----------------------------------------------------------------------------
function display_usage {
echo "dev.sh - development utility"
echo "Usage: ./dev.sh COMMAND"
echo
echo " build Build python packages"
echo " build-docs Build documentation"
echo " clean Cleanup clutter"
echo " debug Display debug / basic health check information"
echo " docs Preview docs locally"
echo " format Format files"
echo " help Show this text"
echo " lint Lint files. You probably want to format first."
echo " push-docs Push docs"
echo " repl Open Python interactive shell with the package imported"
echo " If repl.py exists will use this file instead of default template."
echo " run Run the given file in the python-common container"
echo " tag Tag commit with current version"
echo " test Run unit tests"
echo " test-full Run unit tests on all python versions"
echo " upload Upload built files to where they are distributed from (e.g. PyPI)"
if [[ "$ENABLE_COMMANDS" = 1 ]]; then
# find extra commands
for COMMAND_PATH in lib/commands/*.sh; do
COMMAND=$(basename "$COMMAND_PATH" .sh)
COMMAND_HELP_PATH="lib/commands/${COMMAND}.txt"
if [[ ! -f "$COMMAND_HELP_PATH" ]]; then
printf "%16s\n" "$COMMAND"
else
COMMAND_HELP=$(head -n 1 "$COMMAND_HELP_PATH")
printf "%16s %s\n" "$COMMAND" "$COMMAND_HELP"
if [[ "$(wc -l $COMMAND_HELP_PATH | cut -d ' ' -f 1)" -gt 1 ]]; then
for HELP_LINE in $(tail -n +2 $COMMAND_HELP_PATH); do
echo " $HELP_LINE"
done
fi
fi
done
fi
echo
echo
}
### MAIN
### ============================================================================
case $1 in
"format")
if [[ $CI = 1 ]]; then
echo "ERROR! Do not run format in CI!"
exit 250
fi
heading "tox π - format"
uvx tox -e format || true
;;
"lint")
heading "tox π - lint"
uvx tox -e lint || true
;;
"test")
heading "tox π - single"
uvx tox -e py312 || true
;;
"test-full")
heading "tox π - all"
uvx tox || true
;;
"build")
source ./lib/python/build.sh
;;
"upload")
heading "Upload to ${PYTHON_PACKAGE_REPOSITORY}"
heading "setup π"
if [[ -z $(pip3 list | grep keyrings.alt) ]]; then
pip3 install --user keyrings.alt
pip3 install --user twine
fi
if [ ! -d dist_uploaded ]; then
mkdir dist_uploaded
fi
heading "upload π"
twine upload --repository "${PYTHON_PACKAGE_REPOSITORY}" dist/*.whl
echo "π cleanup"
mv dist/* dist_uploaded
;;
"repl")
heading "REPL π"
if [[ -f "repl.py" ]]; then
echo "Using provided repl.py"
echo
cp repl.py .tmp/repl.py
else
echo "Using default repl.py"
echo
cat > .tmp/repl.py <<EOF
import ${PACKAGE_PYTHON_NAME}
print('Your package is already imported π\nPress ctrl+d to exit')
EOF
fi
uv run python -i .tmp/repl.py
;;
"docs")
heading "Preview Docs π"
uv run --extra dev mkdocs serve -w docs
;;
"build-docs")
heading "Building Docs π"
uv run --extra dev mike deploy "$PACKAGE_VERSION" "latest" \
--update-aliases \
--prop-set-string "git_branch=${GIT_BRANCH}" \
--prop-set-string "git_commit=${GIT_COMMIT}" \
--prop-set-string "git_commit_short=${GIT_COMMIT_SHORT}" \
--prop-set-string "build_timestamp=${BUILD_TIMESTAMP}"
;;
"push-docs")
heading "Pushing docs π"
git push origin gh-pages
;;
"clean")
heading "Cleaning π"
echo "π pyclean"
if ! command -v pyclean &> /dev/null; then
pip3 install --user pyclean
fi
pyclean src
pyclean tests
echo "π clear .tox"
rm -rf .tox
echo "π remove build artifacts"
rm -rf build dist "src/${PACKAGE_PYTHON_NAME}.egg-info"
echo "cleaning .tmp"
rm -rf .tmp/*
;;
"debug")
heading "Debug π"
cat .tmp/env
echo
echo "Checking Directory Layout..."
check_file "pyproject.toml"
check_file "src/${PACKAGE_PYTHON_NAME}/py.typed"
check_file "src/${PACKAGE_PYTHON_NAME}/__init__.py"
check_file "src/${PACKAGE_PYTHON_NAME}/_version.py"
echo
echo "Checking pyproject.toml"
check_pyproject_toml project.optional-dependencies.dev
check_pyproject_toml tool.setuptools.package-data.${PACKAGE_PYTHON_NAME}
echo
;;
"tag")
heading "Tagging Git Commit π οΈ"
TAG="v${PACKAGE_VERSION}"
git tag "$TAG"
;;
"help")
display_usage
;;
*)
if [[ "$ENABLE_COMMANDS" = 1 ]]; then
COMMAND_FILE="lib/commands/${1}.sh"
if [[ -f "$COMMAND_FILE" ]]; then
source "$COMMAND_FILE"
exit 0
fi
fi
echo -e "\e[1;31mUnknown command \"${1}\"\e[0m"
display_usage
exit 255
;;
esac