diff options
author | 2013-07-12 19:07:28 +0200 | |
---|---|---|
committer | 2013-07-12 19:07:28 +0200 | |
commit | 14ebb5207fb1cf44cad45780d1f9ecad5372ec94 (patch) | |
tree | 46560dce52b2960d08d23ae312b3f723b69909bd | |
parent | ebuild: set up roverlay via pkg_config() (diff) | |
download | R_overlay-14ebb5207fb1cf44cad45780d1f9ecad5372ec94.tar.gz R_overlay-14ebb5207fb1cf44cad45780d1f9ecad5372ec94.tar.bz2 R_overlay-14ebb5207fb1cf44cad45780d1f9ecad5372ec94.zip |
roverlay: --run-script (for testing)
Run a command in roverlay's hook environment.
--script-args "<arg arg2...>" can be used to pass args to the called scripts.
Args cannot contain whitespace.
-rw-r--r-- | roverlay/argutil.py | 24 | ||||
-rw-r--r-- | roverlay/main.py | 77 | ||||
-rw-r--r-- | roverlay/tools/shenv.py | 64 |
3 files changed, 114 insertions, 51 deletions
diff --git a/roverlay/argutil.py b/roverlay/argutil.py index 120132c..3cf4428 100644 --- a/roverlay/argutil.py +++ b/roverlay/argutil.py @@ -407,6 +407,19 @@ def get_parser ( command_map, default_config_file, default_command='create' ): type=is_gid, ) + arg ( + '--run-script', '-X', default=argparse.SUPPRESS, + metavar="<script>", + help="run a script in roverlay\'s environment and exit afterwards", + type=is_fs_file, + ) + + arg ( + '--script-args', dest='run_script_args', default=argparse.SUPPRESS, + metavar="<args>", + help="args for --run-script", + ) + # # TODO # arg ( # '--debug', @@ -456,7 +469,9 @@ def parse_argv ( command_map, **kw ): doconf ( value, config_path ) # --- end of doconf_simple (...) --- - commands = ( p.commands, ) if isinstance ( p.commands, str ) else p.commands + commands = ( + ( p.commands, ) if isinstance ( p.commands, str ) else p.commands + ) conf = dict() extra = dict ( # TODO: remove nosync entirely from extra @@ -479,6 +494,13 @@ def parse_argv ( command_map, **kw ): target_gid = p.target_gid, ) + if given ( 'run_script' ): + extra ['run_script'] = p.run_script + extra ['run_script_args'] = tuple ( + getattr ( p, 'run_script_args', "" ).split ( None ) + ) + # or use shlex for splitting + if given ( 'overlay' ): doconf ( p.overlay, 'OVERLAY.dir' ) #extra ['write_overlay'] = True diff --git a/roverlay/main.py b/roverlay/main.py index 0fec74d..24c02c6 100644 --- a/roverlay/main.py +++ b/roverlay/main.py @@ -63,6 +63,29 @@ class DIE ( object ): # --- 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 run_setupdirs ( config, target_uid, target_gid ): import stat import roverlay.util @@ -363,6 +386,10 @@ def main ( overlay_creator.close() # --- end of run_overlay_create() --- + # ******************** + # main() starts here + # ******************** + # get args # imports roverlay.argutil (deleted when done) try: @@ -386,22 +413,9 @@ def main ( } - 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 - + DEFAULT_CONFIG_FILE = locate_config_file ( + ROVERLAY_INSTALLED, CONFIG_FILE_NAME + ) commands, config_file, additional_config, extra_opts = ( roverlay.argutil.parse_argv ( @@ -425,10 +439,17 @@ def main ( want_logging = True do_setupdirs = False + do_runscript = False if 'sync' in actions and OPTION ( 'nosync' ): die ( "sync command blocked by --nosync opt.", DIE.ARG ) - elif 'setupdirs' in actions: + elif 'run_script' in extra_opts: + # add --run-script as command + actions.add ( "run_script" ) + do_runscript = True + want_logging = False + + if 'setupdirs' in actions: do_setupdirs = True want_logging = False if len ( actions ) > 1: @@ -458,9 +479,9 @@ def main ( conf = roverlay.load_config_file ( config_file, - extraconf=additional_config, - setup_logger=want_logging, - load_main_only=do_setupdirs, + extraconf = additional_config, + setup_logger = want_logging, + load_main_only = ( do_setupdirs or do_runscript ), ) del config_file, additional_config except: @@ -474,11 +495,23 @@ def main ( else: raise - - if do_setupdirs: + if do_runscript: + import roverlay.tools.shenv + sys.exit ( + roverlay.tools.shenv.run_script ( + script = extra_opts ['run_script'], + phase = "user", + argv = extra_opts ['run_script_args'], + return_success = False, + log_output = False, + initial_dir = os.getcwd(), + ).returncode + ) + elif do_setupdirs: sys.exit ( run_setupdirs ( conf, extra_opts['target_uid'], extra_opts['target_gid'] ) ) + # -- end commands with partial config / without logging if OPTION ( 'list_config' ): try: diff --git a/roverlay/tools/shenv.py b/roverlay/tools/shenv.py index 5ae0f25..084f318 100644 --- a/roverlay/tools/shenv.py +++ b/roverlay/tools/shenv.py @@ -188,10 +188,10 @@ def setup_env(): shlib_path = [] if ROVERLAY_INSTALLED: - installed_shlib = ( - roverlay.config.get_or_fail ( 'INSTALLINFO.libexec' ) - + os.sep + SHLIB_DIRNAME - ) + data_root = roverlay.config.get_or_fail ( 'INSTALLINFO.libexec' ) + setup ( 'DATADIR', data_root ) + installed_shlib = data_root + os.sep + SHLIB_DIRNAME + if os.path.isdir ( installed_shlib ): shlib_path.append ( installed_shlib ) shlib_file = installed_shlib + os.sep + SHFUNC_FILENAME @@ -288,7 +288,10 @@ def update_env ( **info ): # --- end of update_env (...) --- -def run_script ( script, phase, return_success=False, logger=None ): +def run_script ( + script, phase, argv=(), return_success=False, logger=None, + log_output=True, initial_dir=None, +): # global _SHELL_INTPR # if _SHELL_INTPR is None: # _SHELL_INTPR = roverlay.config.get ( 'SHELL_ENV.shell', '/bin/sh' ) @@ -305,11 +308,11 @@ def run_script ( script, phase, return_success=False, logger=None ): try: script_call = subprocess.Popen ( # ( _SHELL_INTPR, script, ), - ( script, ), + ( script, ) + argv, stdin = None, - stdout = subprocess.PIPE, - stderr = subprocess.PIPE, - cwd = my_env ['S'], + stdout = subprocess.PIPE if log_output else None, + stderr = subprocess.PIPE if log_output else None, + cwd = my_env ['S'] if initial_dir is None else initial_dir, env = my_env, ) @@ -324,27 +327,32 @@ def run_script ( script, phase, return_success=False, logger=None ): raise - log_snip_here = ( - '--- {{}} for script {s!r}, phase {p!r} ---'.format ( - s=script, p=my_env ['ROVERLAY_PHASE'] + if log_output: + log_snip_here = ( + '--- {{}} for script {s!r}, phase {p!r} ---'.format ( + s=script, p=my_env ['ROVERLAY_PHASE'] + ) ) - ) - # log stdout - if output[0] and my_logger.isEnabledFor ( logging.INFO ): - my_logger.info ( log_snip_here.format ( "stdout" ) ) - for line in roverlay.strutil.pipe_lines ( output[0], use_filter=True ): - my_logger.info ( line ) - my_logger.info ( log_snip_here.format ( "end stdout" ) ) - # -- end if stdout; - - # log stderr - if output[1] and my_logger.isEnabledFor ( logging.WARNING ): - my_logger.warning ( log_snip_here.format ( "stderr" ) ) - for line in roverlay.strutil.pipe_lines ( output[1], use_filter=True ): - my_logger.warning ( line ) - my_logger.warning ( log_snip_here.format ( "end stderr" ) ) - # --- end if stderr; + # log stdout + if output[0] and my_logger.isEnabledFor ( logging.INFO ): + my_logger.info ( log_snip_here.format ( "stdout" ) ) + for line in ( + roverlay.strutil.pipe_lines ( output[0], use_filter=True ) + ): + my_logger.info ( line ) + my_logger.info ( log_snip_here.format ( "end stdout" ) ) + # -- end if stdout; + + # log stderr + if output[1] and my_logger.isEnabledFor ( logging.WARNING ): + my_logger.warning ( log_snip_here.format ( "stderr" ) ) + for line in ( + roverlay.strutil.pipe_lines ( output[1], use_filter=True ) + ): + my_logger.warning ( line ) + my_logger.warning ( log_snip_here.format ( "end stderr" ) ) + # --- end if stderr; if return_success: if script_call.returncode == os.EX_OK: |