From bfb66773a438da2f8fb7192b7383f6efe65d87ec Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 25 Aug 2015 16:25:54 +0100 Subject: Allow specifying directories as config files --- synapse/config/_base.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'synapse/config') diff --git a/synapse/config/_base.py b/synapse/config/_base.py index 73f6959959..fd5080a3cb 100644 --- a/synapse/config/_base.py +++ b/synapse/config/_base.py @@ -131,7 +131,8 @@ class Config(object): "-c", "--config-path", action="append", metavar="CONFIG_FILE", - help="Specify config file" + help="Specify config file. Can be given multiple times and" + " may specify directories containing *.yaml files." ) config_parser.add_argument( "--generate-config", @@ -151,14 +152,31 @@ class Config(object): generate_keys = config_args.generate_keys + config_files = [] + if config_args.config_path: + for config_path in config_args.config_path: + if os.path.isdir(config_path): + # We accept specifying directories as config paths, we search + # inside that directory for all files matching *.yaml, and then + # we apply them in *sorted* order. + config_files.extend(sorted( + os.path.join(config_path, entry) + for entry in os.listdir(config_path) + if entry.endswith(".yaml") and os.path.isfile( + os.path.join(config_path, entry) + ) + )) + else: + config_files.append(config_path) + if config_args.generate_config: - if not config_args.config_path: + if not config_files: config_parser.error( "Must supply a config file.\nA config file can be automatically" " generated using \"--generate-config -H SERVER_NAME" " -c CONFIG-FILE\"" ) - (config_path,) = config_args.config_path + (config_path,) = config_files if not os.path.exists(config_path): config_dir_path = os.path.dirname(config_path) config_dir_path = os.path.abspath(config_dir_path) @@ -202,7 +220,7 @@ class Config(object): obj.invoke_all("add_arguments", parser) args = parser.parse_args(remaining_args) - if not config_args.config_path: + if not config_files: config_parser.error( "Must supply a config file.\nA config file can be automatically" " generated using \"--generate-config -H SERVER_NAME" @@ -213,8 +231,8 @@ class Config(object): config_dir_path = os.path.abspath(config_dir_path) specified_config = {} - for config_path in config_args.config_path: - yaml_config = cls.read_config_file(config_path) + for config_file in config_files: + yaml_config = cls.read_config_file(config_file) specified_config.update(yaml_config) server_name = specified_config["server_name"] -- cgit 1.4.1 From 3e1029fe8050edd8f6aed57bdf6507a12ffb0a0c Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 25 Aug 2015 17:08:23 +0100 Subject: Warn if we encounter unexpected files in config directories --- synapse/config/_base.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'synapse/config') diff --git a/synapse/config/_base.py b/synapse/config/_base.py index fd5080a3cb..2f218d4a7b 100644 --- a/synapse/config/_base.py +++ b/synapse/config/_base.py @@ -159,13 +159,23 @@ class Config(object): # We accept specifying directories as config paths, we search # inside that directory for all files matching *.yaml, and then # we apply them in *sorted* order. - config_files.extend(sorted( - os.path.join(config_path, entry) - for entry in os.listdir(config_path) - if entry.endswith(".yaml") and os.path.isfile( - os.path.join(config_path, entry) - ) - )) + files = [] + for entry in os.listdir(config_path): + entry_path = os.path.join(config_path, entry) + if not os.path.isfile(entry_path): + print ( + "Found subdirectory in config directory: %r. IGNORING." + ) % (entry_path, ) + continue + + if not entry.endswith(".yaml"): + print ( + "Found file in config directory that does not" + " end in '.yaml': %r. IGNORING." + ) % (entry_path, ) + continue + + config_files.extend(sorted(files)) else: config_files.append(config_path) -- cgit 1.4.1