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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
#!/bin/bash
# Queries the ABS
# License: GPL3
## TODO
# * Add license text
# * Create symlinks from pkgbase to pkgname[@] for easy package finding
## GOALS
# * Have a searchable database of PKGBUILD metadata
# * Have an interface for source-only builds
# * Possibility to hook up ABS dirs besides ABSROOT (low priority)
# * Tell updates and non available binary packages (working on this)
source /etc/abs.conf
source /etc/libretools.conf
if [ ! -w "$TORUPATH" ]; then
error "Toru's path isn't writable. Please check $TORUPATH"
exit 1
fi
# TODO move to common functions
function in_array { # usage : in_array( $needle, $haystack )
[[ $2 ]] || return 1 # Not found
local needle=$1; shift
local item
for item in "$@"; do
[[ ${item#@} = $needle ]] && return 0 # Found
done
return 1 # Not Found
}
# Stores the lastsync date
lastsync() {
local lastsyncfile
lastsyncfile=$1
[ -e ${lastsyncfile} -a ! -w ${lastsyncfile} ] && {
error "The sync date can't be saved. ${lastsyncfile} isn't writable."
return 1
}
date +%s > ${lastsyncfile}
touch ${lastsyncfile}
}
# Saves contents on a named cache
# $1 cache name (repo)
# $2+ contents
store_cache() {
cache=$1; shift
[ -z "$cache" ] && return 1
cat $@ > ${TORUPATH}/${cache}.cache
return $?
}
# Return cache contents
# $1 cache name
read_cache() {
cat ${TORUPATH}/${1}.cache 2>/dev/null
return $?
}
##
# usage : get_full_version( $epoch, $pkgver, $pkgrel )
# return : full version spec, including epoch (if necessary), pkgver, pkgrel
##
get_full_version() {
if [[ $1 -eq 0 ]]; then
# zero epoch case, don't include it in version
echo $2-$3
else
echo $1:$2-$3
fi
}
# Outputs an ordered package-fullpkgver array
print_package_array() {
echo "$@" | tr " " "\n" | sort -u
}
# Gets repo.db contents (unordered)
# $1 repo
get_db_contents() {
[ ! -r /var/lib/pacman/sync/$1.db ] && return 0
bsdtar -tf /var/lib/pacman/sync/$1.db | cut -d'/' -f1 | sort -u
}
# Get the pkgname
# pkgname from pkgver separator can be either '-' or ' '
extract_pkgname() {
echo "$@" | tr " " "\n" | sed "s/^\(.\+\)[- ][^-]\+-[^-]\+$/\1/"
}
# Get all the pkgnames from a file
# pkgname from pkgver separator can be either '-' or ' '
extract_pkgname_from_file() {
sed "s/^\(.\+\)[- ][^-]\+-[^-]\+$/\1/" $1
}
# Split pkgnames from pkgvers
split_pkgname_from_pkgver() {
sed "s/^\(.\+\)-\([^-]\+-[^-]\+\)$/\1 \2/" $1
}
# Get the fullpkgver
# pkgname from pkgver separator can be either '-' or ' '
extract_fullpkgver() {
echo "$@" | tr " " "\n" | sed "s/^.\+[ -]\([^-]\+-[^-]\+\)$/\1/"
}
# Checks if $1 is a valid repo
is_repo() {
if ! in_array ${1} ${REPOS[@]}; then
$quiet || warning "${1} is not a valid repo (check REPOS array at libretools.conf)"
return 1
fi
}
# Updates the database by finding all PKGBUILDS
# Workflow:
# * Find all PKGBUILDs on the ABS repo specified
# * Get all packages already on package repos
# * Compare them
# Args:
update() {
local update_sync_file=false
# The PKGBUILDs found
local pkgbuilds=()
# The list of pkgname-fullpkgver
local packages_in_abs=()
local packages_in_sync=()
local need_update=()
local -a pkg_updates=()
# Traverse all specified repos
for _repo in $@; do
# Check if the repo is set as such, otherwise skip
is_repo ${_repo} || continue
# This is the syncfile, stores the last date as content and mtime
local lastsyncfile=${TORUPATH}/${_repo}.lastsync
# Find all the PKGBUILDs newer than the last update
# Update newer, otherwise everything
if [[ $force = true || ! -e ${lastsyncfile} ]]; then
$quiet || warning "Forcing upgrade"
pkgbuilds=($(find ${_repo} -maxdepth 2 -type f -name 'PKGBUILD'))
else
pkgbuilds=($(find ${_repo} -maxdepth 2 -type f -name 'PKGBUILD' -newer ${lastsyncfile}))
packages_in_abs=($(read_cache ${_repo}))
$quiet || msg2 "Getting ${#packages_in_abs[@]} packages from cache"
fi
# Inform how many PKGBUILDS were found and quit immediately if none
$quiet || msg "Found $((${#pkgbuilds[*]}-1)) PKGBUILDs to update"
# Traverse all found PKGBUILDs
for _pkgbuild in ${pkgbuilds[@]}; do
# Update the sync file because there are pkgbuilds to update
update_sync_file=true
# Guess pkgbase from PKGBUILD's basedir
_pkgpath=$(dirname "${_pkgbuild}")
_pkgbase=$(basename "${_pkgpath}")
# Load PKGBUILD's metadata
source ${_pkgbuild}
# We won't need this (all unsets are for memory efficiency)
unset build package url md5sums install pkgdesc backup options
# TODO fill a license list
unset license
# TODO create source tarballs?
unset mksource
# TODO solve dependency tree?
unset depends makedepends
for _pkg in ${pkgname[@]}; do
# Keep removing unneeded stuff
unset package_${_pkg} >/dev/null 2>&1
# Fill the list of packages to find
packages_in_abs+=($_pkg-$(get_full_version ${epoch:-0} $pkgver $pkgrel))
done # end pkgnames
unset pkgbase pkgname pkgver pkgrel source epoch
done # end pkgbuilds
# Sync!
${update_sync_file} && lastsync ${lastsyncfile}
# If there isn't an update cache or it's older than the last update, we check
if [ "${lastsyncfile}" -nt "${TORUPATH}/${_repo}.updates.cache" ]; then
# Get repo database contents
packages_in_sync=($(get_db_contents ${_repo}))
# Drops arrays into files
print_package_array "${packages_in_abs[@]}" > ${TMPDIR}/packages_in_abs
print_package_array "${packages_in_sync[@]}" > ${TMPDIR}/packages_in_sync
# Work with files
unset packages_in_abs package_in_sync
# Use a different separator for pkgnames and pkgvers
# so we can join them by pkgname (first field)
split_pkgname_from_pkgver ${TMPDIR}/packages_in_abs | sort -k1b,1 > ${TMPDIR}/in_abs
split_pkgname_from_pkgver ${TMPDIR}/packages_in_sync | sort -k1b,1 > ${TMPDIR}/in_sync
$quiet || msg "This packages are available to update"
# Join both files by pkgname, the end result is:
# pkgname syncver absver
join ${TMPDIR}/in_sync ${TMPDIR}/in_abs | \
while read need_line; do
_pkg=$(echo "${need_line}" | cut -d' ' -f1)
_syncver=$(echo "${need_line}" | cut -d' ' -f2)
_absver=$(echo "${need_line}" | cut -d' ' -f3)
# If the versions differ we need an update
# TODO move this to update query
if [ "${_syncver}" != "${_absver}" ]; then
$quiet || msg2 "$_pkg update from $_syncver to $_absver"
$quiet && echo "$_pkg"
# FIXME this works all right but it's unset once the while ends
#pkg_updates+=("$_pkg")
echo $_pkg >> ${TMPDIR}/updates
fi
done # end need_line
echo "${pkg_updates[@]}"
unset _pkg _syncver _absver need_line
# Save the cache
store_cache ${_repo} ${TMPDIR}/packages_in_abs
# print_package_array "${updates[@]}" > ${TMPDIR}/updates
store_cache ${_repo}.updates ${TMPDIR}/updates
else
msg "Reading updates from cache..."
read_cache ${_repo}.updates
fi
done # end repos
}
# Find all the packages that are missing from the repo dbs (aka not built)
missing() {
true
}
## MAIN
commands=()
repos=()
quiet=false
force=false
while getopts 'hqfum' arg; do
case $arg in
h) usage; exit 0 ;;
q) quiet=true ;;
f) force=true ;;
u) commands+=(update);;
m) commands+=(missing);;
esac
shift $((OPTIND-1))
done
TMPDIR=$(mktemp -d)
[[ -z ${TMPDIR} ]] && exit 1
# TODO this is all for debugging
msg2 "force: $force quiet: $quiet repos: $@ commands: ${commands[@]}"
msg2 "read lastsync: $([ -e $lastsyncfile ])"
${commands[0]} ${@}
#rm -rf ${TMPDIR}
exit $?
|