def test_dicts(): env = {'FOO': 'a', 'BAR': 'z'} assert envsubst({ 'foo': '${FOO}', 'bar': '${BAR}' }, env) == { 'foo': 'a', 'bar': 'z' } assert envsubst({ 'foo': '${FOO}', 'bar': '${BAZ}' }, env) == { 'foo': 'a', 'bar': '${BAZ}' }
def get_daemon_config() -> Tuple[Type[Any], Mapping[str, Any]]: parser = ArgumentParser( prog='plastrond', description='Batch operations daemon for Fedora 4.') parser.add_argument('-c', '--config', dest='config_file', help='path to configuration file', type=FileType(), action='store', required=True) parser.add_argument('-v', '--verbose', help='increase the verbosity of the status output', action='store_true') parser.add_argument('interface', help='interface to run', choices=INTERFACES.keys()) # parse command line args args = parser.parse_args() config = envsubst(yaml.safe_load(args.config_file)) configure_logging(log_filename_base='plastron.daemon', log_dir=config.get('REPOSITORY', {}).get('LOG_DIR', 'logs'), verbose=args.verbose) return INTERFACES[args.interface], config
def main(): """Parse args and handle options.""" parser = ArgumentParser(prog='plastron', description='Batch operation tool for Fedora 4.') parser.set_defaults(cmd_name=None) common_required = parser.add_mutually_exclusive_group(required=True) common_required.add_argument('-r', '--repo', help='Path to repository configuration file.', action='store') common_required.add_argument('-c', '--config', help='Path to configuration file.', action='store', dest='config_file', type=FileType('r')) common_required.add_argument('-V', '--version', help='Print version and exit.', action='version', version=version) parser.add_argument('-v', '--verbose', help='increase the verbosity of the status output', action='store_true') parser.add_argument('-q', '--quiet', help='decrease the verbosity of the status output', action='store_true') parser.add_argument('--on-behalf-of', help='delegate repository operations to this username', dest='delegated_user', action='store') subparsers = parser.add_subparsers(title='commands') command_modules = load_commands(subparsers) # parse command line args args = parser.parse_args() # if no subcommand was selected, display the help if args.cmd_name is None: parser.print_help() sys.exit(0) if args.config_file is not None: # new-style, combined config file (a la plastron.daemon) config = envsubst(yaml.safe_load(args.config_file)) repo_config = config['REPOSITORY'] broker_config = config.get('MESSAGE_BROKER', None) command_config = config.get('COMMANDS', {}) else: # old-style, repository-only config file with open(args.repo, 'r') as repo_config_file: repo_config = yaml.safe_load(repo_config_file) broker_config = None command_config = {} fcrepo = Repository(repo_config, ua_string=f'plastron/{version}', on_behalf_of=args.delegated_user) if broker_config is not None: broker = Broker(broker_config) else: broker = None # get basic logging options if 'LOGGING_CONFIG' in repo_config: with open(repo_config.get('LOGGING_CONFIG'), 'r') as logging_config_file: logging_options = yaml.safe_load(logging_config_file) else: logging_options = DEFAULT_LOGGING_OPTIONS # log file configuration log_dirname = repo_config.get('LOG_DIR') if not os.path.isdir(log_dirname): os.makedirs(log_dirname) log_filename = 'plastron.{0}.{1}.log'.format(args.cmd_name, now) logfile = os.path.join(log_dirname, log_filename) logging_options['handlers']['file']['filename'] = logfile # manipulate console verbosity if args.verbose: logging_options['handlers']['console']['level'] = 'DEBUG' elif args.quiet: logging_options['handlers']['console']['level'] = 'WARNING' # configure logging logging.config.dictConfig(logging_options) # get the selected subcommand command_module = command_modules[args.cmd_name] try: if hasattr(command_module, 'Command'): command = command_module.Command( config=command_config.get(args.cmd_name.upper())) command.repo = fcrepo command.broker = broker else: raise FailureException( f'Unable to execute command {args.cmd_name}') # dispatch to the selected subcommand print_header(args) logger.info( f'Loaded repo configuration from {args.repo or args.config_file.name}' ) if args.delegated_user is not None: logger.info( f'Running repository operations on behalf of {args.delegated_user}' ) command(fcrepo, args) print_footer(args) except FailureException as e: # something failed, exit with non-zero status logger.error(str(e)) sys.exit(1) except KeyboardInterrupt: # aborted due to Ctrl+C sys.exit(2)
def test_unknown_variable_name(): assert envsubst('${FOO}qrs', {}) == '${FOO}qrs'
def test_deep_structure(): env = {'FOO': 'a'} assert envsubst([{'foo': ['${FOO}']}], env) == [{'foo': ['a']}]
def test_simple_strings(): env = {'FOO': 'a', 'BAR': 'z'} assert envsubst('${FOO}bc', env) == 'abc' assert envsubst('${FOO}bc${BAR}yx', env) == 'abczyx'
def test_lists(): env = {'FOO': 'a', 'BAR': 'z'} assert envsubst(['${FOO}', '${BAR}'], env) == ['a', 'z'] assert envsubst(['${FOO}', '${BAR}', '${BAZ}'], env) == ['a', 'z', '${BAZ}']