# Path: wtframework/wtf/constants.py
# WTF_CONFIG_LOCATION = 'configs/'
# 
# WTF_CONFIG_EXT = '.yaml'
# 
# WTF_ENV_VARS = "WTF_ENV"
# 
# Path: wtframework/wtf/utils/project_utils.py
# class ProjectUtils(object):
# 
#     """
#     Utility methods for working with the project.
# 
#     """
# 
#     WTF_HOME_CONFIG_ENV_VAR = "WTF_HOME"
# 
#     __WTF_ROOT_FOLDER_FILE = ".wtf_root_folder"
# 
#     __root_folder__ = None
# 
#     @classmethod
#     def get_project_root(cls):
#         '''
#         Return path of the project directory.  Use this method for getting paths relative to the project.
#         However, for data, it's recommended you use WTF_DATA_MANAGER and for assets it's recommended 
#         to use WTF_ASSET_MANAGER, which are already singleton instances that manger the /data, and /assets 
#         folder for you.
# 
#         Returns:
#             str - path of project root directory.
#         '''
#         if(cls.__root_folder__ != None):
#             return cls.__root_folder__
# 
#         # Check for enviornment variable override.
#         try:
#             cls.__root_folder__ = os.environ[cls.WTF_HOME_CONFIG_ENV_VAR]
#         except KeyError:
#             # Means WTF_HOME override isn't specified.
#             pass
# 
#         # Search starting from the current working directory and traverse up parent directories for the
#         # hidden file denoting the project root folder.
#         path = os.getcwd()
#         seperator_matches = re.finditer("/|\\\\", path)
# 
#         paths_to_search = [path]
#         for match in seperator_matches:
#             p = path[:match.start()]
#             paths_to_search.insert(0, p)
# 
#         for path in paths_to_search:
#             target_path = os.path.join(path, cls.__WTF_ROOT_FOLDER_FILE)
#             if os.path.isfile(target_path):
#                 cls.__root_folder__ = path
#                 return cls.__root_folder__
# 
#         raise RuntimeError(u("Missing root project folder locator file '.wtf_root_folder'.")
#                            + u("Check to make sure this file is located in your project directory."))


from optparse import OptionParser
from wtframework.wtf.constants import WTF_CONFIG_LOCATION, WTF_CONFIG_EXT, \
    WTF_ENV_VARS
from wtframework.wtf.utils.project_utils import ProjectUtils
import os
import re
                  help="path to create result file.", metavar="FILE")
    (options, args) = parser.parse_args()


    if options.config:
        configs = re.split(",|;", options.config)
        configs_valid = True
        for config in configs:
            # check if config exists.
            expected_path = os.path.join(ProjectUtils.get_project_root(),
                                         WTF_CONFIG_LOCATION,
                                         config + WTF_CONFIG_EXT)
            if os.path.exists(expected_path):
                pass  # config exists.
            else:
                configs_valid = False
                print("Cannot find config: ", expected_path)
        
        if configs_valid:
            os.putenv(WTF_ENV_VARS, options.config)
            print("Setting config WTF_ENV to:", options.config)
        else:
            exit(1)  # one or more errors was found in the configs.
        

    # Set PYTHONPATH if not set.
    try:
        if ProjectUtils.get_project_root() not in os.environ["PYTHONPATH"]:
            os.putenv("PYTHONPATH", os.path.join(os.environ["PYTHONPATH"], ProjectUtils.get_project_root()))
    except:

# Path: wtframework/wtf/constants.py
# WTF_CONFIG_LOCATION = 'configs/'
# 
# WTF_CONFIG_EXT = '.yaml'
# 
# WTF_ENV_VARS = "WTF_ENV"
# 
# Path: wtframework/wtf/utils/project_utils.py
# class ProjectUtils(object):
# 
#     """
#     Utility methods for working with the project.
# 
#     """
# 
#     WTF_HOME_CONFIG_ENV_VAR = "WTF_HOME"
# 
#     __WTF_ROOT_FOLDER_FILE = ".wtf_root_folder"
# 
#     __root_folder__ = None
# 
#     @classmethod
#     def get_project_root(cls):
#         '''
#         Return path of the project directory.  Use this method for getting paths relative to the project.
#         However, for data, it's recommended you use WTF_DATA_MANAGER and for assets it's recommended 
#         to use WTF_ASSET_MANAGER, which are already singleton instances that manger the /data, and /assets 
#         folder for you.
# 
#         Returns:
#             str - path of project root directory.
#         '''
#         if(cls.__root_folder__ != None):
#             return cls.__root_folder__
# 
#         # Check for enviornment variable override.
#         try:
#             cls.__root_folder__ = os.environ[cls.WTF_HOME_CONFIG_ENV_VAR]
#         except KeyError:
#             # Means WTF_HOME override isn't specified.
#             pass
# 
#         # Search starting from the current working directory and traverse up parent directories for the
#         # hidden file denoting the project root folder.
#         path = os.getcwd()
#         seperator_matches = re.finditer("/|\\\\", path)
# 
#         paths_to_search = [path]
#         for match in seperator_matches:
#             p = path[:match.start()]
#             paths_to_search.insert(0, p)
# 
#         for path in paths_to_search:
#             target_path = os.path.join(path, cls.__WTF_ROOT_FOLDER_FILE)
#             if os.path.isfile(target_path):
#                 cls.__root_folder__ = path
#                 return cls.__root_folder__
# 
#         raise RuntimeError(u("Missing root project folder locator file '.wtf_root_folder'.")
#                            + u("Check to make sure this file is located in your project directory."))


# Path: runtests.py
from optparse import OptionParser
from wtframework.wtf.constants import WTF_CONFIG_LOCATION, WTF_CONFIG_EXT, \
    WTF_ENV_VARS
from wtframework.wtf.utils.project_utils import ProjectUtils
import os
import re

                  help="path to create result file.", metavar="FILE")
    (options, args) = parser.parse_args()


    if options.config:
        configs = re.split(",|;", options.config)
        configs_valid = True
        for config in configs:
            # check if config exists.
            expected_path = os.path.join(ProjectUtils.get_project_root(),
                                         WTF_CONFIG_LOCATION,
                                         config + WTF_CONFIG_EXT)
            if os.path.exists(expected_path):
                pass  # config exists.
            else:
                configs_valid = False
                print("Cannot find config: ", expected_path)
        
        if configs_valid:
            os.putenv(WTF_ENV_VARS, options.config)
            print("Setting config WTF_ENV to:", options.config)
        else:
            exit(1)  # one or more errors was found in the configs.
        

    # Set PYTHONPATH if not set.
    try:
        if ProjectUtils.get_project_root() not in os.environ["PYTHONPATH"]:
            os.putenv("PYTHONPATH", os.path.join(os.environ["PYTHONPATH"], ProjectUtils.get_project_root()))
    except:

        os.putenv("PYTHONPATH", ProjectUtils.get_project_root())