header.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. # The following bash functions are from
  3. # https://github.com/travis-ci/travis-build/tree/master/lib/travis/build/bash
  4. travis_wait() {
  5. local timeout="${1}"
  6. if [[ "${timeout}" =~ ^[0-9]+$ ]]; then
  7. shift
  8. else
  9. timeout=20
  10. fi
  11. local cmd=("${@}")
  12. local log_file="travis_wait_${$}.log"
  13. "${cmd[@]}" &>"${log_file}" &
  14. local cmd_pid="${!}"
  15. travis_jigger "${!}" "${timeout}" "${cmd[@]}" &
  16. local jigger_pid="${!}"
  17. local result
  18. {
  19. wait "${cmd_pid}" 2>/dev/null
  20. result="${?}"
  21. ps -p"${jigger_pid}" &>/dev/null && kill "${jigger_pid}"
  22. }
  23. if [[ "${result}" -eq 0 ]]; then
  24. printf "\\n${ANSI_GREEN}The command %s exited with ${result}.${ANSI_RESET}\\n" "${cmd[*]}"
  25. else
  26. printf "\\n${ANSI_RED}The command %s exited with ${result}.${ANSI_RESET}\\n" "${cmd[*]}"
  27. fi
  28. echo -e "\\n${ANSI_GREEN}Log:${ANSI_RESET}\\n"
  29. cat "${log_file}"
  30. return "${result}"
  31. }
  32. travis_jigger() {
  33. local cmd_pid="${1}"
  34. shift
  35. local timeout="${1}"
  36. shift
  37. local count=0
  38. echo -e "\\n"
  39. while [[ "${count}" -lt "${timeout}" ]]; do
  40. count="$((count + 1))"
  41. echo -ne "Still running (${count} of ${timeout}): ${*}\\r"
  42. sleep 60
  43. done
  44. echo -e "\\n${ANSI_RED}Timeout (${timeout} minutes) reached. Terminating \"${*}\"${ANSI_RESET}\\n"
  45. kill -9 "${cmd_pid}"
  46. }
  47. travis_retry() {
  48. local result=0
  49. local count=1
  50. while [[ "${count}" -le 3 ]]; do
  51. [[ "${result}" -ne 0 ]] && {
  52. echo -e "\\n${ANSI_RED}The command \"${*}\" failed. Retrying, ${count} of 3.${ANSI_RESET}\\n" >&2
  53. }
  54. "${@}" && { result=0 && break; } || result="${?}"
  55. count="$((count + 1))"
  56. sleep 1
  57. done
  58. [[ "${count}" -gt 3 ]] && {
  59. echo -e "\\n${ANSI_RED}The command \"${*}\" failed 3 times.${ANSI_RESET}\\n" >&2
  60. }
  61. return "${result}"
  62. }
  63. travis_fold() {
  64. local action="${1}"
  65. local name="${2}"
  66. echo -en "travis_fold:${action}:${name}\\r${ANSI_CLEAR}"
  67. }
  68. travis_nanoseconds() {
  69. local cmd='date'
  70. local format='+%s%N'
  71. if hash gdate >/dev/null 2>&1; then
  72. cmd='gdate'
  73. elif [[ "${TRAVIS_OS_NAME}" == osx ]]; then
  74. format='+%s000000000'
  75. fi
  76. "${cmd}" -u "${format}"
  77. }
  78. travis_time_start() {
  79. TRAVIS_TIMER_ID="$(printf %08x $((RANDOM * RANDOM)))"
  80. TRAVIS_TIMER_START_TIME="$(travis_nanoseconds)"
  81. export TRAVIS_TIMER_ID TRAVIS_TIMER_START_TIME
  82. echo -en "travis_time:start:$TRAVIS_TIMER_ID\\r${ANSI_CLEAR}"
  83. }
  84. travis_time_finish() {
  85. local result="${?}"
  86. local travis_timer_end_time
  87. local event="${1}"
  88. travis_timer_end_time="$(travis_nanoseconds)"
  89. local duration
  90. duration="$((travis_timer_end_time - TRAVIS_TIMER_START_TIME))"
  91. echo -en "travis_time:end:${TRAVIS_TIMER_ID}:start=${TRAVIS_TIMER_START_TIME},finish=${travis_timer_end_time},duration=${duration},event=${event}\\r${ANSI_CLEAR}"
  92. return "${result}"
  93. }
  94. fold_cmd() {
  95. local name="${1}"
  96. shift
  97. local cmd=("${@}")
  98. travis_fold start ${name}
  99. "${cmd[@]}"
  100. local result="${?}"
  101. travis_fold end ${name}
  102. return "${result}"
  103. }
  104. title() {
  105. echo -e "\033[1m\033[33m${1}\033[0m"
  106. }
  107. subtitle() {
  108. echo -e "\033[34m${1}\033[0m"
  109. }
  110. # TODO: rewrite this function
  111. fold_timer_cmd() {
  112. local name="${1}"
  113. local title="${2}"
  114. shift 2
  115. local cmd=("${@}")
  116. echo -en "travis_fold:start:${name}\\r${ANSI_CLEAR}"
  117. title "${title}"
  118. travis_time_start
  119. "${cmd[@]}"
  120. local result="${?}"
  121. travis_time_finish
  122. echo -en "travis_fold:end:${name}\\r${ANSI_CLEAR}"
  123. return "${result}"
  124. }