pull/13668/merge
Jörg Behrmann 2024-01-07 06:28:19 +02:00 committed by GitHub
commit 3de3e7e737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 34 deletions

View File

@ -442,6 +442,13 @@ class RootConfig:
if hasattr(config, func_name): if hasattr(config, func_name):
getattr(config, func_name)(*args, **kwargs) getattr(config, func_name)(*args, **kwargs)
@staticmethod
def paths_from_environment(envvar: str) -> List[str]:
envval = os.environ.get(envvar)
if not envval:
return []
return envval.split(":")
def generate_config( def generate_config(
self, self,
config_dir_path: str, config_dir_path: str,
@ -630,7 +637,9 @@ class RootConfig:
action="append", action="append",
metavar="CONFIG_FILE", metavar="CONFIG_FILE",
help="Specify config file. Can be given multiple times and" help="Specify config file. Can be given multiple times and"
" may specify directories containing *.yaml files.", " may specify directories containing *.yaml files."
" Also uses the value of the CONFIGURATION_DIRECTORY environment"
" variable, where multiple values can be delimited by colons.",
) )
# we nest the mutually-exclusive group inside another group so that the help # we nest the mutually-exclusive group inside another group so that the help
@ -695,7 +704,8 @@ class RootConfig:
metavar="DIRECTORY", metavar="DIRECTORY",
help=( help=(
"Specify where data such as the media store and database file should be" "Specify where data such as the media store and database file should be"
" stored. Defaults to the current working directory." " stored. Defaults to the first entry of the STATE_DIRECTORY environment"
" variable or the current working directory in that order."
), ),
) )
generate_group.add_argument( generate_group.add_argument(
@ -710,7 +720,10 @@ class RootConfig:
cls.invoke_all_static("add_arguments", parser) cls.invoke_all_static("add_arguments", parser)
config_args = parser.parse_args(argv) config_args = parser.parse_args(argv)
config_files = find_config_files(search_paths=config_args.config_path) config_paths_env = cls.paths_from_environment("CONFIGURATION_DIRECTORY")
config_files = find_config_files(
search_paths=config_paths_env + config_args.config_path
)
if not config_files: if not config_files:
parser.error( parser.error(
@ -724,7 +737,8 @@ class RootConfig:
else: else:
config_dir_path = os.path.dirname(config_files[-1]) config_dir_path = os.path.dirname(config_files[-1])
config_dir_path = os.path.abspath(config_dir_path) config_dir_path = os.path.abspath(config_dir_path)
data_dir_path = os.getcwd() data_dir_path_env = cls.paths_from_environment("STATE_DIRECTORY")
data_dir_path = data_dir_path_env[0] if data_dir_path_env else os.getcwd()
obj = cls(config_files) obj = cls(config_files)
@ -742,10 +756,7 @@ class RootConfig:
if not path_exists(config_path): if not path_exists(config_path):
print("Generating config file %s" % (config_path,)) print("Generating config file %s" % (config_path,))
if config_args.data_directory: data_dir_path = config_args.data_directory or data_dir_path
data_dir_path = config_args.data_directory
else:
data_dir_path = os.getcwd()
data_dir_path = os.path.abspath(data_dir_path) data_dir_path = os.path.abspath(data_dir_path)
server_name = config_args.server_name server_name = config_args.server_name
@ -905,35 +916,34 @@ def find_config_files(search_paths: List[str]) -> List[str]:
A list of file paths. A list of file paths.
""" """
config_files = [] config_files = {config_path: [] for config_path in search_paths}
if search_paths: for config_path, files in config_files.items():
for config_path in search_paths: if os.path.isdir(config_path):
if os.path.isdir(config_path): # We accept specifying directories as config paths, we search
# We accept specifying directories as config paths, we search # inside that directory for all files matching *.yaml, and then
# inside that directory for all files matching *.yaml, and then # we apply them in *sorted* order.
# we apply them in *sorted* order. found_files = []
files = [] for entry in os.listdir(config_path):
for entry in os.listdir(config_path): entry_path = os.path.join(config_path, entry)
entry_path = os.path.join(config_path, entry) if not os.path.isfile(entry_path):
if not os.path.isfile(entry_path): err = "Found subdirectory in config directory: %r. IGNORING."
err = "Found subdirectory in config directory: %r. IGNORING." print(err % (entry_path,))
print(err % (entry_path,)) continue
continue
if not entry.endswith(".yaml"): if not entry.endswith(".yaml"):
err = ( err = (
"Found file in config directory that does not end in " "Found file in config directory that does not end in "
"'.yaml': %r. IGNORING." "'.yaml': %r. IGNORING."
) )
print(err % (entry_path,)) print(err % (entry_path,))
continue continue
files.append(entry_path) found_files.append(entry_path)
config_files.extend(sorted(files)) files.extend(sorted(found_files))
else: else:
config_files.append(config_path) files.append(config_path)
return config_files return list(itertools.chain(*config_files.values()))
@attr.s(auto_attribs=True) @attr.s(auto_attribs=True)