Useful CLI tools (bash) for Arch Linux administration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env bash
  2. #
  3. # getsource - Get build files from Arch Linux official and AUR repositories
  4. #
  5. # Copyright (C) 2021 Pekka Helenius <pekka.helenius@fjordtek.com>
  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. # TODO: Add support for wider range of processor architectures
  21. # TODO: Add directory support (e.g. getsource wine ~/winesource)
  22. # TODO: create subdir for source files automatically to the current main dir
  23. LIBRARY=${LIBRARY:-'/usr/share/makepkg'}
  24. source "$LIBRARY/util/message.sh"
  25. # check if messages are to be printed using color
  26. if [[ -t 2 && $USE_COLOR != "n" ]]; then
  27. colorize
  28. else
  29. unset ALL_OFF BOLD BLUE GREEN RED YELLOW
  30. fi
  31. PACKAGE=$(pwd | awk '{print $NF}' FS=/)
  32. if [[ -n "$1" ]]; then
  33. PACKAGE="$1"
  34. else
  35. read -r -p "Source package name? [Default: $PACKAGE ] " response
  36. if [[ -n $response ]]; then
  37. PACKAGE=$response
  38. else
  39. echo "Assuming current dir name [ $PACKAGE ]"
  40. fi
  41. fi
  42. INPUT="${CURDIR}"
  43. BUILDSCRIPT="PKGBUILD"
  44. URLFILE="baseurl.html"
  45. ARCHIVEFORMATS=(
  46. 'tar.gz'
  47. 'tar.xz'
  48. 'tar.lz'
  49. 'tar.zst'
  50. )
  51. ##################################
  52. DBS_TO_CHECK=('arch' 'aur' 'arch_deepscan')
  53. ARCH_DATABASES=(
  54. 'core'
  55. 'extra'
  56. 'community'
  57. 'community-testing'
  58. )
  59. # TODO
  60. # Fetch from pacman.conf
  61. CUSTOM_DATABASES=()
  62. ARCH_GITBASES=(
  63. 'archlinux/svntogit-packages'
  64. 'archlinux/svntogit-community'
  65. )
  66. ##################################
  67. function get_url() {
  68. if ! wget -q -T 10 "${1}" -O - >/dev/null; then return 1; fi
  69. if wget -q -c "${1}" -O "${2}"; then return 0; fi
  70. return 1
  71. }
  72. function fetch_database() {
  73. case "${1}" in
  74. #custom) TODO
  75. #BASEURL="<get-from-pacman.conf"
  76. # Doesn't need separate DOMAINURL/BASEURL schema
  77. #get_url archive "${BASEURL}" && \
  78. #tar xf "$PACKAGE.${ARCHIVEFORMAT}" && \
  79. #break
  80. #;;
  81. arch)
  82. GITBASES=(${ARCH_GITBASES[@]})
  83. DOMAINURL="https://github.com"
  84. REPOMSG="Using Arch Linux official repositories"
  85. for GITBASE in ${GITBASES[@]}; do
  86. BASEURL="${DOMAINURL}/${GITBASE}/tree/packages/${PACKAGE}/trunk"
  87. if get_url "${BASEURL}" "${URLFILE}"; then
  88. FILENAMES=()
  89. FILEHREFS=( $(grep -oP '(?<=data-pjax).*?(?=\<\/a)' "${URLFILE}" | sed -r "s/.*href=[\"|'](.*)[\"|']>.*/\1/; s/\/blob//g" | grep trunk) )
  90. for i in ${FILEHREFS[@]}; do
  91. FILENAMES+=( $(echo "${i}" | sed 's/.*\///g') )
  92. done
  93. DOMAINURL="https://raw.githubusercontent.com"
  94. download_sourcefiles && return 0
  95. fi
  96. done
  97. return 1
  98. ;;
  99. aur)
  100. local ISSNAPSHOT
  101. DOMAINURL="https://aur.archlinux.org"
  102. local SNAPSHOTURL="${DOMAINURL}/packages/${PACKAGE}/"
  103. REPOMSG="Using Arch Linux user repositories (AUR)"
  104. if get_url "${SNAPSHOTURL}" "${URLFILE}"; then
  105. FILEHREFS=($(grep -oP '(?<=href\=\"\/).*?(?=\"\>Download snapshot)' "${URLFILE}"))
  106. FILENAMES=($(grep -oP '(?<=snapshot\/).*?(?=\"\>Download snapshot)' "${URLFILE}"))
  107. download_sourcefiles && return 0
  108. fi
  109. return 1
  110. ;;
  111. arch_deepscan)
  112. arch_repos_deepscan
  113. ;;
  114. esac
  115. [[ -f "${URLFILE}" ]] || return 1
  116. }
  117. ##################################
  118. function arch_repos_deepscan() {
  119. for ARCH_DB in ${ARCH_DATABASES[@]}; do
  120. ARCH_DB_URL="https://www.archlinux.org/packages/${ARCH_DB}/x86_64/${PACKAGE}"
  121. get_url "${ARCH_DB_URL}" "${URLFILE}"
  122. done
  123. if [[ -f "${URLFILE}" ]]; then
  124. msg "$(gettext "Selecting another package name:")"
  125. PACKAGE=$(grep "Source Files" "${URLFILE}" | sed "s/.*href=[\"'].*packages\///g; s/[\"'].*//g")
  126. warning "$(gettext "Package name is %s.")" "$PACKAGE"
  127. rm -rf "${URLFILE}"
  128. fetch_database arch ${ARCH_GITBASES[@]}
  129. download_sourcefiles
  130. else
  131. error "$(gettext "Couldn't find package %s.")" "$PACKAGE"
  132. exit 1
  133. fi
  134. }
  135. ##################################
  136. function download_sourcefiles() {
  137. if [[ -f "${URLFILE}" ]]; then
  138. msg "${REPOMSG}"
  139. local a=0
  140. for FILEURL in ${FILEHREFS[@]}; do
  141. echo "${DOMAINURL}/${FILEURL} ${FILENAMES[$a]}"
  142. msg2 "$(gettext "Downloading %s...")" "${FILENAMES[$a]}"
  143. $(wget -q "${DOMAINURL}/${FILEURL}" -O "${FILENAMES[$a]}")
  144. [[ -f "${FILENAMES[$a]}" ]] || warning "$(gettext "Couldn't download %s")" "${FILENAMES[$a]}"
  145. let a++
  146. done
  147. rm -rf "${URLFILE}"
  148. if [[ ISSNAPSHOT ]]; then
  149. for ARCHIVEFORMAT in "${ARCHIVEFORMATS[@]}"; do
  150. find . -iname "*${ARCHIVEFORMAT}" -exec tar xf {} --strip-components=1 \;
  151. done
  152. fi
  153. if [[ $? -eq 0 ]]; then
  154. msg "$(gettext "Source files for %s downloaded".)" "$PACKAGE"
  155. return 0
  156. fi
  157. fi
  158. }
  159. ##################################
  160. # TODO
  161. #<if any custom databases configured in pacman.conf>
  162. #DBS_TO_CHECK+=('custom')
  163. for db in ${DBS_TO_CHECK[@]}; do
  164. [[ $(fetch_database "${db}") ]] && break
  165. done
  166. ##################################
  167. if [[ -f ${BUILDSCRIPT} ]]; then
  168. sed -i "s/^arch=.*/arch=('any')/" ${BUILDSCRIPT}
  169. if [[ $? -eq 0 ]]; then msg "$(gettext "Set architecture to 'any' in ${BUILDSCRIPT}.")"; fi
  170. fi