summaryrefslogtreecommitdiff
path: root/scripts/make_pip.sh
blob: 33ad51ada703ba2d8aecc5688037c259327b9d78 (plain)
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
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0+

# Packages a U-Boot tool
#
# Usage: make_pip.sh <tool_name> [--real]
#
# Where tool_name is one of patman, buildman, dtoc, binman, u_boot_pylib
#
# and --real means to upload to the real server (otherwise the test one is used)
#
# The username for upload is always __token__ so set TWINE_PASSWORD to your
# password before running this script:
#
# export TWINE_PASSWORD=pypi-xxx
#
# To test your new packages:
#
# pip install -i https://test.pypi.org/simple/ <tool_name>
#

# DO NOT use patman or binman

set -xe

# Repo to upload to
repo="--repository testpypi"

# Non-empty to do the actual upload
upload=1

# Non-empty to delete files used for testing
delete_testfiles=1

tool="$1"
shift
flags="$*"

if [[ "${tool}" =~ ^(patman|buildman|dtoc|binman|u_boot_pylib)$ ]]; then
	echo "Building dist package for tool ${tool}"
else
	echo "Unknown tool ${tool}: use u_boot_pylib, patman, buildman, dtoc or binman"
	exit 1
fi

for flag in "${flags}"; do
	if [ "${flag}" == "--real" ]; then
		echo "Using real server"
		repo=
	fi
	if [ "${flag}" == "-n" ]; then
		echo "Doing dry run"
		upload=
	fi
done

if [ -n "${upload}" ]; then
	if [ -z "${TWINE_PASSWORD}" ]; then
		echo "Please set TWINE_PASSWORD to your password and retry"
		exit 1
	fi
fi

if [[ "${tool}" =~ ^(patman|u_boot_pylib)$ ]]; then
	# Leave test_util.py and patman test files alone
	delete_testfiles=
fi

# Create a temp dir to work in
dir=$(mktemp -d)

# Copy in some basic files
cp -v tools/${tool}/pyproject.toml ${dir}
cp -v Licenses/gpl-2.0.txt ${dir}/LICENSE
readme="tools/${tool}/README.*"

# Copy in the README, dropping some Sphinx constructs that PyPi doesn't like
cat ${readme} | sed -E 's/:(doc|ref):`.*`//; /sectionauthor/d; /toctree::/d' \
	> ${dir}/$(basename ${readme})

# Copy the top-level Python and doc files
dest=${dir}/src/${tool}
mkdir -p ${dest}
cp -v tools/$tool/{*.py,*.rst} ${dest}

# Copy over the subdirectories, including any sub files. Drop any cache files
# and other such things
pushd tools/${tool}
for subdir in $(find . -maxdepth 1 -type d | \
		grep -vE "(__pycache__|home|usr|scratch|\.$|pyproject)"); do
	pathname="${dest}/${subdir}"
	echo "Copy ${pathname}"
	cp -a ${subdir} ${pathname}
done
popd

# Remove cache files that accidentally made it through
find ${dest} -name __pycache__ -type f -exec rm {} \;
find ${dest} -depth -name __pycache__ -exec rmdir 112 \;

# Remove test files
if [ -n "${delete_testfiles}" ]; then
	rm -rfv ${dest}/*test*
fi

mkdir ${dir}/tests
cd ${dir}

# Use virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Make sure the tools are up to date
python3 -m pip install --upgrade build
python3 -m pip install --upgrade twine

# Build the PyPi package
python3 -m build

echo "Completed build of ${tool}"

# Use --skip-existing to work even if the version is already present
if [ -n "${upload}" ]; then
	echo "Uploading from ${dir}"
	python3 -m twine upload ${repo} -u __token__ dist/*
	echo "Completed upload of ${tool}"
fi

# Finish using virtual environment
deactivate
rm -rf "${dir}"

echo -e "done\n\n"