示例#1
0
    def run(cls):
        results_dir = None
        start_dir = os.getcwd()
        try:
            LoggerHelper.setup_memory_handler()
            main_handler, output_tool_handler = LoggerHelper.create_stream_handlers(
            )
            cli = CLI()
            if hasattr(cli, 'version'):
                print(VERSION)
                sys.exit(0)

            logger.info('rebase-helper version %s', VERSION)

            config = Config(getattr(cli, 'config-file', None))
            config.merge(cli)
            for handler in [main_handler, output_tool_handler]:
                handler.set_terminal_background(config.background)
            if config.verbose == 0:
                main_handler.setLevel(logging.INFO)
            elif config.verbose == 1:
                main_handler.setLevel(CustomLogger.VERBOSE)
            else:
                main_handler.setLevel(logging.DEBUG)
            ConsoleHelper.use_colors = ConsoleHelper.should_use_colors(config)
            if config.bugzilla_id:
                repo_path, config.config[
                    'sources'] = BugzillaHelper.prepare_rebase_repository(
                        config.bugzilla_id)
                try:
                    os.chdir(repo_path)
                except OSError as e:
                    raise RebaseHelperError(
                        'Could not change directory to the cloned repository'
                    ) from e
                # update relative paths in config
                for option in ('results_dir', 'workspace_dir'):
                    path = getattr(config, option)
                    if path and not os.path.isabs(path):
                        config.config[option] = os.path.join(
                            os.path.relpath(start_dir, os.getcwd()), path)

            if config.config['rpmmacros']:
                macros = ' -D ' + ' -D '.join(
                    '"{}"'.format(s) for s in config.config['rpmmacros'])
                for option in ('builder_options', 'srpm_builder_options'):
                    if config.config[option]:
                        config.config[option] += macros
                    else:
                        config.config[option] = macros

            config.config['rpmmacros'] = cls.convert_macros_to_dict(
                config.rpmmacros)
            execution_dir, results_dir = Application.setup(config)
            app = Application(config, start_dir, execution_dir, results_dir)
            app.run()
        except KeyboardInterrupt:
            logger.info('Interrupted by user')
        except RebaseHelperError as e:
            if e.msg:
                logger.error('%s', e.msg)
            else:
                logger.error('%s', str(e))
            sys.exit(1)
        except SystemExit as e:
            sys.exit(e.code)
        except BaseException:
            logger_traceback: CustomLogger = cast(
                CustomLogger, logging.getLogger('rebasehelper.traceback'))
            logger_traceback.propagate = False
            logger_traceback.setLevel(CustomLogger.TRACE)
            if results_dir:
                debug_log = os.path.join(results_dir, LOGS_DIR, DEBUG_LOG)
                traceback_log = os.path.join(results_dir, LOGS_DIR,
                                             TRACEBACK_LOG)
                logger.error(
                    'rebase-helper failed due to an unexpected error. Please report this problem'
                    '\nusing the following link: %s'
                    '\nand include the content of'
                    '\n\'%s\' and'
                    '\n\'%s\''
                    '\nin the report.'
                    '\nThank you!', NEW_ISSUE_LINK, debug_log, traceback_log)
                LoggerHelper.add_file_handler(logger_traceback, traceback_log)
            else:
                logger.error(
                    'rebase-helper failed due to an unexpected error. Please report this problem'
                    '\nusing the following link: %s'
                    '\nand include the following traceback in the report.'
                    '\nThank you!', NEW_ISSUE_LINK)

                LoggerHelper.add_stream_handler(logger_traceback,
                                                CustomLogger.TRACE)
            logger_traceback.trace('', exc_info=1)
            sys.exit(1)

        sys.exit(0)