init.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 hcv 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 " -c Create links from mmgen-wallet ‘cmds’ subdirectory"
  31. echo " -v Be more verbose"
  32. exit ;;
  33. v) VERBOSE=1 STDOUT_DEVNULL='' STDERR_DEVNULL='' ;;
  34. c) CMD_LINKS=1 ;;
  35. *) exit ;;
  36. esac
  37. done
  38. shift $((OPTIND-1))
  39. wallet_repo='../mmgen-wallet'
  40. die() { echo -e ${YELLOW}ERROR: $1$RESET; false; }
  41. becho() { echo -e $BLUE$1$RESET; }
  42. check_mmgen_repo() {
  43. ( cd $wallet_repo; python3 ./setup.py --url | grep -iq 'mmgen' )
  44. }
  45. build_mmgen_extmod() {
  46. (
  47. cd $wallet_repo
  48. eval "python3 ./setup.py build_ext --inplace $STDOUT_DEVNULL $STDERR_DEVNULL"
  49. )
  50. }
  51. create_dir_links() {
  52. for link_name in 'mmgen' 'scripts'; do
  53. target="$wallet_repo/$link_name"
  54. if [ -L $link_name ]; then
  55. [ "$(realpath --relative-to=. $link_name 2>/dev/null)" == $target ] || {
  56. [ "$VERBOSE" ] && echo "Removing broken symlink '$link_name'"
  57. rm $link_name
  58. }
  59. elif [ -e $link_name ]; then
  60. die "'$link_name' is not a symbolic link. Please remove or relocate it and re-run this script"
  61. fi
  62. if [ ! -e $link_name ]; then
  63. [ "$VERBOSE" ] && echo "Creating symlink: $link_name"
  64. ln -s $target
  65. fi
  66. done
  67. }
  68. delete_old_stuff() {
  69. rm -rf test/unit_tests.py
  70. }
  71. create_test_links() {
  72. paths='
  73. test/include symbolic
  74. test/overlay/__init__.py symbolic
  75. test/overlay/fakemods/mmgen symbolic
  76. test/__init__.py symbolic
  77. test/clean.py symbolic
  78. test/cmdtest.py hard
  79. test/modtest.py hard
  80. test/test-release.sh symbolic
  81. test/cmdtest_d/common.py symbolic
  82. test/cmdtest_d/ct_base.py symbolic
  83. cmds/mmgen-regtest symbolic
  84. '
  85. while read path type; do
  86. [ "$path" ] || continue
  87. pfx=$(echo $path | sed -r 's/[^/]//g' | sed 's/\//..\//g')
  88. symlink_arg=$(if [ $type == 'symbolic' ]; then echo -s; fi)
  89. target="$wallet_repo/$path"
  90. if [ ! -e "$target" ]; then
  91. echo "Target path $target is missing! Cannot proceed"
  92. exit 1
  93. fi
  94. fs="%-8s %-16s %s -> %s\n"
  95. if [ $type == 'hard' ]; then
  96. if [ -L $path ]; then
  97. [ "$VERBOSE" ] && printf "$fs" "Deleting" "symbolic link:" $path $target
  98. rm -rf $path
  99. elif [ -e $path ]; then
  100. if [ "$(stat $stat_fmt_opt %i $path)" -ne "$(stat $stat_fmt_opt %i $target)" ]; then
  101. [ "$VERBOSE" ] && printf "$fs" "Deleting" "stale hard link:" $path "?"
  102. rm -rf $path
  103. fi
  104. fi
  105. fi
  106. if [ ! -e $path ]; then # link is either absent or a broken symlink
  107. [ "$VERBOSE" ] && printf "$fs" "Creating" "$type link:" $path $target
  108. ( cd "$(dirname $path)" && ln -f $symlink_arg $pfx$target )
  109. fi
  110. done <<<$paths
  111. }
  112. create_cmd_links() {
  113. [ "$VERBOSE" ] && becho 'Creating links to mmgen-wallet repo ‘cmds’ subdirectory'
  114. (
  115. filenames=$(cd $wallet_repo/cmds && ls)
  116. cd cmds
  117. for filename in $filenames; do
  118. [ -e $filename ] || ln -s "../$wallet_repo/cmds/$filename"
  119. done
  120. )
  121. }
  122. becho 'Initializing MMGen Node Tools Test Suite'
  123. delete_old_stuff
  124. check_mmgen_repo || die "MMGen Wallet repository not found at $wallet_repo!"
  125. build_mmgen_extmod
  126. [ "$VERBOSE" ] && becho 'Creating links to mmgen-wallet repo'
  127. create_dir_links
  128. create_test_links
  129. [ "$CMD_LINKS" ] && create_cmd_links
  130. [ "$VERBOSE" ] && becho 'OK'
  131. true