Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Managing multiple portage/bashrc handlers
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Rcomian
Apprentice
Apprentice


Joined: 10 Jan 2004
Posts: 174
Location: Uk, Northwest

PostPosted: Sun Jun 25, 2006 2:25 pm    Post subject: Managing multiple portage/bashrc handlers Reply with quote

Portage's new hook ability has meant a lot of bashrc files are now flying around. Obviously, it becomes pretty impossible to merge all these great ideas into a single bashrc, let alone apply updates if there's a new version or a bug fix.

This script is a bashrc file that allows you to break your bashrc into multiple files, and set the order in which the hooks are applied.
The bashrc files are stored in /etc/portage/bashrc.scripts and a symlink to it is made in /etc/portage/bashrc.d. I use symlinks so that you can enable and disable scripts as you want, as well as order them without having to rename the original modules.
I also provide a script (hook_handler) to allow you to install and uninstall these hooks easily.

So, if you have a bashrc, the first step is to make a /etc/portage/bashrc.scripts directory and move your bashrc into it, giving it an appopriate name (I'll use "mybashrc" as an example. Then put the following script into /etc/portage/bashrc, replacing anything that was there before.

Then you can either use the second script to install your old bashrc file like this:
Code:
hook_handler -I mybashrc

or make the symlink yourself like:
Code:
ln -s /etc/portage/bashrc.scripts/mybashrc /etc/portage/bashrc.d/10-mybashrc


Here's the replacement bashrc script:
Code:
pre_pkg_setup() {
   #echo In hook pre_pkg_setup
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_pkg_setup() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_pkg_setup
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_pkg_setup
}

post_pkg_setup() {
   #echo In hook post_pkg_setup
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_pkg_setup() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_pkg_setup
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_pkg_setup
}

pre_pkg_nofetch() {
   #echo In hook pre_pkg_nofetch
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_pkg_nofetch() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_pkg_nofetch
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_pkg_nofetch
}

post_pkg_nofetch() {
   #echo In hook post_pkg_nofetch
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_pkg_nofetch() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_pkg_nofetch
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_pkg_nofetch
}

pre_src_unpack() {
   #echo In hook pre_src_unpack
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_src_unpack() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_src_unpack
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_src_unpack
}

post_src_unpack() {
   #echo In hook post_src_unpack
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_src_unpack() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_src_unpack
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_src_unpack
}

pre_src_compile() {
   #echo In hook pre_src_compile
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_src_compile() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_src_compile
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_src_compile
}

post_src_compile() {
   #echo In hook post_src_compile
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_src_compile() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_src_compile
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_src_compile
}

pre_src_install() {
   #echo In hook pre_src_install
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_src_install() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_src_install
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_src_install
}

post_src_install() {
   #echo In hook post_src_install
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_src_install() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_src_install
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_src_install
}

pre_src_test() {
   #echo In hook pre_src_test
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_src_test() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_src_test
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_src_test
}

post_src_test() {
   #echo In hook post_src_test
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_src_test() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_src_test
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_src_test
}

pre_pkg_preinst() {
   #echo In hook pre_pkg_preinst
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_pkg_preinst() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_pkg_preinst
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_pkg_preinst
}

post_pkg_preinst() {
   #echo In hook post_pkg_preinst
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_pkg_preinst() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_pkg_preinst
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_pkg_preinst
}

pre_pkg_postinst() {
   #echo In hook pre_pkg_postinst
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_pkg_postinst() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_pkg_postinst
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_pkg_postinst
}

post_pkg_postinst() {
   #echo In hook post_pkg_postinst
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_pkg_postinst() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_pkg_postinst
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_pkg_postinst
}

pre_pkg_prerm() {
   #echo In hook pre_pkg_prerm
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_pkg_prerm() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_pkg_prerm
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_pkg_prerm
}

post_pkg_prerm() {
   #echo In hook post_pkg_prerm
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_pkg_prerm() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_pkg_prerm
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_pkg_prerm
}

pre_pkg_postrm() {
   #echo In hook pre_pkg_postrm
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_pkg_postrm() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_pkg_postrm
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_pkg_postrm
}

post_pkg_postrm() {
   #echo In hook post_pkg_postrm
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_pkg_postrm() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_pkg_postrm
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_pkg_postrm
}

pre_pkg_config() {
   #echo In hook pre_pkg_config
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      pre_pkg_config() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      pre_pkg_config
      #echo Finished ... $portage_module
   done
   #echo Finished hook pre_pkg_config
}

post_pkg_config() {
   #echo In hook post_pkg_config
   for portage_module in `ls /etc/portage/bashrc.d/`
   do
      #echo Loading ... $portage_module
      post_pkg_config() { echo -n .; }
      source /etc/portage/bashrc.d/$portage_module
      post_pkg_config
      #echo Finished ... $portage_module
   done
   #echo Finished hook post_pkg_config
}


And here's the hook handler script. You'll need to save it to a file and make it executable with "chmod u+x hook_handler".
Code:
#!/bin/sh

scripts_dir="/etc/portage/bashrc.scripts"
links_dir="/etc/portage/bashrc.d"

available_handlers=
installed_handlers=
install_handler=
install_handler_value=50
uninstall_handler=
while getopts aiI:U name
do
   case $name in
   a)   available_handlers=1;;
   i)   installed_handlers=1;;
   I)   install_handler=1
        install_handler_value="$OPTARG";;
   U)   uninstall_handler=1;;
   ?)   printf "Usage: %s: [-aiIU]\n" $0
             printf " -a                  : List all available hook handlers\n" $0
             printf " -i                  : List all installed hook handlers\n"
             printf " -I[level] <handler> : Install <handler> at level [level]. Level defaults to 50\n"
             printf " -U <handler>        : Uninstall the specified handler\n"
   exit 2;;
   esac
done
if [ ! -z "$available_handlers" ]; then
   ls /etc/portage/bashrc.scripts/ | grep -v '^.*~$'
fi

if [ ! -z "$installed_handlers" ]; then
   ls /etc/portage/bashrc.d/ | grep -v '^.*~$'
fi

if [ ! -z "$install_handler" ]; then
   shift $(($OPTIND - 1))
   module=${1}
   if [[ -z "${module}" && ! -z "${install_handler_value}" ]]; then
      module="${install_handler_value}"
      install_handler_value=50
   fi

   if [[ -f ${scripts_dir}/${module} ]]; then
      echo Installing handler ${module} at level ${install_handler_value}
      ln -s ${scripts_dir}/${module} ${links_dir}/${install_handler_value}-${module}
   else
      echo $module does not exist
   fi
fi

if [ ! -z "$uninstall_handler" ]; then
   shift $(($OPTIND - 1))
   module=${1}

   if [[ -f ${links_dir}/${module} ]]; then
      echo Uninstalling handler ${module}
      rm ${links_dir}/${module}
   else
      echo $module is not installed
   fi
fi
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks 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