Donnie Berkholz wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I hacked up a quick script this morning to handle the new elog scripts
in 2.1. Perhaps some of you can make use of it or help me improve it.

Thanks,
Donnie

I also have a partially completed utility for this, which I had intended to complete when my final exams ended. I'll include it here as well in case anybody wants to play with it. If nobody has a preference wither way, I'll do some more work on this when I get home from college in a day or two.

Please understand that this is incomplete code that doesn't yet work without some tinkering, and I haven't touched it in a few months because of school work. I'm just sharing what I've already got in case anybody is interested.

Tercel
#!/usr/bin/python

"""
elogread-0.1 written by Colin Kingsley ([EMAIL PROTECTED])
Utility for reading messages logged by ebuilds.
"""

__revision__ = '@VER@'

import os, sys, time
from portage import settings
from getopt import GetoptError, gnu_getopt

logdir = settings['PORTAGE_TMPDIR']+'/elogs'

class Log:
        """Abstracts log messages"""

        def __init__(self, name, index):
                self.name = name

                split = name.split(':')
                self.cat = split[0]
                self.pvr = split[1]
                self.tstr = split[2].replace('.log', '')

                self.index = index + 1
        
        def list(self):
                """parses the name of the logfile, and prints it."""

#               get the time string from the filename, and make a gmt time tuple
                gtt = time.strptime(self.tstr, '%Y%m%d-%H%M%S')
#               turn the gtt into second since epoch, and turn it into localtime
                lts = time.mktime(gtt) - time.timezone + (3600 * time.daylight)

                print '%s) %s/%s:\t\t%s' % (self.index, self.cat, self.pvr,
                                time.strftime('%a %b %d, %I:%M%p, %Y', 
time.localtime(lts)))
        
        def display(self):
                """Formats and displays the contents of a log"""
                self.list()
                print '!!! display code incomplete !!!'
#TODO: implement display finctionality
        
        def remove(self):
                """Deletes the log file referenced by the instance."""
                os.remove(logdir +'/' + self.name)

                print 'Logs removed:'
                self.list()
                

def parse_args(argv):
        """parses arguments using gnu_getopt"""

        try:
                ops, args = gnu_getopt(argv[1:], 'hld:r:')
        except GetoptError, e:
                usage(e)

        if args:
                usage('Unexpected arguments passed.')
        
        if len(ops) == 0:
                usage()

        if len(ops) > 1:
                usage('Multipe options passed. Please oly use one.')
        
        return ops[0]


def usage(error=None):
        if error:
                print error
        print 'usage: %s [-l][-h]\n' % sys.argv[0]
        print '-l:\tlist mode: Shows a list of unread log messages.'
        print '-h:\thelp: displays this simple help text.'
        sys.exit(1)

def main(argv):
        ops = parse_args(argv)

        if ops[0] == '-h':      #if we are gonna bail, don't make 'em wait
                usage()
        
        logs = []                       #create log objects
        index = 0
        for name in os.listdir(logdir):
                if name.endswith('.log'):
                        logs.append(Log(name, index))
                        index = index + 1


        ## Start doing things ##

        #list
        if ops[0] == '-l':
                print 'Unread logs:'
                if len(logs) == 0:
                        print 'None!'
                        sys.exit(0)
                else:
                        for log in logs:
                                log.list()
                        sys.exit(0)
        
        #display
        if ops[0] == '-d':
                logs[int(ops[1]) - 1].display()
                sys.exit(0)
        
        #remove
        if ops[0] == '-r':
                logs[int(ops[1]) - 1].remove()
                sys.exit(0)

if __name__ == '__main__':
        main(sys.argv)

Reply via email to