diff --git a/config.sh b/config.sh index 03ad7c8..6750a36 100755 --- a/config.sh +++ b/config.sh @@ -33,18 +33,21 @@ function _configure() { --type directory --format '"\"{}\""' --max-depth 3 --case-sensitive'''} # WARN: this section is not tested - # [ -z "${ETOOLS_FIND_CMD}" ] && ETOOLS_FIND_CMD="find" - # [ -z "${ETOOLS_FIND_ARGS}" ] && \ - # ETOOLS_FIND_ARGS="\ - # ! -path './profile*' ! -path './scripts*' -path './eclass*' ! -path './metadata*' \ - # ! -path './app-emacs*' ! -path './dev-ruby*' ! -path './acct-user*' ! -path './acct-group*' \ - # -type d -name" + # ETOOLS_FIND_CMD=${ETOOLS_FIND_CMD:-"find"} + # ETOOLS_FIND_ARGS=${ETOOLS_FIND_ARGS:-''' \ + # \( -path './profiles' -o -path './scripts' -o -path './eclass' -o -path './metadata' \ + # -o -path './dev-perl' -o -path './dev-ml' -o -path './app-doc' -o -path './app-emacs' \ + # -o -path './dev-ruby' -o -path './acct-user' -o -path './acct-group' -o -path './.git' \) \ + # -prune -o -type d -maxdepth 3 -print -name'''} # A full command that may be specified if FIND_CMD and FIND_ARGS cannot fulfill the needed job # use {package} as a wildcart for the package that should be search for # use {repo} as a wildcart for the repo to search # use any other variable from ETOOLS inside it # [ -z "${ETOOLS_FIND_COMMAND}" ] && ETOOLS_FIND_COMMAND="fd ${ETOOLS_FIND_ARGS} {package} {repo}" + + ETOOLS_CHECK_LIVE=true + ETOOLS_CHECK_TESTING=true # The default path in which to look for the repos ETOOLS_REPO_PATH=${ETOOLS_REPO_PATH:-"/var/db/repos"} diff --git a/etools b/etools index 1874749..ff341ed 100644 --- a/etools +++ b/etools @@ -39,7 +39,47 @@ function etools_smart_find() { } +# NOTE: get latest enabled version for a given package name +function etools_get_version() { + [ -z "$1" ] && eerror "Pass a package name to this function" "\n" && return 2; + [ ! -d ${ETOOLS_REPO_PATH}/*/$1 ] && \ + [ "$ETOOLS_DEBUG" ] && einfo "checked path ${ETOOLS_REPO_PATH}/*/$1" "\n" && \ + [ ! -d /var/db/repos/*/$1 ] && \ + [ "$ETOOLS_DEBUG" ] && einfo "checked path /var/db/repos/*/$1" "\n" && \ + eerror "Pass a package name to this function" "\n" && \ + return 2; + + local latest=: + . ./helper.sh + [ "$ETOOLS_CHECK_LIVE" ] && if _matches_live "$1"; then + latest="$(ls -1vr ${ETOOLS_REPO_PATH:-/var/db/repos}/*/$1/*9999*.ebuild | head "-${2:-1}" 2>/dev/null)" + [ -n "$latest" ] && latest=${latest%.ebuild} && latest=${latest##*-} && \ + ./helper.sh && echo "$latest" && return 0; + fi + if [ "$ETOOLS_CHECK_TESTING" ]; then + . /etc/portage/make.conf + local arch=: + arch=$(uname -m) + if [ "$arch" = "x86_64" ]; then + arch="amd64" + fi + if [[ "$ACCEPT_KEYWORDS" == *"~$arch"* ]] || _matches_testing "$1" "\~$arch"; then + latest=$(_get_latest "$1" "${package_offset[$1]:-0}" "~$arch") + fi + [ -n "$latest" ] && latest=${latest%.ebuild} && latest=${latest##*-} && \ + ./helper.sh && echo "$latest" && return 0; + fi + _get_latest "$1" "${package_offset[$1]:-0}" "$arch" + + # WARN: this will not work for revision, because of the second expansion + [ -n "$latest" ] && latest=${latest%.ebuild} && latest=${latest##*-} && \ + ./helper.sh && echo "$latest" && return 0; + ewarn "No version found for package: $1$( (( ! offset == 0 )) && echo " with offset $offset")" +} + + # INFO: unset all variables +# TODO: move config options to config unset function etools_unset() { for variable in \ ETOOLS_FIND_CMD \ @@ -54,12 +94,15 @@ function etools_unset() { ETOOLS_DEBUG \ etools_configure \ etools_smart_find \ + etools_get_version \ etools_unset \ einfo \ ewarn \ eerror; do unset $variable + ./helper.sh + ./config.sh done } diff --git a/helper.sh b/helper.sh index 32d0c83..1b9feaa 100755 --- a/helper.sh +++ b/helper.sh @@ -11,6 +11,9 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then _etools_print_assoc_array \ _debug_time \ _most_probable \ + _matches_live \ + _matches_testing \ + _get_latest \ _filter; do unset $function || echo "failed to unset function: $function" @@ -23,11 +26,11 @@ function _formatted_find() { # use wordsplitting # shellcheck disable=SC2086 fd $ETOOLS_FIND_ARGS "${1}" "${2}" - [ "$ETOOLS_DEBUG" = "true" ] && einfo fd "${1}" "${2}" "$ETOOLS_FIND_ARGS" >&2 + [ "$ETOOLS_DEBUG" = "true" ] && einfo fd "${1}" "${2}" "$ETOOLS_FIND_ARGS" "\n" >&2 else # shellcheck disable=SC2086 "$ETOOLS_FIND_CMD" "${2}" $ETOOLS_FIND_ARGS "${1}" - [ "$ETOOLS_DEBUG" = "true" ] && einfo "${ETOOLS_FIND_CMD}" "${2}" "$ETOOLS_FIND_ARGS" "${1}" >&2 + [ "$ETOOLS_DEBUG" = "true" ] && einfo "${ETOOLS_FIND_CMD}" "${2}" "$ETOOLS_FIND_ARGS" "${1}" "\n" >&2 fi else # TODO: do this with bash variable expansion only @@ -156,3 +159,42 @@ function _filter() { # _etools_print_assoc_array _get_high } + + +function _matches_live() { + [ ! -f /etc/portage/package.accept_keywords ] && \ + [ ! -d /etc/portage/package.accept_keywords ] && \ + return 1; + "$ETOOLS_GREP_CMD" "^=$1-9999" /etc/portage/package.accept_keywords >>/dev/null +} + +function _matches_testing() { + [[ "$ACCEPT_KEYWORDS" == *"~amd64"* ]] && return 0; + [ ! -f /etc/portage/package.accept_keywords ] && \ + [ ! -d /etc/portage/package.accept_keywords ] && \ + return 1; + + "$ETOOLS_GREP_CMD" \ + "$([ "$ETOOLS_GREP_CMD" == "grep" ] && echo "-E")" \ + "^$1-[0-9r-]+ *((~|\*)\*|$2)" /etc/portage/package.accept_keywords +} + +function _get_latest() { + local offset=$2 + local latest=: + shopt -s globstar nullglob + for ebuild in $(ls -1vr /var/db/repos/*/$1/*.ebuild); do + if [[ ! "$ebuild" == *"9999"* ]]; then + . "$ebuild" >>/dev/null 2>/dev/null + if [[ "$KEYWORDS" == *"$3"* ]]; then + latest="$ebuild" + offset=$((offset - 1)) + if (( offset < 0 )); then + break; + fi + fi + fi + done + (( ! offset < 0 )) && ewarn "Unused offset of: $offset" + echo "$latest" +}