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.

258 lines
6.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env bash
  2. #
  3. # getsource - Get build files from Arch Linux official, AUR & ARM repositories
  4. #
  5. # Copyright (C) 2022 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' 'arm' 'arch_deepscan')
  53. ARCH_DATABASES=(
  54. 'core'
  55. 'extra'
  56. 'community'
  57. 'community-testing'
  58. )
  59. ARM_DATABASES=(
  60. 'alarm'
  61. 'aur'
  62. 'community'
  63. 'core'
  64. 'extra'
  65. )
  66. # TODO
  67. # Fetch from pacman.conf
  68. CUSTOM_DATABASES=()
  69. ARCH_GITBASES=(
  70. 'archlinux/svntogit-packages'
  71. 'archlinux/svntogit-community'
  72. )
  73. ARM_GITBASES=(
  74. 'archlinuxarm/PKGBUILDs'
  75. )
  76. ##################################
  77. function get_url() {
  78. if ! wget --no-check-certificate -q -T 10 "${1}" -O - >/dev/null; then return 1; fi
  79. if wget --no-check-certificate -q -c "${1}" -O "${2}"; then return 0; fi
  80. return 1
  81. }
  82. ##################################
  83. function fetch_database() {
  84. case "${1}" in
  85. #custom) TODO
  86. #BASEURL="<get-from-pacman.conf"
  87. # Doesn't need separate DOMAINURL/BASEURL schema
  88. #get_url archive "${BASEURL}" && \
  89. #tar xf "$PACKAGE.${ARCHIVEFORMAT}" && \
  90. #break
  91. #;;
  92. arch)
  93. GITBASES=(${ARCH_GITBASES[@]})
  94. DOMAINURL="https://github.com"
  95. REPOMSG="Using Arch Linux official repositories"
  96. for GITBASE in ${GITBASES[@]}; do
  97. BASEURL="${DOMAINURL}/${GITBASE}/tree/packages/${PACKAGE}/trunk"
  98. if get_url "${BASEURL}" "${URLFILE}"; then
  99. FILENAMES=()
  100. FILEHREFS=( $(grep -oP '(?<=data-pjax).*?(?=\<\/a)' "${URLFILE}" | sed -r "s/.*href=[\"|'](.*)[\"|']>.*/\1/; s/\/blob//g" | grep trunk) )
  101. for i in ${FILEHREFS[@]}; do
  102. FILENAMES+=( $(echo "${i}" | sed 's/.*\///g') )
  103. done
  104. DOMAINURL="https://raw.githubusercontent.com"
  105. download_sourcefiles && return 0
  106. fi
  107. done
  108. return 1
  109. ;;
  110. aur)
  111. local ISSNAPSHOT
  112. DOMAINURL="https://aur.archlinux.org"
  113. local SNAPSHOTURL="${DOMAINURL}/packages/${PACKAGE}/"
  114. REPOMSG="Using Arch Linux user repositories (AUR)"
  115. if get_url "${SNAPSHOTURL}" "${URLFILE}"; then
  116. FILEHREFS=($(grep -oP '(?<=href\=\"\/).*?(?=\"\>Download snapshot)' "${URLFILE}"))
  117. FILENAMES=($(grep -oP '(?<=snapshot\/).*?(?=\"\>Download snapshot)' "${URLFILE}"))
  118. download_sourcefiles && return 0
  119. fi
  120. return 1
  121. ;;
  122. arm)
  123. GITBASES=(${ARM_GITBASES[@]})
  124. DATABASES=(${ARM_DATABASES[@]})
  125. DOMAINURL="https://github.com"
  126. REPOMSG="Using Arch Linux ARM repositories"
  127. for GITBASE in ${GITBASES[@]}; do
  128. for DATABASE in ${DATABASES[@]}; do
  129. BASEURL="${DOMAINURL}/${GITBASE}/tree/master/${DATABASE}/${PACKAGE}"
  130. if get_url "${BASEURL}" "${URLFILE}"; then
  131. FILENAMES=()
  132. FILEHREFS=( $(grep -oP '(?<=data-pjax).*?(?=\<\/a)' "${URLFILE}" | sed -r "s/.*href=[\"|'](.*)[\"|']>.*/\1/; s/\/blob//g" | grep ${PACKAGE}) )
  133. for i in ${FILEHREFS[@]}; do
  134. FILENAMES+=( $(echo "${i}" | sed 's/.*\///g') )
  135. done
  136. DOMAINURL="https://raw.githubusercontent.com"
  137. download_sourcefiles && return 0
  138. fi
  139. done
  140. done
  141. return 1
  142. ;;
  143. arch_deepscan)
  144. arch_repos_deepscan
  145. ;;
  146. esac
  147. [[ -f "${URLFILE}" ]] || return 1
  148. }
  149. ##################################
  150. function arch_repos_deepscan() {
  151. for ARCH_DB in ${ARCH_DATABASES[@]}; do
  152. ARCH_DB_URL="https://www.archlinux.org/packages/${ARCH_DB}/x86_64/${PACKAGE}"
  153. get_url "${ARCH_DB_URL}" "${URLFILE}"
  154. done
  155. if [[ -f "${URLFILE}" ]]; then
  156. msg "$(gettext "Selecting another package name:")"
  157. PACKAGE=$(grep "Source Files" "${URLFILE}" | sed "s/.*href=[\"'].*packages\///g; s/[\"'].*//g; s/\/.*//")
  158. warning "$(gettext "Package name is %s")" "${PACKAGE}"
  159. rm -rf "${URLFILE}"
  160. fetch_database arch ${ARCH_GITBASES[@]}
  161. download_sourcefiles
  162. else
  163. error "$(gettext "Couldn't find package %s")" "${PACKAGE}"
  164. exit 1
  165. fi
  166. }
  167. ##################################
  168. function download_sourcefiles() {
  169. if [[ -f "${URLFILE}" ]]; then
  170. msg "${REPOMSG}"
  171. local a=0
  172. for FILEURL in ${FILEHREFS[@]}; do
  173. echo "${DOMAINURL}/${FILEURL} ${FILENAMES[$a]}"
  174. msg2 "$(gettext "Downloading %s...")" "${FILENAMES[$a]}"
  175. $(wget --no-check-certificate -q "${DOMAINURL}/${FILEURL}" -O "${FILENAMES[$a]}")
  176. [[ -f "${FILENAMES[$a]}" ]] || warning "$(gettext "Couldn't download %s")" "${FILENAMES[$a]}"
  177. let a++
  178. done
  179. rm -rf "${URLFILE}"
  180. if [[ ISSNAPSHOT ]]; then
  181. for ARCHIVEFORMAT in "${ARCHIVEFORMATS[@]}"; do
  182. find . -iname "*${ARCHIVEFORMAT}" -exec tar xf {} --strip-components=1 \;
  183. done
  184. fi
  185. if [[ $? -eq 0 ]]; then
  186. msg "$(gettext "Source files for %s downloaded")" "${PACKAGE}"
  187. return 0
  188. fi
  189. fi
  190. }
  191. ##################################
  192. # TODO
  193. #<if any custom databases configured in pacman.conf>
  194. #DBS_TO_CHECK+=('custom')
  195. for db in ${DBS_TO_CHECK[@]}; do
  196. [[ $(fetch_database "${db}") ]] && break
  197. done
  198. ##################################
  199. if [[ -f ${BUILDSCRIPT} ]]; then
  200. sed -i "s/^arch=.*/arch=('any')/" ${BUILDSCRIPT}
  201. if [[ $? -eq 0 ]]; then msg "$(gettext "Set architecture to 'any' in ${BUILDSCRIPT}.")"; fi
  202. fi