diff options
author | André Erdmann <dywi@mailerd.de> | 2013-07-30 18:08:03 +0200 |
---|---|---|
committer | André Erdmann <dywi@mailerd.de> | 2013-07-30 18:08:03 +0200 |
commit | 74ab23790239567dee35061cd541eec74b24a434 (patch) | |
tree | 35391fbfb1c49785a9ca8dc4ec5b4c16b878e63b /roverlay | |
parent | roverlay/config: STATS_INTERVAL (diff) | |
download | R_overlay-74ab23790239567dee35061cd541eec74b24a434.tar.gz R_overlay-74ab23790239567dee35061cd541eec74b24a434.tar.bz2 R_overlay-74ab23790239567dee35061cd541eec74b24a434.zip |
move common script functionality to roverlay/core
Diffstat (limited to 'roverlay')
-rw-r--r-- | roverlay/__init__.py | 66 | ||||
-rw-r--r-- | roverlay/argutil.py | 12 | ||||
-rw-r--r-- | roverlay/core.py | 178 | ||||
-rw-r--r-- | roverlay/interface/main.py | 2 | ||||
-rw-r--r-- | roverlay/interface/root.py | 8 | ||||
-rw-r--r-- | roverlay/main.py | 127 | ||||
-rw-r--r-- | roverlay/recipe/easyresolver.py | 7 | ||||
-rw-r--r-- | roverlay/versiontuple.py | 2 |
8 files changed, 213 insertions, 189 deletions
diff --git a/roverlay/__init__.py b/roverlay/__init__.py index f660a4a..db229c0 100644 --- a/roverlay/__init__.py +++ b/roverlay/__init__.py @@ -3,69 +3,3 @@ # Copyright (C) 2012, 2013 André Erdmann <dywi@mailerd.de> # Distributed under the terms of the GNU General Public License; # either version 2 of the License, or (at your option) any later version. - -"""R overlay package - -Provides roverlay initialization helpers (setup_initial_logger, -load_config_file) and some information vars (version, name, ...). -""" - -__all__ = [ 'setup_initial_logger', 'load_config_file', ] - -name = "R_overlay" -version = "0.2.5" - -description_str = "R overlay creation (roverlay) " + version -license_str=( - 'Copyright (C) 2012, 2013 Andr\xc3\xa9 Erdmann\n' - 'Distributed under the terms of the GNU General Public License;\n' - 'either version 2 of the License, or (at your option) any later version.\n' -) - -import roverlay.config -import roverlay.recipe.easylogger - - -def setup_initial_logger(): - """Sets up initial logging.""" - roverlay.recipe.easylogger.setup_initial() - -def load_config_file ( - cfile, extraconf=None, setup_logger=True, load_main_only=False -): - """ - Loads the config, including the field definition file. - Sets up the logger afterwards. - (Don't call this method more than once.) - - arguments: - * cfile -- path to the config file - * extraconf -- a dict with additional config entries that will override - entries read from cfile - * setup_logger -- set up logger (defaults to True) - * load_main_only -- if set and True: load main config file only - (= do not load field def, ...) - """ - roverlay_config = roverlay.config.access() - - confloader = roverlay_config.get_loader() - - if cfile: - confloader.load_config ( cfile ) - - if extraconf is not None: - roverlay_config.merge_with ( extraconf ) - - if setup_logger: - roverlay.recipe.easylogger.setup ( roverlay_config ) - - if not load_main_only: - confloader.load_field_definition ( - roverlay_config.get_or_fail ( "DESCRIPTION.field_definition_file" ) - ) - - confloader.load_use_expand_map ( - roverlay_config.get ( "EBUILD.USE_EXPAND.rename_file" ) - ) - - return roverlay_config diff --git a/roverlay/argutil.py b/roverlay/argutil.py index 04ea84f..b101169 100644 --- a/roverlay/argutil.py +++ b/roverlay/argutil.py @@ -13,7 +13,7 @@ import argparse import pwd import grp -import roverlay +import roverlay.core def get_uid ( user ): try: @@ -119,7 +119,7 @@ def get_parser ( command_map, default_config_file, default_command='create' ): parser = argparse.ArgumentParser ( description='\n'.join (( - roverlay.description_str, roverlay.license_str, + roverlay.core.description_str, roverlay.core.license_str, )), epilog = 'Known commands:\n' + '\n'.join ( ( @@ -145,14 +145,16 @@ def get_parser ( command_map, default_config_file, default_command='create' ): # adding args starts here arg ( - '-V', '--version', action='version', version=roverlay.version + '-V', '--version', action='version', version=roverlay.core.version ) arg ( 'commands', default=default_command, - help="action to perform. choices are " + ', '.join (command_map.keys()) \ - + ". defaults to %(default)s.", + help=( + 'action to perform. choices are ' + ', '.join (command_map.keys()) + + '. defaults to %(default)s.' + ), nargs="*", choices=command_map.keys(), metavar="command" diff --git a/roverlay/core.py b/roverlay/core.py new file mode 100644 index 0000000..6e095e1 --- /dev/null +++ b/roverlay/core.py @@ -0,0 +1,178 @@ +# R overlay -- roverlay package, core functions +# -*- coding: utf-8 -*- +# Copyright (C) 2012, 2013 André Erdmann <dywi@mailerd.de> +# Distributed under the terms of the GNU General Public License; +# either version 2 of the License, or (at your option) any later version. + +"""R overlay package + +Provides roverlay initialization helpers (setup_initial_logger, +load_config_file) and some information vars (version, name, ...). +""" + +__all__ = [ + 'DIE', 'die', 'setup_initial_logger', 'load_config_file', + 'locate_config_file', 'default_helper_setup', 'load_locate_config_file', +] + +import os +import sys +import logging + +import roverlay.config +import roverlay.recipe.easylogger +import roverlay.tools.shenv + + +name = "R_overlay" +version = "0.2.5" + +description_str = "R overlay creation (roverlay) " + version +license_str = ( + 'Copyright (C) 2012, 2013 Andr\xc3\xa9 Erdmann\n' + 'Distributed under the terms of the GNU General Public License;\n' + 'either version 2 of the License, or (at your option) any later version.\n' +) + +DEFAULT_CONFIG_FILE_NAME = "R-overlay.conf" + +# directories where the config file could be found if roverlay has been +# installed, in order: +# * user roverlay dir (${HOME}/roverlay) +# * system config dir /etc/roverlay +# +# Note: $PWD has been removed +# +CONFIG_DIRS = tuple (( + ( + ( os.getenv ( 'HOME' ) or os.path.expanduser ( '~' ) ) + + os.sep + 'roverlay' + ), + # os.sep is '/' if /etc exists, so don't care about that + '/etc/roverlay', +)) + +class DIE ( object ): + """Container class for various system exit 'events'.""" + NOP = os.EX_OK + ERR = 1 + BAD_USAGE = os.EX_USAGE + USAGE = os.EX_USAGE + ARG = 9 + CONFIG = os.EX_CONFIG + OV_CREATE = 20 + SYNC = 30 + CMD_LEFTOVER = 90 + IMPORT = 91 + UNKNOWN = 95 + INTERRUPT = 130 + + @staticmethod + def die ( msg=None, code=None ): + """ + Calls syst.exit (code:=DIE.ERR) after printing a message (if any). + """ + code = DIE.ERR 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 (...) --- + +# --- DIE: exit codes --- +die = DIE.die + + +def setup_initial_logger(): + """Sets up initial logging.""" + roverlay.recipe.easylogger.setup_initial() +# --- end of setup_initial_logger (...) --- + +def force_console_logging ( log_level=logging.DEBUG ): + roverlay.recipe.easylogger.force_reset() + roverlay.recipe.easylogger.setup_initial ( log_level=log_level ) + roverlay.recipe.easylogger.freeze_status() +# --- end of force_console_logging (...) --- + +def load_config_file ( + cfile, extraconf=None, setup_logger=True, load_main_only=False +): + """ + Loads the config, including the field definition file. + Sets up the logger afterwards. + (Don't call this method more than once.) + + arguments: + * cfile -- path to the config file + * extraconf -- a dict with additional config entries that will override + entries read from cfile + * setup_logger -- set up logger (defaults to True) + * load_main_only -- if set and True: load main config file only + (= do not load field def, ...) + """ + roverlay_config = roverlay.config.access() + + confloader = roverlay_config.get_loader() + + if cfile: + confloader.load_config ( cfile ) + + if extraconf is not None: + roverlay_config.merge_with ( extraconf ) + + if setup_logger: + roverlay.recipe.easylogger.setup ( roverlay_config ) + + if not load_main_only: + confloader.load_field_definition ( + roverlay_config.get_or_fail ( "DESCRIPTION.field_definition_file" ) + ) + + confloader.load_use_expand_map ( + roverlay_config.get ( "EBUILD.USE_EXPAND.rename_file" ) + ) + + return roverlay_config + +# --- end of load_config_file (...) --- + +def locate_config_file ( + ROVERLAY_INSTALLED, CONFIG_FILE_NAME=DEFAULT_CONFIG_FILE_NAME +): + DEFAULT_CONFIG_FILE = None + # search for the config file if roverlay has been installed + if ROVERLAY_INSTALLED: + cfg = None + config_dir = None + + for config_dir in CONFIG_DIRS: + cfg = config_dir + os.sep + CONFIG_FILE_NAME + if os.path.isfile ( cfg ): + DEFAULT_CONFIG_FILE = cfg + break + + del config_dir, cfg + elif os.path.exists ( CONFIG_FILE_NAME ): + DEFAULT_CONFIG_FILE = CONFIG_FILE_NAME + + return DEFAULT_CONFIG_FILE +# --- end of locate_config_file (...) --- + +def load_locate_config_file ( + ROVERLAY_INSTALLED, CONFIG_FILE_NAME=DEFAULT_CONFIG_FILE_NAME, **kw +): + return load_config_file ( + locate_config_file ( ROVERLAY_INSTALLED, CONFIG_FILE_NAME ), **kw + ) +# --- end of load_locate_config_file (...) --- + +def default_helper_setup ( ROVERLAY_INSTALLED ): + setup_initial_logger() + config = load_locate_config_file ( + ROVERLAY_INSTALLED, extraconf={ 'installed': ROVERLAY_INSTALLED, }, + setup_logger=False, load_main_only=True, + ) + roverlay.tools.shenv.setup_env() + return config +# --- end of default_helper_setup (...) --- diff --git a/roverlay/interface/main.py b/roverlay/interface/main.py index db4ed50..1e4efb9 100644 --- a/roverlay/interface/main.py +++ b/roverlay/interface/main.py @@ -10,7 +10,7 @@ import roverlay.interface.root import roverlay.interface.depres import roverlay.interface.remote -roverlay.setup_initial_logger() +roverlay.core.setup_initial_logger() class MainInterface ( roverlay.interface.root.RootInterface ): diff --git a/roverlay/interface/root.py b/roverlay/interface/root.py index 5a4f386..1ea4ef1 100644 --- a/roverlay/interface/root.py +++ b/roverlay/interface/root.py @@ -6,14 +6,14 @@ import logging -import roverlay +import roverlay.core import roverlay.errorqueue import roverlay.hook import roverlay.interface.generic # does nothing if already initialized -roverlay.setup_initial_logger() +roverlay.core.setup_initial_logger() class RootInterface ( roverlay.interface.generic.RoverlayInterface ): """Root interfaces for accessing roverlay interfaces. @@ -70,14 +70,14 @@ class RootInterface ( roverlay.interface.generic.RoverlayInterface ): self.config = config elif config_file is not None: if additional_config is None: - self.config = roverlay.load_config_file ( + self.config = roverlay.core.load_config_file ( config_file, extraconf={ 'installed': False, } ) else: # modifies additional_config additional_config.update ( { 'installed': False, } ) - self.config = roverlay.load_config_file ( + self.config = roverlay.core.load_config_file ( config_file, extraconf=additional_config ) else: diff --git a/roverlay/main.py b/roverlay/main.py index 04678fa..9cca0a8 100644 --- a/roverlay/main.py +++ b/roverlay/main.py @@ -10,95 +10,21 @@ __all__ = [ 'main' ] import os import sys +import stat import roverlay +import roverlay.core +import roverlay.argutil import roverlay.tools.shenv import roverlay.stats.collector +import roverlay.util +import roverlay.config.entrymap +import roverlay.config.entryutil +import roverlay.packagerules.rules -# roverlay modules will be imported later - -DEFAULT_CONFIG_FILE_NAME = "R-overlay.conf" - -# directories where the config file could be found if roverlay has been -# installed, in order: -# * ${PWD} -# * user roverlay dir (${HOME}/roverlay) -# * system config dir /etc/roverlay -CONFIG_DIRS = tuple (( - '.', - ( - ( os.getenv ( 'HOME' ) or os.path.expanduser ( '~' ) ) - + os.sep + 'roverlay' - ), - # os.sep is '/' if /etc exists, so don't care about that - '/etc/roverlay', -)) - - -class DIE ( object ): - """Container class for various system exit 'events'.""" - NOP = os.EX_OK - ERR = 1 - BAD_USAGE = os.EX_USAGE - USAGE = os.EX_USAGE - ARG = 9 - CONFIG = os.EX_CONFIG - OV_CREATE = 20 - SYNC = 30 - CMD_LEFTOVER = 90 - IMPORT = 91 - UNKNOWN = 95 - INTERRUPT = 130 - - @staticmethod - def die ( msg=None, code=None ): - """ - Calls syst.exit (code:=DIE.ERR) after printing a message (if any). - """ - code = DIE.ERR 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 (...) --- - -# --- DIE: exit codes --- -die = DIE.die - -def locate_config_file ( - ROVERLAY_INSTALLED, CONFIG_FILE_NAME=DEFAULT_CONFIG_FILE_NAME -): - DEFAULT_CONFIG_FILE = None - # search for the config file if roverlay has been installed - if ROVERLAY_INSTALLED: - cfg = None - config_dir = None - - for config_dir in CONFIG_DIRS: - cfg = config_dir + os.sep + CONFIG_FILE_NAME - if os.path.isfile ( cfg ): - DEFAULT_CONFIG_FILE = cfg - break - - del config_dir, cfg - elif os.path.exists ( CONFIG_FILE_NAME ): - DEFAULT_CONFIG_FILE = CONFIG_FILE_NAME - - return DEFAULT_CONFIG_FILE -# --- end of locate_config_file (...) --- - -def default_helper_setup ( ROVERLAY_INSTALLED ): - roverlay.setup_initial_logger() - config_file = locate_config_file ( ROVERLAY_INSTALLED=ROVERLAY_INSTALLED ) - - config = roverlay.load_config_file ( - config_file, extraconf={ 'installed': ROVERLAY_INSTALLED, }, - setup_logger=False, load_main_only=True, - ) - roverlay.tools.shenv.setup_env() - return config -# --- end of default_helper_setup (...) --- + +DIE = roverlay.core.DIE +die = roverlay.core.die def run_script_main_installed(): return run_script_main ( True ) @@ -107,7 +33,7 @@ def run_script_main ( ROVERLAY_INSTALLED ): if len ( sys.argv ) < 2 or not sys.argv[0]: die ( "no executable specified.", DIE.USAGE ) - default_helper_setup ( ROVERLAY_INSTALLED ) + roverlay.core.default_helper_setup ( ROVERLAY_INSTALLED ) roverlay.tools.shenv.run_script_exec ( sys.argv[1], "runscript", sys.argv[1:], use_path=True ) @@ -117,7 +43,7 @@ def run_shell_main_installed(): return run_shell_main ( True ) def run_shell_main ( ROVERLAY_INSTALLED ): - config = default_helper_setup ( ROVERLAY_INSTALLED ) + config = roverlay.core.default_helper_setup ( ROVERLAY_INSTALLED ) shell = config.get ( 'SHELL_ENV.shell', '/bin/sh' ) roverlay.tools.shenv.run_script_exec ( shell, "shell", [ shell, ] + sys.argv [1:], use_path=False @@ -126,10 +52,6 @@ def run_shell_main ( ROVERLAY_INSTALLED ): def run_setupdirs ( config, target_uid, target_gid ): - import stat - import roverlay.util - import roverlay.config.entrymap - import roverlay.config.entryutil dodir = roverlay.util.dodir find_config_path = roverlay.config.entryutil.find_config_path @@ -181,7 +103,7 @@ def main_installed(): def main ( ROVERLAY_INSTALLED, HIDE_EXCEPTIONS=False, - CONFIG_FILE_NAME=DEFAULT_CONFIG_FILE_NAME + CONFIG_FILE_NAME=roverlay.core.DEFAULT_CONFIG_FILE_NAME ): """main() - parse args, run overlay creation, sync, ... @@ -421,9 +343,9 @@ def main ( # ******************** # get args - # imports roverlay.argutil (deleted when done) try: - import roverlay.argutil + # FIXME: why is the reimport of roverlay necessary? + import roverlay except ImportError: if HIDE_EXCEPTIONS: die ( "Cannot import roverlay modules!", DIE.IMPORT ) @@ -443,7 +365,7 @@ def main ( } - DEFAULT_CONFIG_FILE = locate_config_file ( + DEFAULT_CONFIG_FILE = roverlay.core.locate_config_file ( ROVERLAY_INSTALLED, CONFIG_FILE_NAME ) @@ -457,7 +379,6 @@ def main ( OPTION = extra_opts.get - del roverlay.argutil # -- determine commands to run # (TODO) could replace this section when adding more actions @@ -491,18 +412,10 @@ def main ( # imports: roverlay, roverlay.config.entryutil (if --help-config) try: - import roverlay - except ImportError: - if HIDE_EXCEPTIONS: - die ( "Cannot import roverlay modules!", DIE.IMPORT ) - else: - raise - - try: roverlay.stats.collector.static.time.begin ( "setup" ) - roverlay.setup_initial_logger() + roverlay.core.setup_initial_logger() - conf = roverlay.load_config_file ( + conf = roverlay.core.load_config_file ( config_file, extraconf = additional_config, setup_logger = want_logging, @@ -549,8 +462,6 @@ def main ( if OPTION ( 'print_package_rules' ): # no try-/catch block here - import roverlay.packagerules.rules - package_rules = ( roverlay.packagerules.rules.PackageRules.get_configured() ) @@ -639,7 +550,7 @@ def main ( if STATS_DB_FILE and want_db_commit: - roverlay.stats.collector.static.write_db() + roverlay.stats.collector.static.write_database() roverlay.hook.run ( 'db_written' ) diff --git a/roverlay/recipe/easyresolver.py b/roverlay/recipe/easyresolver.py index 9055516..70cf108 100644 --- a/roverlay/recipe/easyresolver.py +++ b/roverlay/recipe/easyresolver.py @@ -8,10 +8,9 @@ __all__ = [ 'setup', ] -from roverlay import config -from roverlay.depres import listeners, deptype -from roverlay.depres.depresolver import DependencyResolver -from roverlay.depres.simpledeprule import SimpleDependencyRulePool +from roverlay import config +from roverlay.depres import listeners +from roverlay.depres.depresolver import DependencyResolver def setup ( err_queue ): diff --git a/roverlay/versiontuple.py b/roverlay/versiontuple.py index 20d5cf8..6586088 100644 --- a/roverlay/versiontuple.py +++ b/roverlay/versiontuple.py @@ -114,7 +114,7 @@ class VersionTuple ( tuple ): * keep_eq -- preserve VMOD_EQ when determining the inverse of mode (Example: '<' becomes '>' if True, else '>=') """ - f = self.get_comparator ( vmod_inverse ( mode, keep_eq=True ) ) + f = self.get_comparator ( vmod_inverse ( mode, keep_eq=keep_eq ) ) return pkgver_decorator ( f ) if f is not None else None # --- end of get_package_comparator (...) --- |