--- /usr/lib/portage/pym/output.py 2006-07-16 01:38:44.000000000 -0500 +++ /usr/lib/portage/pym/output.py 2006-07-18 02:48:05.000000000 -0500 @@ -2,6 +2,7 @@ # Distributed under the terms of the GNU General Public License v2 # $Id: output.py 3778 2006-07-03 17:35:45Z zmedico $ +__docformat__ = "epytext" import commands,errno,os,re,shlex,sys from portage_const import COLOR_MAP_FILE @@ -228,3 +229,144 @@ for c in compat_functions_colors: setattr(sys.modules[__name__], c, create_color_func(c)) + +class EOutput: + """ + Performs fancy terminal formatting for status and informational messages. + + The provided methods produce identical terminal output to the eponymous + functions in the shell script C{/sbin/functions.sh} and also accept + identical parameters. + + This is not currently a drop-in replacement however, as the output-related + functions in C{/sbin/functions.sh} are oriented for use mainly by system + init scripts and ebuilds and their output can be customized via certain + C{RC_*} environment variables (see C{/etc/conf.d/rc}). B{EOutput} is not + customizable in this manner since it's intended for more general uses. + Likewise, no logging is provided. + + @ivar quiet: Specifies if output should be silenced. + @type quiet: BooleanType + @ivar term_columns: Width of terminal in characters. Defaults to the value + specified by the shell's C{COLUMNS} variable, else to the queried tty + size, else to C{80}. + @type term_columns: IntType + """ + + def __init__(self): + self.__last_e_cmd = "" + self.__last_e_len = 0 + self.quiet = False + self.term_columns = int(os.getenv("COLUMNS", 0)) + if self.term_columns == 0: + self.term_columns = int(commands.getoutput('set -- `stty size 2>/dev/null` ; echo "$2"')) + if self.term_columns == 0: + self.term_columns = 80 + + def __eend(self, caller, errno, msg): + if errno == 0: + status_brackets = colorize("BRACKET", "[ ") + colorize("GOOD", "ok") + colorize("BRACKET", " ]") + else: + status_brackets = colorize("BRACKET", "[ ") + colorize("BAD", "!!") + colorize("BRACKET", " ]") + if msg: + if caller == "eend": + self.eerror(msg[0]) + elif caller == "ewend": + self.ewarn(msg[0]) + if self.__last_e_cmd != "ebegin": + self.__last_e_len = 0 + print "%*s%s" % ((self.term_columns - self.__last_e_len - 6), "", status_brackets) + + def ebegin(self, msg): + """ + Shows a message indicating the start of a process. + + @param msg: A very brief (shorter than one line) description of the + starting process. + @type msg: StringType + """ + msg += " ..." + if not self.quiet: + self.einfon(msg) + self.__last_e_len = len(msg) + 4 + self.__last_e_cmd = "ebegin" + + def eend(self, errno, *msg): + """ + Indicates the completion of a process, optionally displaying a message + via L{eerror} if the process's exit status isn't C{0}. + + @param errno: A standard UNIX C{errno} code returned by processes upon + exit. + @type errno: IntType + @param msg: I{(optional)} An error message, typically a standard UNIX + error string corresponding to C{errno}. + @type msg: StringType + """ + if not self.quiet: + self.__eend("eend", errno, msg) + self.__last_e_cmd = "eend" + + def eerror(self, msg): + """ + Shows an error message. + + @param msg: A very brief (shorter than one line) error message. + @type msg: StringType + """ + if not self.quiet: + if self.__last_e_cmd == "ebegin": print + print colorize("BAD", " * ") + msg + self.__last_e_cmd = "eerror" + + def einfo(self, msg): + """ + Shows an informative message terminated with a newline. + + @param msg: A very brief (shorter than one line) informative message. + @type msg: StringType + """ + if not self.quiet: + if self.__last_e_cmd == "ebegin": print + print colorize("GOOD", " * ") + msg + self.__last_e_cmd = "einfo" + + def einfon(self, msg): + """ + Shows an informative message terminated without a newline. + + @param msg: A very brief (shorter than one line) informative message. + @type msg: StringType + """ + if not self.quiet: + if self.__last_e_cmd == "ebegin": print + print colorize("GOOD", " * ") + msg , + self.__last_e_cmd = "einfon" + + def ewarn(self, msg): + """ + Shows a warning message. + + @param msg: A very brief (shorter than one line) warning message. + @type msg: StringType + """ + if not self.quiet: + if self.__last_e_cmd == "ebegin": print + print colorize("WARN", " * ") + msg + self.__last_e_cmd = "ewarn" + + def ewend(self, errno, *msg): + """ + Indicates the completion of a process, optionally displaying a message + via L{ewarn} if the process's exit status isn't C{0}. + + @param errno: A standard UNIX C{errno} code returned by processes upon + exit. + @type errno: IntType + @param msg: I{(optional)} A warning message, typically a standard UNIX + error string corresponding to C{errno}. + @type msg: StringType + """ + if not self.quiet: + self.__eend("ewend", errno, msg) + self.__last_e_cmd = "ewend"