init.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. test/cmdtest_d/runner.py symbolic
  84. test/cmdtest_d/group_mgr.py symbolic
  85. cmds/mmgen-regtest symbolic
  86. '
  87. while read path type; do
  88. [ "$path" ] || continue
  89. pfx=$(echo $path | sed -r 's/[^/]//g' | sed 's/\//..\//g')
  90. symlink_arg=$(if [ $type == 'symbolic' ]; then echo -s; fi)
  91. target="$wallet_repo/$path"
  92. if [ ! -e "$target" ]; then
  93. echo "Target path $target is missing! Cannot proceed"
  94. exit 1
  95. fi
  96. fs="%-8s %-16s %s -> %s\n"
  97. if [ $type == 'hard' ]; then
  98. if [ -L $path ]; then
  99. [ "$VERBOSE" ] && printf "$fs" "Deleting" "symbolic link:" $path $target
  100. rm -rf $path
  101. elif [ -e $path ]; then
  102. if [ "$(stat $stat_fmt_opt %i $path)" -ne "$(stat $stat_fmt_opt %i $target)" ]; then
  103. [ "$VERBOSE" ] && printf "$fs" "Deleting" "stale hard link:" $path "?"
  104. rm -rf $path
  105. fi
  106. fi
  107. fi
  108. if [ ! -e $path ]; then # link is either absent or a broken symlink
  109. [ "$VERBOSE" ] && printf "$fs" "Creating" "$type link:" $path $target
  110. ( cd "$(dirname $path)" && ln -f $symlink_arg $pfx$target )
  111. fi
  112. done <<<$paths
  113. }
  114. create_cmd_links() {
  115. [ "$VERBOSE" ] && becho 'Creating links to mmgen-wallet repo ‘cmds’ subdirectory'
  116. (
  117. filenames=$(cd $wallet_repo/cmds && ls)
  118. cd cmds
  119. for filename in $filenames; do
  120. [ -e $filename ] || ln -s "../$wallet_repo/cmds/$filename"
  121. done
  122. )
  123. }
  124. becho 'Initializing MMGen Node Tools Test Suite'
  125. delete_old_stuff
  126. check_mmgen_repo || die "MMGen Wallet repository not found at $wallet_repo!"
  127. build_mmgen_extmod
  128. [ "$VERBOSE" ] && becho 'Creating links to mmgen-wallet repo'
  129. create_dir_links
  130. create_test_links
  131. [ "$CMD_LINKS" ] && create_cmd_links
  132. [ "$VERBOSE" ] && becho 'OK'
  133. true