# Path: mongoctl/utils.py
# def resolve_path(path):
#     # handle file uris
#     path = path.replace("file://", "")
# 
#     # expand vars
#     path =  os.path.expandvars(custom_expanduser(path))
#     # Turn relative paths to absolute
#     try:
#         path = os.path.abspath(path)
#     except OSError, e:
#         # handle the case where cwd does not exist
#         if "No such file or directory" in str(e):
#             pass
#         else:
#             raise
# 
#     return path
# 
# def document_pretty_string(document):
#     return json.dumps(document, indent=4, default=json_util.default)
# 
# def is_host_local(host):
#     if (host == "localhost" or
#                 host == "127.0.0.1"):
#         return True
# 
#     return is_same_host(socket.gethostname(), host)
# 
# def timedelta_total_seconds(td):
#     """
#     Equivalent python 2.7+ timedelta.total_seconds()
#      This was added for python 2.6 compatibilty
#     """
#     return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6) / 1e6
# 
# def is_pid_alive(pid):
# 
#     try:
#         os.kill(pid,0)
#         return True
#     except OSError:
#         return False
# 
# Path: mongoctl/mongoctl_logging.py
# def log_verbose(msg):
#     get_logger().log(VERBOSE, msg)
# 
# def log_error(msg):
#     get_logger().error(msg)
# 
# def log_warning(msg):
#     get_logger().warning(msg)
# 
# def log_exception(exception):
#     log_debug("EXCEPTION: %s" % exception)
#     log_debug(traceback.format_exc())
# 
# def log_debug(msg):
#     get_logger().debug(msg)
# 
# Path: mongoctl/config.py
# def get_default_users():
#     return get_mongoctl_config_val('defaultUsers', {})
# 
# Path: mongoctl/errors.py
# class MongoctlException(Exception):
#     def __init__(self, message, cause=None):
#         super(MongoctlException, self).__init__(message)
#         self._cause = cause
# 
# def is_auth_error(e):
#     return isinstance(e, OperationFailure) and e.code == 13
# 
# Path: mongoctl/config.py
# MONGOCTL_CONF_FILE_NAME = "mongoctl.config"
# def set_config_root(root_path):
# def get_mongoctl_config_val(key, default=None):
# def set_mongoctl_config_val(key, value):
# def get_generate_key_file_conf(default=None):
# def get_database_repository_conf():
# def get_file_repository_conf():
# def get_mongodb_installs_dir():
# def set_mongodb_installs_dir(installs_dir):
# def get_default_users():
# def get_cluster_member_alt_address_mapping():
# def to_full_config_path(path_or_url):
# def get_mongoctl_config():
# def read_config_json(name, path_or_url):
# def read_json_string(path_or_url, validate_exists=True):
# 
# Path: mongoctl/users.py
# def parse_global_login_user_arg(username, password, server_id):
# def get_global_login_user(server, dbname):
# def setup_server_users(server):
# def setup_cluster_users(cluster, primary_server):
# def should_seed_db_users(server, dbname):
# def setup_db_users(server, db, db_users):
# def _mongo_add_user(server, db, username, password, read_only=False,
#                     num_tries=1):
# def list_database_users(server, db):
# def setup_server_db_users(server, dbname, db_users):
# def prepend_global_admin_user(other_users, server):
# def setup_server_admin_users(server):
# def setup_root_admin_user(server, admin_users):
# def setup_server_local_users(server):
# def read_seed_password(dbname, username):
# def set_server_login_user(server, dbname, username, password):
# def get_server_login_user(server, dbname):
# def _get_server_login_record(server, create_new=True):
# def is_system_user(username):
# LOGIN_USERS = {}
# 
# Path: mongoctl/mongodb_version.py
# class MongoDBEdition():
#     COMMUNITY = "community"
#     COMMUNITY_SSL = "community_ssl"
#     ENTERPRISE = "enterprise"
# 
#     ALL = [COMMUNITY, COMMUNITY_SSL, ENTERPRISE]
# 
# def make_version_info(version_number, edition=None):
#     if version_number is None:
#         return None
# 
#     version_number = version_number.strip()
#     version_number = version_number.replace("-pre-" , "-pre")
#     version_info = MongoDBVersionInfo(version_number, edition=edition)
# 
#     # validate version string
#     if not is_valid_version_info(version_info):
#         raise MongoctlException("Invalid version '%s." % version_info)
#     else:
#         return version_info
# 
# Path: mongoctl/mongo_utils.py
# CONN_TIMEOUT_MS = 10000
# def mongo_client(*args, **kwargs):


import os
import mongoctl.repository as repository
import pymongo
import datetime
import ssl
from base import DocumentWrapper
from mongoctl.utils import resolve_path, document_pretty_string, is_host_local, timedelta_total_seconds, is_pid_alive
from pymongo.errors import AutoReconnect, OperationFailure, ConnectionFailure
from mongoctl.mongoctl_logging import (
    log_verbose, log_error, log_warning, log_exception, log_debug
    )
from mongoctl.config import get_default_users
from mongoctl.errors import MongoctlException, is_auth_error
from mongoctl.prompt import read_username, read_password
from bson.son import SON
from mongoctl import config
from mongoctl import users
from mongoctl.mongodb_version import MongoDBEdition, make_version_info
from mongoctl import mongo_utils
__author__ = 'abdul'













###############################################################################
# CONSTANTS
###############################################################################

# default pid file name
PID_FILE_NAME = "pid.txt"

LOG_FILE_NAME = "mongodb.log"


# Path: mongoctl/utils.py
# def resolve_path(path):
#     # handle file uris
#     path = path.replace("file://", "")
# 
#     # expand vars
#     path =  os.path.expandvars(custom_expanduser(path))
#     # Turn relative paths to absolute
#     try:
#         path = os.path.abspath(path)
#     except OSError, e:
#         # handle the case where cwd does not exist
#         if "No such file or directory" in str(e):
#             pass
#         else:
#             raise
# 
#     return path
# 
# def document_pretty_string(document):
#     return json.dumps(document, indent=4, default=json_util.default)
# 
# def is_host_local(host):
#     if (host == "localhost" or
#                 host == "127.0.0.1"):
#         return True
# 
#     return is_same_host(socket.gethostname(), host)
# 
# def timedelta_total_seconds(td):
#     """
#     Equivalent python 2.7+ timedelta.total_seconds()
#      This was added for python 2.6 compatibilty
#     """
#     return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6) / 1e6
# 
# def is_pid_alive(pid):
# 
#     try:
#         os.kill(pid,0)
#         return True
#     except OSError:
#         return False
# 
# Path: mongoctl/mongoctl_logging.py
# def log_verbose(msg):
#     get_logger().log(VERBOSE, msg)
# 
# def log_error(msg):
#     get_logger().error(msg)
# 
# def log_warning(msg):
#     get_logger().warning(msg)
# 
# def log_exception(exception):
#     log_debug("EXCEPTION: %s" % exception)
#     log_debug(traceback.format_exc())
# 
# def log_debug(msg):
#     get_logger().debug(msg)
# 
# Path: mongoctl/config.py
# def get_default_users():
#     return get_mongoctl_config_val('defaultUsers', {})
# 
# Path: mongoctl/errors.py
# class MongoctlException(Exception):
#     def __init__(self, message, cause=None):
#         super(MongoctlException, self).__init__(message)
#         self._cause = cause
# 
# def is_auth_error(e):
#     return isinstance(e, OperationFailure) and e.code == 13
# 
# Path: mongoctl/config.py
# MONGOCTL_CONF_FILE_NAME = "mongoctl.config"
# def set_config_root(root_path):
# def get_mongoctl_config_val(key, default=None):
# def set_mongoctl_config_val(key, value):
# def get_generate_key_file_conf(default=None):
# def get_database_repository_conf():
# def get_file_repository_conf():
# def get_mongodb_installs_dir():
# def set_mongodb_installs_dir(installs_dir):
# def get_default_users():
# def get_cluster_member_alt_address_mapping():
# def to_full_config_path(path_or_url):
# def get_mongoctl_config():
# def read_config_json(name, path_or_url):
# def read_json_string(path_or_url, validate_exists=True):
# 
# Path: mongoctl/users.py
# def parse_global_login_user_arg(username, password, server_id):
# def get_global_login_user(server, dbname):
# def setup_server_users(server):
# def setup_cluster_users(cluster, primary_server):
# def should_seed_db_users(server, dbname):
# def setup_db_users(server, db, db_users):
# def _mongo_add_user(server, db, username, password, read_only=False,
#                     num_tries=1):
# def list_database_users(server, db):
# def setup_server_db_users(server, dbname, db_users):
# def prepend_global_admin_user(other_users, server):
# def setup_server_admin_users(server):
# def setup_root_admin_user(server, admin_users):
# def setup_server_local_users(server):
# def read_seed_password(dbname, username):
# def set_server_login_user(server, dbname, username, password):
# def get_server_login_user(server, dbname):
# def _get_server_login_record(server, create_new=True):
# def is_system_user(username):
# LOGIN_USERS = {}
# 
# Path: mongoctl/mongodb_version.py
# class MongoDBEdition():
#     COMMUNITY = "community"
#     COMMUNITY_SSL = "community_ssl"
#     ENTERPRISE = "enterprise"
# 
#     ALL = [COMMUNITY, COMMUNITY_SSL, ENTERPRISE]
# 
# def make_version_info(version_number, edition=None):
#     if version_number is None:
#         return None
# 
#     version_number = version_number.strip()
#     version_number = version_number.replace("-pre-" , "-pre")
#     version_info = MongoDBVersionInfo(version_number, edition=edition)
# 
#     # validate version string
#     if not is_valid_version_info(version_info):
#         raise MongoctlException("Invalid version '%s." % version_info)
#     else:
#         return version_info
# 
# Path: mongoctl/mongo_utils.py
# CONN_TIMEOUT_MS = 10000
# def mongo_client(*args, **kwargs):


# Path: mongoctl/objects/server.py
import os
import mongoctl.repository as repository
import pymongo
import datetime
import ssl
from base import DocumentWrapper
from mongoctl.utils import resolve_path, document_pretty_string, is_host_local, timedelta_total_seconds, is_pid_alive
from pymongo.errors import AutoReconnect, OperationFailure, ConnectionFailure
from mongoctl.mongoctl_logging import (
    log_verbose, log_error, log_warning, log_exception, log_debug
    )
from mongoctl.config import get_default_users
from mongoctl.errors import MongoctlException, is_auth_error
from mongoctl.prompt import read_username, read_password
from bson.son import SON
from mongoctl import config
from mongoctl import users
from mongoctl.mongodb_version import MongoDBEdition, make_version_info
from mongoctl import mongo_utils

__author__ = 'abdul'













###############################################################################
# CONSTANTS
###############################################################################

# default pid file name
PID_FILE_NAME = "pid.txt"

LOG_FILE_NAME = "mongodb.log"


KEY_FILE_NAME = "keyFile"