diff options
author | Fumiaki MATSUSHIMA <mtsmfm@gmail.com> | 2017-12-01 02:40:13 +0900 |
---|---|---|
committer | Joffrey F <joffrey@docker.com> | 2018-01-30 17:16:23 -0800 |
commit | dd858648a0942177995a74e1eda3468a720a3c58 (patch) | |
tree | 2248a09ac1b36bb9bf16bbc7a823741ba5ffd8ee /docker/utils | |
parent | 2e8f1f798a3d6748735481ac519978a8b18a793c (diff) | |
download | docker-py-dd858648a0942177995a74e1eda3468a720a3c58.tar.gz |
Use config.json for detachKeys
Signed-off-by: Fumiaki Matsushima <mtsmfm@gmail.com>
Diffstat (limited to 'docker/utils')
-rw-r--r-- | docker/utils/config.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/docker/utils/config.py b/docker/utils/config.py new file mode 100644 index 0000000..8417261 --- /dev/null +++ b/docker/utils/config.py @@ -0,0 +1,65 @@ +import json +import logging +import os + +from ..constants import IS_WINDOWS_PLATFORM + +DOCKER_CONFIG_FILENAME = os.path.join('.docker', 'config.json') +LEGACY_DOCKER_CONFIG_FILENAME = '.dockercfg' + +log = logging.getLogger(__name__) + + +def find_config_file(config_path=None): + paths = list(filter(None, [ + config_path, # 1 + config_path_from_environment(), # 2 + os.path.join(home_dir(), DOCKER_CONFIG_FILENAME), # 3 + os.path.join(home_dir(), LEGACY_DOCKER_CONFIG_FILENAME), # 4 + ])) + + log.debug("Trying paths: {0}".format(repr(paths))) + + for path in paths: + if os.path.exists(path): + log.debug("Found file at path: {0}".format(path)) + return path + + log.debug("No config file found") + + return None + + +def config_path_from_environment(): + config_dir = os.environ.get('DOCKER_CONFIG') + if not config_dir: + return None + return os.path.join(config_dir, os.path.basename(DOCKER_CONFIG_FILENAME)) + + +def home_dir(): + """ + Get the user's home directory, using the same logic as the Docker Engine + client - use %USERPROFILE% on Windows, $HOME/getuid on POSIX. + """ + if IS_WINDOWS_PLATFORM: + return os.environ.get('USERPROFILE', '') + else: + return os.path.expanduser('~') + + +def load_general_config(config_path=None): + config_file = find_config_file(config_path) + + if not config_file: + return {} + + try: + with open(config_file) as f: + return json.load(f) + except Exception as e: + log.debug(e) + pass + + log.debug("All parsing attempts failed - returning empty config") + return {} |