Simon Glass | d6b6448 | 2023-02-23 18:18:05 -0700 | [diff] [blame] | 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 | |
| 24 | set -xe |
| 25 | |
| 26 | # Repo to upload to |
| 27 | repo="--repository testpypi" |
| 28 | |
| 29 | # Non-empty to do the actual upload |
| 30 | upload=1 |
| 31 | |
Simon Glass | 3ca9e28 | 2023-11-19 08:36:02 -0700 | [diff] [blame] | 32 | # Non-empty to delete files used for testing |
| 33 | delete_testfiles=1 |
| 34 | |
Simon Glass | d6b6448 | 2023-02-23 18:18:05 -0700 | [diff] [blame] | 35 | tool="$1" |
| 36 | shift |
| 37 | flags="$*" |
| 38 | |
| 39 | if [[ "${tool}" =~ ^(patman|buildman|dtoc|binman|u_boot_pylib)$ ]]; then |
| 40 | echo "Building dist package for tool ${tool}" |
| 41 | else |
Simon Glass | d0ddcb9 | 2023-11-19 08:36:04 -0700 | [diff] [blame] | 42 | echo "Unknown tool ${tool}: use u_boot_pylib, patman, buildman, dtoc or binman" |
Simon Glass | d6b6448 | 2023-02-23 18:18:05 -0700 | [diff] [blame] | 43 | exit 1 |
| 44 | fi |
| 45 | |
| 46 | for 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 |
| 55 | done |
| 56 | |
| 57 | if [ -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 |
| 62 | fi |
| 63 | |
Simon Glass | 3ca9e28 | 2023-11-19 08:36:02 -0700 | [diff] [blame] | 64 | if [[ "${tool}" =~ ^(patman|u_boot_pylib)$ ]]; then |
| 65 | # Leave test_util.py and patman test files alone |
| 66 | delete_testfiles= |
| 67 | fi |
| 68 | |
Simon Glass | d6b6448 | 2023-02-23 18:18:05 -0700 | [diff] [blame] | 69 | # Create a temp dir to work in |
| 70 | dir=$(mktemp -d) |
| 71 | |
| 72 | # Copy in some basic files |
| 73 | cp -v tools/${tool}/pyproject.toml ${dir} |
| 74 | cp -v Licenses/gpl-2.0.txt ${dir}/LICENSE |
| 75 | readme="tools/${tool}/README.*" |
| 76 | |
| 77 | # Copy in the README, dropping some Sphinx constructs that PyPi doesn't like |
| 78 | cat ${readme} | sed -E 's/:(doc|ref):`.*`//; /sectionauthor/d; /toctree::/d' \ |
| 79 | > ${dir}/$(basename ${readme}) |
| 80 | |
| 81 | # Copy the top-level Python and doc files |
| 82 | dest=${dir}/src/${tool} |
| 83 | mkdir -p ${dest} |
| 84 | cp -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 |
| 88 | pushd tools/${tool} |
| 89 | for 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} |
| 94 | done |
| 95 | popd |
| 96 | |
| 97 | # Remove cache files that accidentally made it through |
| 98 | find ${dest} -name __pycache__ -type f -exec rm {} \; |
| 99 | find ${dest} -depth -name __pycache__ -exec rmdir 112 \; |
| 100 | |
| 101 | # Remove test files |
Simon Glass | 3ca9e28 | 2023-11-19 08:36:02 -0700 | [diff] [blame] | 102 | if [ -n "${delete_testfiles}" ]; then |
| 103 | rm -rfv ${dest}/*test* |
| 104 | fi |
Simon Glass | d6b6448 | 2023-02-23 18:18:05 -0700 | [diff] [blame] | 105 | |
| 106 | mkdir ${dir}/tests |
| 107 | cd ${dir} |
| 108 | |
| 109 | # Make sure the tools are up to date |
| 110 | python3 -m pip install --upgrade build |
| 111 | python3 -m pip install --upgrade twine |
| 112 | |
| 113 | # Build the PyPi package |
| 114 | python3 -m build |
| 115 | |
| 116 | echo "Completed build of ${tool}" |
| 117 | |
| 118 | # Use --skip-existing to work even if the version is already present |
| 119 | if [ -n "${upload}" ]; then |
| 120 | echo "Uploading from ${dir}" |
| 121 | python3 -m twine upload ${repo} -u __token__ dist/* |
| 122 | echo "Completed upload of ${tool}" |
| 123 | fi |
| 124 | |
| 125 | rm -rf "${dir}" |
| 126 | |
| 127 | echo -e "done\n\n" |