# Path: memsql_loader/util/cli_utils.py
# class InvalidDatabaseException(Exception):
# class AWSBucketNameInvalid(Exception):
# class KeyFilterEmpty(Exception):
# class MissingTable(Exception):
# RE_VALIDATE_BUCKET_NAME = re.compile(r"^(?:[a-z0-9]+(?:[.\-]?[a-z0-9]+)*)+$")
# def prompt(message, default=None, validate=None, string_escape=False):
# def confirm(message, default=True):
#     def _check(value):
# 
# Path: memsql_loader/util/servers.py
# def get_pid_file_path():
# def delete_pid_file():
# def write_pid_file():
# def get_server_pid():
# def is_server_running():
# 
# Path: memsql_loader/util/command.py
# class Command(object):
#     def __init__(self, options):
#         self.options = options
#         self.ensure_bootstrapped()
#         self.run()
# 
#     @staticmethod
#     def configure(parser, subparsers):
#         raise NotImplemented('Every command needs a static configure(...) method')
# 
#     def ensure_bootstrapped(self):
#         if not bootstrap.check_bootstrapped():
#             bootstrap.bootstrap()
# 
#     def run():
#         raise NotImplemented('Every command needs a run method, otherwise not much is going to happen')
# 
# Path: memsql_loader/loader_db/storage.py
# class LoaderStorage(APSWStorage):
#     _instance = None
#     _initialized = False
#     _instance_lock = multiprocessing.RLock()
# 
#     # We use LoaderStorage as a singleton.
#     def __new__(cls, *args, **kwargs):
#         with cls._instance_lock:
#             if cls._instance is None:
#                 cls._instance = super(LoaderStorage, cls).__new__(
#                     cls, *args, **kwargs)
#                 cls._initialized = False
#             return cls._instance
# 
#     @classmethod
#     def drop_database(cls):
#         with cls._instance_lock:
#             if os.path.isfile(get_loader_db_path()):
#                 os.remove(get_loader_db_path())
#             if os.path.isfile(get_loader_db_path() + '-shm'):
#                 os.remove(get_loader_db_path() + '-shm')
#             if os.path.isfile(get_loader_db_path() + '-wal'):
#                 os.remove(get_loader_db_path() + '-wal')
#             cls._instance = None
# 
#     @classmethod
#     @contextlib.contextmanager
#     def fork_wrapper(cls):
#         # This context manager should be used around any code that forks new
#         # processes that will use a LoaderStorage object (e.g. Worker objects).
#         # This ensures that we don't share SQLite connections across forked
#         # processes.
#         with cls._instance_lock:
#             if cls._instance is not None:
#                 cls._instance.close_connections()
#                 # We garbage collect here to clean up any SQLite objects we
#                 # may have missed; this is important because any surviving
#                 # objects post-fork will mess up SQLite connections in the
#                 # child process.  We use generation=2 to collect as many
#                 # objects as possible.
#                 gc.collect(2)
#         yield
#         with cls._instance_lock:
#             if cls._instance is not None:
#                 cls._instance.setup_connections()
# 
#     def __init__(self):
#         with LoaderStorage._instance_lock:
#             # Since this is a singleton object, we don't want to call the
#             # parent object's __init__ if we've already instantiated this
#             # object in __new__.  However, we may have closed this object's
#             # connections in fork_wrapper above; in that case, we want to set
#             # up new database connections.
#             if not LoaderStorage._initialized:
#                 super(LoaderStorage, self).__init__(get_loader_db_path())
#                 LoaderStorage._initialized = True
#                 return
#             elif not self._db or not self._db_t:
#                 self.setup_connections()


import sys
from memsql_loader.util import cli_utils, servers
from memsql_loader.util.command import Command
from memsql_loader.loader_db.storage import LoaderStorage
""" Deletes the loader's database. """




class ClearLoaderDb(Command):
    @staticmethod
    def configure(parser, subparsers):
        subparser = subparsers.add_parser('clear-loader-db', help="Deletes the database containing MemSQL Loader's queued, running, and finished jobs.")
        subparser.set_defaults(command=ClearLoaderDb)

        subparser.add_argument('-f', '--force', help='Clear the loader database even if the MemSQL Loader server is running', action='store_true')

    def run(self):
        if not self.options.force:

# Path: memsql_loader/util/cli_utils.py
# class InvalidDatabaseException(Exception):
# class AWSBucketNameInvalid(Exception):
# class KeyFilterEmpty(Exception):
# class MissingTable(Exception):
# RE_VALIDATE_BUCKET_NAME = re.compile(r"^(?:[a-z0-9]+(?:[.\-]?[a-z0-9]+)*)+$")
# def prompt(message, default=None, validate=None, string_escape=False):
# def confirm(message, default=True):
#     def _check(value):
# 
# Path: memsql_loader/util/servers.py
# def get_pid_file_path():
# def delete_pid_file():
# def write_pid_file():
# def get_server_pid():
# def is_server_running():
# 
# Path: memsql_loader/util/command.py
# class Command(object):
#     def __init__(self, options):
#         self.options = options
#         self.ensure_bootstrapped()
#         self.run()
# 
#     @staticmethod
#     def configure(parser, subparsers):
#         raise NotImplemented('Every command needs a static configure(...) method')
# 
#     def ensure_bootstrapped(self):
#         if not bootstrap.check_bootstrapped():
#             bootstrap.bootstrap()
# 
#     def run():
#         raise NotImplemented('Every command needs a run method, otherwise not much is going to happen')
# 
# Path: memsql_loader/loader_db/storage.py
# class LoaderStorage(APSWStorage):
#     _instance = None
#     _initialized = False
#     _instance_lock = multiprocessing.RLock()
# 
#     # We use LoaderStorage as a singleton.
#     def __new__(cls, *args, **kwargs):
#         with cls._instance_lock:
#             if cls._instance is None:
#                 cls._instance = super(LoaderStorage, cls).__new__(
#                     cls, *args, **kwargs)
#                 cls._initialized = False
#             return cls._instance
# 
#     @classmethod
#     def drop_database(cls):
#         with cls._instance_lock:
#             if os.path.isfile(get_loader_db_path()):
#                 os.remove(get_loader_db_path())
#             if os.path.isfile(get_loader_db_path() + '-shm'):
#                 os.remove(get_loader_db_path() + '-shm')
#             if os.path.isfile(get_loader_db_path() + '-wal'):
#                 os.remove(get_loader_db_path() + '-wal')
#             cls._instance = None
# 
#     @classmethod
#     @contextlib.contextmanager
#     def fork_wrapper(cls):
#         # This context manager should be used around any code that forks new
#         # processes that will use a LoaderStorage object (e.g. Worker objects).
#         # This ensures that we don't share SQLite connections across forked
#         # processes.
#         with cls._instance_lock:
#             if cls._instance is not None:
#                 cls._instance.close_connections()
#                 # We garbage collect here to clean up any SQLite objects we
#                 # may have missed; this is important because any surviving
#                 # objects post-fork will mess up SQLite connections in the
#                 # child process.  We use generation=2 to collect as many
#                 # objects as possible.
#                 gc.collect(2)
#         yield
#         with cls._instance_lock:
#             if cls._instance is not None:
#                 cls._instance.setup_connections()
# 
#     def __init__(self):
#         with LoaderStorage._instance_lock:
#             # Since this is a singleton object, we don't want to call the
#             # parent object's __init__ if we've already instantiated this
#             # object in __new__.  However, we may have closed this object's
#             # connections in fork_wrapper above; in that case, we want to set
#             # up new database connections.
#             if not LoaderStorage._initialized:
#                 super(LoaderStorage, self).__init__(get_loader_db_path())
#                 LoaderStorage._initialized = True
#                 return
#             elif not self._db or not self._db_t:
#                 self.setup_connections()


# Path: memsql_loader/cli/clear_loader_db.py
import sys
from memsql_loader.util import cli_utils, servers
from memsql_loader.util.command import Command
from memsql_loader.loader_db.storage import LoaderStorage

""" Deletes the loader's database. """




class ClearLoaderDb(Command):
    @staticmethod
    def configure(parser, subparsers):
        subparser = subparsers.add_parser('clear-loader-db', help="Deletes the database containing MemSQL Loader's queued, running, and finished jobs.")
        subparser.set_defaults(command=ClearLoaderDb)

        subparser.add_argument('-f', '--force', help='Clear the loader database even if the MemSQL Loader server is running', action='store_true')

    def run(self):
        if not self.options.force:

            if servers.is_server_running():