# 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/errors.py
# class MongoctlException(Exception):
#     def __init__(self, message, cause=None):
#         super(MongoctlException, self).__init__(message)
#         self._cause = cause
# 
# Path: mongoctl/utils.py
# def is_exe(fpath):
#     return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
# 
# def which(program):
# 
#     fpath, fname = os.path.split(program)
#     if fpath:
#         if is_exe(program):
#             return program
#     else:
#         for path in os.environ["PATH"].split(os.pathsep):
#             exe_file = os.path.join(path, program)
#             if is_exe(exe_file):
#                 return exe_file
#     return None
# 
# 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 execute_command(command, **kwargs):
# 
#     # Python 2.7+ : Use the new method because i think its better
#     if  hasattr(subprocess, 'check_output'):
#         return subprocess.check_output(command,stderr=subprocess.STDOUT, **kwargs)
#     else: # Python 2.6 compatible, check_output is not available in 2.6
#         return subprocess.Popen(command,
#                                 stdout=subprocess.PIPE,
#                                 stderr=subprocess.STDOUT,
#                                 **kwargs).communicate()[0]
# 
# Path: mongoctl/mongodb_version.py
# 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
# 
# class MongoDBEdition():
#     COMMUNITY = "community"
#     COMMUNITY_SSL = "community_ssl"
#     ENTERPRISE = "enterprise"
# 
#     ALL = [COMMUNITY, COMMUNITY_SSL, ENTERPRISE]


import os
import re
import mongoctl.repository as repository
from mongoctl.mongoctl_logging import *
from mongoctl import config
from mongoctl.errors import MongoctlException
from mongoctl.utils import is_exe, which, resolve_path, execute_command
from mongoctl.mongodb_version import make_version_info, MongoDBEdition
from mongoctl.mongo_uri_tools import is_mongo_uri
    for mongo_exe,exe_version in exe_ver_tuples:
        strs.append("%s = %s" % (mongo_exe, exe_version))
    return "\n".join(strs)

###############################################################################
def is_valid_mongo_exe(path):
    return path is not None and is_exe(path)


###############################################################################
def get_mongo_home_exe(mongo_home, executable_name):
    return os.path.join(mongo_home, 'bin', executable_name)

###############################################################################
def mongo_exe_version(mongo_exe):
    mongod_path = os.path.join(os.path.dirname(mongo_exe), "mongod")

    try:
        re_expr = "v?((([0-9]+)\.([0-9]+)\.([0-9]+))([^, ]*))"
        vers_spew = execute_command([mongod_path, "--version"])
        # only take first line of spew
        vers_spew_line = vers_spew.split('\n')[0]
        vers_grep = re.findall(re_expr, vers_spew_line)
        help_spew = execute_command([mongod_path, "--help"])
        full_version = vers_grep[-1][0]
        if "subscription" in vers_spew or "enterprise" in vers_spew:
            edition = MongoDBEdition.ENTERPRISE
        elif "SSL" in help_spew:
            edition = MongoDBEdition.COMMUNITY_SSL
        else:

# 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/errors.py
# class MongoctlException(Exception):
#     def __init__(self, message, cause=None):
#         super(MongoctlException, self).__init__(message)
#         self._cause = cause
# 
# Path: mongoctl/utils.py
# def is_exe(fpath):
#     return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
# 
# def which(program):
# 
#     fpath, fname = os.path.split(program)
#     if fpath:
#         if is_exe(program):
#             return program
#     else:
#         for path in os.environ["PATH"].split(os.pathsep):
#             exe_file = os.path.join(path, program)
#             if is_exe(exe_file):
#                 return exe_file
#     return None
# 
# 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 execute_command(command, **kwargs):
# 
#     # Python 2.7+ : Use the new method because i think its better
#     if  hasattr(subprocess, 'check_output'):
#         return subprocess.check_output(command,stderr=subprocess.STDOUT, **kwargs)
#     else: # Python 2.6 compatible, check_output is not available in 2.6
#         return subprocess.Popen(command,
#                                 stdout=subprocess.PIPE,
#                                 stderr=subprocess.STDOUT,
#                                 **kwargs).communicate()[0]
# 
# Path: mongoctl/mongodb_version.py
# 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
# 
# class MongoDBEdition():
#     COMMUNITY = "community"
#     COMMUNITY_SSL = "community_ssl"
#     ENTERPRISE = "enterprise"
# 
#     ALL = [COMMUNITY, COMMUNITY_SSL, ENTERPRISE]


# Path: mongoctl/commands/command_utils.py
import os
import re
import mongoctl.repository as repository
from mongoctl.mongoctl_logging import *
from mongoctl import config
from mongoctl.errors import MongoctlException
from mongoctl.utils import is_exe, which, resolve_path, execute_command
from mongoctl.mongodb_version import make_version_info, MongoDBEdition
from mongoctl.mongo_uri_tools import is_mongo_uri

    for mongo_exe,exe_version in exe_ver_tuples:
        strs.append("%s = %s" % (mongo_exe, exe_version))
    return "\n".join(strs)

###############################################################################
def is_valid_mongo_exe(path):
    return path is not None and is_exe(path)


###############################################################################
def get_mongo_home_exe(mongo_home, executable_name):
    return os.path.join(mongo_home, 'bin', executable_name)

###############################################################################
def mongo_exe_version(mongo_exe):
    mongod_path = os.path.join(os.path.dirname(mongo_exe), "mongod")

    try:
        re_expr = "v?((([0-9]+)\.([0-9]+)\.([0-9]+))([^, ]*))"
        vers_spew = execute_command([mongod_path, "--version"])
        # only take first line of spew
        vers_spew_line = vers_spew.split('\n')[0]
        vers_grep = re.findall(re_expr, vers_spew_line)
        help_spew = execute_command([mongod_path, "--help"])
        full_version = vers_grep[-1][0]
        if "subscription" in vers_spew or "enterprise" in vers_spew:
            edition = MongoDBEdition.ENTERPRISE
        elif "SSL" in help_spew:
            edition = MongoDBEdition.COMMUNITY_SSL
        else:

            edition = MongoDBEdition.COMMUNITY