Script to trigger re-build of slop - https://github.com/naelstrof/slop

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