make_pip.sh 2.7 KB

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