-
Notifications
You must be signed in to change notification settings - Fork 0
/
getsource.sh
executable file
·262 lines (204 loc) · 6.68 KB
/
getsource.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env bash
#
# getsource - Get build files from Arch Linux official, AUR & ARM repositories
#
# Copyright (C) 2022 Pekka Helenius <pekka.helenius@fjordtek.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# TODO: Add support for wider range of processor architectures
# TODO: Add directory support (e.g. getsource wine ~/winesource)
# TODO: create subdir for source files automatically to the current main dir
LIBRARY=${LIBRARY:-'/usr/share/makepkg'}
source "$LIBRARY/util/message.sh"
# check if messages are to be printed using color
if [[ -t 2 && $USE_COLOR != "n" ]]; then
colorize
else
unset ALL_OFF BOLD BLUE GREEN RED YELLOW
fi
PACKAGE=$(pwd | awk '{print $NF}' FS=/)
if [[ -n "$1" ]]; then
PACKAGE="$1"
else
read -r -p "Source package name? [Default: $PACKAGE ] " response
if [[ -n $response ]]; then
PACKAGE=$response
else
echo "Assuming current dir name [ $PACKAGE ]"
fi
fi
INPUT="${CURDIR}"
BUILDSCRIPT="PKGBUILD"
URLFILE="baseurl.html"
ARCHIVEFORMATS=(
'tar.gz'
'tar.xz'
'tar.lz'
'tar.zst'
)
##################################
DBS_TO_CHECK=('arch' 'aur' 'arm' 'arch_deepscan')
ARCH_DATABASES=(
'core'
'extra'
'community'
'community-testing'
)
ARM_DATABASES=(
'alarm'
'aur'
'community'
'core'
'extra'
)
# TODO
# Fetch from pacman.conf
CUSTOM_DATABASES=()
ARCH_GITBASES=(
'archlinux/svntogit-packages'
'archlinux/svntogit-community'
)
ARM_GITBASES=(
'archlinuxarm/PKGBUILDs'
)
##################################
function get_url() {
if ! wget --no-check-certificate -q -T 10 "${1}" -O - >/dev/null; then return 1; fi
if wget --no-check-certificate -q -c "${1}" -O "${2}"; then return 0; fi
return 1
}
##################################
function fetch_database() {
case "${1}" in
#custom) TODO
#BASEURL="<get-from-pacman.conf"
# Doesn't need separate DOMAINURL/BASEURL schema
#get_url archive "${BASEURL}" && \
#tar xf "$PACKAGE.${ARCHIVEFORMAT}" && \
#break
#;;
arch)
GITBASES=(${ARCH_GITBASES[@]})
DOMAINURL="https://github.com"
REPOMSG="Using Arch Linux official repositories"
for GITBASE in ${GITBASES[@]}; do
BASEURL="${DOMAINURL}/${GITBASE}/tree/packages/${PACKAGE}/trunk"
BASEURL_RELATIVE_FILES="/${GITBASE}/blob/packages/${PACKAGE}/trunk"
if get_url "${BASEURL}" "${URLFILE}"; then
FILENAMES=()
FILEHREFS=( $(grep -oP "(?=${BASEURL_RELATIVE_FILES}).*?(?=\"\>)" "${URLFILE}" | sed 's/blob//') )
for i in ${FILEHREFS[@]}; do
FILENAMES+=( $(echo "${i}" | sed 's/.*\///g') )
done
DOMAINURL="https://raw.githubusercontent.com"
download_sourcefiles && return 0
fi
done
return 1
;;
aur)
local ISSNAPSHOT
DOMAINURL="https://aur.archlinux.org"
local SNAPSHOTURL="${DOMAINURL}/packages/${PACKAGE}/"
REPOMSG="Using Arch Linux user repositories (AUR)"
if get_url "${SNAPSHOTURL}" "${URLFILE}"; then
FILEHREFS=($(grep -oPza '(?<=href\=\"\/).*?(?=\"\>(\s*)Download snapshot)' "${URLFILE}" | tr -d '\0'))
FILENAMES=(${FILEHREFS[@]/*\//})
download_sourcefiles && return 0
fi
return 1
;;
arm)
GITBASES=(${ARM_GITBASES[@]})
DATABASES=(${ARM_DATABASES[@]})
DOMAINURL="https://github.com"
REPOMSG="Using Arch Linux ARM repositories"
for GITBASE in ${GITBASES[@]}; do
for DATABASE in ${DATABASES[@]}; do
BASEURL="${DOMAINURL}/${GITBASE}/tree/master/${DATABASE}/${PACKAGE}"
BASEURL_RELATIVE_FILES="/${GITBASE}/blob/master/${DATABASE}/${PACKAGE}"
if get_url "${BASEURL}" "${URLFILE}"; then
FILENAMES=()
FILEHREFS=( $(grep -oP "(?=${BASEURL_RELATIVE_FILES}).*?(?=\"\>)" "${URLFILE}" | sed 's/blob//') )
for i in ${FILEHREFS[@]}; do
FILENAMES+=( $(echo "${i}" | sed 's/.*\///g') )
done
DOMAINURL="https://raw.githubusercontent.com"
download_sourcefiles && return 0
fi
done
done
return 1
;;
arch_deepscan)
arch_repos_deepscan
;;
esac
[[ -f "${URLFILE}" ]] || return 1
}
##################################
function arch_repos_deepscan() {
for ARCH_DB in ${ARCH_DATABASES[@]}; do
ARCH_DB_URL="https://www.archlinux.org/packages/${ARCH_DB}/x86_64/${PACKAGE}"
get_url "${ARCH_DB_URL}" "${URLFILE}"
done
if [[ -f "${URLFILE}" ]]; then
msg "$(gettext "Selecting another package name:")"
PACKAGE=$(grep "Source Files" "${URLFILE}" | sed "s/.*href=[\"'].*packages\///g; s/[\"'].*//g; s/\/.*//")
warning "$(gettext "Package name is %s")" "${PACKAGE}"
rm -rf "${URLFILE}"
fetch_database arch ${ARCH_GITBASES[@]}
download_sourcefiles
else
error "$(gettext "Couldn't find package %s")" "${PACKAGE}"
exit 1
fi
}
##################################
function download_sourcefiles() {
if [[ -f "${URLFILE}" ]]; then
msg "${REPOMSG}"
local a=0
for FILEURL in ${FILEHREFS[@]}; do
echo "${DOMAINURL}/${FILEURL} ${FILENAMES[$a]}"
msg2 "$(gettext "Downloading %s...")" "${FILENAMES[$a]}"
$(wget --no-check-certificate -q "${DOMAINURL}/${FILEURL}" -O "${FILENAMES[$a]}")
[[ -f "${FILENAMES[$a]}" ]] || warning "$(gettext "Couldn't download %s")" "${FILENAMES[$a]}"
let a++
done
rm -rf "${URLFILE}"
if [[ ISSNAPSHOT ]]; then
for ARCHIVEFORMAT in "${ARCHIVEFORMATS[@]}"; do
find . -iname "*${ARCHIVEFORMAT}" -exec tar xf {} --strip-components=1 \;
done
fi
if [[ $? -eq 0 ]]; then
msg "$(gettext "Source files for %s downloaded")" "${PACKAGE}"
return 0
fi
fi
}
##################################
# TODO
#<if any custom databases configured in pacman.conf>
#DBS_TO_CHECK+=('custom')
for db in ${DBS_TO_CHECK[@]}; do
[[ $(fetch_database "${db}") ]] && break
done
##################################
if [[ -f ${BUILDSCRIPT} ]]; then
sed -i "s/^arch=.*/arch=('any')/" ${BUILDSCRIPT}
if [[ $? -eq 0 ]]; then msg "$(gettext "Set architecture to 'any' in ${BUILDSCRIPT}.")"; fi
fi