From fbad1405cd0531e737b937291cf46bb4a6423268 Mon Sep 17 00:00:00 2001 From: Nicolas Reynolds Date: Fri, 15 Jul 2011 00:53:57 -0300 Subject: Toru now caches all it's processes for better performance --- toru | 120 +++++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 46 deletions(-) diff --git a/toru b/toru index 2d42abc..f74581d 100755 --- a/toru +++ b/toru @@ -34,13 +34,17 @@ function in_array { # usage : in_array( $needle, $haystack ) # Stores the lastsync date lastsync() { - [ -e ${lastsyncfile} -a ! -w ${lastsyncfile} ] && { - error "The sync date can't be saved. ${lastsyncfile} isn't writable." - return 1 - } + local lastsyncfile - date +%s > ${lastsyncfile} - touch ${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 @@ -59,9 +63,7 @@ store_cache() { # Return cache contents # $1 cache name read_cache() { - [ ! -e "$1" ] && return 1 - - cat ${TORUPATH}/${1}.cache + cat ${TORUPATH}/${1}.cache 2>/dev/null return $? } @@ -93,7 +95,6 @@ get_db_contents() { 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() { @@ -117,6 +118,13 @@ 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: @@ -125,19 +133,22 @@ extract_fullpkgver() { # * 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 - if ! in_array ${_repo} ${REPOS[@]}; then - warning "You don't have access to this repo (check REPOS at libretools.conf)" - continue - fi +# 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 @@ -152,23 +163,20 @@ update() { # Inform how many PKGBUILDS were found and quit immediately if none $quiet || msg "Found $((${#pkgbuilds[*]}-1)) PKGBUILDs to update" - if [ ${#pkgbuilds[*]} -eq 1 ]; then - $quiet || msg2 "There's nothing to be done. Phew!" - exit 0 - fi # Traverse all found PKGBUILDs for _pkgbuild in ${pkgbuilds[@]}; do +# Update the sync file because there are pkgbuilds to update + update_sync_file=true -# The repo name is guessed -# You *must* use repo/pkgbase structure +# Guess pkgbase from PKGBUILD's basedir _pkgpath=$(dirname "${_pkgbuild}") _pkgbase=$(basename "${_pkgpath}") # Load PKGBUILD's metadata source ${_pkgbuild} -# We won't need this +# 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 @@ -187,43 +195,65 @@ update() { 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})) + 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 + print_package_array "${packages_in_abs[@]}" > ${TMPDIR}/packages_in_abs + print_package_array "${packages_in_sync[@]}" > ${TMPDIR}/packages_in_sync - unset packages_in_abs package_in_sync +# Work with files + unset packages_in_abs package_in_sync -# Use a different separator for pkgnames and pkvers -# so we can join them by pkgname - 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 +# 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" + $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) + 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 - if [ "${_syncver}" != "${_absver}" ]; then - $quiet || msg2 "$_pkg update from $_syncver to $_absver" - $quiet && echo "$_pkg" - fi - done # end need_line +# 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 - unset _pkg _syncver _absver need_line # Save the cache - store_cache ${_repo} ${TMPDIR}/packages_in_abs + store_cache ${_repo} ${TMPDIR}/packages_in_abs - done # end repos + # print_package_array "${updates[@]}" > ${TMPDIR}/updates + store_cache ${_repo}.updates ${TMPDIR}/updates - lastsync + 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) @@ -250,8 +280,6 @@ while getopts 'hqfum' arg; do shift $((OPTIND-1)) done -# This is the syncfile, stores the last date as content and mtime -lastsyncfile=${TORUPATH}/lastsync TMPDIR=$(mktemp -d) -- cgit v1.2.2