#!/usr/bin/python -O # # $Header: $ # Author: Eldad Zack # Enhancements: Lindsay Haisley # # enotice: Gentoo Notice Reading Tool # version 0.2.2 import sys import os import re import string import readline from output import * sys.path.insert(0, "/usr/lib/portage/pym") import portage port_enotice_dir = portage.settings["PORT_ENOTICE_DIR"] + "/" if (not sys.stdout.isatty()) or (portage.settings["NOCOLOR"] in ["yes","true"]): nocolor() oinfo = "" def enoticeprint(notice): global oinfo try: file = open(port_enotice_dir + notice) oinfo = oinfo + darkgreen("Package:") + white(notice) + "\n" for line in file.readlines(): line = string.rstrip(line) if re.match("^(info|warn|error):",line): msg = re.split(":",line,1) if msg[0] == "info": oinfo = oinfo + green(" * ") if msg[0] == "warn": oinfo = oinfo + yellow(" * ") if msg[0] == "error": oinfo = oinfo + red(" * ") oinfo = oinfo + msg[1] + "\n" else: oinfo = oinfo + line + "\n" file.close() oinfo = oinfo + "\n" except: print red("!!! Reading/Parsing error in " + notice) def enoticedict(notices): dict = {} for i, notice in zip(range(1,len(notices)+1),notices): dict[i] = notice return dict def enoticelist(notices): nlist = white("Notices available:") + "\n" for i, notice in notices.iteritems(): nlist = nlist + white(str(i) + ". ") + notice + "\n" mpipe = os.popen("more", "w") try: mpipe.write(nlist) except: pass # mpipe.close() def enoticematchread(notices,ltr): for i, notice in notices.iteritems(): if notice[0] == ltr: enoticeread(notices,i) def enoticeexist(notices,num): if notices.has_key(num): return 1 else: print red("Notice doesn't exist.") return 0 def enoticeread(notices,num): if enoticeexist(notices,num): enoticeprint(notices[num]) def enoticereadall(notices): for notice in notices.keys(): enoticeread(notices,notice) def enoticedel(notices,num): if enoticeexist(notices,num): try: os.remove(port_enotice_dir + notices[num]) del(notices[num]) except: print red("!!! Can't remove notice file: " + notices[num]) def enoticepurge(notices): for notice in notices.keys(): enoticedel(notices,notice) def tscompare(x, y): if os.stat(port_enotice_dir + x).st_mtime > os.stat(port_enotice_dir + y).st_mtime: return 1 if os.stat(port_enotice_dir + y).st_mtime > os.stat(port_enotice_dir + x).st_mtime: return -1 else: return 0 def enoticedisplay(): global oinfo opipe = os.popen("less", "w") try: opipe.write(oinfo) except IOError: pass opipe.close() oinfo = "" def enoticeref(): print """ q)uit s)ort a | t - sort alphabetical or by timestamp r)ead - read notice ("r" is optional) r)ead - read all notices from thru ("r" is optional) r)ead - read all notices whose package names start with a)ll notices d)elete p)urge all notices """ def main(): global oinfo if port_enotice_dir == "": print red("!!! PORT_ENOTICE_DIR undefined.") return try: noticeslist = os.listdir(port_enotice_dir) noticeslist.sort() try: if len(noticeslist) == 0: print "No notices found." else: print "Found " + white(str(len(noticeslist))) + " notices to read." notices = enoticedict(noticeslist) usercmd = "" while (usercmd != "q") and (len(notices) > 0): print "" enoticelist(notices) enoticeref() print ">", usercmd = string.rstrip(sys.stdin.readline()) print "" if re.match("^(?:r\s+){0,1}\d+$",usercmd): num = int(re.match("^(?:r\s+){0,1}(\d+)$",usercmd).group(1)) enoticeread(notices,num) enoticedisplay() elif re.match("^(?:r\s+){0,1}\d+\s+\d+$",usercmd): start = int(re.match("^(?:r\s+){0,1}(\d+)\s+\d+$",usercmd).group(1)) end = int(re.match("^(?:r\s+){0,1}\d+\s+(\d+)$",usercmd).group(1)) if start > end: foo = start start = end end = foo for num in range(start, end+1): enoticeread(notices,num) enoticedisplay() elif re.match("^r\s+\S$",usercmd): matchletter = re.match("^r\s+(\S)$",usercmd).group(1) enoticematchread(notices,matchletter) enoticedisplay(); elif re.match("^a$",usercmd): enoticereadall(notices) enoticedisplay() elif re.match("^d \d+$",usercmd): num = int(re.split(" ",usercmd,1)[1]) enoticedel(notices,num) elif re.match("^p$",usercmd): enoticepurge(notices) elif re.match("^s\s+\S$",usercmd): sortsel = re.match("^s\s+(\S)$",usercmd).group(1) if sortsel == "a": noticeslist.sort() elif sortsel == "t": noticeslist.sort(tscompare) else: noticeslist.sort() notices = enoticedict(noticeslist) elif not re.match("^q$",usercmd): print red("Imcomplete or unknown command.") except: # Uncomment the following line to see native python traceback on errors # sys.excepthook(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) print red("!!! User-Break or unexpected error encountered.") except: # Uncomment the following line to see native python traceback on errors # sys.excepthook(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) print red("!!! Error: Can't read " + port_enotice_dir) main() print ""