Skip to content

Instantly share code, notes, and snippets.

@skarllot
Created October 17, 2012 19:57
Show Gist options
  • Save skarllot/3907753 to your computer and use it in GitHub Desktop.
Save skarllot/3907753 to your computer and use it in GitHub Desktop.
List all installed Gentoo packages with USE flags.
#!/bin/bash
#
# list-gentoo-packages.sh v0.2
# Copyright 2007-2009, Nikita Melnichenko [http://nikita.melnichenko.name]
# License: GPL-2 (http://opensource.org/licenses/gpl-license.php)
#
# List all installed Gentoo packages with USE flags.
# generate use flags for package $1 (official, but very slow)
function portage_gen_use_flags_by_equery ()
{
equery -N -C uses '='"$1" | grep '^ [+-]' | awk '{ printf "%s%s\n", $2, $3 }'
}
# generate use flags for package $1 (unofficial, but fast)
function portage_gen_use_flags_from_var_db_pkg ()
{
if ! [ -f /var/db/pkg/"$1"/IUSE ]
then
return
fi
local use_flags=`cat /var/db/pkg/"$1"/USE`
for iuse in `cat /var/db/pkg/"$1"/IUSE | sed -e "s/ /\n/g" | LC_ALL=C sort | uniq`
do
iuse=`echo "$iuse" | sed -e "s/^[+-]//g"`
used=0
for use in $use_flags
do
if [ "$use" == "$iuse" ]
then
used=1
break
fi
done
if [ $used -eq 0 ]
then
echo -n '-'
else
echo -n '+'
fi
echo $iuse
done
}
# generate use flags for package $1
function portage_gen_use_flags ()
{
portage_gen_use_flags_from_var_db_pkg "$1"
}
# generate list of all installed packages with their USE flags
function portage_list_installed ()
{
find /var/db/pkg -mindepth 2 -type d | sed -e 's|^/var/db/pkg/||' | LC_ALL=C sort | while read pkg
do
echo -n "$pkg"
portage_gen_use_flags "$pkg" | LC_ALL=C sort | while read flag
do
echo -n " $flag"
done
echo
done
}
portage_list_installed
@jimadilo
Copy link

jimadilo commented Oct 31, 2022

After a mishap, I came looking for ways of recreating my package.use file. This gave me some ideas, but it will not produce the correct output because it disregards package slots. I have added this comment as a pointer so that others are aware of this issue and can decide if they can also disregard package slots.

I am currently writing something to help me recreate what I want, and I will post a gist, and link from here should I ever finish!

Edit: I looked through source of portage to see if there was a simple way of dragging out all the packages, with the use flags that I used, and pay attention to the slots, but realised that there weren't a whole lot of them, and so just resorted to this oneliner:

# emerge --info @installed 2>/dev/null |
    sed -nr 's#^([a-z]+-[a-z]+/[^:]+:[^:]*:[^ ]+).*#\1#p' |
    while read fullAtom; do
        atom=${fullAtom%:*:*};
        if [ -e "/var/db/pkg/$atom/PKGUSE" ]; then
            echo ${atom%-[0-9]*} `cat /var/db/pkg/$atom/PKGUSE`;
        fi;
    done |
    sort

It uses the PKGUSE files in the portage db, because these seem to have the user use flags set when the package was emerged.

@jimadilo
Copy link

jimadilo commented Nov 1, 2022

grknight, one of the Gentoo devs, pointed me towards the enalyze tool, that does all this, and deals with slots properly too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment