Welcome, guest | Sign In | My Account | Store | Cart

This allows for on-the-fly editing. Simply drop abused.py into your path, and ensure that -a is not set in EMERGE_DEFAULT_OPTS in /etc/portage/make.conf. Then whenver you are installing new packages, use abused in place of emerge (eg: abused multitail) you will be presented with a list of use flags that are used in this action, and a prompt for editing any of them, simply hit enter with no changes to fire off the build.

Python, 139 lines
  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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python

import os
import re
import sys
import string
import subprocess

class Abused:

    def __init__(self, args):
        if args:
            self.args = ' '.join(args)
        else:
            self.args = ''
            
        self.pcmd     = 'emerge -pv --color y'
        self.rcmd     = 'emerge -v --quiet-build --color y'
        self.flags_d  = []
        self.flags_c  = []
        self.pkglines = []
        
        # pretend so we can see the use flags
        print 'Examining USE flags...'
        self.__getUse()

        # display the current USE flags, and allow some editin
        while not self.__editor():
            pass

        # and fire off the real emerge
        self.__doEmerge()

    def __sanitize(self, data):
        retv = ''
        if data.find('\x1b') != -1:
            tmp = filter(lambda x: x in string.printable, data)
            retv += re.sub('(\{|\})', '', re.sub('\[[0-9\;]+m', '', tmp))
            return retv
        return False
    
    def __getUse(self):        
        cmd_s = 'USE="%s" %s %s' % (
            ' '.join(self.flags_c),
            self.pcmd,
            self.args
        )
        cmd_p = subprocess.Popen(
            cmd_s,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            shell=True,
            executable="/bin/bash"
        )

        self.pkglines  = []
        self.flags_d   = []
        self.flags_c   = []
        self.longest_c = ''
        self.longest_n = 0
        
        buff = cmd_p.stdout.readline()
        while buff:
            if buff.strip().find('USE="') != -1:
                self.pkglines.append(buff.strip())
                flags_t = buff.strip().split('USE="')[1].split('"')[0].split()
                for f in flags_t:
                    if f not in self.flags_d:
                        self.flags_d.append(f)
                        if len(f) > self.longest_n:
                            self.longest_n = len(f)
                            self.longest_c = f
            buff = cmd_p.stdout.readline()

        # And build our sanitized list of use flags
        for f in self.flags_d:
            t = self.__sanitize(f)
            self.flags_c.append(t)

    def __editor(self):
        print
        for l in self.pkglines:
            print l
        print
        a = 0
        for i in self.flags_d:
            eval("sys.stdout.write('%%%ds' %% i)" % (self.longest_n+1))
            if a < 4:
                a += 1
            else:
                print
                a = 0

        sys.stdout.write("\n\n")
        sys.stdout.write('>> ')
        sys.stdout.flush()
        
        data = sys.stdin.readline().strip().split()        
        if len(data) == 0:
            return True
        else:
            # go through and replace the flags that are edited
            for f in data:
                for ix in range(0, len(self.flags_c)):
                    if self.flags_c[ix].find(f[1:]) != -1:
                        if f[0] == '-' and self.flags_c[ix][0] != '-':
                            tt = self.flags_c[ix]
                            self.flags_c[ix] = '-%s' % tt
                        elif f[0] != '-' and self.flags_c[ix][0] == '-':
                            tt = self.flags_c[ix][1:]
                            self.flags_c[ix] = tt
            # then refresh the data
            self.__getUse()
            return False

    def __doEmerge(self):
        cmd_s = 'USE="%s" %s %s' % (
            ' '.join(self.flags_c),
            self.rcmd,
            self.args
        )
        cmd_p = subprocess.Popen(
            cmd_s,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            shell=True,
            executable="/bin/bash"
        )
        buff = cmd_p.stdout.readline()
        while buff:
            sys.stdout.write(buff)
            sys.stdout.flush()
            buff = cmd_p.stdout.readline()
    
if __name__ == '__main__':
    try:
        app = Abused(sys.argv[1:])
    except KeyboardInterrupt:
        sys.exit(1)