From e641703adb337d045213b06a832fca17b8c9c2b2 Mon Sep 17 00:00:00 2001 From: EsGeh Date: Fri, 29 Nov 2019 15:21:25 +0100 Subject: [PATCH] init script won' overwrite existing `.env` file --- README.md | 2 +- scripts/utils/settings.py | 43 ++++++++++++++++++++++----------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 5f99257..9501ebf 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Dependencies to other git repositories are handled using [git_deps_py](https://g - Environment variables: see file `.env` These variables are available inside the `docker-compose.yaml` file and are also loaded into the python scripts. - The file is created/overwritten with default settings by the `init.py` script. + (The `.env` file is initially created with default settings by the `init.py` script) - Django settings: see `src/eoa/settings.py` diff --git a/scripts/utils/settings.py b/scripts/utils/settings.py index c3e0f63..3e58f19 100644 --- a/scripts/utils/settings.py +++ b/scripts/utils/settings.py @@ -31,26 +31,31 @@ def load_config( config = { "PATH": path, ** config } return config -def create_docker_env_file(): - import subprocess - user = subprocess.check_output( - shlex.split("id -u"), - universal_newlines=True - ).rstrip("\n") - group = subprocess.check_output( - shlex.split("id -g"), - universal_newlines=True - ).rstrip("\n") +def create_docker_env_file( + overwrite = False +): + if overwrite or not(env_file.exists()): + import subprocess + user = subprocess.check_output( + shlex.split("id -u"), + universal_newlines=True + ).rstrip("\n") + group = subprocess.check_output( + shlex.split("id -g"), + universal_newlines=True + ).rstrip("\n") - print( "user id: {}, group id: {}".format( user, group) ) - print( "writing docker .env file to {}, appended user and group".format( env_file ) ) - config = __load_orig_config( orig_config_file ) - config['config']['USER'] = user - config['config']['GROUP'] = group - __write_as_env( - config, - env_file - ) + print( "user id: {}, group id: {}".format( user, group) ) + print( "writing docker .env file to {}, appended user and group".format( env_file ) ) + config = __load_orig_config( orig_config_file ) + config['config']['USER'] = user + config['config']['GROUP'] = group + __write_as_env( + config, + env_file + ) + else: + print( f"INFO: '{env_file}' already exists" ) def __load_orig_config( filename ): config = ConfigParser(