1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3
4# Packages a U-Boot tool
5#
6# Usage: make_pip.sh <tool_name> [--real]
7#
8# Where tool_name is one of patman, buildman, dtoc, binman, u_boot_pylib
9#
10# and --real means to upload to the real server (otherwise the test one is used)
11#
12# The username for upload is always __token__ so set TWINE_PASSWORD to your
13# password before running this script:
14#
15# export TWINE_PASSWORD=pypi-xxx
16#
17# To test your new packages:
18#
19# pip install -i https://test.pypi.org/simple/ <tool_name>
20#
21
22# DO NOT use patman or binman
23
24set -xe
25
26# Repo to upload to
27repo="--repository testpypi"
28
29# Non-empty to do the actual upload
30upload=1
31
32# Non-empty to delete files used for testing
33delete_testfiles=1
34
35tool="$1"
36shift
37flags="$*"
38
39if [[ "${tool}" =~ ^(patman|buildman|dtoc|binman|u_boot_pylib)$ ]]; then
40	echo "Building dist package for tool ${tool}"
41else
42	echo "Unknown tool ${tool}: use u_boot_pylib, patman, buildman, dtoc or binman"
43	exit 1
44fi
45
46for flag in "${flags}"; do
47	if [ "${flag}" == "--real" ]; then
48		echo "Using real server"
49		repo=
50	fi
51	if [ "${flag}" == "-n" ]; then
52		echo "Doing dry run"
53		upload=
54	fi
55done
56
57if [ -n "${upload}" ]; then
58	if [ -z "${TWINE_PASSWORD}" ]; then
59		echo "Please set TWINE_PASSWORD to your password and retry"
60		exit 1
61	fi
62fi
63
64if [[ "${tool}" =~ ^(patman|u_boot_pylib)$ ]]; then
65	# Leave test_util.py and patman test files alone
66	delete_testfiles=
67fi
68
69# Create a temp dir to work in
70dir=$(mktemp -d)
71
72# Copy in some basic files
73cp -v tools/${tool}/pyproject.toml ${dir}
74cp -v Licenses/gpl-2.0.txt ${dir}/LICENSE
75readme="tools/${tool}/README.*"
76
77# Copy in the README, dropping some Sphinx constructs that PyPi doesn't like
78cat ${readme} | sed -E 's/:(doc|ref):`.*`//; /sectionauthor/d; /toctree::/d' \
79	> ${dir}/$(basename ${readme})
80
81# Copy the top-level Python and doc files
82dest=${dir}/src/${tool}
83mkdir -p ${dest}
84cp -v tools/$tool/{*.py,*.rst} ${dest}
85
86# Copy over the subdirectories, including any sub files. Drop any cache files
87# and other such things
88pushd tools/${tool}
89for subdir in $(find . -maxdepth 1 -type d | \
90		grep -vE "(__pycache__|home|usr|scratch|\.$|pyproject)"); do
91	pathname="${dest}/${subdir}"
92	echo "Copy ${pathname}"
93	cp -a ${subdir} ${pathname}
94done
95popd
96
97# Remove cache files that accidentally made it through
98find ${dest} -name __pycache__ -type f -exec rm {} \;
99find ${dest} -depth -name __pycache__ -exec rmdir 112 \;
100
101# Remove test files
102if [ -n "${delete_testfiles}" ]; then
103	rm -rfv ${dest}/*test*
104fi
105
106mkdir ${dir}/tests
107cd ${dir}
108
109# Make sure the tools are up to date
110python3 -m pip install --upgrade build
111python3 -m pip install --upgrade twine
112
113# Build the PyPi package
114python3 -m build
115
116echo "Completed build of ${tool}"
117
118# Use --skip-existing to work even if the version is already present
119if [ -n "${upload}" ]; then
120	echo "Uploading from ${dir}"
121	python3 -m twine upload ${repo} -u __token__ dist/*
122	echo "Completed upload of ${tool}"
123fi
124
125rm -rf "${dir}"
126
127echo -e "done\n\n"
128