imapfilter convenience wrapper

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. mkit_import build
  5. mkit_import target
  6. pystuff__main() {
  7. #
  8. # Build setup.py and mkvenv.sh
  9. #
  10. local ModName # Python module name
  11. local ProjVersion # MKit project version
  12. local Skel # setup.py skeleton
  13. local MkvenvDir # venv maker: target venv directory
  14. local MkvenvScript # venv maker: script name
  15. local MkvenvPost # venv maker: post commands
  16. target__run build || die
  17. Skel="$(ini 1value "dist:pystuff")"
  18. MkvenvDir=$(__pystuff__load_mkvenv_dir)
  19. MkvenvScript=$(__pystuff__load_mkvenv_script)
  20. MkvenvPost=$(__pystuff__load_mkvenv_post)
  21. ProjVersion=$(build__cached semver)
  22. ModName=$(ini 1value macros:__VDK_PYLIB_MODNAME__)
  23. build__fact "pystuff/mkvenv_dir" <<<"$MkvenvDir"
  24. build__fact "pystuff/mkvenv_script" <<<"$MkvenvScript"
  25. test -n "$Skel" || die "dist:pystuff not specified"
  26. test -f "$Skel" || die "setup.py template not found: $Skel"
  27. __pystuff__make_setup_py
  28. __pystuff__make_mkvenv
  29. }
  30. __pystuff__find_dist() {
  31. local candidate
  32. candidate=$(
  33. find "dist" -maxdepth 1 -mindepth 1 -printf '%P\n' \
  34. | grep -m1 -e "$ModName-.*tar.gz"
  35. )
  36. test -f "dist/$candidate" && echo "dist/$candidate" && return 0
  37. #shellcheck disable=SC2016
  38. warn "could not find dist package in: dist" \
  39. " .. hint: looking for $ModName-*.tar.gz" \
  40. ' .. hint: did you run `make pystuff` first?'
  41. return 3
  42. }
  43. __pystuff__make_setup_py() {
  44. build__file "$Skel" "setup.py" pystuff
  45. build__recordr dist
  46. build__recordr "$ModName.egg-info"
  47. python3 -m build
  48. }
  49. __pystuff__load_mkvenv_dir() {
  50. #
  51. # Safely load directory for the venv
  52. #
  53. util__loadenv PYSTUFF__VENV_DIR && return 0
  54. plugin__option_single "dir" && return 0
  55. echo venv
  56. }
  57. __pystuff__load_mkvenv_script() {
  58. #
  59. # Safely load command for the venv
  60. #
  61. util__loadenv PYSTUFF__VENV_SCRIPT && return 0
  62. plugin__option_single "mkvenv_script" && return 0
  63. echo mkvenv.sh
  64. }
  65. __pystuff__load_mkvenv_post() {
  66. #
  67. # Safely load command for the venv
  68. #
  69. util__loadenv PYSTUFF__VENV_POST && return 0
  70. plugin__option_multi "mkvenv_post" && return 0
  71. }
  72. __pystuff__q() {
  73. #
  74. # Quote command $@
  75. #
  76. printf '%q ' "$@";
  77. echo
  78. }
  79. __pystuff__make_mkvenv() {
  80. #
  81. # Create the mkvenv.sh script
  82. #
  83. local dist
  84. local req
  85. dist=$(__pystuff__find_dist) \
  86. || die "could not find dist"
  87. build__record "$MkvenvScript"
  88. {
  89. echo "#!/bin/sh -e"
  90. echo "#"
  91. echo "# created by MKit $MKIT_VERSION pystuff for $ModName-$ProjVersion"
  92. echo "#"
  93. echo ""
  94. __pystuff__q python3 -m venv "$MkvenvDir"
  95. __pystuff__q . "$MkvenvDir/bin/activate"
  96. python3 "setup.py" --requires \
  97. | grep . \
  98. | while read -r req; do
  99. __pystuff__q python3 -m pip install --force-reinstall "$req"
  100. done
  101. __pystuff__q python3 -m pip install --force-reinstall "$dist"
  102. echo ""
  103. echo "echo >&2 'Virtual environment should be ready. run following command:'"
  104. echo "echo >&2 ''"
  105. echo "echo >&2 ' source venv/bin/activate'"
  106. echo "echo >&2 ''"
  107. echo "echo >&2 'to activate it.'"
  108. test -n "$MkvenvPost" && {
  109. echo
  110. echo "#"
  111. echo "# user POST action ([options:pystuff:mkvenv_post]"
  112. echo "#"
  113. echo "$MkvenvPost"
  114. }
  115. } | sed 's/ *$//' >"$MkvenvScript"
  116. bash -n "$MkvenvScript" || die "script has syntax errors"
  117. chmod +x "$MkvenvScript"
  118. }