def system(args, timeout=None): stime = time.time() retcode, stdout = proc.run_cmd(args, timeout=timeout) d = time.time() - stime pub_args = garble_password(args) msg = [] a = msg.append a("%s %s %r\n" % (retcode, d, pub_args)) writemsg = lambda: sys.stderr.write("".join(msg)) if retcode != 0: a(stdout) a("\n====================\n") writemsg() lines = [" " + x for x in stdout[-4096:].split("\n")] raise RuntimeError( "command failed with returncode %s: %r\nLast Output:\n%s" % (retcode, pub_args, "\n".join(lines))) writemsg()
def system(args, timeout=None): stime = time.time() retcode, stdout = proc.run_cmd(args, timeout=timeout) d = time.time() - stime pub_args = garble_password(args) msg = [] a = msg.append a("%s %s %r\n" % (retcode, d, pub_args)) writemsg = lambda: sys.stderr.write("".join(msg)) if retcode != 0: a(stdout) a("\n====================\n") writemsg() lines = [" " + x for x in stdout[-4096:].split("\n")] raise RuntimeError("command failed with returncode %s: %r\nLast Output:\n%s" % (retcode, pub_args, "\n".join(lines))) writemsg()
def test_garble_password(): x = utils.garble_password(['foo', '--password', 'secret']) assert 'secret' not in x utils.garble_password(['foo', '--password']) utils.garble_password(['foo'])
def __call__(self): options, args, parser = self.parse_options() conf.readrc() self.parser = parser self.options = options import tempfile from mwlib.writerbase import WriterError from mwlib.status import Status use_help = 'Use --help for usage information.' if options.list_writers: self.list_writers() return if options.writer_info: self.show_writer_info(options.writer_info) return if options.output is None: parser.error('Please specify an output file with --output.\n' + use_help) options.output = os.path.abspath(options.output) if options.writer is None: parser.error('Please specify a writer with --writer.\n' + use_help) writer = self.load_writer(options.writer) writer_options = {} if options.writer_options: for wopt in options.writer_options.split(';'): if '=' in wopt: key, value = wopt.split('=', 1) else: key, value = wopt, True writer_options[str(key)] = value if options.language: writer_options['lang'] = options.language for option in writer_options.keys(): if option not in getattr(writer, 'options', {}): print 'Warning: unknown writer option %r' % option del writer_options[option] init_tmp_cleaner() self.status = Status(options.status_file, progress_range=(1, 33)) self.status(progress=0) env = None try: env = self.get_environment() try: _locale.set_locale_from_lang( env.wiki.siteinfo["general"]["lang"]) except BaseException as err: print "Error: could not set locale", err basename = os.path.basename(options.output) if '.' in basename: ext = '.' + basename.rsplit('.', 1)[-1] else: ext = '' fd, tmpout = tempfile.mkstemp(dir=os.path.dirname(options.output), suffix=ext) os.close(fd) writer(env, output=tmpout, status_callback=self.status, **writer_options) os.rename(tmpout, options.output) kwargs = {} if hasattr(writer, 'content_type'): kwargs['content_type'] = writer.content_type if hasattr(writer, 'file_extension'): kwargs['file_extension'] = writer.file_extension self.status(status='finished', progress=100, **kwargs) if options.keep_zip is None and self.zip_filename is not None: utils.safe_unlink(self.zip_filename) except Exception as e: import traceback self.status(status='error') if options.error_file: fd, tmpfile = tempfile.mkstemp( dir=os.path.dirname(options.error_file)) f = os.fdopen(fd, 'wb') if isinstance(e, WriterError): f.write(str(e)) else: f.write('traceback\n') traceback.print_exc(file=f) f.write("sys.argv=%r\n" % (utils.garble_password(sys.argv), )) f.close() os.rename(tmpfile, options.error_file) raise finally: if env is not None and env.images is not None: try: if not options.keep_tmpfiles: env.images.clear() except OSError as e: if e.errno != errno.ENOENT: print 'ERROR: Could not remove temporary images: %s' % e, e.errno