Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Collection of scripts for Portage messages during emerge
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
morellik
l33t
l33t


Joined: 03 Feb 2003
Posts: 629
Location: Firenze

PostPosted: Tue Nov 02, 2004 8:49 am    Post subject: Collection of scripts for Portage messages during emerge Reply with quote

Dear all,

I wrote a little python script that execute emerge -u|-p|-D world|system, grab the
ewarn messages and write its via email, in a file, or print in the screen.

Code:

emesg.py -m -uD world

execute [b]emerge -uD world[/b] and send via email the ewarn messages.


You can download it here: http://www.gentoo.it/utility/emesg.py
Code:
#!/usr/bin/env python

import ConfigParser
from output import *
import sys, string
import os, popen2

VERSION='1.0'
confile=os.path.abspath(os.path.join(os.environ.get('HOME'),'.emesgrc') )
doing="Doing: emerge "

def emsg(msg):
    print green(" *"), msg

def man():
    print green(" ### emesg ###")
    print red("Author: morellik <gentoo-dev@gentoo.it>\n")
    print
    print ("emesg [-s|-m|-f]{-u|-p|-D} [world|system]")
    print
    print ("-s: print ebuild warnings on the screen")
    print ("-m: send ebuild warnings via email")
    print ("-f: write ebuild warnings in a file")
    print
    print ("e.g. 'emesg -s -uD world' execute 'emerge -uD world' and print ebuild warnings on the screen.")
   
def version():
    print "emesg v %s" % VERSION
    print
    print "Author: morellik <gentoo-dev@gentoo.it>"
    print
   
   
##########################################
# Check configuration
def checkconfig():

    if not os.path.isfile(confile):
        mail=raw_input("Enter your email address: ")
        mailserver=raw_input("Enter the IP address of your mail server: ")
        try:
            fout=open(confile, 'w')
        except IOError, (errno, strerror):
            sys.exit("I/O Error(%s): %s %s " % (errno, confile, strerror))
        fout.write("[general]\n")
        fout.write("email=%s\n" % mail)
        fout.write("mailserver=%s\n" % mailserver)
        fout.close()
       
    else:
        config = ConfigParser.ConfigParser()
        config.read(confile)
        mail=config.get('general','email')
        mailserver=config.get('general','mailserver')
   
    return(mail,mailserver)
       
       
###############################################
# Main program
def main(command, target, _to_video=0, _to_mail=0, _to_file=0, email=None, mailserver=None):
   
    portage_dir='/usr/portage'
    tofile='emesg.txt'
    notify={}
   
    if _to_mail:
        command=string.replace(command, '--mail','')
    if _to_file:
        command=string.replace(command, '--file','')
    if _to_video:
        command=string.replace(command, '--print','')
       
    emsg(doing +  command +  ' ' + target )
    try:
        pipe=popen2.popen2("emerge " +  command +  ' ' + target )
    except:
        sys.exit("An error occurred during emerge execution\n")
    output = pipe[0].readlines()
    pipe[0].close
   
    print green("These are the packages that I would merge, in order:")
    print
    for i in range(len(output)):
        if string.find(output[i], 'ebuild') != -1:
            pkg=string.split(output[i])[3]
            epkg=string.split(pkg, '/')
            idx=string.find(epkg[1],'-')
            npkg=epkg[1][:idx]
            ebuild=portage_dir+'/'+ epkg[0] + '/' + npkg + '/' + epkg[1] +'.ebuild'
           
           
            try:
                cont_ebuild=open(ebuild,'r').readlines()
            except:
                continue
           
            cont=""
            for ii in range(len(cont_ebuild)):
                if string.find(cont_ebuild[ii],'ewarn') != -1:
                    ncont=string.replace(cont_ebuild[ii],'ewarn','')
                    cont=cont + ncont
            notify[pkg]=cont
            print output[i][:-1]
    print
    if _to_video:
        print green('*'),
        r=raw_input("Do you want to see possible warnings? (y/n): ")
        if r=='y':
            for k,v in notify.items():
                if v:
                    print k
                    print v
        print
   
    print green('*'),
    r=raw_input("Proceed with the packages update (y/n): ")
   
    if r=='y':
       
        if _to_file:
           
            print blue("Possible warnings are stored in: "),
            print tofile
            print
        try:
            fout=open(tofile,'w')
        except IOError, (errno, strerror):
            sys.exit("I/O Error(%s): %s %s\n" % (errno, tofile, strerror))
           
        for k,v in notify.items():
                if v:
                    fout.write(k)
                    fout.write(v)
        fout.close()
           
        if _to_mail:
           
            import smtplib, time
            print blue("Possible warnings are send to: "),
            print email
            print
           
            fromaddr = "root"
            toaddr = email
            subject = "Update ebuild warnings"
           
            m = open(tofile, 'r').readlines()
            message=''
            for i in range(len(m)):
                message=message+m[i]
       
            sendDate = time.strftime("%d %b %Y %H:%M:%S", time.gmtime(time.time()))
            sendmsg = "From: %s\r\nTo: %s\r\nDate:%s\r\nSubject:%s\r\n\r\n%s" % (fromaddr, toaddr, sendDate, subject, message)
            SMTPServer = smtplib.SMTP(mailserver)
            SMTPServer.sendmail(fromaddr, toaddr, sendmsg) 
            SMTPServer.quit()
            os.unlink(tofile)
           
        command=string.replace(command,'--pretend','')
        os.system("emerge " +  command +  ' ' + target)
       
   
#####################################################
uid=os.getuid()
if uid!=0:
    print red('!!Error!!'),
    print "You must to be root"
    print
    sys.exit()
   
actions=["system",  "world"]

options=[
        "--no-color",
        "--help",   
        "--version",
        "--pretend",
        "--update",
        "--deep",
        "--mail",
        "--print",
        "--file"
       
       
    ]

shortmapping={
        "h":"--help",
        "v" : "--version",
        "p":"--pretend", 
        "u":"--update", 
        "D":"--deep",
        "m":"--mail",
        "s":"--print",
        "f":"--file"
    }

           
myaction=None
myopts=[]
myfiles=[]
email=None
mailserver=None
_to_file=0
_to_video=0
_to_mail=0

# process short actions and options
tmpcmdline=sys.argv[1:]
#tmpcmdline.extend(portage.settings["EMERGE_OPTS"].split())
cmdline=[]
for x in tmpcmdline:
    if x[0:1]=="-"and x[1:2]!="-":
        for y in x[1:]:
            if shortmapping.has_key(y):
                if shortmapping[y] in cmdline:
                    print
                    print "*** Warning: Redundant use of",shortmapping[y]
                else:
                    cmdline.append(shortmapping[y])
            else:
                print "!!! Error: -"+y+" is an invalid short action or option."
                sys.exit(1)
    else:
        cmdline.append(x)

# process the command arguments
for x in cmdline:
    if not x:
        continue
    if len(x)>=2 and x[0:2]=="--":
            if x in options:
                myopts.append(x)
            elif x[2:] in actions:
               
                if myaction:
                    if myaction not in ["system", "world"]:
                        myaction="--"+myaction
                    print
                    print red("!!!")+green(" Multiple actions requested... Please choose one only.")
                    print red("!!!")+" '"+darkgreen(myaction)+"' "+red("or")+" '"+darkgreen(x)+"'"
                    print
                    sys.exit(1)
                myaction=x[2:]
            else:
                print "!!! Error:",x,"is an invalid option."
                sys.exit(1)
    elif (not myaction) and (x in actions):
       
        if myaction:
            print
            print red("!!!")+green(" Multiple actions requested... Please choose one only.")
            print red("!!! '")+darkgreen(myaction)+"' "+red("or")+" '"+darkgreen(x)+"'"
            print
            sys.exit(1)
        myaction=x
    elif x[-1]=="/":
        # this little conditional helps tab completion
        myfiles.append(x[:-1])
    else:
        myfiles.append(x)

if (myaction in ["world", "system"]) and myfiles:
    print "emerge: please specify a package class (\"world\" or \"system\") or individual packages, but not both."
    sys.exit(1)

#print myaction, myfiles, myopts
command=""
target=""

if ('--no-color' in  myopts) or (not sys.stdout.isatty()):
    if '--no-color' in  myopts:
        myopts.remove('--no-color')
    nocolor()

if '--help' in myopts:
    man()
    sys.exit()
elif '--version' in myopts:
    version()
    sys.exit()
elif '--mail' in myopts:
    (email,mailserver)=checkconfig()
    _to_mail=1
elif '--file' in myopts:
    _to_file=1
elif '--print' in myopts:
    _to_video=1
   
   
if not myopts:
    command='-up'   
elif not '--pretend' in myopts:
    command=' --pretend '

for c in range(len(myopts)):
    command=command+' '+myopts[c]

   
if myfiles and myaction==None:
    for t in range(len(myfiles)):
        target=target+' '+myfiles[t]
elif not myfiles and myaction:
    target=myaction
else:
    target="world"


main(command, target, _to_video, _to_mail, _to_file, email, mailserver)


morellik


EDIT: Copied code from link and added in code tags, and changed title from '[TOOL] emesg'. --pjp
Back to top
View user's profile Send private message
MaxDamage
l33t
l33t


Joined: 03 Jan 2004
Posts: 650
Location: Oviedo, Spain

PostPosted: Sat Jan 08, 2005 1:37 pm    Post subject: [Howto] ENOTICE: don't miss any emerge message anymore Reply with quote

It's well known that when you emerge a package, a warning or informative message is often displayed at the end of the process. But when you emerge a bunch of packages in a row, is very easy to miss a lot of them. You'd have to be watching the console all the time to notice when some info is displayed, and to read it in a few seconds. This clearly isn't a valid option :)

Well, there is a solution ;) If you visit http://gentooexperimental.org/ you'll find a lot of useful scripts, and there is one that allows you to archive all the messages portage displays, and have a look at them after installing, when you want, in a etc-update fashion. This is the webpage:

:arrow: http://dev.gentoo.org/~eldad/

Basically you have to follow those simple steps:

1) Create the profile directory in your portage configuration dir:
Code:
sudo mkdir -p /etc/portage/profile


2) Download the profile.bashrc file provided and place it there:
Code:
cd /etc/portage/profile
wget http://dev.gentoo.org/~eldad/enotice/profile.bashrc


3) Set PORT_ENOTICE_DIR in /etc/make.conf to a directory which you would like to keep the notices:
Code:
sudo vim /etc/make.conf
add at the end: PORT_ENOTICE_DIR=/var/log/enotice


4) And, finally, install the enotice program:
Code:
cd /usr/local/bin
wget http://dev.gentoo.org/~eldad/enotice/enotice
chmod +x enotice


Now when you want to read the notes, just execute enotice, and select them from the menu. I personally like pressing "a" to read them all in a row, and then "p" to purge them. Now remember to exec enotice after each emerge -uDtav world!
_________________
La PDA de tungsteno
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