Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python -O
- #
- # $Header: $
- # Author: Eldad Zack <eldad@gentoo.org>
- # Enhancements: Lindsay Haisley <fmouse@fmp.com>
- # Update for python 3.3 : CNE <cnegroumf@free.fr>
- #
- # enotice: Gentoo Notice Reading Tool
- # version 0.2.10
- import sys
- import os
- import re
- import string
- import readline
- sys.path.insert(1, "/usr/lib/portage/pym")
- import portage
- from portage.output import *
- port_enotice_dir = portage.settings["PORT_ENOTICE_DIR"] + "/"
- pe_sort = portage.settings["PORT_ENOTICE_SORT"]
- pe_level = portage.settings["PORT_ENOTICE_LEVEL"]
- if pe_level == "verbose": lev = 3
- elif pe_level == "info": lev = 2
- elif pe_level == "crit": lev = 0
- else: lev = 1
- if pe_sort == "": pe_sort = "alpha"
- if (not sys.stdout.isatty()) or (portage.settings["NOCOLOR"] in ["yes","true"]):
- nocolor()
- oinfo = ""
- debug_mode = False
- allcount = 0
- def enoticeprint(notice):
- global oinfo
- global lev
- global debug_mode
- in_epatch = False
- try:
- local_oinfo = ""
- file = open(port_enotice_dir + notice)
- local_oinfo = local_oinfo + darkgreen("Package: ") + white(notice) + "\n"
- for line in file.readlines():
- line = str(line).rstrip()
- if lev < 3 and re.match("info:Applying various patches \(bugfixes/updates\) \.\.\.$", line):
- in_epatch = True
- if in_epatch and re.match("info:Done with patching", line):
- in_epatch = False
- continue
- if lev < 3 and re.match(".*Applying .*\.\.\.$", line): continue
- if lev < 3 and re.match(".* Updating .*config\.guess", line): continue
- if lev < 3 and re.match(".* Updating .*config\.sub", line): continue
- if re.match("^(info|warn|error):", line):
- msg = re.split(":",line,1)
- if msg[0] == "info":
- if (not debug_mode) and (lev < 2): continue
- if in_epatch: continue
- local_oinfo = local_oinfo + green(" * ")
- if msg[0] == "warn":
- if (not debug_mode) and (lev < 1): continue
- local_oinfo = local_oinfo + yellow(" * ")
- if msg[0] == "error":
- local_oinfo = local_oinfo + red(" * ")
- local_oinfo = local_oinfo + msg[1] + "\n"
- else:
- local_oinfo = local_oinfo + line + "\n"
- file.close()
- local_oinfo = local_oinfo + "\n"
- if re.match("^.*Package: .*\n$", local_oinfo):
- return 0
- else:
- oinfo = oinfo + local_oinfo
- return 1
- 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.items():
- nlist = nlist + white(str(i) + ". ") + notice + "\n"
- mpipe = os.popen("more", "w")
- try:
- mpipe.write(nlist)
- mpipe.close()
- except:
- pass
- def enoticematchread(notices,ltr):
- for i, notice in notices.items():
- if notice[0] == ltr: enoticeread(notices,i)
- def enoticeexist(notices,num):
- if num in notices:
- return 1
- else:
- print(red("Notice %u doesn't exist." % (num)))
- return 0
- def enoticeread(notices,num):
- if enoticeexist(notices,num):
- enoticeprint(notices[num])
- return True
- else:
- return False
- def enoticereadall(notices):
- for notice in notices.keys():
- enoticeread(notices,notice)
- def enoticepurge(notices):
- for notice in notices.keys(): enoticedel(notices,notice)
- def enoticedel(notices,num):
- if enoticeexist(notices,num):
- try:
- os.remove(port_enotice_dir + notices[num])
- except:
- print(red("!!! Can't remove notice file: " + notices[num]))
- def enoticedisplay():
- global oinfo
- if oinfo != "":
- opipe = os.popen("less", "w")
- try:
- opipe.write(oinfo)
- except IOError:
- pass
- opipe.close()
- oinfo = ""
- return True
- else:
- return False
- def enoticeref(count):
- global lev
- global pe_sort
- global allcount
- global debug_mode
- allcount = len(os.listdir(port_enotice_dir))
- if debug_mode: dbg = " - Debug mode is ON"
- else: dbg = ""
- if count > 10:
- print("Press Enter ..."),
- sys.stdin.readline()
- print(green("\n%u notices found at reporting level %u (%u hidden) - Sorted by %s%s\n" % (count,lev,allcount-count,pe_sort,dbg)) + """
- q)uit
- s)ort a | t - sort alphabetical or by timestamp
- r)ead <num1> [<num2>] - read notice <num1>, or all notices from <num1> thru <num2> ("r" is optional)
- r)ead <letter> - read all notices whose package names start with <letter>
- l)evel <num> - reporting level: 3=verbose; 2=info; 1=warning; 0=critical
- a)ll notices
- d)ebug mode - Toggle display of enotice lines with priority < reporting level
- d)elete <num1> [<num2>] - delete notice <num1>, or all notices from <num1> thru <num2>
- p)urge all notices
- """)
- def sortedbymtime(path):
- mtime = lambda f: os.stat(os.path.join(path, f)).st_mtime
- return list(sorted(os.listdir(path), key=mtime))
- # Obtain, sort and return a list of notices which contain
- # lines at or above the current reporting level
- def mknoticelist():
- global lev
- global pe_sort
- llev = min(2, lev)
- lfiles = []
- allfiles = []
- if pe_sort == "time":
- allfiles = sortedbymtime(port_enotice_dir)
- else:
- allfiles = os.listdir(port_enotice_dir)
- allfiles.sort()
- for efile in allfiles:
- minlev = 2
- efile_h = open(port_enotice_dir + efile)
- for line in efile_h.readlines():
- if re.match("^(info|warn|error):",line):
- msg = re.split(":",line,1)
- if msg[0] == "info":
- minlev = min(minlev,2)
- if msg[0] == "warn":
- minlev = min(minlev,1)
- if msg[0] == "error":
- minlev = min(minlev,0)
- if minlev <= llev:
- lfiles.append(efile)
- return lfiles
- def main():
- global oinfo
- global lev
- global pe_sort
- global debug_mode
- if port_enotice_dir == "/":
- print(red("!!! PORT_ENOTICE_DIR undefined."))
- return
- try:
- usercmd = ""
- while (usercmd != "q"):
- noticeslist = mknoticelist()
- notices = enoticedict(noticeslist)
- if len(noticeslist) == 0:
- print(white("No notices found."))
- return
- print("")
- enoticelist(notices)
- enoticeref(len(noticeslist))
- usercmd = sys.stdin.readline()
- usercmd = str(usercmd).rstrip()
- print("")
- # read a given notice by number
- if re.match("^(?:r\s+){0,1}\d+$",usercmd):
- num = int(re.match("^(?:r\s+){0,1}(\d+)$",usercmd).group(1))
- if enoticeread(notices,num): enoticedisplay()
- continue
- # change the reporting level
- elif re.match("^(?:l\s+){0,1}\d+$",usercmd):
- num = int(re.match("^(?:l\s+){0,1}(\d+)$",usercmd).group(1))
- if num > 3: lev = 3
- else: lev = num
- # read a range of notices
- 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()
- # read notices starting with a matched letter
- elif re.match("^r\s+\S$",usercmd):
- matchletter = re.match("^r\s+(\S)$",usercmd).group(1)
- enoticematchread(notices,matchletter)
- enoticedisplay()
- # read all notices
- elif re.match("^a$",usercmd):
- enoticereadall(notices)
- enoticedisplay()
- # delete a given notice
- elif re.match("^d \d+$",usercmd):
- num = int(re.split(" ",usercmd,1)[1])
- enoticedel(notices,num)
- # delete a range of notices
- elif re.match("^(?:d\s+){0,1}\d+\s+\d+$",usercmd):
- start = int(re.match("^(?:d\s+){0,1}(\d+)\s+\d+$",usercmd).group(1))
- end = int(re.match("^(?:d\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):
- enoticedel(notices,num)
- # set/unset debug mode
- elif re.match("^d$",usercmd):
- if debug_mode == True:
- debug_mode = False
- else:
- debug_mode = True
- # purge notices
- elif re.match("^p$",usercmd):
- enoticepurge(notices)
- # change sort mode
- elif re.match("^s\s+\S$",usercmd):
- sortsel = re.match("^s\s+(\S)$",usercmd).group(1)
- if sortsel == "a":
- pe_sort = "alpha"
- elif sortsel == "t":
- pe_sort = "time"
- elif not re.match("^q$",usercmd):
- print(red("Imcomplete or unknown command."))
- except:
- print(red("!!! User-Break or unexpected error encountered."))
- sys.excepthook(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])
- main()
- print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement