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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. ;;
  122. demo)
  123. warn "demo mode active, we would build:"
  124. warn " at $CoprProject"
  125. test -n "$Branch" && warn " using branch $Branch"
  126. test -n "$Branch" || warn " using last tag"
  127. warn " from $UrlBase"
  128. warn " yielding $PkgName-$Version-$Release.*.rpm"
  129. warn ""
  130. warn "===== BEGIN $PkgName.spec ====="
  131. cat "$Tmp/$PkgName.spec"
  132. warn "===== END $PkgName.spec ====="
  133. return 1
  134. ;;
  135. mkspec)
  136. cat "$Tmp/$PkgName.spec"
  137. ;;
  138. rpmstuff)
  139. url=$(
  140. grep -o 'Source.*:.*http.*' "$Tmp/$PkgName.spec" \
  141. | sed "
  142. s/.*http/http/
  143. s/ *$//
  144. s/%{version}/$Version/
  145. "
  146. )
  147. wget --quiet "$url"
  148. cat "$Tmp/$PkgName.spec" > "$PkgName.spec"
  149. ;;
  150. esac
  151. }
  152. main() {
  153. local Version # Version in SPEC file
  154. local Release # Release in SPEC file
  155. local CoprProject # COPR project
  156. local UrlBase # GitHub URL base
  157. local Tmp # our temp
  158. local Branch # branch to use, if empty, tags are considered
  159. local Mode=scratch # implies COPR project and release prefix
  160. local Action=build # what to do
  161. local ConfFile # config file to use
  162. local PkgName # package name
  163. local es=0 # exit status
  164. which copr >/dev/null \
  165. || die "copr not found, try 'sudo install copr-cli'"
  166. Tmp=$(mktemp -d)
  167. while true; do case $1 in
  168. -C) ConfFile=$2; shift 2 || usage ;;
  169. -a) Action=$2; shift 2 || usage ;;
  170. -b) Branch=$2; shift 2 || usage ;;
  171. -c) CoprProject=$2; shift 2 || usage ;;
  172. -u) UrlBase=$2; shift 2 || usage ;;
  173. -r) Release=$2; shift 2 || usage ;;
  174. -v) Version=${2#v}; shift 2 || usage ;;
  175. -n) Action=demo; shift ;;
  176. -*) usage ;;
  177. *) break ;;
  178. esac done
  179. Mode=${1:-$Mode}
  180. case $Action in
  181. build|demo|mkspec|rpmstuff) : ;;
  182. *) usage ;;
  183. esac
  184. test -n "$ConfFile" || ConfFile=$TRIGGER_COPR_CONFIG
  185. test -n "$ConfFile" || ConfFile=$(choose_conffile)
  186. test -n "$ConfFile" || die "could not find config file"
  187. test -r "$ConfFile" || die "could not read config file"
  188. test -n "$PkgName" || PkgName=$TRIGGER_COPR_PKGNAME
  189. test -n "$PkgName" || PkgName=${ConfFile%.trigger}
  190. test -n "$CoprProject" || CoprProject=$(read_conffile copr)
  191. test -n "$UrlBase" || UrlBase=$(read_conffile urlbase)
  192. if test -n "$Branch"; then
  193. die "not implemented"
  194. test -n "$Version" || Version=$(git_guess ver)
  195. test -n "$Release" || Release=$(git_guess rel)
  196. else
  197. test -n "$Version" || Version=$(last_version)
  198. test -n "$Release" || Release=1
  199. fi
  200. Release=$(choose_relpfx)$Release
  201. mkspec >"$Tmp/$PkgName.spec"
  202. do_action
  203. rm -rf "$Tmp"
  204. return $es
  205. }
  206. main "$@"