def checker(hook, repo_path, txn_name, profile_name, halt_on_exception): """ Function to singularize the repoguard in precommit or postcommit mode. :param hook: Execute the repoguard as pre- or postcommit. :type hook: string :param repo_path: The path to the repository. :type repo_path: string :param txn_name: The name of the current transaction. :type txn_name: string :param halt_on_exception: Flag which indicates whether we halt on unexpected exceptions or not. :type halt_on_exception: boolean """ logger = LoggerFactory().create('%s.tools.checker' % constants.NAME) try: hooks_path = os.path.abspath(os.path.join(repo_path, "hooks")) project_config = os.path.join(hooks_path, constants.CONFIG_FILENAME) os.chdir(hooks_path) logger.debug("RepoGuard initializing...") repoguard = RepoGuard(hook, repo_path) logger.debug("Loading transaction...") repoguard.load_transaction(txn_name) logger.debug("Loading configuration...") main_config = RepoGuardConfig(constants.CONFIG_PATH) repoguard.load_config(main_config.template_dirs, project_config) logger.debug("Validating configuration...") if main_config.validate: repoguard.validate() else: logger.warning("Validation skipped.") logger.debug("RepoGuard running...") if profile_name: result = repoguard.run_profile(profile_name) else: result = repoguard.run() logger.debug("RepoGuard finished with %s.", result) if result == constants.SUCCESS: return 0 else: return 1 except validate.ValidateError: logger.exception("The configuration is invalid!") return 1 except: # pylint: disable=W0702 logger.exception( "An unexpected error occurred during the RepoGuard run! Halt on exception is '%s'." % halt_on_exception) if not halt_on_exception: return 0 else: return 1
def setup_method(self, _): self._checker = RepoGuard(constants.PRECOMMIT, "/repo/dir") self._checker.checks = mock.Mock() self._checker.handlers = mock.Mock() self._checker.transaction = transaction.Transaction( "/path/to/repository", "10") self._checker.transaction._execute_svn = mock.Mock(return_value=dict()) self._checker.load_config("/template/dir", _CONFIG_DEFAULT.splitlines())