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.

262 lines
6.7 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, 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. BASEURL_RELATIVE_FILES="/${GITBASE}/blob/packages/${PACKAGE}/trunk"
  99. if get_url "${BASEURL}" "${URLFILE}"; then
  100. FILENAMES=()
  101. FILEHREFS=( $(grep -oP "(?=${BASEURL_RELATIVE_FILES}).*?(?=\"\>)" "${URLFILE}" | sed 's/blob//') )
  102. for i in ${FILEHREFS[@]}; do
  103. FILENAMES+=( $(echo "${i}" | sed 's/.*\///g') )
  104. done
  105. DOMAINURL="https://raw.githubusercontent.com"
  106. download_sourcefiles && return 0
  107. fi
  108. done
  109. return 1
  110. ;;
  111. aur)
  112. local ISSNAPSHOT
  113. DOMAINURL="https://aur.archlinux.org"
  114. local SNAPSHOTURL="${DOMAINURL}/packages/${PACKAGE}/"
  115. REPOMSG="Using Arch Linux user repositories (AUR)"
  116. if get_url "${SNAPSHOTURL}" "${URLFILE}"; then
  117. FILEHREFS=($(grep -oPza '(?<=href\=\"\/).*?(?=\"\>(\s*)Download snapshot)' "${URLFILE}" | tr -d '\0'))
  118. FILENAMES=(${FILEHREFS[@]/*\//})
  119. download_sourcefiles && return 0
  120. fi
  121. return 1
  122. ;;
  123. arm)
  124. GITBASES=(${ARM_GITBASES[@]})
  125. DATABASES=(${ARM_DATABASES[@]})
  126. DOMAINURL="https://github.com"
  127. REPOMSG="Using Arch Linux ARM repositories"
  128. for GITBASE in ${GITBASES[@]}; do
  129. for DATABASE in ${DATABASES[@]}; do
  130. BASEURL="${DOMAINURL}/${GITBASE}/tree/master/${DATABASE}/${PACKAGE}"
  131. BASEURL_RELATIVE_FILES="/${GITBASE}/blob/master/${DATABASE}/${PACKAGE}"
  132. if get_url "${BASEURL}" "${URLFILE}"; then
  133. FILENAMES=()
  134. FILEHREFS=( $(grep -oP "(?=${BASEURL_RELATIVE_FILES}).*?(?=\"\>)" "${URLFILE}" | sed 's/blob//') )
  135. for i in ${FILEHREFS[@]}; do
  136. FILENAMES+=( $(echo "${i}" | sed 's/.*\///g') )
  137. done
  138. DOMAINURL="https://raw.githubusercontent.com"
  139. download_sourcefiles && return 0
  140. fi
  141. done
  142. done
  143. return 1
  144. ;;
  145. arch_deepscan)
  146. arch_repos_deepscan
  147. ;;
  148. esac
  149. [[ -f "${URLFILE}" ]] || return 1
  150. }
  151. ##################################
  152. function arch_repos_deepscan() {
  153. for ARCH_DB in ${ARCH_DATABASES[@]}; do
  154. ARCH_DB_URL="https://www.archlinux.org/packages/${ARCH_DB}/x86_64/${PACKAGE}"
  155. get_url "${ARCH_DB_URL}" "${URLFILE}"
  156. done
  157. if [[ -f "${URLFILE}" ]]; then
  158. msg "$(gettext "Selecting another package name:")"
  159. PACKAGE=$(grep "Source Files" "${URLFILE}" | sed "s/.*href=[\"'].*packages\///g; s/[\"'].*//g; s/\/.*//")
  160. warning "$(gettext "Package name is %s")" "${PACKAGE}"
  161. rm -rf "${URLFILE}"
  162. fetch_database arch ${ARCH_GITBASES[@]}
  163. download_sourcefiles
  164. else
  165. error "$(gettext "Couldn't find package %s")" "${PACKAGE}"
  166. exit 1
  167. fi
  168. }
  169. ##################################
  170. function download_sourcefiles() {
  171. if [[ -f "${URLFILE}" ]]; then
  172. msg "${REPOMSG}"
  173. local a=0
  174. for FILEURL in ${FILEHREFS[@]}; do
  175. echo "${DOMAINURL}/${FILEURL} ${FILENAMES[$a]}"
  176. msg2 "$(gettext "Downloading %s...")" "${FILENAMES[$a]}"
  177. $(wget --no-check-certificate -q "${DOMAINURL}/${FILEURL}" -O "${FILENAMES[$a]}")
  178. [[ -f "${FILENAMES[$a]}" ]] || warning "$(gettext "Couldn't download %s")" "${FILENAMES[$a]}"
  179. let a++
  180. done
  181. rm -rf "${URLFILE}"
  182. if [[ ISSNAPSHOT ]]; then
  183. for ARCHIVEFORMAT in "${ARCHIVEFORMATS[@]}"; do
  184. find . -iname "*${ARCHIVEFORMAT}" -exec tar xf {} --strip-components=1 \;
  185. done
  186. fi
  187. if [[ $? -eq 0 ]]; then
  188. msg "$(gettext "Source files for %s downloaded")" "${PACKAGE}"
  189. return 0
  190. fi
  191. fi
  192. }
  193. ##################################
  194. # TODO
  195. #<if any custom databases configured in pacman.conf>
  196. #DBS_TO_CHECK+=('custom')
  197. for db in ${DBS_TO_CHECK[@]}; do
  198. [[ $(fetch_database "${db}") ]] && break
  199. done
  200. ##################################
  201. if [[ -f ${BUILDSCRIPT} ]]; then
  202. sed -i "s/^arch=.*/arch=('any')/" ${BUILDSCRIPT}
  203. if [[ $? -eq 0 ]]; then msg "$(gettext "Set architecture to 'any' in ${BUILDSCRIPT}.")"; fi
  204. fi