Skip to content

Commit

Permalink
Added debugging of local config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
markgw committed Mar 19, 2020
1 parent bb86ad2 commit 1f96532
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/python/pimlico/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ def run_command(self, pipeline, opts):
parser = argparse.ArgumentParser(description="Main command line interface to PiMLiCo")
parser.add_argument("pipeline_config", help="Config file to load a pipeline from")
parser.add_argument("--debug", "-d", help="Output verbose debugging info", action="store_true")
parser.add_argument("--trace-config",
help="Trace the process of resolving which local config file(s) to use. Useful for debugging "
"the resolution on a given server or with a given set of config files",
action="store_true")
parser.add_argument("--non-interactive",
help="Don't output things like progress bars that rely on being in a terminal or similar. "
"Equivalent to setting environment variable PIM_NON_INT=1",
Expand Down Expand Up @@ -192,6 +196,21 @@ def run_command(self, pipeline, opts):
)
else:
override_local = {}

if opts.trace_config:
# Trace the loading of local config files
if opts.override_local_config is not None:
print("Using some local config overrides from command line: {}".format(
", ".join(opts.override_local_config)))
if opts.local_config:
print("Using local config source given on command line: {}".format(opts.local_config))

# Run the usual LC loading routine that the pipeline config loader uses
for l, line in enumerate(
PipelineConfig.trace_load_local_config(filename=opts.local_config, override=override_local)):
print("{}: {}".format(l, line))
sys.exit(0)

if opts.processes is not None:
# Override the processes local config setting
override_local["processes"] = opts.processes
Expand Down
61 changes: 61 additions & 0 deletions src/python/pimlico/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,67 @@ def _load_config_file(fn):

return local_config_data, used_config_sources

@staticmethod
def trace_load_local_config(filename=None, override={}, only_override=False):
"""
Trace the process of loading local config file(s). Follows exactly the same logic
as load_local_config(), but documents what it finds/doesn't find.
"""
# Keep a record of where we got config from, for debugging purposes
config_sources = []

if only_override:
config_sources.append("Using only the overriden LC settings")
else:
if filename is None:
home_dir = os.path.expanduser("~")
# Use the default locations for local config file
# First, look for the basic config
# If other config files are found below, we don't complain about this not existing,
# but usually it must exist: we will complain below if nothing else is found
basic_config_filename = os.path.join(home_dir, ".pimlico")
if os.path.exists(basic_config_filename):
config_sources.append("Basic config in home dir: {}".format(basic_config_filename))

# Allow other files to override the settings in this basic one
# Look for any files matching the pattern .pimlico_*

# You may specify config files for specific hosts
hostname = gethostname()
if hostname is not None and len(hostname) > 0:
alt_config_files = [f for f in os.listdir(home_dir) if f.startswith(".pimlico_")]
found_already = False
for alt_config_filename in alt_config_files:
suffix = alt_config_filename[9:]
if hostname == suffix:
# Our hostname matches a hostname-specific config file .pimlico_<hostname>
if found_already:
config_sources.append("Host-specific config file {} matches, but won't be used, as "
"another match has already been found".format(alt_config_filename))
else:
config_sources.append("Hostname ({}) matches host-specific config file: {}".format(
hostname, alt_config_filename
))
# Only allow one host-specific config file
found_already = True
elif suffix.endswith("-") and hostname.startswith(suffix[:-1]):
# Hostname match hostname-prefix-specific config file .pimlico_<hostname_prefix>-
if found_already:
config_sources.append("Host-specific config file {} matches, but won't be used, as "
"another match has already been found".format(alt_config_filename))
else:
config_sources.append("Hostname ({}) matches host-prefix-specific config file: {}".format(
hostname, alt_config_filename
))
found_already = True
else:
config_sources.append("Config file {} does not match the hostname ({})".format(
alt_config_filename, hostname))
else:
config_sources.append("Using giving local config filename: {}".format(filename))
return config_sources

@staticmethod
def empty(local_config=None, override_local_config={}, override_pipeline_config={}, only_override_config=False):
"""
Expand Down

0 comments on commit 1f96532

Please sign in to comment.