Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[EBUILD] pmg-tools manage portage.{use,...} [UPDAT 20063005]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
distances
n00b
n00b


Joined: 03 May 2006
Posts: 15
Location: France

PostPosted: Sun May 21, 2006 7:11 pm    Post subject: [EBUILD] pmg-tools manage portage.{use,...} [UPDAT 20063005] Reply with quote

Last update [2006-30-05]

ChangeLog [2006-30-05]

  • corrected a forgotten upgrade with function split_pkg :oops:
  • made some corrections on some options -- didn't have time to deeply test everything, so, please! Send bug repports...
  • added skeleton functions to implement next version:

    • find masked or unstable package dependencies and include them automatically or manually
    • shows USEs of the package when using -U (pmg-use) with option -su (just a hack, for now, using raw equery output)



Some links:





Hello!

I've made some tools to manage my custom files in /etc/portage, like package.keywords, .use, mask-unmask, etc.
It's actually far from finished, but is helpful enough yet to avoid annoying echo "bla/bla..." and vim on these files... ;)
For now, it can add, remove, modify and replace entries in these files.
Just take a look and tell me what you think 'bout that...

Code:

Usage: pmg <CONFIG> [OPTIONS] <ACTION> PKG [PKG_CONFIG]

OPTIONS:     
        -d       Delete entrie from config file
        -s       Show actual config for PKG
        -r       Replace actual config with PKG_CFG
        -a       Add new entrie to config
        -h       Show this help
        -q       Execute actions silently, only show errors
        -qq      Print nothing -- Warning: if -y (don't ask) is not set, won't perform any action!
        -y       Perform actions without prompting
        -ss      Show config file and exit
        -v       Show version informations
        -u       Update config after PKG_CFG

CONFIG:
        -K       manage package.keywords
        -U       manage package.use
        -UM      manage package.unmask
        -M       manage package.mask
        -C       manage package.cflags
        -P       manage package.provided


Some sym. links on pmg like pmg-use, pmg-keywords, etc can also be used instead of CONFIG options.

There is an ebuild:
pmg-tools.ebuild

Code:


# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

DESCRIPTION="Bash script to manage unstable packages via /etc/portage"
HOMEPAGE="http://distances.ath.cx/"
SRC_URI="http://distances.ath.cx/files/${P}.tar.bz2"

LICENSE="GPL"
SLOT="0"
KEYWORDS="~x86"

IUSE=""
DEPEND=""
RESTRICT="nomirror"

src_install() {
   exeinto /usr/bin
   dobin pmg
   insinto /usr/share/pmg
   doins messages
   insinto /etc/pmg
   doins pmgrc
}



Here is the code; there are two files: pmg and messages. Put messages in /usr/share/pmg


pmg

Code:


#!/bin/bash
# distances.dbdl -- MMVI
# $Id: pmg 63 2006-05-30 16:49:36Z root $
# $Rev: 63 $
# $Author: root $
# $URL: file:///var/svn_root/admin/pmg-tools/branches/pmg-tools-br-0-29-1/pmg $
#
# pmg -- tool to manage packages in /etc/portage/config.files
# Usage: pmg <CONFIG> <ACTION> <pkg> [FLAGS]
#        pmg[-CONFIG] <action> <pkg> [FLAGS]
#
shopt -s extglob

#
# init config#
[ -f /etc/pmg/pmgrc ] && . /etc/pmg/pmgrc
[ -f $HOME/.pmgrc ] && . $HOME/.pmgrc
COLORS=${COLORS:=1}
LIBRARIES_PATH=${LIBRARIES_PATH:=/usr/share/pmg}
. $LIBRARIES_PATH/messages 2>/dev/null || { echo -ne "Error: LIBRARIES_PATH not found! Can't go on!\n"; exit 1; }
. /etc/make.globals
. /etc/make.conf
 

#set -x
#
# vars #
VERSION='0.9 $Rev 63 $'
AUTHOR="distances -- s.distances.dbdl - at - free fr"
LICENCE=GPL
DATE=20063005

DEFAULT_ARCH=${DEFAULT_ARCH:=~x86}
DEBUG=${DEBUG:=0}

ADD_ENTRIE=0
ADD_COMMENTS=0
DELETE_CONFIG=0
SHOW_CONFIG_FILE=0
SHOW_ONLY=0
REPLACE_CONFIG=0
UPDATE_CONFIG=0
QUIET=0
REALLYQUIET=0
PKG_REPOS="null"
MIN_ARGS=1
DONT_ASK=0

# how will we speak, today?...
LG=${LANG%@*}; LG=${LG%_*}; LG=$(echo $LG | tr '[a-z]' '[A-Z]'); LG=${LG:=EN}
if [ ! "$LG" = "EN" ] && [ ! "$LG" = "FR" ]; then
   LG="EN"; fi

# this array and the VARs fallowing are used to parse valid options for programs that invoque us, as these
#+are sym. links and don't all have the same exact functions, they don't have the same exact options neither...
# the array contains all the options and their descritions (a VAR, that'll be sent to 'mess')
# the VARs below this array contain only indexes from the array that represent valid options for the invoking program
DESCRIPTIONS=(\
-d "OPT_DELETE_ENTRY" \
-s "OPT_SHOW_CONFIG" \
-r "OPT_REPLACE_CONFIG" \
-u "OPT_UPDATE_CONFIG" \
-a "OPT_ADD_ENTRY" \
-h "OPT_HELP" \
-q "OPT_BE_QUIET" \
-qq "OPT_REALLYQUIET" \
-y "OPT_DONT_ASK" \
-ss "OPT_SHOW_CONFIG_FILE" \
-c "OPT_COMMENTS" \
-V "OPT_VERSION" \
-K "OPT_KEYWORDS" \
-U "OPT_USE" \
-UM "OPT_UNMASK" \
-M "OPT_MASK" \
-C "OPT_CFLAGS" \
-P "OPT_PROVIDED" \
-su "OPT_SHOW_EUSES" \
)

VALID_FOR_all="1 3 5 9 11 13 15 17 19 21 23" # -d -s -r -a -h -q -qq -ss -c -V
VALID_FOR_keywords="$VALID_FOR_all"
VALID_FOR_use="$VALID_FOR_all 7 37"
VALID_FOR_unmask="$VALID_FOR_all"
VALID_FOR_mask="$VALID_FOR_all"
VALID_FOR_cflags="$VALID_FOR_all"
VALID_FOR_provided="${VALID_FOR_all}"
VALID_FOR_pmg="$VALID_FOR_all 7 23 25 27 29 31 33 35 37" # everything :)

#
# functions ##

# usage: version [-v|-a]
version() {
    case $1 in
   -a)
       echo "${0##*/} -- licence $LICENCE -- version $VERSION - $DATE -- $AUTHOR"
       ;;
   *)
       echo "$VERSION"
       ;;
    esac
}

usage() {
    local myuse=VALID_FOR_${ME}
    mess -e "${CY}Usage: ${0##*/} ${BL}<CONFIG> [OPTIONS] <ACTION> ${GR}PKG ${YL}[PKG_CONFIG]${NO}\n"
    local -a new_DESC
    for opt_usage in ${!myuse}
      do
      mess -e "\t"${BL}${DESCRIPTIONS[$((${opt_usage}-1))]}${NO}"\t"${CY} ${DESCRIPTIONS[$opt_usage]} ${NO}
    done
    mess -e ""
    exit 1
}

# check if options are valid for this program
# do this by parsing with the array we talked about before (DESCRIPTORS)
# usage: check_options OPT
check_options() {
    local LOPT=$1; shift
    local LLINE=$@
    local myuse=VALID_FOR_${ME}
      #-a|-s|-h|-q|-d|-r)
    case $LOPT in
      *)
      for opt in ${!myuse}
      do
      if [ x${LOPT} = x${DESCRIPTIONS[$((opt-1))]} ]; then
         return 0
         fi
      done
      return 1
      ;;
   esac
      return 0
}

# check for possible package(s) in portage repositories
# usage: c_i_p_e PKG CATEGORY
check_if_pkg_exists() {
   local LPKG=$1
   local LREPOS=$2
   local LPKG_VERSION=${3:-null}
   local LPKG_MODIF=${4:-null}
   local LPKG_REPOS="null"
   if [ "$LREPOS" = "null" -o "$LREPOS" = "" ]; then
      for ovl in ${PORTDIR} ${PORTDIR_OVERLAY}
      do
         pushd $ovl >/dev/null
         for rep in *
         do
            if [ -d ${rep}/${LPKG} ]; then
               LPKG_REPOS=${rep}
               break
            fi
         done
         popd >/dev/null
      done
      if [ ! "$LPKG_REPOS" = "null" ]; then
         PKG_REPOS=$LPKG_REPOS
         #get_ebuild $LPKG_REPOS $LPKG
         return 0
      fi
   else
      for rep in $PORTDIR $PORTDIR_OVERLAY
      do
         if [ -d ${rep%/}/${LREPOS%/}/${LPKG} ]; then
            return 0
         fi
      done
   fi
   return 1
}

# split pkg name in version, repository, etc.
# usage: split_pkg PKG (PKG not necessarilly in the form repos/pkg; can be only pkg -- for external use, however, it should be repos/pkg...)
# usage split_pkg [M][REPOS/]PKG[-VERSION] [DEST_VAR]
split_pkg() {
    local LDEST_VAR=PKG
        [ "$1" = "-d" ] && { LDEST_VAR=$2; shift 2; }
    local LUSER_PKG=${1%.ebuild}
    local LPKG=${LUSER_PKG##*/}
    local LREPOS=${LUSER_PKG%%/*}
    case $LREPOS in
        ">="*|"<="*)
        LDEST_GLE="\${LREPOS:0:2}"
                ldgle="${LDEST_VAR}_GLE"
        LDEST_NAME=${LPKG%%-[0-9]*}
        LDEST_VERSION=${LPKG/${LDEST_NAME}}
        LDEST_REPOS=${LREPOS:2}
        ;; 
        '='*)
        LDEST_GLE='='
        LDEST_NAME=${LPKG%%-[0-9]*}
        LDEST_VERSION=${LPKG/${LDEST_NAME}}
        LDEST_REPOS=${LREPOS:1}
        ;; 
        '<'*|'>'*)
        LDEST_GLE="\${LREPOS:0:1}"=
        LDEST_NAME=${LPKG%%-[0-9]*}
        LDEST_VERSION=${LPKG/${LDEST_NAME}}
        LDEST_REPOS=${LREPOS:1}
        ;; 
      *)
      LDEST_GLE=null
      LDEST_NAME=${USER_PKG##*/}
      LDEST_LNAME=$LDEST_NAME
      LDEST_NAME=${LDEST_NAME%%-[0-9]*}
      LDEST_VERSION=${LDEST_LNAME/${LDEST_NAME}}
      LDEST_REPOS=${USER_PKG%%/*}
      [ x"$LDEST_REPOS" = x"${USER_PKG}" ] && LDEST_REPOS=null
      ;;
   esac
   [ "$LDEST_GLE" == "null" ] && LDEST_GLE=""
   [ "$LDEST_VERSION" == "null" ] && LDEST_VERSION=""
   [ "$LDEST_REPOS" == "$USER_PKG" ] && LDEST_REPOS="null"
   # fill DEST_VAR
   eval $"$LDEST_VAR"_GLE=${LDEST_GLE}
   eval $"$LDEST_VAR"_REPOS=${LDEST_REPOS}
   eval $"$LDEST_VAR"_NAME=${LDEST_NAME}
   eval $"$LDEST_VAR"_VERSION=${LDEST_VERSION}
}

# calls split_pkg and chech_if_pkg_exits then build PKG var, according to which config is managed
# for exemple, package.use doesn't support [><]=repos/pkg-VERSION as a valid atom
# usage: g_r_p_n PKG
get_real_pkg_name() {
    local USER_PKG=$1
    #split PKG in PKG_VERSION, PKG_REPOS, etc.
    split_pkg $USER_PKG
    if ((SHOW_ONLY)); then
      [ ! x"${PKG_REPOS}" = x"null" ] && PKG=${PKG_GLE}${PKG_REPOS}/${PKG_NAME}${PKG_VERSION} || PKG=${PKG_NAME}${PKG_VERSION}
    elif check_if_pkg_exists $PKG_NAME $PKG_REPOS; then
      case $ME in
         use|cflags)
         # package.use don't want of a versionned package...
         # nor my hacked packaged.cflags
         PKG=${PKG_REPOS}/${PKG_NAME}
         ;;
         mask|unmask)
         PKG=${PKG_GLE}${PKG_REPOS}/${PKG_NAME}${PKG_VERSION}
         # don't need FLAGS for these ones
         USER_CFG=""
         PKG_CFG=""
         MIN_ARGS=1
         ;;
         keywords)
         PKG=${PKG_GLE}${PKG_REPOS}/${PKG_NAME}${PKG_VERSION}
         USER_CFG=${USER_CFG:=${DEFAULT_ARCH}}
         ;;
         provided)
         if [ "$PKG_VERSION" = "" ]; then
            # mess ERR_VERSION_REQUIRED FOR $LPORTAGE_CONFIG
            # exit 1
            return 1
         fi
         PKG=${PKG_REPOS}/${PKG_NAME}${PKG_VERSION}
         MIN_ARGS=1
         ;;
         *)
         PKG=${PKG_GLE}${PKG_REPOS}/${PKG_NAME}${PKG_VERSION}
         ;;
      esac
   # all found repositories are separated by spaces, then if we find one, that means there's more than one possible package...
        if [ ! "${PKG_REPOS/ /}" = "${PKG_REPOS}" ]; then
         #mess -e $RD TOO_MANY_REPOS FOR_PKG \"${NO}${GR}${USER_PKG}${NO}\":
         #for atom in $PKG_REPOS
         #do
         #   mess -e "\t${GR}${atom}/${PKG_NAME}${NO}"
         #done
         #exit 1
         return 2
      fi
    else
      #mess -e ERR_PKG_NOT_FOUND FOR_PKG ${GR}${USER_PKG}${NO}
      #exit 1
      return 3
    fi
    return 0
}

# get multiple RE lines
grep_mlines() {
    local LRE="$1"; shift
    local LFILE="$@"
    LOUT="$(sed -n -e '/^'$LRE'/ {
    /.*\"$/ !b label
    b out
    :label
    N
    /\n.*\"$/ !b label
    :out
    p
}' $LFILE)"
   if [ x"${LOUT}" = "" ]; then
      return 1
   else
       echo "$LOUT"
      return 0
   fi
return 1
}

# get package dependencies
# usage: PKG_EBUILD [DEST_VAR]
get_dependencies() {
   local LPKG_EBUILD=${1:-${PKG_EBUILD}}
   [ x"${LPKG_EBUILD}" = x"" ] && return 1
   local LDEST_VAR=${2:-${PKG_DEPEND}}
   local LMDEPENDS=""
   if LMDEPENDS="$(grep_mlines DEPENDS ${LPKG_EBUILD})"; then
      LMDEPENDS=${LMDEPENDS/#DEPENDS=\"}
      LMDEPENDS=${LMDEPENDS/%\"}
   fi
   echo $LMDEPENDS
}

check_dependencies() {
   return 0
}

# get best ebuild for PKG
# usage: get_ebuild [-d DEST_VAR] [PKG_REPOS [PKG_NAME [PKG_VERSION [PKG_LGE]]]]
get_ebuild() {
   local LDEST_VAR=PKG
   [ "$1" = "-d" ] && { LDEST_VAR=$2; shift 2; }
   local LPKG_REPOS=${1:-${PKG_REPOS}}
   local LPKG_NAME=${2:-${PKG_NAME}}
   local LPKG_VERSION=${3:-${PKG_VERSION}}
   local LPKG_LGE=${4:-${PKG_LGE}}
   local LEBUILD=$(equery w ${LPKG_GLE}${PKG_REPOS}${PKG_NAME}${PKG_VERSION})
   # equery doesn't return error code... Why, but why
   [ x"${LEBUILD%%.ebuild}" = x"${LEBUILD}" ] && LEBUILD=""
   eval $"$DEST_VAR"_EBUILD=\$LEBUILD
}

# create backup of given or current config file
# usage: backup_config [PORTAGE_CONFIG]
backup_config() {
    LCFG_FILE=${1:-${LPORTAGE_CONFIG}}
    \cp -f ${LCFG_FILE} $LCFG_FILE.bak && return 0 || cant_write
}

# get values for pkg withing actual or given config file
# usage: get_pkg_config PKG [CONFIG_FILE]
get_pkg_config() {
    local LPKG=${1:-${PKG}}; shift
   local LPKG_REPOS=${PKG_REPOS}
    local LLPORTAGE_CONFIG=${@:-${LPORTAGE_CONFIG}}
   local LPATTERN
   #split_pkg LPKG LDEST_PKG
   # temporary solution while split_pkg's not achieved
   if [ ! "$PKG_REPOS" = "" -a ! "$PKG_REPOS" = "null" ]; then
      LPATTERN=${LPKG_REPOS}/${LPKG/${LPKG_REPOS}\/}
      if [ ! "$PKG_GLE" = "" -a ! "$PKG_GLE" = "null" ]; then
         LPATTERN="${PKG_GLE}${LPATTERN/${PKG_GLE}}"
      fi
   else
      if [ ! "$PKG_GLE" = "" -a ! "$PKG_GLE" = "null" ]; then
         LPATTERN="^${PKG_GLE}.*/${LPKG}"
      else
         LPATTERN="^.*/${LPKG}"
      fi
   fi
   if [ ! "$PKG_VERSION" = "" ]; then
      LPATTERN=${LPATTERN/${PKG_VERSION}}${PKG_VERSION}
   fi
   ((DEBUG)) && echo $LPATTERN
    # if something's found for PKG, then PKG_CFG contains something
    if PKG_CFG=$(grep -w "${LPATTERN}" $LLPORTAGE_CONFIG); then
      ((DEBUG)) && echo -ne "first found:\n$PKG_CFG\n"
      LPKG=${PKG_CFG%% *}; PKG=$LPKG
      PKG_CFG=${PKG_CFG/$LPKG}; PKG_CFG=${PKG_CFG/# }; return 0
   # didn't find something? try to expand a little bit...
   elif PKG_CFG=$(grep -w "${LPATTERN}.*[0-9] " $LLPORTAGE_CONFIG); then
      ((DEBUG)) && echo -ne "second found:\n$PKG_CFG\n"
      LPKG=${PKG_CFG%% *}; PKG=$LPKG
      PKG_CFG=${PKG_CFG/$LPKG}; PKG_CFG=${PKG_CFG/# }; return 0
   # really found nothing ? (package.provided case)
    elif PKG_CFG=$(grep -w "${LPATTERN}" $LLPORTAGE_CONFIG); then
      ((DEBUG)) && echo -ne "first found:\n$PKG_CFG\n"
      LPKG=${PKG_CFG%% *}; PKG=$LPKG
      PKG_CFG=${PKG_CFG/$LPKG}; PKG_CFG=${PKG_CFG/# }; return 0
   elif PKG_CFG=$(grep "${LPKG}.*\$" $LLPORTAGE_CONFIG); then
      ((DEBUG)) && echo -ne "third found:\n$PKG_CFG\n"
      LPKG=${PKG_CFG%% *}; PKG=$LPKG
      PKG_CFG=${PKG_CFG/$LPKG}; PKG_CFG=${PKG_CFG/# }; return 0
   fi
   ((DEBUG)) && echo "lpkg.config: $(grep "${LPKG}" $LLPORTAGE_CONFIG)"
   return 1
}

#
# usage: show_ebuild_uses [ -d DEST_VAR ] PKG_ALL
show_ebuild_uses() {
   local LDEST_VAR=PKG
   [ "$1" = "-d" ] && { DEST_VAR=$2; shift 2; }
   local LPKG_ALL=$1
   local LPKG_EUSES
   LPKG_EUSES="$(equery -N uses -a $LPKG_ALL)"
   eval $"$LDEST_VAR"_EUSES=\$LPKG_EUSES
}

# show actual or given config with plenty of colors :)
# usage: show_config [-i|-e] [PKG [PKG_CFG]]
show_config() {
    [ "$1" = "-e" -o "$1" = "-i" ] && { local MOPT=$1; shift; }
    local LPKG=${1:-${PKG}}; shift
    local LPKG_CFG=${@:-${PKG_CFG}}
   use_msg=$(echo " $LPKG_CFG" | sed -e "s:\([+\ ][a-z]*[-]*[0-9a-z]*\):"$BL"\1"$NO":g;s:\(\ -[a-z]*[-]*[0-9a-z]*\):"$RD"\1"$NO":g;s:~amd64\|~hppa\|~ia64\|~mips\|~ppc\|~ppc64\|~sparc\|~x86\|\-\*\|~\*:${RD}&${NO}:g;s:\ \+: :g;s:^\ ::;s:\ $::")
    mess $MOPT "${GR}${LPKG}${NO}${use_msg}"
}

# here we are, the big stuff!
update_config() {
   # is there really a change ?
    if [ ! x${USER_CFG// } = x${PKG_CFG// } ]; then
   local NEW_PKG_CFG=" $PKG_CFG "

        # look inside the users given flags
   for u in $USER_CFG
     do
     local tmp_NP=$NEW_PKG_CFG
     local REPLACED=0
          # to manage signs +- and space in front of flag
     usign="${u:0:1}"
     case $usign in
              +)
        u=${u:1}
        usign=""
        ;;
         -)
        u=${u:1}
        usign="-"
        ;;
         *)
        usign=""
        ;;
     esac

     # do we have a "no sign" in USEs matching our FLAG?
     if [ ! "${tmp_NP}" = "${NEW_PKG_CFG// ${u} }" ]; then
         # if then, is our $sign$flag is the same ?
         if [ "${tmp_NP}" = "${NEW_PKG_CFG// ${u} / ${usign}${u} }" ]; then
        # yes, it is... No change, then
        REPLACED=1
         else
        # ok, let me put my sign into you :/
        NEW_PKG_CFG=${NEW_PKG_CFG// ${u} / ${usign}${u} }; REPLACED=1; fi
     fi
     # or do we have a sign ?
       if ((!$REPLACED)) && [ ! "${tmp_NP}" = "${NEW_PKG_CFG// [-+]${u} }" ]; then
         if [ "${tmp_NP}" = "${NEW_PKG_CFG// [-+]${u} / ${usign}${u} }" ]; then
        REPLACED=1
         else
        # let's exchange signs, together...
        NEW_PKG_CFG=${NEW_PKG_CFG// [-+]${u} / ${usign}${u} }; REPLACED=1; fi
     fi
     if ((! $REPLACED)) && [ x"${NEW_PKG_CFG// }" = x"${tmp_NP// }" ]; then
         NEW_PKG_CFG="${NEW_PKG_CFG/% } ${usign}${u/# }"; fi
   done
   # has something changed?
   if [ ! x"${NEW_PKG_CFG// }" = x"${PKG_USE// }" ]; then
            # question users about their real motivations...
       if ((! $DONT_ASK)); then
      mess -i ABOUT_TO_UPDATE FOR_PKG ${GR}${PKG} IN ${YL}${LPORTAGE_CONFIG}${NO}"\n" FROM
      show_config -i
      mess -i TO
      show_config -i $PKG $NEW_PKG_CFG
      if ! ask_user; then mess -e ERR_ABORTED_BY_USER; exit 1; fi
       fi
            # make a safety backup
       backup_config
            # sed here is much more efficient
       sed -i "s:${PKG_CFG}:${NEW_PKG_CFG/# }:" $LPORTAGE_CONFIG >/dev/null || cant_write
       mess CONFIG_CHANGED FOR_PKG ${GR}$PKG${NO}
      fi
   else
      mess NOTHING_TO_CHANGE FOR_PKG ${GR}${PKG} IN ${YL}${LPORTAGE_CONFIG}${NO}
    fi

}

# the easy-with-sed-in-place replace string function
replace_config() {
    if [ ! x${USER_CFG// } = x${PKG_CFG// } ]; then
      if ((! $DONT_ASK)); then
         mess -e ABOUT_TO_REPLACE ${GR}${PKG}${NO}
         show_config
         mess -e FROM
         mess -e ${YL}${LPORTAGE_CONFIG}
         mess -e TO
         show_config $PKG $USER_CFG
         if ! ask_user; then
            mess ERR_ABORTED_BY_USER && exit 1
         fi
      fi
      backup_config
      sed -i "s:^"$PKG".*$:${PKG} ${USER_CFG}:" $LPORTAGE_CONFIG || cant_write
      mess CONFIG_CHANGED FOR_PKG ${GR}$PKG"\n"
   else
      mess NOTHING_TO_CHANGE FOR_PKG ${GR}${PKG} IN ${YL}${LPORTAGE_CONFIG}${NO}
      exit 0
   fi
}

# and the real-still-with-sed-easy delete function
delete_config() {
    if ((! $DONT_ASK)); then
   mess -e ABOUT_TO_DELETE ${GR}${PKG} FROM ${YL}${LPORTAGE_CONFIG}${NO}
   if ! ask_user; then mess ERR_ABORTED_BY_USER; exit 1; fi
    fi
    backup_config
    sed -i 's:^'$PKG'.*$::;/^$/ d' $LPORTAGE_CONFIG >/dev/null || cant_write
    show_config
    mess HAS_BEEN_DELETED FROM ${YL}${LPORTAGE_CONFIG}${NO}
}

# this one is so easy it does even not use sed :)
# usage add_entrie [PKG[PKG_CFG]]
add_entrie() {
    local LPKG=${1:-${PKG}}; shift
    local LUSER_CFG=${@:-${USER_CFG}}
    echo -ne "${LPKG} ${LUSER_CFG}\n" >> $LPORTAGE_CONFIG || cant_write
    mess ${GR}${LPKG} HAS_BEEN_ADDED IN ${YL}${LPORTAGE_CONFIG}${NO}
}

# oops... some kinda write access problem!... Are you root?...
cant_write() {
    mess -e ERR_CANT_WRITE_TO_CONFIG ${YL}${LPORTAGE_CONFIG}${NO}
    exit 1
}

# ask only one but strict thing: are you still ok ?
ask_user() {
    ((REALLYQUIET)) && return 1
    local LMSG=$(mess -i REALLY_WANNA_DO_THAT)
    echo -ne ${LMSG}
    resp='N'; read resp
    [ "$resp" = "Y" -o "$resp" = "y" ] && return 0
    return 1
}
   
#
# main ############################

#
# init ##

# first of all,
# tell me my name, I'll tell you what I'll manage...
ME=${0##*/}; ME=${ME##*-}
((DEBUG)) && echo "me: $ME"
if [ x"${0##*/}" = x"${ME}" ]; then
   ((DEBUG)) && echo "looking for my name..."
   unset ME
   # parse command line options
   case $1 in
      -U|--use|use)
      ME=use
      shift
      ;;
      -K|--keywords|--keyword|--keywor|--keywo|--keyw|--key|key)
      ME=keywords
      shift
      ;;
      -C|[--]cflags|[--]cflag|[--]cfla|[--]cfl)
      ME=cflags
      shift
      ;;
      -UM|--unmask|--unmas|--unma|--unm)
      ME=unmask
      shift
      ;;
      -M|--mask|--mas)
      ME=mask
      shift
      ;;
      -P|--provided|--provide|--provid|--provi|--prov|--pro)
      ME=provided
      shift
      ;;
      -h|--help)
      ME=pmg
      usage
      shift
      ;;
      -V)
      version
      shift
      exit 0
      ;;
      --version|--versio|--versi|--vers|--ver)
      version -a
      shift
      exit 0
      ;;
   esac
fi

# I feel like I'm nobody...
[ -z $ME ] && { mess -e ERR_MISSING_CONFIG; ME=pmg; usage; }

PORTAGE_CONFIG=${PORTAGE_CONFIG:=/etc/portage}
case $ME in
   provided)
   LPORTAGE_CONFIG=${PORTAGE_CONFIG}/profile/package.${ME}
   ;;
   *)
   LPORTAGE_CONFIG=${PORTAGE_CONFIG}/package.${ME}
   ;;
esac

# check if ACTION is valid
i=0; END_OPTIONS=0
while [ $i -lt $# ] || ((! $END_OPTIONS))
do
   # the check option will let us know if given arguments are corrects
   case $1 in [a-z]*|'<='[a-z]*|'>='[a-z]*|'='[a-z]*)break;;esac
   if check_options $1; then
      case $1 in
         -y) DONT_ASK=1; shift;;
         -h) usage;;
         -su) SHOW_EUSES=1; shift; MIN_ARGS=1;;
         -ss) cat $LPORTAGE_CONFIG; exit 0;;
         -s) SHOW_ONLY=1; shift;;
         -r) REPLACE_CONFIG=1;MIN_ARGS=2;shift;;
         -u) UPDATE_CONFIG=1;MIN_ARGS=2;shift;;
         -d) DELETE_CONFIG=1;shift;;
         -q) QUIET=1;shift;;
         -qq) REALLYQUIET=1;shift;;
         -c) ADD_COMMENTS=1; shift; MIN_ARGS=1;;
         -a) ADD_ENTRIE=1; MIN_ARGS=2
         case $ME in
            unmask|mask|keywords|provided) MIN_ARGS=1
         esac
         shift
         ;;
         -V)
         version
         shift
         exit 0
         ;;
         --version|--versio|--versi|--vers|--ver)
         version -a
         shift
         exit 0
         ;;
         --) END_OPTIONS=1; shift;;
         *) END_OPTIONS=1; break;;
         #'.'[a-z]|'>='[a-z]*|'<='[a-z]*|'='[a-z]*) END_OPTIONS=1; break;;
         #*)
         #    mess -e ERR_MISSING_ACTION; usage;;
      esac
   else
      usage
   fi
done

# MIN_ARGS is 1 by default; however, some options, like the '-r' one, need at least two arguments to work
# this is done by the case-select, just up-there
[ -z "$1" ] && { mess -e ERR_MISSING_PKG; usage; }
[ $# -lt $MIN_ARGS ] && { mess -e ERR_MISSING_FLAGS; usage; }

USER_PKG=$1
shift
USER_CFG=$@

#get_real_pkg_name $USER_PKG
get_real_pkg_name $USER_PKG
resp=$?
if ! ((ADD_COMMENTS)); then
   case $resp in
      1)
      mess ERR_VERSION_REQUIRED FOR $LPORTAGE_CONFIG
      exit 1
      ;;
      2)
      mess -e $RD TOO_MANY_REPOS FOR_PKG \"${NO}${GR}${USER_PKG}${NO}\":
      for atom in $PKG_REPOS
      do
         mess -e "\t${GR}${atom}/${PKG_NAME}${NO}"
      done
      exit 1
      ;;
      3)
      mess -e ERR_PKG_NOT_FOUND FOR_PKG ${GR}${USER_PKG}${NO}
      exit 1
      ;;
      *)
      ((DEBUG)) && echo "autre sortie ($?)"
      ;;
   esac
fi
# in the check down-there, SHOW_ONLY is absent as it is the default
# that means one can use pmg-use vim,
# it'll show the config for vim
# ADD_ENTRIE is both in pkg_cfg found and not found, to check config's not already present
if get_pkg_config $PKG && ! ((ADD_COMMENTS)) && ! ((SHOW_EUSES)); then
   # we check that only one config exists for that package...
   if [ $(echo "$PKG_CFG" | wc -l) -gt 1 ] || [ $(echo "$PKG" | wc -l) -gt 1 ]; then
      USER_PKG=${USER_PKG//.\*}
      mess -e ERR_TOO_MANY_PKG FOR_REQUEST ${YL}${USER_PKG}${NO}:"\n${PKG} ${PKG_CFG//${USER_PKG}/${RD}${USER_PKG}${NO}}";  exit 1; fi
   if ((ADD_ENTRIE)); then
      mess -e ERR_PKG_EXISTS ${GR}$PKG IN ${YL}${LPORTAGE_CONFIG}${NO} ; exit 1
   elif ((UPDATE_CONFIG)); then
      update_config $PKG "$PKG_CFG" || exit 1
   elif ((REPLACE_CONFIG)); then
      replace_config $PKG "$PKG_CFG" || exit 1
   elif ((DELETE_CONFIG)); then
      delete_config $PKG "$PKG_CFG" || exit 1
   else
      # default -- show_config
      mess CONFIG_FOUND FOR_REQUEST ${GR}${PKG} IN ${YL}${LPORTAGE_CONFIG}${NO}:
      show_config -i ${PKG} "$PKG_CFG" && exit 0
   fi
elif ((ADD_COMMENTS)); then
   add_entrie "# COMMENT " "$USER_PKG $USER_CFG" || exit 1
elif ((SHOW_EUSES)); then
   mess MSG_PLEASE_WAIT
   if ! show_ebuild_uses ${PKG_GLE}${PKG_REPOS}/${PKG_NAME}${PKG_VERSION}; then
      mess ERR_EQUERY
   else
      echo "$PKG_EUSES"
   fi
elif ((ADD_ENTRIE)); then
   if ! check_dependencies; then
      mess DEPENDENCIES_FOUND
   fi
   add_entrie $PKG "$USER_CFG" || exit 1
else
   mess ERR_CONFIG_NOT_FOUND IN ${YL}${LPORTAGE_CONFIG} FOR_PKG ${GR}$PKG${NO}
   exit 1
fi

exit 0
bye! bye!




message
Code:


#!/bin/bash
# distances.dbdl -- MMVI
# $Author: root $
# $Id: messages 61 2006-05-30 16:50:10Z root $
# $Rev: 61 $
# $URL: file:///var/svn_root/admin/pmg-tools/branches/pmg-tools-br-0-29-1/messages $

# localized messages and some colors
if ((COLORS)); then
NO=$'\x1b[0;0m'
BR=$'\x1b[0;01m'
RD=$'\x1b[31;01m'
GR=$'\x1b[32;01m'
YL=$'\x1b[33;01m'
BL=$'\x1b[34;01m'
FC=$'\x1b[35;01m'
CY=$'\x1b[36;01m'
fi

LG_VARS=(\
"BY" \
"IN" \
"FOR" \
"FOR_PKG" \
)

EN_VARS=(\
"by" \
"in" \
"for" \
"for package" \
)

FR_VARS=(\
"par" \
"dans" \
"pour" \
"pour le paquet" \
)

index_of() {
   imax=${#LG_VARS[@]}
   [ -z $1 ] && return 1
   XVAR=${1}
   for ((i=0; i<imax;i++))
   do
      if [ "$XVAR" = "${LG_VARS[$i]}" ]; then
         echo $i
         return 0
      fi
   done
   return 1
}

lg_mess() {
local ERROR=0 INFO=0
local msg show_msg
case "$1" in
   error|-e) ERROR=1; shift;;
   infos|-i) INFO=1; shift;;
esac

if ((INFO)) || ((! QUIET)) || ((ERROR)); then
   if ((! REALLYQUIET)); then
      for m in $@
      do
         #if [ ! x"${!msg}" = x"" ]; then
         if idx=$(index_of $m); then
            msg="${LG}_VARS[$idx]"
            if [ ! "${msg}" = "" ]; then
               if ((COLORS)); then
                  case $m in
                     OPT_*) msg="${CY}${!msg}${NO}";;
                     ERR_*|WARNING) msg="${RD}${!msg}${NO}";;
                     ABOUT_*) msg="${RD}${!msg}${NO}";;
                     *) msg="${CY}${!msg}${NO}";;
                  esac
               else
                  msg="${!msg}"
               fi
               [ x"$show_msg" = x"" ] && show_msg="${msg}" || show_msg="${show_msg} ${msg}"
            else
               [ x"$show_msg" = x"" ] && show_msg="${1}" || show_msg="${show_msg} ${1}"
            fi
         fi
         shift
      done
   fi
fi

if ((! REALLYQUIET)); then
   if ((ERROR)); then
      echo -ne "${show_msg}\n" >&2
   elif ((INFO)) || ((!QUIET)); then
      echo -ne "${show_msg}\n"
   fi
fi
}


# EN -- English
EN_BY="by"
EN_IN="in"
EN_FOR="for"
EN_FOR_PKG="for package"
EN_FOR_REQUEST="for request"
EN_TO="to"
EN_WARNING="Warning!"
EN_ERR_CONFIG_NOT_FOUND="Config not found"
EN_ERR_BAD_EXEC="Please do not use ${0##*/} directly; use one of ${0##*/}-{use,package,mask,...} instead."
EN_ERR_CANT_WRITE_TO_CONFIG="Can't write in config file"
EN_ERR_PKG_NOT_FOUND="No valid package were found in portage tree"
EN_ERR_TOO_MANY_REPOS="Many reposotories were found"
EN_ERR_PKG_EXISTS="Package already exists"
EN_ERR_TOO_MANY_PKG="Many entries found"
EN_ERR_ABORTED_BY_USER="Aborted by user"
EN_ERR_MISSING_CONFIG="Target config missing"
EN_ERR_MISSING_ACTION="Action parameter missing"
EN_ERR_MISSING_PKG="Missing package name"
EN_ERR_MISSING_FLAGS="Missing flags parameters"
EN_ERR_VERSION_REQUIRED="Version required"
EN_CONFIG_FOUND="Found config"
EN_CONFIG_CHANGED="Congiguration changed with success"
EN_FROM="from"
EN_NOTHING_TO_CHANGE="Nothing to change"
EN_HAS_BEEN_DELETED="has been deleted"
EN_HAS_BEEN_ADDED="has been added"
EN_OPT_DELETE_ENTRY="Delete entrie from config file"
EN_OPT_SHOW_CONFIG="Show actual config for PKG"
EN_OPT_REPLACE_CONFIG="Replace actual config with PKG_CFG"
EN_OPT_UPDATE_CONFIG="Update config after PKG_CFG"
EN_OPT_ADD_ENTRY="Add new entrie to config"
EN_OPT_HELP="Show this help"
EN_OPT_DONT_ASK="Perform actions without prompting"
EN_OPT_REALLYQUIET="Print nothing -- Warning: if -a (don't ask) is not set, won't perform any action!"
EN_OPT_BE_QUIET="Execute actions silently, only show errors"
EN_OPT_SHOW_CONFIG_FILE="Show config file and exit"
EN_OPT_VERSION="Show version informations"
EN_OPT_USE="manage package.use"
EN_OPT_UNMASK="manage package.unmask"
EN_OPT_MASK="manage package.mask"
EN_OPT_CFLAGS="manage package.cflags"
EN_OPT_KEYWORDS="manage package.keywords"
EN_OPT_PROVIDED="manage package.provided"
EN_ABOUT_TO_DELETE="Config will be deleted"
EN_ABOUT_TO_REPLACE="Config will be replaced"
EN_ABOUT_TO_UPDATE="Config will be updated"
EN_REALLY_WANNA_DO_THAT="Are you sure you wan't to perform this action ? [N/y]"


# FR -- Français
FR_BY="par"
FR_IN="dans"
FR_FOR="pour"
FR_FROM="de"
FR_TO="vers"
FR_WARNING="Attention!"
FR_FOR_PKG="pour le paquet"
FR_FOR_REQUEST="pour la recherche"
FR_ERR_CONFIG_NOT_FOUND="Aucune configuration trouvée"
FR_ERR_PKG_NOT_FOUND="Aucun paquet trouvé dans l'arbre portage"
FR_ERR_TOO_MANY_REPOS="Plusieurs paquets on été trouvés"
FR_ERR_CANT_WRITE_TO_CONFIG="Ne peut pas écrire dans le fichier de configuration"
FR_ERR_ABORTED_BY_USER="Opération interrompue par l'utilisateur!"
FR_ERR_PKG_EXISTS="Le paquet existe déjà"
FR_ERR_TOO_MANY_PKG="Plusieurs entrées existent"
FR_ERR_MISSING_CONFIG="Configuration cible manquante"
FR_ERR_MISSING_ACTION="Paramètres d'action manquants pour la commande"
FR_ERR_MISSING_PKG="Nom du paquet manquant"
FR_ERR_MISSING_FLAGS="Paramètres de configuration manquants"
FR_ERR_VERSION_REQUIRED="Une version est requise"
FR_CONFIG_FOUND="Configuration trouvée"
FR_CONFIG_CHANGED="La configuration a été changée avec succès"
FR_NOTHING_TO_CHANGE="Rien à modifier"
FR_HAS_BEEN_DELETED="a été effacé"
FR_HAS_BEEN_ADDED="a été ajouté"
FR_OPT_DELETE_ENTRY="Effacer l'entrée du fichier de configuration"
FR_OPT_SHOW_CONFIG="Affiche la configuration actuelle du paquet"
FR_OPT_REPLACE_CONFIG="Remplace la configuration actuelle par PKG_CFG"
FR_OPT_UPDATE_CONFIG="Modifie la configuration actuelle selon PKG_CFG"
FR_OPT_ADD_ENTRY="Ajoute une nouvelle entrée dans la configuration"
FR_OPT_HELP="Affiche cet écran"
FR_OPT_VERSION="Affiche les informations de version"
FR_OPT_DONT_ASK="Effectue les actions sans confirmation"
FR_OPT_BE_QUIET="Exécute les actions silencieusement, n'affiche que les erreurs"
FR_OPT_REALLYQUIET="N'affiche absolument aucune sortie -- Attention: si -y (\"sans confimation\") n'est pas activé, n'effectue aucune action!"
FR_OPT_SHOW_CONFIG_FILE="Affiche le fichier de configuration en cours"
FR_OPT_USE="gère package.use"
FR_OPT_UNMASK="gère package.unmask"
FR_OPT_MASK="gère package.mask"
FR_OPT_CFLAGS="gère package.cflags"
FR_OPT_KEYWORDS="gère package.keywords"
FR_OPT_PROVIDED="gère package.provided"
FR_ERR_BAD_EXEC="Veuillez ne pas utiliser ${0##*/} directement: utilisez plutôt l'un des ${0##*/}-{use,package,mask,...}."
FR_ABOUT_TO_DELETE="La configuration va être effacée"
FR_ABOUT_TO_REPLACE="La configuration va être écrasée"
FR_ABOUT_TO_UPDATE="La configuration va être modifiée"
FR_REALLY_WANNA_DO_THAT="Voulez-vous poursuivre ? [N/y] \"y\" pour \"oui\""


# Show localized messages
# usage: mess [-e] [LOCALE_VAR] [text]
# -e: show message despite of QUIET but not of REALLYQUIET (use to print errors)
# ex. mess Attention: PKG_NOT_FOUND FOR_PKG $PKG

messinfo() { mess info $@; }
messerror() { mess error $@; }
mess() {
    local ERROR=0 INFO=0
    local msg show_msg
    case "$1" in
   error|-e) ERROR=1; shift;;
   infos|-i) INFO=1; shift;;
    esac
   
    if ((INFO)) || ((! QUIET)) || ((ERROR)); then
   if ((! REALLYQUIET)); then
       for m in $@
         do
         msg="${LG}_$m"
         if [ ! x"${!msg}" = x"" ]; then
          if ((COLORS)); then
            case $m in
           OPT_*) msg="${CY}${!msg}${NO}";;
           ERR_*|WARNING) msg="${RD}${!msg}${NO}";;
           ABOUT_*) msg="${RD}${!msg}${NO}";;
           *) msg="${CY}${!msg}${NO}";;
            esac
        else
            msg="${!msg}"
        fi
        [ x"$show_msg" = x"" ] && show_msg="${msg}" || show_msg="${show_msg} ${msg}"
         else
        [ x"$show_msg" = x"" ] && show_msg="${1}" || show_msg="${show_msg} ${1}"
         fi
         shift
       done
   fi
    fi

    if ((! REALLYQUIET)); then
   if ((ERROR)); then
       echo -ne "${show_msg}\n" >&2
   elif ((INFO)) || ((!QUIET)); then
       echo -ne "${show_msg}\n"
   fi
    fi
}






Please, if you find it useful, useless, have some contributions, corrections, suggestions...

Let me know ;)


Last edited by distances on Tue May 30, 2006 8:49 pm; edited 5 times in total
Back to top
View user's profile Send private message
steveb
Advocate
Advocate


Joined: 18 Sep 2002
Posts: 4564

PostPosted: Sun May 21, 2006 9:49 pm    Post subject: Reply with quote

Uhhh... this is a big shell script :)
Back to top
View user's profile Send private message
distances
n00b
n00b


Joined: 03 May 2006
Posts: 15
Location: France

PostPosted: Tue May 30, 2006 6:16 pm    Post subject: Reply with quote

steveb wrote:
Uhhh... this is a big shell script :)


Well, yes... Hope it's as useful as it's long :D
Back to top
View user's profile Send private message
distances
n00b
n00b


Joined: 03 May 2006
Posts: 15
Location: France

PostPosted: Tue May 30, 2006 8:19 pm    Post subject: am so sorry... :oops: Reply with quote

:oops:
For those who've downloaded the script this afternoon, please get the new corrected version here or on my server...
Sorry 'bout that...
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum