-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpublish.sh
More file actions
executable file
·443 lines (397 loc) · 12.6 KB
/
Copy pathpublish.sh
File metadata and controls
executable file
·443 lines (397 loc) · 12.6 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env bash
__doc__='
Script to build, sign, tag, and optionally upload Python distributions.
This script is intentionally local-first: it can perform the same release
steps that CI performs, but without requiring a CI provider. It supports both
PEP 517 / pyproject.toml projects and older setup.py projects.
Common dry-run usage:
DO_UPLOAD=0 DO_TAG=0 ./publish.sh
Common live usage:
load_secrets
TWINE_REPOSITORY_URL=live DO_UPLOAD=1 DO_TAG=1 ./publish.sh
Useful variables:
MODE=pure|binary|all|sdist|native|bdist
pure -> build sdist and pure wheel with python -m build
binary -> build sdist and use prebuilt wheelhouse wheels
all -> build sdist and pure wheel, and include wheelhouse wheels
sdist -> only build/use sdist
native -> only build/use native wheel from python -m build
bdist -> only use prebuilt wheelhouse wheels
DO_BUILD=True|False|auto
DO_GPG=True|False|auto
DO_OTS=True|False|auto
DO_UPLOAD=True|False
DO_TAG=True|False
ENSURE_BUILD_DEPS=True|False
'
set -e
DEBUG=${DEBUG:=''}
if [[ "${DEBUG}" != "" ]]; then
set -x
fi
check_variable(){
KEY=$1
HIDE=${2:-}
VAL=${!KEY:-}
if [[ "$HIDE" == "" ]]; then
echo "[DEBUG] CHECK VARIABLE: $KEY=\"$VAL\""
else
echo "[DEBUG] CHECK VARIABLE: $KEY=<hidden>"
fi
if [[ "$VAL" == "" ]]; then
echo "[ERROR] UNSET VARIABLE: $KEY=\"$VAL\""
exit 1
fi
}
normalize_boolean(){
ARG=${1:-}
ARG=$(echo "$ARG" | awk '{print tolower($0)}')
if [ "$ARG" = "true" ] || [ "$ARG" = "1" ] || [ "$ARG" = "yes" ] || [ "$ARG" = "y" ] || [ "$ARG" = "on" ]; then
echo "True"
elif [ "$ARG" = "false" ] || [ "$ARG" = "0" ] || [ "$ARG" = "no" ] || [ "$ARG" = "n" ] || [ "$ARG" = "off" ]; then
echo "False"
else
echo "$ARG"
fi
}
project_name(){
python - <<'PY'
from __future__ import annotations
import pathlib
try:
import tomllib
except ModuleNotFoundError: # pragma: no cover
import tomli as tomllib # type: ignore
root = pathlib.Path.cwd()
pyproject = root / 'pyproject.toml'
if pyproject.exists():
data = tomllib.loads(pyproject.read_text())
name = data.get('project', {}).get('name')
if name:
print(name)
raise SystemExit
setup_py = root / 'setup.py'
if setup_py.exists():
ns = {}
exec(setup_py.read_text(), ns)
name = ns.get('NAME')
if name:
print(name)
raise SystemExit
print(root.name)
PY
}
project_dist_prefix(){
python - "$1" <<'PY'
import re
import sys
name = sys.argv[1]
print(re.sub(r'[-_.]+', '_', name).lower())
PY
}
project_version(){
python - <<'PY'
from __future__ import annotations
import ast
import pathlib
try:
import tomllib
except ModuleNotFoundError: # pragma: no cover
import tomli as tomllib # type: ignore
root = pathlib.Path.cwd()
pyproject = root / 'pyproject.toml'
def static_parse_version(fpath: pathlib.Path, varname: str = '__version__'):
try:
tree = ast.parse(fpath.read_text())
except Exception:
return None
for node in tree.body:
if isinstance(node, ast.Assign):
for target in node.targets:
if getattr(target, 'id', None) == varname:
try:
return ast.literal_eval(node.value)
except Exception:
return None
return None
if pyproject.exists():
data = tomllib.loads(pyproject.read_text())
project = data.get('project', {})
if project.get('version'):
print(project['version'])
raise SystemExit
tool = data.get('tool', {})
setuptools_dynamic = tool.get('setuptools', {}).get('dynamic', {})
version_spec = setuptools_dynamic.get('version', {})
attr = version_spec.get('attr') if isinstance(version_spec, dict) else None
if attr:
module_name, _, attr_name = attr.rpartition('.')
xcookie_config = tool.get('xcookie', {})
rel_parent = pathlib.Path(xcookie_config.get('rel_mod_parent_dpath', '.'))
candidate_roots = [rel_parent]
find_cfg = tool.get('setuptools', {}).get('packages', {}).get('find', {})
if isinstance(find_cfg, dict):
for item in find_cfg.get('where', []) or []:
candidate_roots.append(pathlib.Path(item))
candidate_roots.append(pathlib.Path('.'))
rel_module = pathlib.Path(*module_name.split('.'))
for base in dict.fromkeys(candidate_roots):
for rel in [base / rel_module / '__init__.py', base / (str(rel_module) + '.py')]:
version = static_parse_version(root / rel, attr_name)
if version:
print(version)
raise SystemExit
setup_py = root / 'setup.py'
if setup_py.exists():
ns = {}
exec(setup_py.read_text(), ns)
version = ns.get('VERSION')
if version:
print(version)
raise SystemExit
raise SystemExit('Could not determine project version')
PY
}
ls_array(){
local arr_name="$1"
local glob_pattern="$2"
shopt -s nullglob
# shellcheck disable=SC2206
array=($glob_pattern)
shopt -u nullglob
# shellcheck disable=SC2086
readarray -t $arr_name < <(printf '%s\n' "${array[@]}")
}
DEPLOY_REMOTE=${DEPLOY_REMOTE:=origin}
NAME=${NAME:=$(project_name)}
VERSION=${VERSION:=$(project_version)}
DIST_PREFIX=${DIST_PREFIX:=$(project_dist_prefix "$NAME")}
DIST_DPATH=${DIST_DPATH:=dist}
WHEELHOUSE_DPATH=${WHEELHOUSE_DPATH:=wheelhouse}
check_variable DEPLOY_REMOTE
ARG_1=${1:-}
DO_UPLOAD=${DO_UPLOAD:=$ARG_1}
DO_TAG=${DO_TAG:=$ARG_1}
DO_UPLOAD=$(normalize_boolean "$DO_UPLOAD")
DO_TAG=$(normalize_boolean "$DO_TAG")
DO_GPG=${DO_GPG:="auto"}
if [ "$DO_GPG" == "auto" ]; then
DO_GPG="True"
fi
DO_OTS=${DO_OTS:="auto"}
if [ "$DO_OTS" == "auto" ]; then
if type ots >/dev/null 2>&1 ; then
DO_OTS="True"
else
DO_OTS="False"
fi
fi
DO_BUILD=${DO_BUILD:="auto"}
if [ "$DO_BUILD" == "auto" ]; then
DO_BUILD="True"
fi
ENSURE_BUILD_DEPS=${ENSURE_BUILD_DEPS:="auto"}
if [ "$ENSURE_BUILD_DEPS" == "auto" ]; then
ENSURE_BUILD_DEPS="$DO_BUILD"
fi
DO_GPG=$(normalize_boolean "$DO_GPG")
DO_OTS=$(normalize_boolean "$DO_OTS")
DO_BUILD=$(normalize_boolean "$DO_BUILD")
ENSURE_BUILD_DEPS=$(normalize_boolean "$ENSURE_BUILD_DEPS")
TWINE_USERNAME=${TWINE_USERNAME:=""}
TWINE_PASSWORD=${TWINE_PASSWORD:=""}
DEFAULT_TEST_TWINE_REPO_URL="https://test.pypi.org/legacy/"
DEFAULT_LIVE_TWINE_REPO_URL="https://upload.pypi.org/legacy/"
TWINE_REPOSITORY_URL=${TWINE_REPOSITORY_URL:="auto"}
if [[ "${TWINE_REPOSITORY_URL}" == "auto" ]]; then
if [[ "$DEBUG" == "" ]]; then
TWINE_REPOSITORY_URL="live"
else
TWINE_REPOSITORY_URL="test"
fi
fi
if [[ "${TWINE_REPOSITORY_URL}" == "live" ]]; then
TWINE_REPOSITORY_URL=$DEFAULT_LIVE_TWINE_REPO_URL
elif [[ "${TWINE_REPOSITORY_URL}" == "test" ]]; then
TWINE_REPOSITORY_URL=$DEFAULT_TEST_TWINE_REPO_URL
fi
GPG_EXECUTABLE=${GPG_EXECUTABLE:="auto"}
if [[ "$GPG_EXECUTABLE" == "auto" ]]; then
if [[ "$(command -v gpg2 || true)" != "" ]]; then
GPG_EXECUTABLE="gpg2"
else
GPG_EXECUTABLE="gpg"
fi
fi
GPG_KEYID=${GPG_KEYID:="auto"}
if [[ "$GPG_KEYID" == "auto" ]]; then
GPG_KEYID=$(git config --local user.signingkey || true)
if [[ "$GPG_KEYID" == "" ]]; then
GPG_KEYID=$(git config --global user.signingkey || true)
fi
fi
if [ -f CMakeLists.txt ] ; then
DEFAULT_MODE="binary"
else
DEFAULT_MODE="pure"
fi
MODE=${MODE:=$DEFAULT_MODE}
if [[ "$MODE" == "all" ]]; then
MODE_LIST=("sdist" "native" "bdist")
elif [[ "$MODE" == "pure" ]]; then
MODE_LIST=("sdist" "native")
elif [[ "$MODE" == "binary" ]]; then
MODE_LIST=("sdist" "bdist")
else
MODE_LIST=("$MODE")
fi
MODE_LIST_STR=$(printf '"%s" ' "${MODE_LIST[@]}")
WAS_INTERACTION="False"
echo "
=== PYPI BUILDING SCRIPT ==
NAME='$NAME'
VERSION='$VERSION'
DIST_PREFIX='$DIST_PREFIX'
TWINE_USERNAME='$TWINE_USERNAME'
TWINE_REPOSITORY_URL='$TWINE_REPOSITORY_URL'
GPG_KEYID='$GPG_KEYID'
DO_UPLOAD=${DO_UPLOAD}
DO_TAG=${DO_TAG}
DO_GPG=${DO_GPG}
DO_OTS=${DO_OTS}
DO_BUILD=${DO_BUILD}
MODE_LIST_STR=${MODE_LIST_STR}
"
if [[ "$DO_TAG" == "" ]]; then
read -r -p "Do you want to git tag and push version='$VERSION'? (input 'yes' to confirm) " ANS
WAS_INTERACTION="True"
DO_TAG=$(normalize_boolean "$ANS")
fi
if [[ "$DO_BUILD" == "" ]]; then
read -r -p "Do you need to build distributions? (input 'yes' to confirm) " ANS
WAS_INTERACTION="True"
DO_BUILD=$(normalize_boolean "$ANS")
fi
if [[ "$DO_UPLOAD" == "" ]]; then
read -r -p "Are you ready to directly publish version='$VERSION'? ('yes' will twine upload) " ANS
WAS_INTERACTION="True"
DO_UPLOAD=$(normalize_boolean "$ANS")
fi
if [[ "$WAS_INTERACTION" == "True" ]]; then
echo "
VERSION='$VERSION'
DO_UPLOAD=${DO_UPLOAD}
DO_TAG=${DO_TAG}
DO_GPG=${DO_GPG}
DO_BUILD=${DO_BUILD}
MODE_LIST_STR='${MODE_LIST_STR}'
"
read -r -p "Look good? Enter any text to continue " ANS
fi
if [ "$ENSURE_BUILD_DEPS" == "True" ]; then
python -m pip install pip build twine -U
fi
if [ "$DO_BUILD" == "True" ]; then
echo "=== <BUILD DISTRIBUTIONS> ==="
for _MODE in "${MODE_LIST[@]}"; do
echo "_MODE=$_MODE"
if [[ "$_MODE" == "sdist" ]]; then
python -m build --sdist --outdir "$DIST_DPATH" || { echo 'failed to build sdist' ; exit 1; }
elif [[ "$_MODE" == "native" ]]; then
python -m build --wheel --outdir "$DIST_DPATH" || { echo 'failed to build wheel' ; exit 1; }
elif [[ "$_MODE" == "bdist" ]]; then
echo "Assume binary wheels have already been built in $WHEELHOUSE_DPATH"
else
echo "ERROR: bad mode: $_MODE"
exit 1
fi
done
echo "=== <END BUILD DISTRIBUTIONS> ==="
else
echo "DO_BUILD=False, skipping build"
fi
WHEEL_FPATHS=()
for _MODE in "${MODE_LIST[@]}"; do
if [[ "$_MODE" == "sdist" ]]; then
ls_array "_NEW_WHEEL_PATHS" "$DIST_DPATH/${DIST_PREFIX}-${VERSION}*.tar.gz"
elif [[ "$_MODE" == "native" ]]; then
ls_array "_NEW_WHEEL_PATHS" "$DIST_DPATH/${DIST_PREFIX}-${VERSION}*.whl"
elif [[ "$_MODE" == "bdist" ]]; then
ls_array "_NEW_WHEEL_PATHS" "$WHEELHOUSE_DPATH/${DIST_PREFIX}-${VERSION}-*.whl"
else
echo "ERROR: bad mode: $_MODE"
exit 1
fi
for new_item in "${_NEW_WHEEL_PATHS[@]}"; do
if [[ "$new_item" != "" ]]; then
WHEEL_FPATHS+=("$new_item")
fi
done
done
readarray -t WHEEL_FPATHS < <(printf '%s\n' "${WHEEL_FPATHS[@]}" | sort -u)
if [[ "${#WHEEL_FPATHS[@]}" -eq 0 ]]; then
echo "ERROR: no distributions found for NAME=$NAME VERSION=$VERSION"
exit 1
fi
WHEEL_PATHS_STR=$(printf '"%s" ' "${WHEEL_FPATHS[@]}")
echo "WHEEL_PATHS_STR=$WHEEL_PATHS_STR"
python -m twine check "${WHEEL_FPATHS[@]}"
WHEEL_SIGNATURE_FPATHS=()
if [ "$DO_GPG" == "True" ]; then
echo "=== <GPG SIGN> ==="
for WHEEL_FPATH in "${WHEEL_FPATHS[@]}"; do
check_variable WHEEL_FPATH
check_variable GPG_EXECUTABLE
check_variable GPG_KEYID
GPG_SIGN_CMD="$GPG_EXECUTABLE --batch --yes --detach-sign --armor --local-user $GPG_KEYID"
echo "GPG_SIGN_CMD=$GPG_SIGN_CMD"
$GPG_SIGN_CMD --output "$WHEEL_FPATH".asc "$WHEEL_FPATH"
python -m twine check "$WHEEL_FPATH"
$GPG_EXECUTABLE --verify "$WHEEL_FPATH".asc "$WHEEL_FPATH"
WHEEL_SIGNATURE_FPATHS+=("$WHEEL_FPATH".asc)
done
echo "=== <END GPG SIGN> ==="
else
echo "DO_GPG=False, skipping GPG sign"
fi
if [ "$DO_OTS" == "True" ]; then
echo "=== <OTS SIGN> ==="
if [ "$DO_GPG" == "True" ]; then
ots stamp "${WHEEL_FPATHS[@]}" "${WHEEL_SIGNATURE_FPATHS[@]}"
else
ots stamp "${WHEEL_FPATHS[@]}"
fi
echo "=== <END OTS SIGN> ==="
else
echo "DO_OTS=False, skipping OTS sign"
fi
if [[ "$DO_TAG" == "True" ]]; then
TAG_NAME="v${VERSION}"
git tag "$TAG_NAME" -m "tarball tag $VERSION"
git push --tags "$DEPLOY_REMOTE"
echo "Tagged and pushed $TAG_NAME to $DEPLOY_REMOTE"
else
echo "Not tagging"
fi
if [[ "$DO_UPLOAD" == "True" ]]; then
check_variable TWINE_USERNAME
check_variable TWINE_PASSWORD "hide"
python -m twine upload --username "$TWINE_USERNAME" "--password=$TWINE_PASSWORD" \
--repository-url "$TWINE_REPOSITORY_URL" \
"${WHEEL_FPATHS[@]}" --skip-existing --verbose || { echo 'failed to twine upload' ; exit 1; }
echo "!!! FINISH: LIVE RUN !!!"
else
echo "
DRY RUN ... Skipping upload
DEPLOY_REMOTE='$DEPLOY_REMOTE'
DO_UPLOAD='$DO_UPLOAD'
WHEEL_PATHS_STR='$WHEEL_PATHS_STR'
MODE_LIST_STR='$MODE_LIST_STR'
VERSION='$VERSION'
NAME='$NAME'
TWINE_USERNAME='$TWINE_USERNAME'
GPG_KEYID='$GPG_KEYID'
To do live run set DO_UPLOAD=1.
!!! FINISH: DRY RUN !!!
"
fi