version_constraint.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/bash
  2. ## configuration ##
  3. # NOTE: test data versions are extremely brittle - these will need adjustments routinely.
  4. # usually, these values will be the reported failed 'actual'. - `pacman -Syu` first!
  5. readonly OPENEXR_VER_MIN_PR_1=3
  6. readonly OPENEXR_VER_MIN_PR_2=3.2
  7. readonly OPENEXR_VER_MIN_PR_3=3.2.1
  8. readonly OPENEXR_VER_MIN_FULL=3.2.1-1
  9. readonly OPENSHADINGLANGUAGE_VER_MIN_PR_1=1
  10. readonly OPENSHADINGLANGUAGE_VER_MIN_PR_2=1.12
  11. readonly OPENSHADINGLANGUAGE_VER_MIN_PR_3=1.12.14
  12. readonly OPENSHADINGLANGUAGE_VER_MIN_PR_4=1.12.14.0
  13. readonly OPENSHADINGLANGUAGE_VER_MIN_FULL=1.12.14.0-2
  14. readonly WEBKIT2GTK_VER_MIN_FULL=2.42.4-2
  15. readonly QT5BASE_VER_MIN_PR_3=5.15.12
  16. readonly QT5BASE_VER_MIN_FULL=5.15.12+kde+r147-1
  17. readonly OPENCASCADE_VER_MIN_FULL=1:7.7.2-4
  18. readonly RED='\033[01;31m'
  19. readonly GREEN='\033[00;32m'
  20. readonly AQUA='\033[00;36m'
  21. readonly CEND='\033[00m'
  22. ## main function for PKGBUILDs ##
  23. _version_constraint() # (dep_pkgname [precision])
  24. {
  25. Log() { [[ "${FUNCNAME[2]}" == package ]] && echo "$@" >&2 || : ; }
  26. local dep_pkgname=$1
  27. declare -i req_precision=$2
  28. local full_version=$(pacman -S --print-format='%v' ${dep_pkgname} 2> /dev/null | tail -n 1)
  29. local n_dots=$(tmp=${full_version%-*} ; tmp=${tmp//[^\.]} ; echo "${#tmp}" ;)
  30. local def_precision=$(( n_dots + 1 ))
  31. local is_prec_valid=$(( req_precision > 0 && req_precision <= def_precision ))
  32. local precision=$((( is_prec_valid )) && echo ${req_precision} || echo ${def_precision})
  33. local epoch_rx='[0-9]+:'
  34. local pkgver_rx='[0-9A-Za-z_]+'
  35. pkgver_rx=$(sed 's|\]|\+]|' <<<${pkgver_rx}) # according to the wiki, '+' is not allowed,
  36. # but some pkgver have it (eg: 5.15.10+kde+r130)
  37. local subver_rx='\.'${pkgver_rx}
  38. local pkgrel_rx='[0-9]+'
  39. local garbage_rx='[^0-9].*'
  40. local capture_rx=${pkgver_rx}
  41. for (( n_dots=1 ; n_dots < precision ; ++n_dots )) ; do capture_rx+=${subver_rx} ; done ;
  42. local epoch version pkgrel has_dot_char version_min version_max constraint_string
  43. declare -i subver subver_inc pkgrel_inc
  44. if [[ "${full_version}" =~ ^(${epoch_rx})*(${capture_rx})(${subver_rx})*-(${pkgrel_rx}).*$ ]]
  45. then epoch=${BASH_REMATCH[1]} # optional epoch
  46. version=${BASH_REMATCH[2]} # pkgver cut to the requested precision
  47. #unused=${BASH_REMATCH[3]} # discarded pkgver segments
  48. pkgrel=${BASH_REMATCH[4]} # pkgrel with non-numerics right-trimmed
  49. has_dot_char=$([[ "${version}" =~ \. ]] ; echo $(( ! $? )) ; )
  50. subver=$(sed "s|${garbage_rx}||" <<<${version##*.}) # right-trim from any non-numeric
  51. version=$( (( has_dot_char )) && echo ${version%.*}.${subver} || echo ${subver} )
  52. version=${epoch}${version}
  53. subver_inc=$(( subver + 1 ))
  54. pkgrel_inc=$(( pkgrel + 1 ))
  55. version_min=$( (( ! is_prec_valid )) && echo ${full_version%-*}-${pkgrel} || \
  56. echo ${version} )
  57. version_max=$( ( (( ! is_prec_valid )) && echo ${full_version%-*}-${pkgrel_inc} ) || \
  58. ( [[ "${version}" =~ \. ]] && echo ${version%.*}.${subver_inc} ) || \
  59. echo ${subver_inc} )
  60. constraint_string="${dep_pkgname}>=${version_min} ${dep_pkgname}<${version_max}"
  61. Log "Applied version constraint: '${constraint_string}'"
  62. else Log "ERROR: in _version_constraint() parsing: dep_pkgname='${dep_pkgname}' full_version='${full_version}'"
  63. exit 1
  64. fi
  65. unset -f Log
  66. echo -n "${constraint_string}"
  67. }
  68. ## tests ##
  69. VerIncrement() # (version_string)
  70. {
  71. # NOTE This is not the same parsing logic as _version_constraint().
  72. # It is intended to be as simple as possible, only for the mock data.
  73. # Write a new test to verify anything novel or peculiar.
  74. local version_string=$1
  75. local has_pkgrel=$([[ "$1" =~ \- ]] ; echo $((! $?)) ;)
  76. local split_char=$((( has_pkgrel )) && echo '-' || echo '.')
  77. local has_split_char=$([[ "$1" =~ "${split_char}" ]] ; echo $(( ! $? )) ; )
  78. local unchanged_segment=${version_string%${split_char}*}
  79. declare -i incremented_segment=$(( ${version_string##*${split_char}} + 1 ))
  80. (( ! has_split_char )) || echo -n ${unchanged_segment}${split_char}
  81. echo ${incremented_segment}
  82. }
  83. readonly OPENEXR_VER_MAX_PR_1=$( VerIncrement ${OPENEXR_VER_MIN_PR_1} )
  84. readonly OPENEXR_VER_MAX_PR_2=$( VerIncrement ${OPENEXR_VER_MIN_PR_2} )
  85. readonly OPENEXR_VER_MAX_PR_3=$( VerIncrement ${OPENEXR_VER_MIN_PR_3} )
  86. readonly OPENEXR_VER_MAX_FULL=$( VerIncrement ${OPENEXR_VER_MIN_FULL} )
  87. readonly OPENSHADINGLANGUAGE_VER_MAX_PR_1=$(VerIncrement ${OPENSHADINGLANGUAGE_VER_MIN_PR_1})
  88. readonly OPENSHADINGLANGUAGE_VER_MAX_PR_2=$(VerIncrement ${OPENSHADINGLANGUAGE_VER_MIN_PR_2})
  89. readonly OPENSHADINGLANGUAGE_VER_MAX_PR_3=$(VerIncrement ${OPENSHADINGLANGUAGE_VER_MIN_PR_3})
  90. readonly OPENSHADINGLANGUAGE_VER_MAX_PR_4=$(VerIncrement ${OPENSHADINGLANGUAGE_VER_MIN_PR_4})
  91. readonly OPENSHADINGLANGUAGE_VER_MAX_FULL=$(VerIncrement ${OPENSHADINGLANGUAGE_VER_MIN_FULL})
  92. readonly WEBKIT2GTK_VER_MAX_FULL=$( VerIncrement ${WEBKIT2GTK_VER_MIN_FULL} )
  93. readonly QT5BASE_VER_MAX_PR_3=$( VerIncrement ${QT5BASE_VER_MIN_PR_3} )
  94. readonly QT5BASE_VER_MAX_FULL=$( VerIncrement ${QT5BASE_VER_MIN_FULL} )
  95. readonly OPENCASCADE_VER_MAX_FULL=$( VerIncrement ${OPENCASCADE_VER_MIN_FULL} )
  96. TestVersionConstraint() # ("pkgname" "precision" "expected_res")
  97. {
  98. local pkgname="$1"
  99. local precision="$2"
  100. local expected_res="$3"
  101. local actual_res="$(_version_constraint "${pkgname}" "${precision}")"
  102. if [[ "${actual_res}" == "${expected_res}" ]]
  103. then echo -e "${GREEN}PASS <- ${pkgname}${CEND}" ; return 0 ;
  104. else echo -e "${RED}FAIL <- ${pkgname}${CEND}"
  105. echo -e "${AQUA} expected='${expected_res}'\n actual ='${actual_res}'${CEND}" ; return 1 ;
  106. fi
  107. }
  108. RunTests()
  109. {
  110. set -e
  111. precision=1 ; echo -e "\n=== precision='${precision}' ===" ;
  112. TestVersionConstraint 'openexr' "${precision}" "openexr>=${OPENEXR_VER_MIN_PR_1} openexr<${OPENEXR_VER_MAX_PR_1}"
  113. TestVersionConstraint 'openshadinglanguage' "${precision}" "openshadinglanguage>=${OPENSHADINGLANGUAGE_VER_MIN_PR_1} openshadinglanguage<${OPENSHADINGLANGUAGE_VER_MAX_PR_1}"
  114. precision=2 ; echo -e "\n=== precision='${precision}' ===" ;
  115. TestVersionConstraint 'openexr' "${precision}" "openexr>=${OPENEXR_VER_MIN_PR_2} openexr<${OPENEXR_VER_MAX_PR_2}"
  116. TestVersionConstraint 'openshadinglanguage' "${precision}" "openshadinglanguage>=${OPENSHADINGLANGUAGE_VER_MIN_PR_2} openshadinglanguage<${OPENSHADINGLANGUAGE_VER_MAX_PR_2}"
  117. precision=3 ; echo -e "\n=== precision='${precision}' ===" ;
  118. TestVersionConstraint 'openexr' "${precision}" "openexr>=${OPENEXR_VER_MIN_PR_3} openexr<${OPENEXR_VER_MAX_PR_3}"
  119. TestVersionConstraint 'openshadinglanguage' "${precision}" "openshadinglanguage>=${OPENSHADINGLANGUAGE_VER_MIN_PR_3} openshadinglanguage<${OPENSHADINGLANGUAGE_VER_MAX_PR_3}"
  120. precision=4 ; echo -e "\n=== precision='${precision}' ===" ;
  121. TestVersionConstraint 'openexr' "${precision}" "openexr>=${OPENEXR_VER_MIN_FULL} openexr<${OPENEXR_VER_MAX_FULL}"
  122. TestVersionConstraint 'openshadinglanguage' "${precision}" "openshadinglanguage>=${OPENSHADINGLANGUAGE_VER_MIN_PR_4} openshadinglanguage<${OPENSHADINGLANGUAGE_VER_MAX_PR_4}"
  123. precision= ; echo -e "\n=== full precision (default) ===" ;
  124. TestVersionConstraint 'openexr' "${precision}" "openexr>=${OPENEXR_VER_MIN_FULL} openexr<${OPENEXR_VER_MAX_FULL}"
  125. TestVersionConstraint 'openshadinglanguage' "${precision}" "openshadinglanguage>=${OPENSHADINGLANGUAGE_VER_MIN_FULL} openshadinglanguage<${OPENSHADINGLANGUAGE_VER_MAX_FULL}"
  126. precision= ; echo -e "\n=== non-existent-package ===" ;
  127. TestVersionConstraint 'non-existent-package' "${precision}" ''
  128. precision= ; echo -e "\n=== specific repo ===" ;
  129. TestVersionConstraint 'extra/webkit2gtk' "${precision}" "extra/webkit2gtk>=${WEBKIT2GTK_VER_MIN_FULL} extra/webkit2gtk<${WEBKIT2GTK_VER_MAX_FULL}"
  130. precision=3 ; echo -e "\n=== non-numeric version ===" ;
  131. TestVersionConstraint 'qt5-base' "${precision}" "qt5-base>=${QT5BASE_VER_MIN_PR_3} qt5-base<${QT5BASE_VER_MAX_PR_3}"
  132. precision= ; echo -e "\n=== non-numeric version - full precision (default) ===" ;
  133. TestVersionConstraint 'qt5-base' "${precision}" "qt5-base>=${QT5BASE_VER_MIN_FULL} qt5-base<${QT5BASE_VER_MAX_FULL}"
  134. precision= ; echo -e "\n=== version with epoch ===" ;
  135. TestVersionConstraint 'opencascade' "${precision}" "opencascade>=${OPENCASCADE_VER_MIN_FULL} opencascade<${OPENCASCADE_VER_MAX_FULL}"
  136. }
  137. RunTests