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

trigger_copr 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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] COPR_PROJECT"
  11. warn ""
  12. warn "Options:"
  13. warn ""
  14. warn " -b BRN build from branch BRN (default: last tag)"
  15. warn " -n dry mode, don't do anything (just show"
  16. warn " what would be done)"
  17. warn " -r REL use REL as Release number in SPEC file"
  18. warn " -v VER use VER as Version number in SPEC file"
  19. warn " -u URL use URL as base (to get last tag and"
  20. warn " compose Source0 in SPEC file)"
  21. warn ""
  22. warn "If -b is not used, build is launched from last tag available"
  23. warn "in the GitHub repo. In that case, Release is '1' and Version"
  24. warn "is deduced from the tag by removing the initial 'v'."
  25. warn ""
  26. warn "If -b is used, the project repo is temporarily clonded, and"
  27. warn "both Version and Release are found by consulting git-describe"
  28. warn "on the specified branch."
  29. exit 2
  30. }
  31. last_version() {
  32. git ls-remote --tag "$UrlBase" \
  33. | grep '/tags/' \
  34. | cut -d/ -f3 \
  35. | sort -V \
  36. | tail -1 \
  37. | sed "s/^v//"
  38. }
  39. mkspec() {
  40. local self_version
  41. self_version="slop-build-copr $(git describe --tags)"
  42. sed -e "
  43. /^Version/ s|__SLOP_VERSION__|$Version|
  44. /^Release/ s|__SLOP_RELEASE__|$Release|
  45. /^Source0/ s|__SLOP_URLBASE__|$UrlBase|
  46. /^# spec file/ s|__SLOP_BUILDSCRIPT_VERSION__|$self_version|
  47. " <slop.spec.in
  48. }
  49. git_guess() {
  50. #
  51. # Print git-guessed $1
  52. #
  53. local what=$1 # what we want (ver|rel)
  54. local describe # full git-describe output
  55. local xtra= # extra part (-N-gHASH)
  56. local num=0 # num. of commits since tag (N)
  57. local sha= # '.g'+sha1 of this commit (gHASH)
  58. describe=$(git describe --tags --always HEAD)
  59. case $describe in
  60. *-*) # v1.2.3-21-g654cba
  61. tag=${describe%%-*} # tag=v1.2.3
  62. xtra=${describe#$tag-} # xtra=-21-g654cba
  63. num=${xtra%%-*} # num=21
  64. sha=.${xtra#$num-} # sha=.g654cba
  65. ;;
  66. *)
  67. tag=$describe
  68. ;;
  69. esac
  70. case $what in
  71. ver) echo "${tag#v}" ;;
  72. rel) echo "$((num + 1))$sha" ;;
  73. esac
  74. }
  75. main() {
  76. local Version # Version in SPEC file
  77. local Release # Release in SPEC file
  78. local CoprProject # COPR project
  79. local UrlBase # GitHub URL base
  80. local Tmp # our temp
  81. local Branch # branch to use, if empty, tags are considered
  82. local DryRun=false # do not do anything
  83. UrlBase=https://github.com/AloisMahdal/slop
  84. Tmp=$(mktemp -d)
  85. while true; do case $1 in
  86. -b) Branch=$2; shift 2 || usage ;;
  87. -u) UrlBase=$2; shift 2 || usage ;;
  88. -r) Release=$2; shift 2 || usage ;;
  89. -v) Version=${2#v}; shift 2 || usage ;;
  90. -n) DryRun=true; shift ;;
  91. -*) usage ;;
  92. *) break ;;
  93. esac done
  94. CoprProject=$1
  95. test -n "$CoprProject" || usage
  96. which copr >/dev/null \
  97. || die "copr not found, try 'sudo install python-copr'"
  98. if test -n "$Branch"; then
  99. die "not implemented"
  100. test -n "$Version" || Version=$(git_guess ver)
  101. test -n "$Release" || Release=$(git_guess rel)
  102. else
  103. test -n "$Version" || Version=$(last_version)
  104. test -n "$Release" || Release=1
  105. fi
  106. mkspec >"$Tmp/slop.spec"
  107. $DryRun && {
  108. warn "dry mode active, we would build:"
  109. warn " at $CoprProject"
  110. test -n "$Branch" && warn " using branch $Branch"
  111. test -n "$Branch" || warn " using last tag"
  112. warn " from $UrlBase"
  113. warn " yielding slop-$Version-$Release.*.rpm"
  114. warn ""
  115. warn "===== BEGIN slop.spec ====="
  116. cat "$Tmp/slop.spec"
  117. warn "===== END slop.spec ====="
  118. exit 1
  119. }
  120. copr build --nowait "$CoprProject" "$Tmp/slop.spec"
  121. rm -rf "$Tmp"
  122. }
  123. main "$@"