init.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env bash
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-node-tools
  9. # https://gitlab.com/mmgen/mmgen-node-tools
  10. RED="\e[31;1m" GREEN="\e[32;1m" YELLOW="\e[33;1m" BLUE="\e[34;1m" RESET="\e[0m"
  11. set -e
  12. set -o errtrace
  13. set -o functrace
  14. trap 'echo -e "${GREEN}Exiting at user request$RESET"; exit' INT
  15. trap 'echo -e "${RED}Node Tools test suite initialization exited with error (line $BASH_LINENO) $RESET"' ERR
  16. umask 0022
  17. for i in '-c' '-f'; do
  18. stat $i %i / >/dev/null 2>&1 && stat_fmt_opt=$i
  19. done
  20. [ "$stat_fmt_opt" ] || { echo 'No suitable ‘stat’ binary found. Cannot proceed'; exit; }
  21. STDOUT_DEVNULL='>/dev/null'
  22. STDERR_DEVNULL='2>/dev/null'
  23. PROGNAME=$(basename $0)
  24. while getopts hv OPT
  25. do
  26. case "$OPT" in
  27. h) printf " %-16s Initialize the MMGen Node Tools test suite\n" "${PROGNAME}:"
  28. echo " USAGE: $PROGNAME"
  29. echo " OPTIONS: '-h' Print this help message"
  30. echo " -v Be more verbose"
  31. exit ;;
  32. v) VERBOSE=1 STDOUT_DEVNULL='' STDERR_DEVNULL='' ;;
  33. *) exit ;;
  34. esac
  35. done
  36. shift $((OPTIND-1))
  37. wallet_repo='../mmgen-wallet'
  38. die() { echo -e ${YELLOW}ERROR: $1$RESET; false; }
  39. becho() { echo -e $BLUE$1$RESET; }
  40. check_mmgen_repo() {
  41. ( cd $wallet_repo; python3 ./setup.py --url | grep -iq 'mmgen' )
  42. }
  43. build_mmgen_extmod() {
  44. (
  45. cd $wallet_repo
  46. eval "python3 ./setup.py build_ext --inplace $STDOUT_DEVNULL $STDERR_DEVNULL"
  47. )
  48. }
  49. create_dir_links() {
  50. for link_name in 'mmgen' 'scripts'; do
  51. target="$wallet_repo/$link_name"
  52. if [ -L $link_name ]; then
  53. [ "$(realpath --relative-to=. $link_name 2>/dev/null)" == $target ] || {
  54. [ "$VERBOSE" ] && echo "Removing broken symlink '$link_name'"
  55. rm $link_name
  56. }
  57. elif [ -e $link_name ]; then
  58. die "'$link_name' is not a symbolic link. Please remove or relocate it and re-run this script"
  59. fi
  60. if [ ! -e $link_name ]; then
  61. [ "$VERBOSE" ] && echo "Creating symlink: $link_name"
  62. ln -s $target
  63. fi
  64. done
  65. }
  66. delete_old_stuff() {
  67. rm -rf test/unit_tests.py
  68. }
  69. create_test_links() {
  70. paths='
  71. test/include symbolic
  72. test/overlay/__init__.py symbolic
  73. test/overlay/fakemods/mmgen symbolic
  74. test/__init__.py symbolic
  75. test/clean.py symbolic
  76. test/cmdtest.py hard
  77. test/modtest.py hard
  78. test/test-release.sh symbolic
  79. test/cmdtest_d/common.py symbolic
  80. test/cmdtest_d/ct_base.py symbolic
  81. cmds/mmgen-regtest symbolic
  82. '
  83. while read path type; do
  84. [ "$path" ] || continue
  85. pfx=$(echo $path | sed -r 's/[^/]//g' | sed 's/\//..\//g')
  86. symlink_arg=$(if [ $type == 'symbolic' ]; then echo -s; fi)
  87. target="$wallet_repo/$path"
  88. if [ ! -e "$target" ]; then
  89. echo "Target path $target is missing! Cannot proceed"
  90. exit 1
  91. fi
  92. fs="%-8s %-16s %s -> %s\n"
  93. if [ $type == 'hard' ]; then
  94. if [ -L $path ]; then
  95. [ "$VERBOSE" ] && printf "$fs" "Deleting" "symbolic link:" $path $target
  96. rm -rf $path
  97. elif [ -e $path ]; then
  98. if [ "$(stat $stat_fmt_opt %i $path)" -ne "$(stat $stat_fmt_opt %i $target)" ]; then
  99. [ "$VERBOSE" ] && printf "$fs" "Deleting" "stale hard link:" $path "?"
  100. rm -rf $path
  101. fi
  102. fi
  103. fi
  104. if [ ! -e $path ]; then # link is either absent or a broken symlink
  105. [ "$VERBOSE" ] && printf "$fs" "Creating" "$type link:" $path $target
  106. ( cd "$(dirname $path)" && ln -f $symlink_arg $pfx$target )
  107. fi
  108. done <<<$paths
  109. }
  110. becho 'Initializing MMGen Node Tools Test Suite'
  111. delete_old_stuff
  112. check_mmgen_repo || die "MMGen Wallet repository not found at $wallet_repo!"
  113. build_mmgen_extmod
  114. [ "$VERBOSE" ] && becho 'Creating links to mmgen-wallet repo'
  115. create_dir_links
  116. create_test_links
  117. [ "$VERBOSE" ] && becho 'OK'
  118. true