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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. trigger() {
  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 >"$Tmp/slop.spec"
  48. copr build --nowait "$CoprProject" "$Tmp/slop.spec"
  49. }
  50. git_guess() {
  51. #
  52. # Print git-guessed $1
  53. #
  54. local what=$1 # what we want (ver|rel)
  55. local describe # full git-describe output
  56. local xtra= # extra part (-N-gHASH)
  57. local num=0 # num. of commits since tag (N)
  58. local sha= # '.g'+sha1 of this commit (gHASH)
  59. describe=$(git describe --tags --always HEAD)
  60. case $describe in
  61. *-*) # v1.2.3-21-g654cba
  62. tag=${describe%%-*} # tag=v1.2.3
  63. xtra=${describe#$tag-} # xtra=-21-g654cba
  64. num=${xtra%%-*} # num=21
  65. sha=.${xtra#$num-} # sha=.g654cba
  66. ;;
  67. *)
  68. tag=$describe
  69. ;;
  70. esac
  71. case $what in
  72. ver) echo "${tag#v}" ;;
  73. rel) echo "$((num + 1))$sha" ;;
  74. esac
  75. }
  76. main() {
  77. local Version # Version in SPEC file
  78. local Release # Release in SPEC file
  79. local CoprProject # COPR project
  80. local UrlBase # GitHub URL base
  81. local Tmp # our temp
  82. local Branch # branch to use, if empty, tags are considered
  83. local DryRun=false # do not do anything
  84. UrlBase=https://github.com/AloisMahdal/slop
  85. Tmp=$(mktemp -d)
  86. while true; do case $1 in
  87. -b) Branch=$2; shift 2 || usage ;;
  88. -u) UrlBase=$2; shift 2 || usage ;;
  89. -r) Release=$2; shift 2 || usage ;;
  90. -v) Version=${2#v}; shift 2 || usage ;;
  91. -n) DryRun=true; shift ;;
  92. -*) usage ;;
  93. *) break ;;
  94. esac done
  95. CoprProject=$1
  96. test -n "$CoprProject" || usage
  97. which copr >/dev/null \
  98. || die "copr not found, try 'sudo install python-copr'"
  99. if test -n "$Branch"; then
  100. die "not implemented"
  101. test -n "$Version" || Version=$(git_guess ver)
  102. test -n "$Release" || Release=$(git_guess rel)
  103. else
  104. test -n "$Version" || Version=$(last_version)
  105. test -n "$Release" || Release=1
  106. fi
  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. exit 1
  115. }
  116. trigger
  117. rm -rf "$Tmp"
  118. }
  119. main "$@"