Hi, I've made a preliminar patch for the debian stgit bug stgit #633512 [1]. The patch adds a set_file to the MessagePrinter class to be used by the mail subcommand. The patch is mostly a proof of concept and I would like to have some feedback about it before spending too much time in it.
The next step would be to keep the stgit output messages (the output not usable by other programs) in the stdout. Happy hacking, [1]: http://bugs.debian.org/633512 -- "The day Microsoft makes something that doesn't suck, is probably the day Microsoft starts making vacuum cleaners." -- Ernst Jan Plugge Saludos /\/\ /\ >< `/
commit 8ae30f3f8dc41ba0f6f2ada1bde8dca798f7a06e Author: Maximiliano Curia <[email protected]> Date: Tue Sep 23 19:36:48 2014 +0200 Allow user to set the output file. diff --git a/stgit/commands/mail.py b/stgit/commands/mail.py index d6fbca8..a2719d1 100644 --- a/stgit/commands/mail.py +++ b/stgit/commands/mail.py @@ -147,6 +147,8 @@ options = [ short = 'Use BRANCH instead of the default branch'), opt('-m', '--mbox', action = 'store_true', short = 'Generate an mbox file instead of sending'), + opt('-o', '--output', action = 'store', default = '', + short = 'Redirect output to.'), opt('--git', action = 'store_true', short = 'Use git send-email (EXPERIMENTAL)') ] + argparse.diff_opts_option() @@ -677,6 +679,12 @@ def func(parser, options, args): # early test for sender identity __get_sender() + if options.output: + if options.output == '-': + out.set_file(file=None) + else: + out.set_file(file=file(options.output, 'w+')) + out.start('Checking the validity of the patches') for p in patches: if crt_series.empty_patch(p): diff --git a/stgit/out.py b/stgit/out.py index aa66f3d..78ab5b4 100644 --- a/stgit/out.py +++ b/stgit/out.py @@ -20,63 +20,63 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import sys, textwrap class MessagePrinter(object): - def __init__(self, file = None): - class Output(object): - def __init__(self, write, flush): - self.write = write - self.flush = flush + class Output(object): + def __init__(self, write, flush): + self.write = write + self.flush = flush + self.at_start_of_line = True + self.level = 0 + def new_line(self): + """Ensure that we're at the beginning of a line.""" + if not self.at_start_of_line: + self.write('\n') self.at_start_of_line = True - self.level = 0 - def new_line(self): - """Ensure that we're at the beginning of a line.""" - if not self.at_start_of_line: - self.write('\n') - self.at_start_of_line = True - def single_line(self, msg, print_newline = True, - need_newline = True): - """Write a single line. Newline before and after are - separately configurable.""" - if need_newline: - self.new_line() - if self.at_start_of_line: - self.write(' '*self.level) - self.write(msg) - if print_newline: - self.write('\n') - self.at_start_of_line = True - else: - self.flush() - self.at_start_of_line = False - def tagged_lines(self, tag, lines): - tag += ': ' - width = 79 - 2*self.level - len(tag) - lines = [wl for line in lines - for wl in textwrap.wrap(line, width, - break_long_words = False)] - for line in lines: - self.single_line(tag + line) - tag = ' '*len(tag) - def write_line(self, line): - """Write one line of text on a lines of its own, not - indented.""" + def single_line(self, msg, print_newline = True, + need_newline = True): + """Write a single line. Newline before and after are + separately configurable.""" + if need_newline: self.new_line() - self.write('%s\n' % line) + if self.at_start_of_line: + self.write(' '*self.level) + self.write(msg) + if print_newline: + self.write('\n') self.at_start_of_line = True - def write_raw(self, string): - """Write an arbitrary string, possibly containing - newlines.""" - self.new_line() - self.write(string) - self.at_start_of_line = string.endswith('\n') + else: + self.flush() + self.at_start_of_line = False + def tagged_lines(self, tag, lines): + tag += ': ' + width = 79 - 2*self.level - len(tag) + lines = [wl for line in lines + for wl in textwrap.wrap(line, width, + break_long_words = False)] + for line in lines: + self.single_line(tag + line) + tag = ' '*len(tag) + def write_line(self, line): + """Write one line of text on a lines of its own, not + indented.""" + self.new_line() + self.write('%s\n' % line) + self.at_start_of_line = True + def write_raw(self, string): + """Write an arbitrary string, possibly containing + newlines.""" + self.new_line() + self.write(string) + self.at_start_of_line = string.endswith('\n') + def __init__(self, file = None): if file: - self.__stdout = self.__stderr = Output(file.write, file.flush) + self.__stdout = self.__stderr = self.Output(file.write, file.flush) else: - self.__stdout = Output(sys.stdout.write, sys.stdout.flush) - self.__stderr = Output(sys.stderr.write, sys.stderr.flush) + self.__stdout = self.Output(sys.stdout.write, sys.stdout.flush) + self.__stderr = self.Output(sys.stderr.write, sys.stderr.flush) if file or sys.stdout.isatty(): self.__out = self.__stdout else: - self.__out = Output(lambda msg: None, lambda: None) + self.__out = self.Output(lambda msg: None, lambda: None) self.__err = self.__stderr def stdout(self, line): """Write a line to stdout.""" @@ -109,5 +109,16 @@ class MessagePrinter(object): else: msg = 'done' self.__out.single_line(msg, need_newline = False) + def set_file(self, file=None): + if file: + self.__stdout = self.__stderr = self.Output(file.write, file.flush) + else: + self.__stdout = self.Output(sys.stdout.write, sys.stdout.flush) + self.__stderr = self.Output(sys.stderr.write, sys.stderr.flush) + if (file and file.isatty()) or (not file and sys.stdout.isatty()): + self.__out = self.__stdout + else: + self.__out = self.Output(lambda msg: None, lambda: None) + self.__err = self.__stderr out = MessagePrinter()
signature.asc
Description: Digital signature
_______________________________________________ stgit-users mailing list [email protected] https://mail.gna.org/listinfo/stgit-users
