summaryrefslogtreecommitdiff
blob: b8fc1b5f321b66abf3671c36144eb1d750104ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
#
# eaudit - Lists installed packages with useflags.
#
# Copyright (c) 2006, Alastair Tse <alastair@liquidx.net>
# All rights reserved.
#

from optparse import OptionParser
from os.path import join
import os
import sys
import string

__doc__ = "Lists installed packages in human and machine readable form."
__version__ = '0.1'

PKG_DB = '/var/db/pkg'
PKG_WORLD = '/var/lib/portage/world'

def strip_ver(pkg_ver):
    parts = pkg_ver.split('-')
    for i in range(1, len(parts)):
        if parts[i][0] in string.digits:
            return '-'.join(parts[:i]), '-'.join(parts[i:])
    return pkg_ver, ''

def world():
    world_index = {}
    for cat_pkg in open(PKG_WORLD).xreadlines():
        cat, pkg = cat_pkg.strip().split('/')
        if cat in world_index:
            world_index[cat].add(pkg)
        else:
            world_index[cat] = set([pkg])
    return world_index

def allpkgs():
    """ An iterator that outputs all packages in the form:
        category, package, version, path_to_db
    """
    for cat in sorted(os.listdir(PKG_DB)):
        for pkg_ver in sorted(os.listdir(join(PKG_DB, cat))):
            pkg, ver = strip_ver(pkg_ver)
            yield cat, pkg, ver, join(PKG_DB, cat, pkg_ver)

def use_status(path):
    """ Returns a set of enable and disabled useflags for packages. """
    use_flags = set(open(join(path, 'USE')).read().split())
    iuse_flags = set(open(join(path, 'IUSE')).read().split())
    
    use_enabled = sorted(use_flags & iuse_flags)
    use_disabled = sorted(iuse_flags - set(use_enabled))
    return use_enabled, use_disabled

def listpkgs(with_use = False, with_status = False, format = '%s %s %s'):

    world_index = world()
    
    for cat, pkg, ver, path in allpkgs():
        output = ''
        if with_status:
            if pkg in world_index.get(cat, set()):
                output += 'W '
            else:
                output += '  '

        output += format % (cat, pkg, ver)
        
        if with_use:
            enabled, disabled = use_status(path)
            output += ' ' + ' '.join(enabled + ['-%s' % u for u in disabled])

        print output

def main(args = sys.argv[1:]):
    parser = OptionParser(version = '%%prog %s' % __version__,
                          description = __doc__)
    parser.add_option('-u', '--use', action='store_true',
                      help = 'List USE flags')
    parser.add_option('-s', '--status', action='store_true',
                      help = 'Display WORLD status')
    parser.add_option('-f', '--format', default='%s %s %s',
                      help = 'Display format for category, package, version. '
                             'Example: "%s/%s-%s" = "sys-apps/portage-2.1"')

    options, params = parser.parse_args(args)
    listpkgs(with_use = options.use, with_status = options.status,
             format = options.format)


if __name__ == "__main__":
    main()