From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id BCB531381F3 for ; Fri, 6 Sep 2013 17:27:48 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id D4D90E0FB8; Fri, 6 Sep 2013 17:27:47 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 23C39E0FB0 for ; Fri, 6 Sep 2013 17:27:47 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 07AEE33EBA2 for ; Fri, 6 Sep 2013 17:27:46 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 94E5AE5469 for ; Fri, 6 Sep 2013 17:27:43 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1378488268.fba66f36ad0cad176d7b024880d92d2693058800.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/runtime.py X-VCS-Directories: roverlay/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: fba66f36ad0cad176d7b024880d92d2693058800 X-VCS-Branch: master Date: Fri, 6 Sep 2013 17:27:43 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: c6ba7aaf-796c-4b35-b573-155c046fa8a7 X-Archives-Hash: d5e183644f35fd46dca965cf1d652db0 commit: fba66f36ad0cad176d7b024880d92d2693058800 Author: André Erdmann mailerd de> AuthorDate: Fri Sep 6 17:24:28 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Fri Sep 6 17:24:28 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=fba66f36 roverlay/runtime: IndependentRuntimeEnvironment env for scripts that are independent of roverlay's core functionality. This allows to add a setup script that handles config file creation (already implemented as roverlay-mkconfig), config initialization (fs layout and what's currently done by pkg_config() in the live ebuild, but more controlled) and enabling/disabling hooks. --- roverlay/runtime.py | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 3 deletions(-) diff --git a/roverlay/runtime.py b/roverlay/runtime.py index de20f91..bbc73d7 100644 --- a/roverlay/runtime.py +++ b/roverlay/runtime.py @@ -14,10 +14,54 @@ import roverlay.core import roverlay.hook import roverlay.remote.repolist import roverlay.stats.collector +import roverlay.util.objects +import roverlay.recipe.easylogger from roverlay.core import DIE, die -class RuntimeEnvironmentBase ( object ): +# TODO: move/merge roverlay.core.DIE into runtime env + + +class MinimalRuntimeEnvironment ( object ): + + HLINE = 79 * '-' + + def __init__ ( self ): + super ( MinimalRuntimeEnvironment, self ).__init__() + self.logger = None + self.bind_logger ( logging.getLogger() ) + # -- end of __init__ (...) --- + + def bind_logger ( self, logger ): + self.logger = logger + self.log_debug = logger.debug + self.log_info = logger.info + self.log_warn = logger.warn + self.log_warning = logger.warning + self.log_error = logger.error + self.log_critical = logger.critical + # --- end of bind_logger (...) --- + + @roverlay.util.objects.abstractmethod + def setup ( self ): + pass + + def die ( self, msg=None, code=None ): + """ + Calls syst.exit (code:=1) after printing a message (if any). + """ + code = 1 if code is None else code + if msg is not None: + sys.stderr.write ( msg + "\n" ) +# else: +# sys.stderr.write ( "died.\n" ) + sys.exit ( code ) + # --- end of die (...) --- + +# --- end of MinimalRuntimeEnvironment --- + + +class RuntimeEnvironmentBase ( MinimalRuntimeEnvironment ): ARG_PARSER_CLS = None KEEP_ARG_PARSER = False @@ -28,11 +72,9 @@ class RuntimeEnvironmentBase ( object ): config_file_name=roverlay.core.DEFAULT_CONFIG_FILE_NAME ): super ( RuntimeEnvironmentBase, self ).__init__() - self.logger = logging.getLogger() self.installed = bool ( installed ) self.hide_exceptions = bool ( hide_exceptions ) self.config_file_name = str ( config_file_name ) - self.HLINE = 79 * '-' self.stats = roverlay.stats.collector.static self.config = None @@ -220,3 +262,80 @@ class RuntimeEnvironment ( RuntimeEnvironmentBase ): # --- end of optionally (...) --- # --- end of RuntimeEnvironment --- + + +class IndependentRuntimeEnvironment ( MinimalRuntimeEnvironment ): + + LOG_FORMAT = None + + def __init__ ( self, installed=True ): + super ( IndependentRuntimeEnvironment, self ).__init__() + self.config = roverlay.config.tree.ConfigTree ( register_static=False ) + + self.CONFIG_DEFAULTS = { 'installed': installed, } + self.extend_config ( self.CONFIG_DEFAULTS ) + + self.parser = None + self.options = None + self.commands = None + # --- end of __init__ (...) --- + + def extend_config ( self, additional_config ): + self.config.merge_with ( additional_config ) + # --- end of extend_config (...) --- + + def reset_config ( self ): + self.config.reset() + self.extend_config ( self.CONFIG_DEFAULTS ) + # --- end of reset_config (...) --- + + def inject_config_path ( self, path, value ): + return self.config.inject ( path, value, suppress_log=True ) + # --- end of inject_config_path (...) --- + + @roverlay.util.objects.abstractmethod + def create_argparser ( self ): + pass + # --- end of create_argparser (...) --- + + def setup_argparser ( self ): + parser = self.create_argparser() + if parser is not False: + parser.setup() + self.parser = parser + + parser.parse() + + self.options = parser.get_options() + self.commands = parser.get_commands() + # --- end of do_setup_parser (...) --- + + def setup_common ( self ): + roverlay.recipe.easylogger.force_console_logging ( + log_formatter=logging.Formatter ( self.LOG_FORMAT ) + ) + self.setup_argparser() + # --- end of setup_common (...) --- + + def setup ( self ): + self.setup_common() + # --- end of setup (...) --- + + def option ( self, key, fallback=None ): + return self.options.get ( key, fallback ) + # --- end of option (...) --- + + def optionally ( self, func, key, *args, **kwargs ): + if self.options.get ( key, False ): + return func ( *args, **kwargs ) + else: + return None + # --- end of optionally (...) --- + + def is_installed ( self ): + return self.config.get_or_fail ( 'installed' ) + # --- end of is_installed (...) --- + + installed = property ( is_installed ) + +# --- end of IndependentRuntimeEnvironment ---