# Path: src/myaas/utils/container.py
# def find_container(name):
# def list_containers(all=True):
# def translate_host_basedir(path):
# def get_random_cpuset(cores_to_assign):
# def get_mapped_cpuset():
# 
# Path: src/myaas/utils/database.py
# def get_enabled_backend():
#     return importlib.import_module(settings.BACKEND)
# 
# Path: src/myaas/utils/filesystem.py
# def is_empty(path):
#     return not getsize(path) > 100
# 
# Path: src/myaas/utils/retry.py
# class RetryPolicy(object):
#     def __init__(self, maxtries, delay=None, exceptions=(Exception,)):
#         if delay is None:
#             # 100ms +/- 50ms of randomized jitter
#             self.delay = lambda i: 0.1 + ((random.random() - 0.5) / 10)
#         else:
#             self.delay = lambda i: delay
# 
#         self.maxtries = maxtries
#         self.exceptions = exceptions
# 
#     def __call__(self, function):
#         for i in range(0, self.maxtries):
#             try:
#                 return function()
#             except self.exceptions as error:
#                 last_exception = error
#                 time.sleep(self.delay(i))
#         raise last_exception
# 
# Path: src/myaas/backends/exceptions.py
# class NonExistentTemplate(NonExistentDatabase):
#     pass
# 
# class ImportDataError(Exception):
#     pass


import os
import sys
import traceback
import functools
        import sys
from docker.errors import NotFound as ImageNotFound
from sentry_sdk import init, capture_message, configure_scope
from . import settings
from .utils.container import client
from .utils.database import get_enabled_backend
from .utils.filesystem import is_empty
from .utils.retry import RetryPolicy
from .backends.exceptions import NonExistentTemplate, ImportDataError




def list_dump_files():
    files_in_dir = os.listdir(settings.DUMP_DIR)
    return filter(lambda x: x.endswith('.sql'), files_in_dir)


def indent(string, level=1):
    spacing = "  " * level
    return spacing + string


def remove_recreate_database(template):
    """
    find existing database, remove it, then recreate
    """
    backend = get_enabled_backend().Template
    try:
        db = backend(client, template, False)
        if db.running():
            db.stop()
        db.do_backup()
        db.remove()
    except NonExistentTemplate:
        pass  # this means this database is being imported for the first time

    try:

# Path: src/myaas/utils/container.py
# def find_container(name):
# def list_containers(all=True):
# def translate_host_basedir(path):
# def get_random_cpuset(cores_to_assign):
# def get_mapped_cpuset():
# 
# Path: src/myaas/utils/database.py
# def get_enabled_backend():
#     return importlib.import_module(settings.BACKEND)
# 
# Path: src/myaas/utils/filesystem.py
# def is_empty(path):
#     return not getsize(path) > 100
# 
# Path: src/myaas/utils/retry.py
# class RetryPolicy(object):
#     def __init__(self, maxtries, delay=None, exceptions=(Exception,)):
#         if delay is None:
#             # 100ms +/- 50ms of randomized jitter
#             self.delay = lambda i: 0.1 + ((random.random() - 0.5) / 10)
#         else:
#             self.delay = lambda i: delay
# 
#         self.maxtries = maxtries
#         self.exceptions = exceptions
# 
#     def __call__(self, function):
#         for i in range(0, self.maxtries):
#             try:
#                 return function()
#             except self.exceptions as error:
#                 last_exception = error
#                 time.sleep(self.delay(i))
#         raise last_exception
# 
# Path: src/myaas/backends/exceptions.py
# class NonExistentTemplate(NonExistentDatabase):
#     pass
# 
# class ImportDataError(Exception):
#     pass


# Path: src/myaas/update.py
import os
import sys
import traceback
import functools
        import sys
from docker.errors import NotFound as ImageNotFound
from sentry_sdk import init, capture_message, configure_scope
from . import settings
from .utils.container import client
from .utils.database import get_enabled_backend
from .utils.filesystem import is_empty
from .utils.retry import RetryPolicy
from .backends.exceptions import NonExistentTemplate, ImportDataError





def list_dump_files():
    files_in_dir = os.listdir(settings.DUMP_DIR)
    return filter(lambda x: x.endswith('.sql'), files_in_dir)


def indent(string, level=1):
    spacing = "  " * level
    return spacing + string


def remove_recreate_database(template):
    """
    find existing database, remove it, then recreate
    """
    backend = get_enabled_backend().Template
    try:
        db = backend(client, template, False)
        if db.running():
            db.stop()
        db.do_backup()
        db.remove()
    except NonExistentTemplate:
        pass  # this means this database is being imported for the first time

    try:

        database = backend(client, template, True)