Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Portage Script: addkeywords
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: 459
Location: Little Rock, Arkansas, USA

PostPosted: Sun May 21, 2006 8:23 am    Post subject: Portage Script: addkeywords Reply with quote

Here's a bash script to easily add keywords to your /etc/portage/package.keywords file (the right way). I'm lazy and tend to make lots of typos, or can't remember the category, etc. when editing the keywords file, so this script is useful to me. Hopefully you will find it usefull as well. I am new to bash scripting (my first real script!), and this is not as elegant as it probably could be, but it works.

It depends upon the portage indexer eix (in portage).

usage: addkeywords <package>
<package> can be a partial match as this is based on eix, and it will prompt you to choose which package you mean.
You have the option to choose to unmask a particular version or all versions of a package.

Updated 6/1/06
I updated the script to search the portage overlay for recently added packages that might not yet be in the eix database. You can also download the script from http://countzero.amidal.com/files/addkeywords

Code:
#!/bin/bash
# Addkeywords 1.1
# A simple bash script to add keywords to package.keywords
#
# written by count_zero
# Distributed under the GPL v2
#
# Revision History:
# 1.1: updated syntax for new eix format
# 1.0: initial release
#


#### Enter your system settings here ####
arch=~x86

#### Set Temporary File Directory ####
# This script empties the temporary file directory, so be sure not to set it
# to anything important like '/'.  The default directory should be fine for
# most people.
tmpdir=/tmp/addkeywords/



# Defining the overlay from make.conf
overlay=`grep PORTDIR_OVERLAY /etc/make.conf | grep -o \".*$ | sed 's/^"//' | sed 's/"$//' | sed 's/[^/]$/&\//'`

# making temporary file directory
mkdir -p $tmpdir
rm -f $tmpdir/*
#### Defining temporary files ####
raweix=$tmpdir/raweix
raweix2=$tmpdir/raweix2
prerawpackages=$tmpdir/prerawpackages
preprerawpackages=$tmpdir/preprerawpackages
prerawpackages2=$tmpdir/prerawpackages2
rawpackages=$tmpdir/rawpackages
packages=$tmpdir/packages
versions=$tmpdir/versions
finalpackage=$tmpdir/finalpackage
finalversion=$tmpdir/finalversion
query=$1
overlaypackage2=$tmpdir/overlaypackage2

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

This is a small bash script to easily add keywords such as ~x86
to your /etc/portage/package.keywords file.  It is uses
the program eix (in portage), and will attempt to look up
packages in the eix database.  It will also parse the last few
emerges of /var/log/emerge.log looking for packages that may
have been added to your portage overlay since the last eix update.

Set your ~arch in the script (defaults to "~x86")

Usage: addkeywords <package query>
'
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
# Searches the overlay for recently added packages which may not yet be in the eix database
overlaypackageversion=`find $overlay -iname *$1* | grep ebuild | grep  -o [^/]*/[^/]*/[^/]*.ebuild | sed 's/\/[^/]*//' | sed 's/\.ebuild//' | sed 's/-[0-9]/ &/' | sed 's/ -/ /'`
echo $overlaypackageversion >> $prerawpackages
cat $prerawpackages | sed 's/\ [a-z]/\n&/g' | sed 's/^\ //g' > $preprerawpackages
overlaypackage=`cat $preprerawpackages | sed 's/.*/& /' | sed 's/\ [^a-z].*\ //g' | sed 's/[ ]$//g'`
overlaypackage2=`echo $overlaypackage | sed 's_\/_\\\\/_g' | sed 's/\ [a-z]/\n&/g' | sed 's/^\ //g' | sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'`
for package in `echo $overlaypackage2`
   do
   overlayversion=`cat $preprerawpackages | sed 's/\ [a-z]/\n&/g' | sed 's/^\ //g' | grep "$package " | sed "s/$package//" | sed 's/^\ //' | sed 'N;s/\n/ /g'`
   echo `echo $package | sed 's/[\]//g'` $overlayversion >> $prerawpackages2
   echo \*\ `echo $package | sed 's/[\]//g'` >> $raweix2
   done
sed '/^$/d' $prerawpackages2 | awk '{print NR,$0 }' | sed 's/^[0-9]*/&)/' | sed 's/\ *$//' > $rawpackages

#### Preparing the list of packages ####
grep \*\  $raweix2 | 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 0
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

#### Preparing the Versions ####
sed 's/^.*/& All_Versions/' $rawpackages | sed 's/[ ][^ ]*//' | grep ''$input')' | sed 's/\ /\n/g' | sed '/)/ d' | sed '/^[ ]*$/ d' | sed 's/\[.*$//g' | sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' | sed '/^$/d'  | awk '{print NR,$0 }' | sed 's/^[0-9]*/&)/' > $versions

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

grep -w ''$input2')' $versions | sed 's/\[[A-Z]\]//' | sed 's/^[0-9]*) //' | sed 's/\[[0-9]\]//' | sed 's/^[^0-9]//' | sed 's/^.*/-&/' | sed 's/-ll_Versions//' > $finalversion

#### preparing string for package.keywords ####
cat $packages | grep -w "$input" | sed 's/^[0-9]*) //' | sed 's/\ *$//' > $finalpackage

#### Adding the string to package.mask ####
if [ -z `cat $finalversion` ]
then
   echo "\"`cat $finalpackage` $arch\" added to /etc/portage/package.keywords" && echo "`cat $finalpackage` $arch" >> /etc/portage/package.keywords
else
   echo "\"=`cat $finalpackage``cat $finalversion` $arch\" added to package.keywords" && echo "=`cat $finalpackage``cat $finalversion` $arch" >> /etc/portage/package.keywords
fi

# cleaning up temporary files
rm -f $tmpdir/*

exit 0


For some reason, I often have trouble with copy-and-pasting scripts from a forum, it seems that sometimes extra blanks are added to the ends of the lines. If this script won't execute for you (don't forget to chmod +x), use this command:
Code:
sed 's/\ *$//' SCRIPT > SCRIPT

sed is cool :D

[EDIT] updated script to reflect new 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:18 pm; edited 2 times in total
Back to top
View user's profile Send private message
Chaosite
Guru
Guru


Joined: 13 Dec 2003
Posts: 540
Location: Right over here.

PostPosted: Sun May 21, 2006 11:09 am    Post subject: Re: Portage Script: addkeywords Reply with quote

count_zero wrote:

For some reason, I often have trouble with copy-and-pasting scripts from a forum, it seems that sometimes extra blanks are added to the ends of the lines. If this script won't execute for you (don't forget to chmod +x), use this command:
Code:
sed 's/\ *$//' SCRIPT > SCRIPT

sed is cool :D


This problem is effectively solved if you find some hosting and put the file on the internet =)


Anyway, nice script =) I didn't actually try it, but here are some coding style notes:


    * You can run multiple regexs with sed like so:
    Multiple regexes with sed:
    sed -e 's/foo/bar/' -e 's/bar/foobar/'

    * You don't need to use cat if all you want to do is read files. Theses 2 commands do the same thing (except for the second one you don't need to start another process)
    Bad:
    cat file | command

    Good:
    command < file

    * You can get sed to read files, too:
    Reading files with sed:
    sed -e 's/ome/regex/' file



Nice job =)
Back to top
View user's profile Send private message
distances
n00b
n00b


Joined: 03 May 2006
Posts: 15
Location: France

PostPosted: Sun May 21, 2006 7:17 pm    Post subject: Reply with quote

Hello,
Hello, count_zero

I've made a contrib in the French part of the forum which has the same ambition than yours... I found your approach is quite sympathic and more efficient than mine, in a certain way :D

If you wish to take a look at mine, maybe we could join to merge our projects into one?...

(in english, fresh thread...)
http://forums.gentoo.org/viewtopic-t-464344.html

Bye!
Back to top
View user's profile Send private message
count_zero
Guru
Guru


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

PostPosted: Sun May 21, 2006 8:18 pm    Post subject: Reply with quote

distances wrote:

If you wish to take a look at mine, maybe we could join to merge our projects into one?...

(in english, fresh thread...)
http://forums.gentoo.org/viewtopic-t-464344.html

Bye!


Your program looks quite nice and I'll have to try it out. Your programming skills are far beyond mine, however. You are of course free to use any code or ideas from my script! Good luck.

count_zero
_________________
"We must all hang together, or assuredly we shall all hang separately."
-Ben Franklin
Back to top
View user's profile Send private message
count_zero
Guru
Guru


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

PostPosted: Fri Jun 02, 2006 3:32 am    Post subject: Re: Portage Script: addkeywords Reply with quote

Chaosite wrote:

This problem is effectively solved if you find some hosting and put the file on the internet =)

Yeah, I guess you're right :D
I added a link to download the script from a webhost. You can get the script at http://countzero.amidal.com/files/addkeywords

I also updated the script to search the portage overlay even if the ebuild hasn't been added to the eix database yet.
_________________
"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