#!/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 \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 " 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)