PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/libmakepkg/util/source.sh.in

https://bitbucket.org/andrewgregory/pacman
Autoconf | 176 lines | 115 code | 25 blank | 36 comment | 14 complexity | 5fffaee5f1008dab6667b177f35f3629 MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/bin/bash
  2. #
  3. # source.sh - functions to extract information from source URLs
  4. #
  5. # Copyright (c) 2010-2021 Pacman Development Team <pacman-dev@archlinux.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. [[ -n "$LIBMAKEPKG_UTIL_SOURCE_SH" ]] && return
  21. LIBMAKEPKG_UTIL_SOURCE_SH=1
  22. LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
  23. source "$LIBRARY/util/message.sh"
  24. # a source entry can have two forms :
  25. # 1) "filename::http://path/to/file"
  26. # 2) "http://path/to/file"
  27. # extract the URL from a source entry
  28. get_url() {
  29. # strip an eventual filename
  30. printf "%s\n" "${1#*::}"
  31. }
  32. # extract the protocol from a source entry - return "local" for local sources
  33. get_protocol() {
  34. if [[ $1 = *://* ]]; then
  35. # strip leading filename
  36. local proto="${1#*::}"
  37. proto="${proto%%://*}"
  38. # strip proto+uri://
  39. printf "%s\n" "${proto%%+*}"
  40. elif [[ $1 = *lp:* ]]; then
  41. local proto="${1#*::}"
  42. printf "%s\n" "${proto%%+lp:*}"
  43. else
  44. printf "%s\n" local
  45. fi
  46. }
  47. # extract the filename from a source entry
  48. get_filename() {
  49. local netfile=$1
  50. # if a filename is specified, use it
  51. if [[ $netfile = *::* ]]; then
  52. printf "%s\n" "${netfile%%::*}"
  53. return
  54. fi
  55. local proto=$(get_protocol "$netfile")
  56. case $proto in
  57. bzr|fossil|git|hg|svn)
  58. filename=${netfile%%#*}
  59. filename=${filename%%\?*}
  60. filename=${filename%/}
  61. filename=${filename##*/}
  62. if [[ $proto = bzr ]]; then
  63. filename=${filename#*lp:}
  64. fi
  65. if [[ $proto = fossil ]]; then
  66. filename=$filename.fossil
  67. fi
  68. if [[ $proto = git ]]; then
  69. filename=${filename%%.git*}
  70. fi
  71. ;;
  72. *)
  73. # if it is just an URL, we only keep the last component
  74. filename="${netfile##*/}"
  75. ;;
  76. esac
  77. printf "%s\n" "${filename}"
  78. }
  79. # Return the absolute filename of a source entry
  80. get_filepath() {
  81. local file="$(get_filename "$1")"
  82. local proto="$(get_protocol "$1")"
  83. case $proto in
  84. bzr|git|hg|svn)
  85. if [[ -d "$startdir/$file" ]]; then
  86. file="$startdir/$file"
  87. elif [[ -d "$SRCDEST/$file" ]]; then
  88. file="$SRCDEST/$file"
  89. else
  90. return 1
  91. fi
  92. ;;
  93. *)
  94. if [[ -f "$startdir/$file" ]]; then
  95. file="$startdir/$file"
  96. elif [[ -f "$SRCDEST/$file" ]]; then
  97. file="$SRCDEST/$file"
  98. else
  99. return 1
  100. fi
  101. ;;
  102. esac
  103. printf "%s\n" "$file"
  104. }
  105. # extract the VCS revision/branch specifier from a source entry
  106. get_uri_fragment() {
  107. local netfile=$1
  108. local fragment=${netfile#*#}
  109. if [[ $fragment = "$netfile" ]]; then
  110. unset fragment
  111. fi
  112. fragment=${fragment%\?*}
  113. printf "%s\n" "$fragment"
  114. }
  115. # extract the VCS "signed" status from a source entry
  116. get_uri_query() {
  117. local netfile=$1
  118. local query=${netfile#*\?}
  119. if [[ $query = "$netfile" ]]; then
  120. unset query
  121. fi
  122. query=${query%#*}
  123. printf "%s\n" "$query"
  124. }
  125. get_downloadclient() {
  126. local proto=$1
  127. # loop through DOWNLOAD_AGENTS variable looking for protocol
  128. local i
  129. for i in "${DLAGENTS[@]}"; do
  130. local handler="${i%%::*}"
  131. if [[ $proto = "$handler" ]]; then
  132. local agent="${i#*::}"
  133. break
  134. fi
  135. done
  136. # if we didn't find an agent, return an error
  137. if [[ -z $agent ]]; then
  138. error "$(gettext "Unknown download protocol: %s")" "$proto"
  139. plainerr "$(gettext "Aborting...")"
  140. exit 1 # $E_CONFIG_ERROR
  141. fi
  142. # ensure specified program is installed
  143. local program="${agent%% *}"
  144. if [[ ! -x $program ]]; then
  145. local baseprog="${program##*/}"
  146. error "$(gettext "The download program %s is not installed.")" "$baseprog"
  147. plainerr "$(gettext "Aborting...")"
  148. exit 1 # $E_MISSING_PROGRAM
  149. fi
  150. printf "%s\n" "$agent"
  151. }