Script to trigger re-build of vimb - https://github.com/fanglingsu/vimb

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/bin/bash
  2. TRIGGER_COPR_CONFIG=${TRIGGER_COPR_CONFIG:-}
  3. TRIGGER_COPR_PKGNAME=${TRIGGER_COPR_PKGNAME:-}
  4. warn() {
  5. echo "$1" >&2
  6. }
  7. die() {
  8. warn "fatal: $1"
  9. exit 3
  10. }
  11. usage() {
  12. warn "usage: $0 [options] MODE"
  13. warn ""
  14. warn "Options:"
  15. warn ""
  16. warn " -b BRN build from branch BRN (default: last tag)"
  17. warn " -c COPR_PROJECT COPR project name"
  18. warn " -C CONF config file"
  19. warn " -n dry mode, don't do anything (just show"
  20. warn " what would be done)"
  21. warn " -r REL use REL as Release number in SPEC file"
  22. warn " -v VER use VER as Version number in SPEC file"
  23. warn " -u URL use URL as base (to get last tag and"
  24. warn " compose Source0 in SPEC file)"
  25. warn ""
  26. warn "If -b is not used, build is launched from last tag available"
  27. warn "in the GitHub repo. In that case, Release is '1' and Version"
  28. warn "is deduced from the tag by removing the initial 'v'."
  29. warn ""
  30. warn "If -b is used, the project repo is temporarily cloned, and"
  31. warn "both Version and Release are found by consulting git-describe"
  32. warn "on the specified branch."
  33. warn ""
  34. warn "If MODE is 'scratch' (default), Release is pre-fixed by string"
  35. warn "'0.scratch.' and build is triggered in scratch COPR project."
  36. exit 2
  37. }
  38. last_version() {
  39. git ls-remote --tag "$UrlBase" \
  40. | grep '/tags/' \
  41. | cut -d/ -f3 \
  42. | grep -v '[^0-9a-z.]' \
  43. | sort -V \
  44. | tail -1 \
  45. | sed "s/^v//"
  46. }
  47. mkspec() {
  48. local self_version
  49. local cl_date
  50. self_version="$(basename "$0") $(git describe --tags)"
  51. cl_date=$(LC_ALL=C date +"%a %b %d %Y")
  52. sed -e "
  53. s|__APP_VERSION__|$Version|
  54. s|__APP_RELEASE__|$Release|
  55. s|__APP_URLBASE__|$UrlBase|
  56. s|__APP_BUILDSCRIPT_VERSION__|$self_version|
  57. s|__APP_DATE__|$cl_date|
  58. " <"$PkgName.spec.in"
  59. }
  60. git_guess() {
  61. #
  62. # Print git-guessed $1
  63. #
  64. local what=$1 # what we want (ver|rel)
  65. local describe # full git-describe output
  66. local xtra= # extra part (-N-gHASH)
  67. local num=0 # num. of commits since tag (N)
  68. local sha= # '.g'+sha1 of this commit (gHASH)
  69. describe=$(git describe --tags --always HEAD)
  70. case $describe in
  71. *-*) # v1.2.3-21-g654cba
  72. tag=${describe%%-*} # tag=v1.2.3
  73. xtra=${describe#$tag-} # xtra=-21-g654cba
  74. num=${xtra%%-*} # num=21
  75. sha=.${xtra#$num-} # sha=.g654cba
  76. ;;
  77. *)
  78. tag=$describe
  79. ;;
  80. esac
  81. case $what in
  82. ver) echo "${tag#v}" ;;
  83. rel) echo "$((num + 1))$sha" ;;
  84. esac
  85. }
  86. choose_relpfx() {
  87. #
  88. # Choose COPR project based on $Mode
  89. #
  90. test "$Mode" == scratch && echo 0.scratch.
  91. return 0
  92. }
  93. read_conffile() {
  94. #
  95. # Read item T
  96. #
  97. local what=$1
  98. local fieldn
  99. case $what in
  100. urlbase) fieldn=2 ;;
  101. copr) fieldn=3 ;;
  102. esac
  103. grep -v '^[[:blank:]]*#' "$ConfFile" \
  104. | grep "^ *$Mode\>" \
  105. | sed 's/ */ /g' \
  106. | cut -d' ' -f$fieldn
  107. }
  108. choose_conffile() {
  109. #
  110. # Find config file and echo its name
  111. #
  112. find . -name '*.trigger' \
  113. | cut -d/ -f2 \
  114. | grep .
  115. }
  116. do_action() {
  117. local url
  118. case $Action in
  119. build)
  120. copr build "$CoprProject" "$Tmp/$PkgName.spec"
  121. printf '\a'
  122. ;;
  123. demo)
  124. warn "demo mode active, we would build:"
  125. warn " at $CoprProject"
  126. test -n "$Branch" && warn " using branch $Branch"
  127. test -n "$Branch" || warn " using last tag"
  128. warn " from $UrlBase"
  129. warn " yielding $PkgName-$Version-$Release.*.rpm"
  130. warn ""
  131. warn "===== BEGIN $PkgName.spec ====="
  132. cat "$Tmp/$PkgName.spec"
  133. warn "===== END $PkgName.spec ====="
  134. return 1
  135. ;;
  136. mkspec)
  137. cat "$Tmp/$PkgName.spec"
  138. ;;
  139. rpmstuff)
  140. url=$(
  141. grep -o 'Source.*:.*http.*' "$Tmp/$PkgName.spec" \
  142. | sed "
  143. s/.*http/http/
  144. s/ *$//
  145. s/%{version}/$Version/
  146. "
  147. )
  148. wget --quiet "$url"
  149. cat "$Tmp/$PkgName.spec" > "$PkgName.spec"
  150. ;;
  151. esac
  152. }
  153. main() {
  154. local Version # Version in SPEC file
  155. local Release # Release in SPEC file
  156. local CoprProject # COPR project
  157. local UrlBase # GitHub URL base
  158. local Tmp # our temp
  159. local Branch # branch to use, if empty, tags are considered
  160. local Mode=scratch # implies COPR project and release prefix
  161. local Action=build # what to do
  162. local ConfFile # config file to use
  163. local PkgName # package name
  164. local es=0 # exit status
  165. which copr >/dev/null \
  166. || die "copr not found, try 'sudo install copr-cli'"
  167. Tmp=$(mktemp -d)
  168. while true; do case $1 in
  169. -C) ConfFile=$2; shift 2 || usage ;;
  170. -a) Action=$2; shift 2 || usage ;;
  171. -b) Branch=$2; shift 2 || usage ;;
  172. -c) CoprProject=$2; shift 2 || usage ;;
  173. -u) UrlBase=$2; shift 2 || usage ;;
  174. -r) Release=$2; shift 2 || usage ;;
  175. -v) Version=${2#v}; shift 2 || usage ;;
  176. -n) Action=demo; shift ;;
  177. -*) usage ;;
  178. *) break ;;
  179. esac done
  180. Mode=${1:-$Mode}
  181. case $Action in
  182. build|demo|mkspec|rpmstuff) : ;;
  183. *) usage ;;
  184. esac
  185. test -n "$ConfFile" || ConfFile=$TRIGGER_COPR_CONFIG
  186. test -n "$ConfFile" || ConfFile=$(choose_conffile)
  187. test -n "$ConfFile" || die "could not find config file"
  188. test -r "$ConfFile" || die "could not read config file"
  189. test -n "$PkgName" || PkgName=$TRIGGER_COPR_PKGNAME
  190. test -n "$PkgName" || PkgName=${ConfFile%.trigger}
  191. test -n "$CoprProject" || CoprProject=$(read_conffile copr)
  192. test -n "$UrlBase" || UrlBase=$(read_conffile urlbase)
  193. if test -n "$Branch"; then
  194. die "not implemented"
  195. test -n "$Version" || Version=$(git_guess ver)
  196. test -n "$Release" || Release=$(git_guess rel)
  197. else
  198. test -n "$Version" || Version=$(last_version)
  199. test -n "$Release" || Release=1
  200. fi
  201. Release=$(choose_relpfx)$Release
  202. mkspec >"$Tmp/$PkgName.spec"
  203. do_action
  204. rm -rf "$Tmp"
  205. return $es
  206. }
  207. main "$@"