Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Portage Utility: adduseflags
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
count_zero
Guru
Guru


Joined: 17 May 2004
Posts: 460
Location: Little Rock, Arkansas, USA

PostPosted: Thu Jul 27, 2006 3:03 am    Post subject: Portage Utility: adduseflags Reply with quote

I've written a bash script to make managing the /etc/portage/package.use file easier. It's based in part on my earlier attempt at bash scripting, addkeywords (https://forums.gentoo.org/viewtopic-t-464213.html). This script also requires the program "eix" (in portage) to search for packages. This script searches the eix database for a package, then lists the USE flags and their descriptions, prompting you to add or remove flags.

Usage: adduseflags <package query>

You can download it here: http://countzero.amidal.com/files/

Code:
#!/bin/bash
# count_zero 2006
# adduseflags 1.1
# A utility for managing the /etc/portage/package.use file.
#
# Revision History:
# version 1.1: updated syntax for new eix format
# version 1.0: initial release
#

# making temporary file directory
tmpdir=/tmp/adduse/
rm -rf $tmpdir
mkdir $tmpdir
#### Defining temporary files ####
raweix=$tmpdir/raweix
raweix2=$tmpdir/raweix2
prerawpackages=$tmpdir/prerawpackages
rawpackages=$tmpdir/rawpackages
packages=$tmpdir/packages
finalpackage=$tmpdir/finalpackage
query=$1
uses=$tmpdir/uses

#### 'Help' output for any argument starting with "-" ####
if [[ $query = -*  || $query = "" ]]
then echo 'Adduseflags 1.0
Written by count_zero, distributed under the GPL v2

This is a small bash script to easily add USE flags
to your /etc/portage/package.use file.  It is uses
the program "eix" (in portage) to search for packages.


Usage: adduseflags <package query>

To enable a USE flag, enter the flag name.
To disable a USE flag, enter the flag name preceeded by a "-".
example:

Enter new flags: -arts visualization kde

will enable visualization and kde, and disable arts.
'
exit 0
else :
fi

#### Preparing packages with versions ####
eix $query > $raweix
sed '/^[a-z,A-Z]/ d' $raweix | sed 's/\[[0-9]*\]//' > $raweix2
sed 'N;s/\n/ /' $raweix2 | sed 's/\ *\ Available\ versions: //' | sed '/^ / d' | sed 's/*\ //' | sed 's/^\[.\]\ //' > $prerawpackages
sed '/^$/d' $prerawpackages | awk '{print NR,$0 }' | sed 's/^[0-9]*/&)/' | sed 's/\ *$//' > $rawpackages
#### Preparing the list of packages ####
grep "^\[" $raweix2 | sed 's/\[.\]\ //' | sed 's/^\* //' | sed '/^\ / d' | sed '/^[ ]*$/ d' | sed 's/[ ]*$//' | sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' | awk '{print NR,$0 }' | sed 's/^[0-9]*/&)/' > $packages
# See if the search matched any packages
if [ -z "`cat $packages`" ]
then echo "Sorry, no packages match the query."
exit 1
else :
fi

#### Choosing the Package ####
test=""
until [ $test ]
do
cat $packages
echo -n "Which Package? "
read input
test=`cat $rawpackages | grep -o \^$input\)`
if [ $test ]
then :
else
   echo 'Sorry, that is not a choice'
fi
done

finalpackage=`grep -w "$input" $packages | sed 's/^[0-9]*) //' | sed 's/\ *$//'`
echo $finalpackage

#### Choosing the USE Flags ####
package=`emerge -pv --nodeps $finalpackage`
echo $package | sed 's/^.*\ \]\ //' | sed 's/\"\ .*/\"/' | sed 's/.*=\"//' | sed 's/"//' | sed '/Calculating/d' | sed '/These/d' | sed '/Total/d' | sed '/^$/d' | sed 's/[*%]//g' | sed 's/[a-zA-Z0-9-]*\ /+&/g' | sed 's/[a-zA-Z0-9-]*$/+&/g' | sed 's/+-/-/g' > $uses
list=`cat $uses | sed 's/\ [+-]/ /g' | sed 's/^[+-]//g'`
echo $package | sed 's/^.*done! //' | sed 's/VIDEO.*//' | sed 's/LINGUAS.*//'
echo
echo
echo '      (+) USE flag currently activated, (-) USE flag currently disabled'
for flag in `echo $list`
   do
      if grep -q [-]$flag $uses
      then
      local=`grep $finalpackage:$flag\  /usr/portage/profiles/use.local.desc | sed 's/^.*://' | sed 's/.*/(-) &/'`
      global=`grep ^$flag\  /usr/portage/profiles/use.desc | sed 's/.*/(-) &/'`
         if [[ $local ]]
         then echo $local
         elif [[ $global ]]
         then echo $global
         else echo "(-) $flag - no description"
         fi
      elif grep -q [+]$flag $uses
      then
      local=`grep $finalpackage:$flag\  /usr/portage/profiles/use.local.desc | sed 's/^.*://' | sed 's/.*/(+) &/'`
      global=`grep ^$flag\  /usr/portage/profiles/use.desc | sed 's/.*/(+) &/'`
         if [[ $local ]]
         then echo $local
         elif [[ $global ]]
         then echo $global
         else echo "(+) $flag - no description"
         fi
      else echo nogrep
      fi
   done
echo


package2=`echo $finalpackage | sed 's|\/|\\\/|'`
flags=`grep $package2 /etc/portage/package.use | sed "s/$package2 //"`

### delete obsolete flags from $flags
for each in $flags
   do
   each2=`echo $each | sed 's/\ -//' | sed 's/^-//'`
   test=`echo $list | sed "/$each2/d"`
   if [[  $test == $list ]]
   then flags=`echo $flags | sed "s/\ $each2\ / /g" | sed "s/^$each2\ / /g" | sed "s/\ $each2$/ /g" | sed "s/^$each2$/ /g" | sed "s/\ -$each2\ / /g" | sed "s/^-$each2\ / /g" | sed "s/\ -$each2$/ /g" | sed "s/^-$each2$/ /g"`
   else :
   fi
   done

### delete duplicate flags from $flags
for each in $flags
   do
   each2=`echo $each | sed 's/\ -//' | sed 's/^-//'`
   flags="`echo $flags | sed "s/\ $each2\ / /g" | sed "s/^$each2\ / /g" | sed "s/\ $each2$/ /g" | sed "s/^$each2$/ /g" | sed "s/\ -$each2\ / /g" | sed "s/^-$each2\ / /g" | sed "s/\ -$each2$/ /g" | sed "s/^-$each2$/ /g"` $each"
   done

### get new flags from user
test="1"
while [ $test -gt 0 ]
   do
   test=0
   error=""
   echo -n "Enter new flags: "
   read newflags
   for each in $newflags
      do
      each2=`echo $each | sed 's/\ -//' | sed 's/^-//'`
      test2=`echo $list | sed "/\ $each2\ /d" | sed "/\ $each2$/d" | sed "/^$each2\ /d" | sed "/^$each2$/d"`
      if [[ $test2 == "" ]]
      then :
      else
      error="$error $each2"
      test=$(( test + 1 ))
      fi
      done
   if [[ $error ]]
   then echo Sorry, these USE flags are not valid: $error
   else :
   fi
   done
   
newflags2=`echo $newflags | sed 's/-//'`

### delete old flags that will be replaced by new flags
for each in $newflags2
   do
   flags=`echo $flags | sed "s/\ $each2\ / /g" | sed "s/^$each2\ / /g" | sed "s/\ $each2$/ /g" | sed "s/^$each2$/ /g" | sed "s/\ -$each2\ / /g" | sed "s/^-$each2\ / /g" | sed "s/\ -$each2$/ /g" | sed "s/^-$each2$/ /g"`
   done

### format final package.use entry
finalflags=`echo $flags $newflags | sed 's|\ \ | |g'`
entry="$finalpackage $finalflags"
echo "This will be entered into package.use: $entry"
echo -n "Continue? (y/n) "
proceed=""
read proceed
if [[ $proceed == y || $proceed == Y || $proceed == yes || $proceed == Yes || $proceed == YES || $proceed == "" ]]
   then :
   else echo exiting.
   exit 0
fi

# deletes previous use entries from package.use
cat /etc/portage/package.use | sed "/$package2/d" > /etc/portage/package.use
# adds new entry to package.use
echo $entry >> /etc/portage/package.use
echo Done!



[EDIT] updated script to reflect changes in eix format.
_________________
"We must all hang together, or assuredly we shall all hang separately."
-Ben Franklin


Last edited by count_zero on Mon Sep 11, 2006 5:15 pm; edited 1 time in total
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Thu Jul 27, 2006 8:21 am    Post subject: Reply with quote

You could greatly reduce the complexity of these by just using "emerge" rather than eix.

http://68.111.181.40:9002/unmask.sh

Does the same thing as your addkeywords program. Though, it doesn't list all packages with similar names like eix.

But I prefer it that way.
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Thu Jul 27, 2006 9:45 am    Post subject: Reply with quote

The following replaces everything from "preparing packages" to "choosing package" sections.

Code:
finalpackage=`emerge $query --nodeps -qp|cut -c17-60| \
grep "/"|cut -f1 -d "["|sed 's/-[0-9].*//'`

if [ "$finalpackage" != "" ]
then
        echo $finalpackage
else
        echo "Sorry, no packages match the query." && exit 1
fi


Note that after the = and at the end of the 2nd line, that is a back-tick (shift+tilda) and not an apostrophe. Otherwise, this won't work.

Also, I think it would be better to not use external files for storing use flags. They could be stored in a simple variable that is just a list of them.

That would keep the script more self-contained and would avoid having a useless temp directory.
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Thu Jul 27, 2006 10:18 am    Post subject: Reply with quote

Here it is in it's full glory:

Code:
#!/bin/bash
# count_zero 2006
# adduseflags 1.0
# A utility for managing the /etc/portage/package.use file.

query=$1

#### 'Help' output for any argument starting with "-" ####
if [[ $query = -*  || $query = "" ]]
then echo 'Adduseflags 1.0
Written by count_zero, distributed under the GPL v2

This is a small bash script to easily add USE flags
to your /etc/portage/package.use file.  It is uses
the program "eix" (in portage) to search for packages.


Usage: adduseflags <package query>

To enable a USE flag, enter the flag name.
To disable a USE flag, enter the flag name preceeded by a "-".
example:

Enter new flags: -arts visualization kde

will enable visualization and kde, and disable arts.
'
exit 0
else :
fi

finalpackage=`emerge $query --nodeps -qp|cut -c17-60| \
grep "/"|cut -f1 -d "["|sed 's/-[0-9].*//'`

if [ "$finalpackage" != "" ]
then
        echo $finalpackage
else
        echo "Sorry, no packages match the query." && exit 1
fi

#### Choosing the USE Flags ####
package=`emerge -pv --nodeps $finalpackage`
uses=`echo $package | sed 's/^.*\ \]\ //' | sed 's/\"\ .*/\"/' | sed 's/.*=\"//' | sed 's/"//' | sed '/Calculating/d' | sed '/These/d' | sed '/Total/d' | sed '/^$/d' | sed 's/[*%]//g' | sed 's/[a-zA-Z0-9-]*\ /+&/g' | sed 's/[a-zA-Z0-9-]*$/+&/g' | sed 's/+-/-/g'`
echo $uses
list=`echo $uses | sed 's/\ [+-]/ /g' | sed 's/^[+-]//g'`
echo $package | sed 's/^.*done! //' | sed 's/VIDEO.*//' | sed 's/LINGUAS.*//'
echo
echo
echo '      (+) USE flag currently activated, (-) USE flag currently disabled'
for flag in `echo $list`
   do
      if echo $uses | grep -q [-]$flag
      #if grep -q [-]$flag $uses
      then
      local=`grep $finalpackage:$flag\  /usr/portage/profiles/use.local.desc | sed 's/^.*://' | sed 's/.*/(-) &/'`
      global=`grep ^$flag\  /usr/portage/profiles/use.desc | sed 's/.*/(-) &/'`
         if [[ $local ]]
         then echo $local
         elif [[ $global ]]
         then echo $global
         else echo "(-) $flag - no description"
         fi
      elif echo $uses | grep -q [+]$flag
      then
      local=`grep $finalpackage:$flag\  /usr/portage/profiles/use.local.desc | sed 's/^.*://' | sed 's/.*/(+) &/'`
      global=`grep ^$flag\  /usr/portage/profiles/use.desc | sed 's/.*/(+) &/'`
         if [[ $local ]]
         then echo $local
         elif [[ $global ]]
         then echo $global
         else echo "(+) $flag - no description"
         fi
      else echo nogrep
      fi
   done
echo


package2=`echo $finalpackage | sed 's|\/|\\\/|'`
flags=`grep $package2 /etc/portage/package.use | sed "s/$package2 //"`

### delete obsolete flags from $flags
for each in $flags
   do
   each2=`echo $each | sed 's/\ -//' | sed 's/^-//'`
   test=`echo $list | sed "/$each2/d"`
   if [[  $test == $list ]]
   then flags=`echo $flags | sed "s/\ $each2\ / /g" | sed "s/^$each2\ / /g" | sed "s/\ $each2$/ /g" | sed "s/^$each2$/ /g" | sed "s/\ -$each2\ / /g" | sed "s/^-$each2\ / /g" | sed "s/\ -$each2$/ /g" | sed "s/^-$each2$/ /g"`
   else :
   fi
   done

### delete duplicate flags from $flags
for each in $flags
   do
   each2=`echo $each | sed 's/\ -//' | sed 's/^-//'`
   flags="`echo $flags | sed "s/\ $each2\ / /g" | sed "s/^$each2\ / /g" | sed "s/\ $each2$/ /g" | sed "s/^$each2$/ /g" | sed "s/\ -$each2\ / /g" | sed "s/^-$each2\ / /g" | sed "s/\ -$each2$/ /g" | sed "s/^-$each2$/ /g"` $each"
   done

### get new flags from user
test="1"
while [ $test -gt 0 ]
   do
   test=0
   error=""
   echo -n "Enter new flags: "
   read newflags
   for each in $newflags
      do
      each2=`echo $each | sed 's/\ -//' | sed 's/^-//'`
      test2=`echo $list | sed "/\ $each2\ /d" | sed "/\ $each2$/d" | sed "/^$each2\ /d" | sed "/^$each2$/d"`
      if [[ $test2 == "" ]]
      then :
      else
      error="$error $each2"
      test=$(( test + 1 ))
      fi
      done
   if [[ $error ]]
   then echo Sorry, these USE flags are not valid: $error
   else :
   fi
   done
   
newflags2=`echo $newflags | sed 's/-//'`

### delete old flags that will be replaced by new flags
for each in $newflags2
   do
   flags=`echo $flags | sed "s/\ $each2\ / /g" | sed "s/^$each2\ / /g" | sed "s/\ $each2$/ /g" | sed "s/^$each2$/ /g" | sed "s/\ -$each2\ / /g" | sed "s/^-$each2\ / /g" | sed "s/\ -$each2$/ /g" | sed "s/^-$each2$/ /g"`
   done

### format final package.use entry
finalflags=`echo $flags $newflags | sed 's|\ \ | |g'`
entry="$finalpackage $finalflags"
echo "This will be entered into package.use: $entry"
echo -n "Continue? (y/n) "
proceed=""
read proceed
if [[ $proceed == y || $proceed == Y || $proceed == yes || $proceed == Yes || $proceed == YES || $proceed == "" ]]
   then :
   else echo exiting.
   exit 0
fi

# deletes previous use entries from package.use
cat /etc/portage/package.use | sed "/$package2/d" > /etc/portage/package.use
# adds new entry to package.use
echo $entry >> /etc/portage/package.use
echo Done!


Changes I made:
1) No longer uses eix. Just uses emerge, which makes it much simpler (both to use and maintain).
2) No longer uses temp files. After I got rid of eix stuff, the only temp file was a single line of text that had the USE flag settings used. So I edited so that "uses" was no longer a file but just a string.

That's it. Nice job; I just felt like tweaking it.
Back to top
View user's profile Send private message
Janax
Apprentice
Apprentice


Joined: 17 Aug 2004
Posts: 162
Location: Iowa

PostPosted: Thu Jul 27, 2006 2:37 pm    Post subject: Reply with quote

slycordinator wrote:

Changes I made:
1) No longer uses eix. Just uses emerge, which makes it much simpler (both to use and maintain).
2) No longer uses temp files. After I got rid of eix stuff, the only temp file was a single line of text that had the USE flag settings used. So I edited so that "uses" was no longer a file but just a string.

That's it. Nice job; I just felt like tweaking it.


Nice job to both of you! For grins, I just tried it out and have two small suggestions to make:

Since you no longer use eix, you may want to:
  • change the help text.
  • Do something different for packages that match two or more entries in portage:
    Code:
    $ adduseflags kopete
    Sorry, no packages match the query.
    $ emerge -pv kopete

    These are the packages that would be merged, in order:

    Calculating dependencies

    !!! The short ebuild name "kopete" is ambiguous.  Please specify
    !!! one of the following fully-qualified ebuild names instead:

        net-im/kopete
        kde-base/kopete


Otherwise, again - nice job!
_________________
Americans for Fair Taxation because the current tax system is not only burdensome but corrupt as well!
Back to top
View user's profile Send private message
count_zero
Guru
Guru


Joined: 17 May 2004
Posts: 460
Location: Little Rock, Arkansas, USA

PostPosted: Fri Jul 28, 2006 2:49 am    Post subject: Reply with quote

I'm glad someone found my script interesting/helpful. :D

Quote:
Also, I think it would be better to not use external files for storing use flags. They could be stored in a simple variable that is just a list of them.
That would keep the script more self-contained and would avoid having a useless temp directory.

I agree. However, being the newbie bash scripter that I am, I couldn't get the eix-related part to format correctly unless I stored some of the output in temp files. A lot of this code is re-used from my other script, addkeywords.

Quote:
Do something different for packages that match two or more entries in portage:

This is handled nicely by eix (why I like it), but could also be handled by requiring the category name when specifying a package Or if you wanted to get more complicated, you could make an if-then statement to detect when there was ambiguity, and then choose.

Quote:
That's it. Nice job; I just felt like tweaking it.

That's what makes open source great. 8)
_________________
"We must all hang together, or assuredly we shall all hang separately."
-Ben Franklin
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