init.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. create_test_links() {
  67. paths='
  68. test/include symbolic
  69. test/overlay/__init__.py symbolic
  70. test/overlay/fakemods/mmgen symbolic
  71. test/__init__.py symbolic
  72. test/clean.py symbolic
  73. test/cmdtest.py hard
  74. test/unit_tests.py hard
  75. test/test-release.sh symbolic
  76. test/cmdtest_py_d/common.py symbolic
  77. test/cmdtest_py_d/ct_base.py symbolic
  78. cmds/mmgen-regtest symbolic
  79. '
  80. while read path type; do
  81. [ "$path" ] || continue
  82. pfx=$(echo $path | sed -r 's/[^/]//g' | sed 's/\//..\//g')
  83. symlink_arg=$(if [ $type == 'symbolic' ]; then echo -s; fi)
  84. target="$wallet_repo/$path"
  85. if [ ! -e "$target" ]; then
  86. echo "Target path $target is missing! Cannot proceed"
  87. exit 1
  88. fi
  89. fs="%-8s %-16s %s -> %s\n"
  90. if [ $type == 'hard' ]; then
  91. if [ -L $path ]; then
  92. [ "$VERBOSE" ] && printf "$fs" "Deleting" "symbolic link:" $path $target
  93. rm -rf $path
  94. elif [ -e $path ]; then
  95. if [ "$(stat $stat_fmt_opt %i $path)" -ne "$(stat $stat_fmt_opt %i $target)" ]; then
  96. [ "$VERBOSE" ] && printf "$fs" "Deleting" "stale hard link:" $path "?"
  97. rm -rf $path
  98. fi
  99. fi
  100. fi
  101. if [ ! -e $path ]; then # link is either absent or a broken symlink
  102. [ "$VERBOSE" ] && printf "$fs" "Creating" "$type link:" $path $target
  103. ( cd "$(dirname $path)" && ln -f $symlink_arg $pfx$target )
  104. fi
  105. done <<<$paths
  106. }
  107. becho 'Initializing MMGen Node Tools Test Suite'
  108. check_mmgen_repo || die "MMGen Wallet repository not found at $wallet_repo!"
  109. build_mmgen_extmod
  110. [ "$VERBOSE" ] && becho 'Creating links to mmgen-wallet repo'
  111. create_dir_links
  112. create_test_links
  113. [ "$VERBOSE" ] && becho 'OK'
  114. true