# Path: django_docker_helpers/utils.py
# def run_env_once(f: t.Callable) -> t.Callable:
#     """
#     A decorator to prevent ``manage.py`` from running code twice for everything.
#     (https://stackoverflow.com/questions/16546652/why-does-django-run-everything-twice)
# 
#     :param f: function or method to decorate
#     :return: callable
#     """
# 
#     @wraps(f)
#     def wrapper(*args, **kwargs):
#         key = _build_cache_key_for_env(wrapper.__name__, *args, **kwargs)
#         has_run = os.environ.get(key)
#         if has_run == '1':
#             return
#         result = f(*args, **kwargs)
#         os.environ[key] = '1'
#         return result
# 
#     return wrapper
# 
# def wf(raw_str: str,
#        flush: bool = True,
#        prevent_completion_polluting: bool = True,
#        stream: t.TextIO = sys.stdout):
#     """
#     Writes a given ``raw_str`` into a ``stream``. Ignores output if ``prevent_completion_polluting`` is set and there's
#     no extra ``sys.argv`` arguments present (a bash completion issue).
# 
#     :param raw_str: a raw string to print
#     :param flush: execute ``flush()``
#     :param prevent_completion_polluting: don't write anything if ``len(sys.argv) <= 1``
#     :param stream: ``sys.stdout`` by default
#     :return: None
#     """
#     if prevent_completion_polluting and len(sys.argv) <= 1:
#         return
# 
#     stream.write(raw_str)
#     flush and hasattr(stream, 'flush') and stream.flush()


from time import sleep
from django.conf import settings
from django.core.cache import caches
from django.core.management import execute_from_command_line
from django.db import OperationalError, connections
from django_docker_helpers.utils import run_env_once, wf
        Password: {PASSWORD}
    =============================\n"""
    for connection_name in connections:
        _db_settings = dict.fromkeys(['ENGINE', 'HOST', 'NAME', 'USER', 'PASSWORD'])
        _db_settings.update(settings.DATABASES[connection_name])
        _db_settings['CONNECTION'] = connection_name
        if _db_settings.get('PASSWORD'):
            _db_settings['PASSWORD'] = 'set'

        wf(template.format(**_db_settings))
        wf('Checking db connection alive... ', False)

        for i in range(max_retries):
            try:
                cursor = connections[connection_name].cursor()
                cursor.execute('SELECT 1')
                cursor.fetchone()

                wf('[+]\n')
                break
            except OperationalError as e:
                wf(str(e))
                sleep(retry_timeout)
        else:
            wf('Tried %s time(s). Shutting down.\n' % max_retries)
            exit_on_failure and exit(1)
            return False
    return True



# Path: django_docker_helpers/utils.py
# def run_env_once(f: t.Callable) -> t.Callable:
#     """
#     A decorator to prevent ``manage.py`` from running code twice for everything.
#     (https://stackoverflow.com/questions/16546652/why-does-django-run-everything-twice)
# 
#     :param f: function or method to decorate
#     :return: callable
#     """
# 
#     @wraps(f)
#     def wrapper(*args, **kwargs):
#         key = _build_cache_key_for_env(wrapper.__name__, *args, **kwargs)
#         has_run = os.environ.get(key)
#         if has_run == '1':
#             return
#         result = f(*args, **kwargs)
#         os.environ[key] = '1'
#         return result
# 
#     return wrapper
# 
# def wf(raw_str: str,
#        flush: bool = True,
#        prevent_completion_polluting: bool = True,
#        stream: t.TextIO = sys.stdout):
#     """
#     Writes a given ``raw_str`` into a ``stream``. Ignores output if ``prevent_completion_polluting`` is set and there's
#     no extra ``sys.argv`` arguments present (a bash completion issue).
# 
#     :param raw_str: a raw string to print
#     :param flush: execute ``flush()``
#     :param prevent_completion_polluting: don't write anything if ``len(sys.argv) <= 1``
#     :param stream: ``sys.stdout`` by default
#     :return: None
#     """
#     if prevent_completion_polluting and len(sys.argv) <= 1:
#         return
# 
#     stream.write(raw_str)
#     flush and hasattr(stream, 'flush') and stream.flush()


# Path: django_docker_helpers/db.py
from time import sleep
from django.conf import settings
from django.core.cache import caches
from django.core.management import execute_from_command_line
from django.db import OperationalError, connections
from django_docker_helpers.utils import run_env_once, wf

        Password: {PASSWORD}
    =============================\n"""
    for connection_name in connections:
        _db_settings = dict.fromkeys(['ENGINE', 'HOST', 'NAME', 'USER', 'PASSWORD'])
        _db_settings.update(settings.DATABASES[connection_name])
        _db_settings['CONNECTION'] = connection_name
        if _db_settings.get('PASSWORD'):
            _db_settings['PASSWORD'] = 'set'

        wf(template.format(**_db_settings))
        wf('Checking db connection alive... ', False)

        for i in range(max_retries):
            try:
                cursor = connections[connection_name].cursor()
                cursor.execute('SELECT 1')
                cursor.fetchone()

                wf('[+]\n')
                break
            except OperationalError as e:
                wf(str(e))
                sleep(retry_timeout)
        else:
            wf('Tried %s time(s). Shutting down.\n' % max_retries)
            exit_on_failure and exit(1)
            return False
    return True



@run_env_once