def resolve(cls, config_file): """Parse a keystore config file and return a list of Keystore objects.""" config_file = os.path.expanduser(config_file) config = Config.create_parser() try: with open(config_file, 'rb') as keystore_config: config.readfp(keystore_config) except IOError as e: raise KeystoreResolver.Error('Problem parsing config at {}: {}'.format(config_file, e)) parser = SingleFileConfig(config_file, config) key_names = config.sections() keys = {} def create_key(key_name): """Instantiate Keystore objects.""" keystore = Keystore(keystore_name=key_name, build_type=parser.get_required(key_name, 'build_type'), keystore_location=parser.get_required(key_name, 'keystore_location'), keystore_alias=parser.get_required(key_name, 'keystore_alias'), keystore_password=parser.get_required(key_name, 'keystore_password'), key_password=parser.get_required(key_name, 'key_password')) return keystore for name in key_names: try: keys[name] = create_key(name) except Config.ConfigError as e: raise KeystoreResolver.Error(e) return keys
def resolve(cls, config_file): """Parse a keystore config file and return a list of Keystore objects.""" config = Config.create_parser() try: with open(config_file, 'rb') as keystore_config: config.readfp(keystore_config) except IOError: raise KeystoreResolver.Error('The \'--{0}\' option must point at a valid .ini file holding ' 'keystore definitions.'.format(cls._CONFIG_SECTION)) parser = SingleFileConfig(config_file, config) key_names = config.sections() keys = {} def create_key(key_name): """Instantiate Keystore objects.""" keystore = Keystore(keystore_name=key_name, build_type=parser.get_required(key_name, 'build_type'), keystore_location=parser.get_required(key_name, 'keystore_location'), keystore_alias=parser.get_required(key_name, 'keystore_alias'), keystore_password=parser.get_required(key_name, 'keystore_password'), key_password=parser.get_required(key_name, 'key_password')) return keystore for name in key_names: try: keys[name] = create_key(name) except Config.ConfigError as e: raise KeystoreResolver.Error(e) return keys
def check_config_file(path): cp = Config.create_parser() with open(path, 'r') as ini: cp.readfp(ini) print('Checking config file at {0} for unmigrated keys.'.format(path), file=sys.stderr) def section(s): return cyan('[{0}]'.format(s)) for src, dst in migrations.items(): check_option(cp, src, dst) # Special-case handling of per-task subsystem options, so we can sweep them up in all # sections easily. def check_task_subsystem_options(subsystem_sec, options_map, sections=None): sections = sections or cp.sections() for src_sec in ['DEFAULT'] + sections: dst_sec = subsystem_sec if src_sec == 'DEFAULT' else '{}.{}'.format(subsystem_sec, src_sec) for src_key, dst_key in options_map.items(): check_option(cp, (src_sec, src_key), (dst_sec, dst_key)) artifact_cache_options_map = { 'read_from_artifact_cache': 'read', 'write_to_artifact_cache': 'write', 'overwrite_cache_artifacts': 'overwrite', 'read_artifact_caches': 'read_from', 'write_artifact_caches': 'write_to', 'cache_compression': 'compression_level', } check_task_subsystem_options('cache', artifact_cache_options_map) jvm_options_map = { 'jvm_options': 'options', 'args': 'program_args', 'debug': 'debug', 'debug_port': 'debug_port', 'debug_args': 'debug_args', } jvm_options_sections = [ 'repl.scala', 'test.junit', 'run.jvm', 'bench', 'doc.javadoc', 'doc.scaladoc' ] check_task_subsystem_options('jvm', jvm_options_map, sections=jvm_options_sections) # Check that all values are parseable. for sec in ['DEFAULT'] + cp.sections(): for key, value in cp.items(sec): value = value.strip() if value.startswith('['): try: custom_types.list_type(value) except ParseError: print('Value of {key} in section {section} is not a valid ' 'JSON list.'.format(key=green(key), section=section(sec))) elif value.startswith('{'): try: custom_types.dict_type(value) except ParseError: print('Value of {key} in section {section} is not a valid ' 'JSON object.'.format(key=green(key), section=section(sec)))
def resolve(cls, config_file): """Parse a keystore config file and return a list of Keystore objects.""" config_file = os.path.expanduser(config_file) config = Config.create_parser() try: with open(config_file, 'rb') as keystore_config: config.readfp(keystore_config) except IOError as e: raise KeystoreResolver.Error( 'Problem parsing config at {}: {}'.format(config_file, e)) parser = SingleFileConfig(config_file, config) key_names = config.sections() keys = {} def create_key(key_name): """Instantiate Keystore objects.""" keystore = Keystore( keystore_name=key_name, build_type=parser.get_required(key_name, 'build_type'), keystore_location=parser.get_required(key_name, 'keystore_location'), keystore_alias=parser.get_required(key_name, 'keystore_alias'), keystore_password=parser.get_required(key_name, 'keystore_password'), key_password=parser.get_required(key_name, 'key_password')) return keystore for name in key_names: try: keys[name] = create_key(name) except Config.ConfigError as e: raise KeystoreResolver.Error(e) return keys
def create_config(sample_ini=''): """Creates a ``Config`` from the ``sample_ini`` file contents. :param string sample_ini: The contents of the ini file containing the config values. """ if not isinstance(sample_ini, Compatibility.string): raise ValueError('The sample_ini supplied must be a string, given: %s' % sample_ini) parser = Config.create_parser() with io.BytesIO(sample_ini.encode('utf-8')) as ini: parser.readfp(ini) return SingleFileConfig('dummy/path', parser)
def create_config(sample_ini='', defaults=None): """Creates a ``Config`` from the ``sample_ini`` file contents. :param string sample_ini: The contents of the ini file containing the config values. :param dict defaults: An optional dict of global default ini values to seed. """ if not isinstance(sample_ini, Compatibility.string): raise ValueError('The sample_ini supplied must be a string, given: %s' % sample_ini) parser = Config.create_parser(defaults) with io.BytesIO(sample_ini.encode('utf-8')) as ini: parser.readfp(ini) return Config(parser)
def check_config_file(path): cp = Config.create_parser() with open(path, 'r') as ini: cp.readfp(ini) config = SingleFileConfig(path, cp) print('Checking config file at {0} for unmigrated keys.'.format(path), file=sys.stderr) def section(s): return cyan('[{0}]'.format(s)) for (src_section, src_key), dst in migrations.items(): def has_explicit_option(section, key): # David tried to avoid poking into cp's guts in https://rbcommons.com/s/twitter/r/1451/ but # that approach fails for the important case of boolean options. Since this is a ~short term # tool and its highly likely its lifetime will be shorter than the time the private # ConfigParser_sections API we use here changes, its worth the risk. return cp.has_section(section) and (key in cp._sections[section]) if has_explicit_option(src_section, src_key): if dst is not None: dst_section, dst_key = dst print('Found {src_key} in section {src_section}. Should be {dst_key} in section ' '{dst_section}.'.format(src_key=green(src_key), src_section=section(src_section), dst_key=green(dst_key), dst_section=section(dst_section)), file=sys.stderr) elif (src_section, src_key) not in notes: print('Found {src_key} in section {src_section} and there is no automated migration path' 'for this option. Please consult the ' 'codebase.'.format(src_key=red(src_key), src_section=red(src_section))) if (src_section, src_key) in notes: print(' Note: {0}'.format(yellow(notes[(src_section, src_key)]))) # Check that all values are parseable. for sec in ['DEFAULT'] + cp.sections(): for key, value in cp.items(sec): value = value.strip() if value.startswith('['): try: custom_types.list_type(value) except ParseError: print('Value of {key} in section {section} is not a valid ' 'JSON list.'.format(key=green(key), section=section(sec))) elif value.startswith('{'): try: custom_types.dict_type(value) except ParseError: print('Value of {key} in section {section} is not a valid ' 'JSON object.'.format(key=green(key), section=section(sec)))
def apply_defaults(self, commands, args): """Augment arguments with defaults found for the given commands. The returned arguments will be a new copy of the given args with possibly extra augmented arguments. Default options are applied from the following keys under a section with the name of the sub-command the default options apply to: * `options` - These options are either prepended or appended to the command line args as specified in the constructor with default_prepend. * `prepend-options` - These options are prepended to the command line args. * `append-options` - These options are appended to the command line args. """ args = args[:] if RcFile._DISABLE_PANTS_RC_OPTION in args: return args config = Config.create_parser() read_from = config.read(self.paths) if not read_from: log.debug('no rcfile found') return args log.debug('using rcfiles: %s to modify args' % ','.join(read_from)) def get_rcopts(command, key): return config.get(command, key).split() if config.has_option( command, key) else [] commands = list(commands) if self.process_default: commands.insert(0, Config.DEFAULT_SECTION) for cmd in commands: opts = get_rcopts(cmd, 'options') args = (opts + args) if self.default_prepend else (args + opts) args = get_rcopts(cmd, 'prepend-options') + args + get_rcopts( cmd, 'append-options') return args
def apply_defaults(self, commands, args): """Augment arguments with defaults found for the given commands. The returned arguments will be a new copy of the given args with possibly extra augmented arguments. Default options are applied from the following keys under a section with the name of the sub-command the default options apply to: * `options` - These options are either prepended or appended to the command line args as specified in the constructor with default_prepend. * `prepend-options` - These options are prepended to the command line args. * `append-options` - These options are appended to the command line args. """ args = args[:] if RcFile._DISABLE_PANTS_RC_OPTION in args: return args config = Config.create_parser() read_from = config.read(self.paths) if not read_from: log.debug('no rcfile found') return args log.debug('using rcfiles: %s to modify args' % ','.join(read_from)) def get_rcopts(command, key): return config.get(command, key).split() if config.has_option(command, key) else [] commands = list(commands) if self.process_default: commands.insert(0, Config.DEFAULT_SECTION) for cmd in commands: opts = get_rcopts(cmd, 'options') args = (opts + args) if self.default_prepend else (args + opts) args = get_rcopts(cmd, 'prepend-options') + args + get_rcopts(cmd, 'append-options') return args
def check_config_file(path): cp = Config.create_parser() with open(path, 'r') as ini: cp.readfp(ini) config = SingleFileConfig(path, cp) print('Checking config file at {0} for unmigrated keys.'.format(path), file=sys.stderr) def section(s): return cyan('[{0}]'.format(s)) for (src_section, src_key), (dst_section, dst_key) in migrations.items(): def has_non_default_option(section, key): return cp.has_option(section, key) and cp.get(section, key) != cp.defaults().get(key, None) if config.has_section(src_section) and has_non_default_option(src_section, src_key): print('Found {src_key} in section {src_section}. Should be {dst_key} in section ' '{dst_section}.'.format(src_key=green(src_key), src_section=section(src_section), dst_key=green(dst_key), dst_section=section(dst_section)), file=sys.stderr) if (src_section, src_key) in notes: print(' Note: {0}'.format(notes[(src_section, src_key)])) # Check that all values are parseable. for sec in ['DEFAULT'] + cp.sections(): for key, value in cp.items(sec): value = value.strip() if value.startswith('['): try: custom_types.list_type(value) except ParseError: print('Value of {key} in section {section} is not a valid ' 'JSON list.'.format(key=green(key), section=section(sec))) elif value.startswith('{'): try: custom_types.dict_type(value) except ParseError: print('Value of {key} in section {section} is not a valid ' 'JSON object.'.format(key=green(key), section=section(sec)))
def create_empty_config(): """Creates an empty ``Config``.""" parser = Config.create_parser() with io.BytesIO(b"") as ini: parser.readfp(ini) return SingleFileConfig("dummy/path", parser)
def check_config_file(path): cp = Config.create_parser() with open(path, 'r') as ini: cp.readfp(ini) print('Checking config file at {0} for unmigrated keys.'.format(path), file=sys.stderr) def section(s): return cyan('[{0}]'.format(s)) for src, dst in migrations.items(): check_option(cp, src, dst) # Special-case handling of per-task subsystem options, so we can sweep them up in all # sections easily. def check_task_subsystem_options(subsystem_sec, options_map, sections=None): sections = sections or cp.sections() for src_sec in ['DEFAULT'] + sections: dst_sec = subsystem_sec if src_sec == 'DEFAULT' else '{}.{}'.format( subsystem_sec, src_sec) for src_key, dst_key in options_map.items(): check_option(cp, (src_sec, src_key), (dst_sec, dst_key)) artifact_cache_options_map = { 'read_from_artifact_cache': 'read', 'write_to_artifact_cache': 'write', 'overwrite_cache_artifacts': 'overwrite', 'read_artifact_caches': 'read_from', 'write_artifact_caches': 'write_to', 'cache_compression': 'compression_level', } check_task_subsystem_options('cache', artifact_cache_options_map) jvm_options_map = { 'jvm_options': 'options', 'args': 'program_args', 'debug': 'debug', 'debug_port': 'debug_port', 'debug_args': 'debug_args', } jvm_options_sections = [ 'repl.scala', 'test.junit', 'run.jvm', 'bench', 'doc.javadoc', 'doc.scaladoc' ] check_task_subsystem_options('jvm', jvm_options_map, sections=jvm_options_sections) # Check that all values are parseable. for sec in ['DEFAULT'] + cp.sections(): for key, value in cp.items(sec): value = value.strip() if value.startswith('['): try: custom_types.list_option(value) except ParseError: print('Value of {key} in section {section} is not a valid ' 'JSON list.'.format(key=green(key), section=section(sec))) elif value.startswith('{'): try: custom_types.dict_option(value) except ParseError: print('Value of {key} in section {section} is not a valid ' 'JSON object.'.format(key=green(key), section=section(sec)))
def check_config_file(path): cp = Config.create_parser() with open(path, "r") as ini: cp.readfp(ini) print("Checking config file at {0} for unmigrated keys.".format(path), file=sys.stderr) def section(s): return cyan("[{0}]".format(s)) for src, dst in migrations.items(): check_option(cp, src, dst) # Special-case handling of per-task subsystem options, so we can sweep them up in all # sections easily. def check_task_subsystem_options(subsystem_sec, options_map, sections=None): sections = sections or cp.sections() for src_sec in ["DEFAULT"] + sections: dst_sec = subsystem_sec if src_sec == "DEFAULT" else "{}.{}".format(subsystem_sec, src_sec) for src_key, dst_key in options_map.items(): check_option(cp, (src_sec, src_key), (dst_sec, dst_key)) artifact_cache_options_map = { "read_from_artifact_cache": "read", "write_to_artifact_cache": "write", "overwrite_cache_artifacts": "overwrite", "read_artifact_caches": "read_from", "write_artifact_caches": "write_to", "cache_compression": "compression_level", } check_task_subsystem_options("cache", artifact_cache_options_map) jvm_options_map = { "jvm_options": "options", "args": "program_args", "debug": "debug", "debug_port": "debug_port", "debug_args": "debug_args", } jvm_options_sections = ["repl.scala", "test.junit", "run.jvm", "bench", "doc.javadoc", "doc.scaladoc"] check_task_subsystem_options("jvm", jvm_options_map, sections=jvm_options_sections) # Check that all values are parseable. for sec in ["DEFAULT"] + cp.sections(): for key, value in cp.items(sec): value = value.strip() if value.startswith("["): try: custom_types.list_option(value) except ParseError: print( "Value of {key} in section {section} is not a valid " "JSON list.".format(key=green(key), section=section(sec)) ) elif value.startswith("{"): try: custom_types.dict_option(value) except ParseError: print( "Value of {key} in section {section} is not a valid " "JSON object.".format(key=green(key), section=section(sec)) )