Advertisement
Guest User

enotice.0.2.10

a guest
Dec 18th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.27 KB | None | 0 0
  1. #!/usr/bin/python -O
  2. #
  3. # $Header: $
  4. # Author: Eldad Zack <eldad@gentoo.org>
  5. # Enhancements: Lindsay Haisley <fmouse@fmp.com>
  6. # Update for python 3.3 : CNE <cnegroumf@free.fr>
  7. #
  8. # enotice: Gentoo Notice Reading Tool
  9. # version 0.2.10
  10.  
  11. import sys
  12. import os
  13. import re
  14. import string
  15. import readline
  16.  
  17. sys.path.insert(1, "/usr/lib/portage/pym")
  18. import portage
  19. from portage.output import *
  20.  
  21. port_enotice_dir = portage.settings["PORT_ENOTICE_DIR"] + "/"
  22. pe_sort = portage.settings["PORT_ENOTICE_SORT"]
  23. pe_level = portage.settings["PORT_ENOTICE_LEVEL"]
  24.  
  25. if pe_level == "verbose": lev = 3
  26. elif pe_level == "info":  lev = 2
  27. elif pe_level == "crit": lev = 0
  28. else: lev = 1
  29.  
  30. if pe_sort == "": pe_sort = "alpha"
  31.  
  32. if (not sys.stdout.isatty()) or (portage.settings["NOCOLOR"] in ["yes","true"]):
  33.         nocolor()
  34.  
  35. oinfo = ""
  36. debug_mode = False
  37. allcount = 0
  38.  
  39. def enoticeprint(notice):
  40.         global oinfo
  41.         global lev
  42.         global debug_mode
  43.         in_epatch = False
  44.         try:
  45.                 local_oinfo = ""
  46.                 file = open(port_enotice_dir + notice)
  47.  
  48.                 local_oinfo = local_oinfo + darkgreen("Package: ") + white(notice) + "\n"
  49.                 for line in file.readlines():
  50.                         line = str(line).rstrip()
  51.                         if lev < 3 and re.match("info:Applying various patches \(bugfixes/updates\) \.\.\.$", line):
  52.                                 in_epatch = True
  53.                         if in_epatch and re.match("info:Done with patching", line):
  54.                                 in_epatch = False
  55.                                 continue
  56.                         if lev < 3 and re.match(".*Applying .*\.\.\.$", line): continue
  57.                         if lev < 3 and re.match(".* Updating .*config\.guess", line): continue
  58.                         if lev < 3 and re.match(".* Updating .*config\.sub", line): continue
  59.                         if re.match("^(info|warn|error):", line):
  60.                                 msg = re.split(":",line,1)
  61.                                 if msg[0] == "info":
  62.                                         if (not debug_mode) and (lev < 2): continue
  63.                                         if in_epatch: continue
  64.                                         local_oinfo =  local_oinfo + green(" * ")
  65.                                 if msg[0] == "warn":
  66.                                         if (not debug_mode) and  (lev < 1): continue
  67.                                         local_oinfo = local_oinfo + yellow(" * ")
  68.                                 if msg[0] == "error":
  69.                                         local_oinfo = local_oinfo +  red(" * ")
  70.                                 local_oinfo = local_oinfo +  msg[1] + "\n"
  71.                         else:
  72.                                 local_oinfo = local_oinfo + line + "\n"
  73.                         file.close()
  74.                 local_oinfo = local_oinfo +  "\n"
  75.                 if re.match("^.*Package: .*\n$", local_oinfo):
  76.                         return 0
  77.                 else:
  78.                         oinfo = oinfo + local_oinfo
  79.                         return 1
  80.         except:
  81.                 print(red("!!! Reading/Parsing error in " + notice))
  82.  
  83. def enoticedict(notices):
  84.         dict = {}
  85.         for i, notice in zip(range(1,len(notices)+1),notices):
  86.                 dict[i] = notice
  87.         return dict
  88.  
  89. def enoticelist(notices):
  90.         nlist = white("Notices available:") + "\n"
  91.         for i, notice in notices.items():
  92.                 nlist =  nlist + white(str(i) + ". ") + notice + "\n"
  93.         mpipe = os.popen("more", "w")
  94.         try:
  95.                 mpipe.write(nlist)
  96.                 mpipe.close()
  97.         except:
  98.                 pass
  99.  
  100. def enoticematchread(notices,ltr):
  101.         for i, notice in notices.items():
  102.                 if notice[0] == ltr: enoticeread(notices,i)
  103.  
  104. def enoticeexist(notices,num):
  105.         if num in notices:
  106.                 return 1
  107.         else:
  108.                 print(red("Notice %u doesn't exist." % (num)))
  109.                 return 0
  110.  
  111. def enoticeread(notices,num):
  112.         if enoticeexist(notices,num):
  113.                 enoticeprint(notices[num])
  114.                 return True
  115.         else:
  116.                 return False
  117.  
  118. def enoticereadall(notices):
  119.         for notice in notices.keys():
  120.                 enoticeread(notices,notice)
  121.  
  122. def enoticepurge(notices):
  123.         for notice in notices.keys(): enoticedel(notices,notice)
  124.  
  125. def enoticedel(notices,num):
  126.         if enoticeexist(notices,num):
  127.                 try:
  128.                         os.remove(port_enotice_dir + notices[num])
  129.                 except:
  130.                         print(red("!!! Can't remove notice file: " + notices[num]))
  131. def enoticedisplay():
  132.         global oinfo
  133.         if oinfo != "":
  134.                 opipe = os.popen("less", "w")
  135.                 try:
  136.                         opipe.write(oinfo)
  137.                 except IOError:
  138.                         pass
  139.                 opipe.close()
  140.                 oinfo = ""
  141.                 return True
  142.         else:
  143.                 return False
  144.  
  145. def enoticeref(count):
  146.         global lev
  147.         global pe_sort
  148.         global allcount
  149.         global debug_mode
  150.         allcount = len(os.listdir(port_enotice_dir))
  151.         if debug_mode: dbg = " - Debug mode is ON"
  152.         else: dbg = ""
  153.         if count > 10:
  154.                 print("Press Enter ..."),
  155.                 sys.stdin.readline()
  156.  
  157.         print(green("\n%u notices found at reporting level %u (%u hidden) - Sorted by %s%s\n" % (count,lev,allcount-count,pe_sort,dbg)) + """
  158. q)uit
  159. s)ort a | t              - sort alphabetical or by timestamp
  160. r)ead <num1> [<num2>]    - read notice <num1>, or all notices from <num1> thru <num2> ("r" is optional)
  161. r)ead <letter>           - read all notices whose package names start with <letter>
  162. l)evel <num>             - reporting level: 3=verbose; 2=info; 1=warning; 0=critical
  163. a)ll notices
  164. d)ebug mode              - Toggle display of enotice lines with priority < reporting level
  165. d)elete <num1> [<num2>]  - delete notice <num1>, or all notices from <num1> thru <num2>
  166. p)urge all notices
  167.                """)
  168.  
  169. def sortedbymtime(path):
  170.         mtime = lambda f: os.stat(os.path.join(path, f)).st_mtime
  171.         return list(sorted(os.listdir(path), key=mtime))
  172.  
  173. # Obtain, sort and return a list of notices which contain
  174. # lines at or above the current reporting level
  175. def mknoticelist():
  176.         global lev
  177.         global pe_sort
  178.         llev = min(2, lev)
  179.         lfiles = []
  180.         allfiles = []
  181.         if pe_sort == "time":
  182.                 allfiles = sortedbymtime(port_enotice_dir)
  183.         else:
  184.                 allfiles = os.listdir(port_enotice_dir)
  185.                 allfiles.sort()
  186.  
  187.         for efile in allfiles:
  188.                 minlev = 2
  189.                 efile_h = open(port_enotice_dir + efile)
  190.                 for line in efile_h.readlines():
  191.                         if re.match("^(info|warn|error):",line):
  192.                                 msg = re.split(":",line,1)
  193.                                 if msg[0] == "info":
  194.                                         minlev = min(minlev,2)
  195.                                 if msg[0] == "warn":
  196.                                         minlev = min(minlev,1)
  197.                                 if msg[0] == "error":
  198.                                         minlev = min(minlev,0)
  199.                 if minlev <= llev:
  200.                         lfiles.append(efile)
  201.         return lfiles
  202.  
  203. def main():
  204.         global oinfo
  205.         global lev
  206.         global pe_sort
  207.         global debug_mode
  208.         if port_enotice_dir == "/":
  209.                 print(red("!!! PORT_ENOTICE_DIR undefined."))
  210.                 return
  211.  
  212.         try:
  213.                 usercmd = ""
  214.                 while (usercmd != "q"):
  215.                         noticeslist = mknoticelist()
  216.                         notices = enoticedict(noticeslist)
  217.  
  218.                         if len(noticeslist) == 0:
  219.                                 print(white("No notices found."))
  220.                                 return
  221.  
  222.                         print("")
  223.                         enoticelist(notices)
  224.                         enoticeref(len(noticeslist))
  225.  
  226.                         usercmd = sys.stdin.readline()
  227.                         usercmd = str(usercmd).rstrip()
  228.                         print("")
  229.  
  230.                         # read a given notice by number
  231.                         if re.match("^(?:r\s+){0,1}\d+$",usercmd):
  232.                                 num = int(re.match("^(?:r\s+){0,1}(\d+)$",usercmd).group(1))
  233.                                 if enoticeread(notices,num): enoticedisplay()
  234.                                 continue
  235.  
  236.                         # change the reporting level
  237.                         elif re.match("^(?:l\s+){0,1}\d+$",usercmd):
  238.                                 num = int(re.match("^(?:l\s+){0,1}(\d+)$",usercmd).group(1))
  239.                                 if num > 3: lev = 3
  240.                                 else: lev = num
  241.  
  242.                         # read a range of notices
  243.                         elif re.match("^(?:r\s+){0,1}\d+\s+\d+$",usercmd):
  244.                                 start = int(re.match("^(?:r\s+){0,1}(\d+)\s+\d+$",usercmd).group(1))
  245.                                 end =   int(re.match("^(?:r\s+){0,1}\d+\s+(\d+)$",usercmd).group(1))
  246.                                 if start > end:
  247.                                         foo = start
  248.                                         start = end
  249.                                         end = foo
  250.                                 for num in range(start, end+1):
  251.                                         enoticeread(notices,num)
  252.                                 enoticedisplay()
  253.  
  254.                         # read notices starting with a matched letter
  255.                         elif re.match("^r\s+\S$",usercmd):
  256.                                 matchletter = re.match("^r\s+(\S)$",usercmd).group(1)
  257.                                 enoticematchread(notices,matchletter)
  258.                                 enoticedisplay()
  259.  
  260.                         # read all notices
  261.                         elif re.match("^a$",usercmd):
  262.                                 enoticereadall(notices)
  263.                                 enoticedisplay()
  264.  
  265.                         # delete a given notice
  266.                         elif re.match("^d \d+$",usercmd):
  267.                                 num = int(re.split(" ",usercmd,1)[1])
  268.                                 enoticedel(notices,num)
  269.  
  270.                         # delete a range of notices
  271.                         elif re.match("^(?:d\s+){0,1}\d+\s+\d+$",usercmd):
  272.                                 start = int(re.match("^(?:d\s+){0,1}(\d+)\s+\d+$",usercmd).group(1))
  273.                                 end =   int(re.match("^(?:d\s+){0,1}\d+\s+(\d+)$",usercmd).group(1))
  274.                                 if start > end:
  275.                                         foo = start
  276.                                         start = end
  277.                                         end = foo
  278.                                 for num in range(start, end+1):
  279.                                         enoticedel(notices,num)
  280.  
  281.                         # set/unset debug mode
  282.                         elif re.match("^d$",usercmd):
  283.                                 if debug_mode == True:
  284.                                         debug_mode = False
  285.                                 else:
  286.                                         debug_mode = True
  287.  
  288.                         # purge notices
  289.                         elif re.match("^p$",usercmd):
  290.                                 enoticepurge(notices)
  291.  
  292.                         # change sort mode
  293.                         elif re.match("^s\s+\S$",usercmd):
  294.                                 sortsel = re.match("^s\s+(\S)$",usercmd).group(1)
  295.                                 if sortsel == "a":
  296.                                         pe_sort = "alpha"
  297.                                 elif sortsel == "t":
  298.                                         pe_sort = "time"
  299.  
  300.                         elif not re.match("^q$",usercmd):
  301.                                 print(red("Imcomplete or unknown command."))
  302.  
  303.         except:
  304.                 print(red("!!! User-Break or unexpected error encountered."))
  305.                 sys.excepthook(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])
  306.  
  307. main()
  308. print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement