Esempio n. 1
0
    def __init__(self):

        self.manager = stored.SettingsManager(self.TVB_CONFIG_FILE)

        # Actual storage of all TVB related files
        self.KEYCLOAK_CONFIG = self.manager.get_attribute(
            stored.KEY_KC_CONFIGURATION, '')
        self.KEYCLOAK_LOGIN_ENABLED = self.manager.get_attribute(
            stored.KEY_ENABLE_KC_LOGIN, False, eval)
        self.KEYCLOAK_WEB_CONFIG = self.manager.get_attribute(
            stored.KEY_KC_WEB_CONFIGURATION, '')
        self.TVB_STORAGE = self.manager.get_attribute(stored.KEY_STORAGE,
                                                      self.FIRST_RUN_STORAGE,
                                                      str)
        self.UPLOAD_KEY_PATH = self.manager.get_attribute(
            stored.KEY_UPLOAD_PRIVATE_KEY_PATH, None, str)
        self.TRACE_USER_ACTIONS = self.manager.get_attribute(
            stored.KEY_TRACE_USER_ACTIONS, False, eval)
        self.TVB_LOG_FOLDER = os.path.join(self.TVB_STORAGE, "logs")
        self.TVB_TEMP_FOLDER = os.path.join(self.TVB_STORAGE, "TEMP")

        self.env = Environment()
        self.cluster = ClusterSettings(self.manager)
        self.hpc = HPCSettings(self.manager)
        self.web = WebSettings(self.manager)
        self.db = DBSettings(self.manager, self.DEFAULT_STORAGE,
                             self.TVB_STORAGE)
        self.version = VersionSettings(self.manager, self.BIN_FOLDER)
        self.file_storage = self.manager.get_attribute(stored.KEY_FILE_STORAGE,
                                                       'h5', str)

        self.EXTERNALS_FOLDER_PARENT = os.path.dirname(self.BIN_FOLDER)
        if not self.env.is_distribution():
            self.EXTERNALS_FOLDER_PARENT = os.path.dirname(
                self.EXTERNALS_FOLDER_PARENT)

        # The path to the matlab executable (if existent). Otherwise just return an empty string.
        value = self.manager.get_attribute(stored.KEY_MATLAB_EXECUTABLE, '',
                                           str) or ''
        if value == 'None':
            value = ''
        self.MATLAB_EXECUTABLE = value

        # Maximum number of vertices acceptable o be part of a surface at import time.
        self.MAX_SURFACE_VERTICES_NUMBER = self.manager.get_attribute(
            stored.KEY_MAX_NR_SURFACE_VERTEX, 300000, int)
        # Max number of ops that can be scheduled from UI in a PSE. To be correlated with the oarsub limitations
        self.MAX_RANGE_NUMBER = self.manager.get_attribute(
            stored.KEY_MAX_RANGE_NR, 2000, int)
        # Max number of threads in the pool of ops running in parallel. TO be correlated with CPU cores
        self.MAX_THREADS_NUMBER = self.manager.get_attribute(
            stored.KEY_MAX_THREAD_NR, 4, int)
        self.OPERATIONS_BACKGROUND_JOB_INTERVAL = self.manager.get_attribute(
            stored.KEY_OP_BACKGROUND_INTERVAL, 60, int)
        # The maximum disk space that can be used by one single user, in KB.
        self.MAX_DISK_SPACE = self.manager.get_attribute(
            stored.KEY_MAX_DISK_SPACE_USR, 5 * 1024 * 1024, int)
Esempio n. 2
0
    def __init__(self, web_enabled=True):

        self.manager = stored.SettingsManager(self.TVB_CONFIG_FILE)

        ## Actual storage of all TVB related files
        self.TVB_STORAGE = self.manager.get_attribute(stored.KEY_STORAGE,
                                                      self.FIRST_RUN_STORAGE,
                                                      unicode)
        self.TVB_LOG_FOLDER = os.path.join(self.TVB_STORAGE, "logs")
        self.TVB_TEMP_FOLDER = os.path.join(self.TVB_STORAGE, "TEMP")
        self.TVB_PATH = self.manager.get_attribute(stored.KEY_TVB_PATH, '')

        self.env = Environment()
        self.cluster = ClusterSettings(self.manager)
        self.web = WebSettings(self.manager, web_enabled)
        self.db = DBSettings(self.manager, self.DEFAULT_STORAGE,
                             self.TVB_STORAGE)
        self.version = VersionSettings(self.manager, self.BIN_FOLDER)

        self.EXTERNALS_FOLDER_PARENT = os.path.dirname(self.BIN_FOLDER)
        if not self.env.is_distribution():
            self.EXTERNALS_FOLDER_PARENT = os.path.dirname(
                self.EXTERNALS_FOLDER_PARENT)

        # The path to the matlab executable (if existent). Otherwise just return an empty string.
        value = self.manager.get_attribute(stored.KEY_MATLAB_EXECUTABLE, '',
                                           str) or ''
        if value == 'None':
            value = ''
        self.MATLAB_EXECUTABLE = value

        # Maximum number of vertices acceptable o be part of a surface at import time.
        self.MAX_SURFACE_VERTICES_NUMBER = self.manager.get_attribute(
            stored.KEY_MAX_NR_SURFACE_VERTEX, 300000, int)
        # Max number of ops that can be scheduled from UI in a PSE. To be correlated with the oarsub limitations
        self.MAX_RANGE_NUMBER = self.manager.get_attribute(
            stored.KEY_MAX_RANGE_NR, 2000, int)
        # Max number of threads in the pool of ops running in parallel. TO be correlated with CPU cores
        self.MAX_THREADS_NUMBER = self.manager.get_attribute(
            stored.KEY_MAX_THREAD_NR, 4, int)
        # The maximum disk space that can be used by one single user, in KB.
        self.MAX_DISK_SPACE = self.manager.get_attribute(
            stored.KEY_MAX_DISK_SPACE_USR, 5 * 1024 * 1024, int)

        ## Configure Traits
        self.TRAITS_CONFIGURATION = EnhancedDictionary()
        self.TRAITS_CONFIGURATION.interface_method_name = 'interface'
        self.TRAITS_CONFIGURATION.use_storage = True
Esempio n. 3
0
    def new_function(*args, **kwargs):
        """
        Wrapper function for Linux TVB altered environment variables.
        Apply this only on Linux, as that is the only env in which TVB start scripts alter LD_*_PATH env vars.

        :return: Result of the wrapped function
        """
        if not Environment().is_linux_deployment():
            # Do nothing
            return func(*args, **kwargs)

        original_ld_library_path = _remove_from_path('LD_LIBRARY_PATH', 2, 'tvb_data')
        original_ld_run_path = _remove_from_path('LD_RUN_PATH', 2, 'tvb_data')

        result = func(*args, **kwargs)
        ## Restore environment settings after function executed.
        if original_ld_library_path:
            os.environ['LD_LIBRARY_PATH'] = original_ld_library_path
        if original_ld_run_path:
            os.environ['LD_RUN_PATH'] = original_ld_run_path
        return result
Esempio n. 4
0
class TvbProfile():
    """
    ENUM-like class with current TVB profile and accepted values.
    """

    LIBRARY_PROFILE = "LIBRARY_PROFILE"
    COMMAND_PROFILE = "COMMAND_PROFILE"
    WEB_PROFILE = "WEB_PROFILE"
    DESKTOP_PROFILE = "DESKTOP_PROFILE"

    TEST_LIBRARY_PROFILE = "TEST_LIBRARY_PROFILE"
    TEST_POSTGRES_PROFILE = "TEST_POSTGRES_PROFILE"
    TEST_SQLITE_PROFILE = "TEST_SQLITE_PROFILE"

    ALL = [
        LIBRARY_PROFILE, COMMAND_PROFILE, WEB_PROFILE, DESKTOP_PROFILE,
        TEST_POSTGRES_PROFILE, TEST_SQLITE_PROFILE, TEST_LIBRARY_PROFILE
    ]

    REGISTERED_PROFILES = {}

    CURRENT_PROFILE_NAME = None

    current = BaseSettingsProfile(False)
    env = Environment()

    _meta_path_copier = MetaPathCopier()
    _meta_path_copier.deepcopy()

    @classmethod
    def set_profile(cls, selected_profile, in_operation=False, run_init=True):
        """
        Sets TVB profile and do related initializations.
        """

        ### Ensure Python is using UTF-8 encoding (otherwise default encoding is ASCII)
        ### We should make sure UTF-8 gets set before reading from any TVB files
        ### e.g. TVB_STORAGE will differ if the .tvb.configuration file contains non-ascii bytes
        ### most of the comments in the simulator are having pieces outside of ascii coverage
        if cls.env.is_development(
        ) and sys.getdefaultencoding().lower() != 'utf-8':
            reload(sys)
            sys.setdefaultencoding('utf-8')

        if selected_profile is not None:
            ## Restore sys.meta_path, as some profiles (Library) are adding something
            cls._meta_path_copier.restore()

            cls._load_framework_profiles(selected_profile)
            cls._build_profile_class(selected_profile, in_operation, run_init)

    @classmethod
    def _build_profile_class(cls,
                             selected_profile,
                             in_operation=False,
                             run_init=True):
        """
        :param selected_profile: Profile name to be loaded.
        """

        if selected_profile in cls.REGISTERED_PROFILES:
            current_class = cls.REGISTERED_PROFILES[selected_profile]

            cls.current = current_class()
            cls.CURRENT_PROFILE_NAME = selected_profile

            if in_operation:
                # set flags IN_OPERATION,  before initialize** calls, to avoid LoggingBuilder being created there
                cls.current.prepare_for_operation_mode()

            if not cls.env.is_development():
                # initialize deployment first, because in case of a contributor setup this tried to reload
                # and initialize_profile loads already too many tvb modules,
                # making the reload difficult and prone to more failures
                cls.current.initialize_for_deployment()
            if run_init:
                cls.current.initialize_profile()

        else:
            msg = "Invalid profile name %r, expected one of %r"
            msg %= (selected_profile, cls.ALL)
            raise Exception(msg)

    @classmethod
    def _load_framework_profiles(cls, new_profile):

        from tvb.basic.config.profile_settings import LibrarySettingsProfile, TestLibraryProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.LIBRARY_PROFILE] = LibrarySettingsProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.TEST_LIBRARY_PROFILE] = TestLibraryProfile

        if not cls.is_library_mode(new_profile):
            try:
                from tvb.config.profile_settings import CommandSettingsProfile, WebSettingsProfile
                from tvb.config.profile_settings import TestPostgresProfile, TestSQLiteProfile

                cls.REGISTERED_PROFILES[
                    TvbProfile.COMMAND_PROFILE] = CommandSettingsProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.WEB_PROFILE] = WebSettingsProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.TEST_POSTGRES_PROFILE] = TestPostgresProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.TEST_SQLITE_PROFILE] = TestSQLiteProfile

            except ImportError:
                pass

    @staticmethod
    def is_library_mode(new_profile=None):

        lib_profiles = [
            TvbProfile.LIBRARY_PROFILE, TvbProfile.TEST_LIBRARY_PROFILE
        ]
        result = (new_profile in lib_profiles
                  or (new_profile is None
                      and TvbProfile.CURRENT_PROFILE_NAME in lib_profiles)
                  or not TvbProfile.env.is_framework_present())

        # Make sure default settings are not failing because we are not finding some modules
        if (new_profile is None and TvbProfile.CURRENT_PROFILE_NAME is None
                and not TvbProfile.env.is_framework_present()):
            TvbProfile.set_profile(TvbProfile.LIBRARY_PROFILE)

        return result

    @staticmethod
    def is_first_run():

        return TvbProfile.current.manager.is_first_run()
Esempio n. 5
0
class TvbProfile(object):
    """
    ENUM-like class with current TVB profile and accepted values.
    """

    LIBRARY_PROFILE = "LIBRARY_PROFILE"
    COMMAND_PROFILE = "COMMAND_PROFILE"
    WEB_PROFILE = "WEB_PROFILE"
    MATLAB_PROFILE = "MATLAB_PROFILE"

    TEST_LIBRARY_PROFILE = "TEST_LIBRARY_PROFILE"
    TEST_POSTGRES_PROFILE = "TEST_POSTGRES_PROFILE"
    TEST_SQLITE_PROFILE = "TEST_SQLITE_PROFILE"

    ALL = [
        LIBRARY_PROFILE, COMMAND_PROFILE, WEB_PROFILE, MATLAB_PROFILE,
        TEST_POSTGRES_PROFILE, TEST_SQLITE_PROFILE, TEST_LIBRARY_PROFILE
    ]

    REGISTERED_PROFILES = {}

    CURRENT_PROFILE_NAME = None

    current = BaseSettingsProfile(False)
    env = Environment()

    @classmethod
    def set_profile(cls, selected_profile, in_operation=False, run_init=True):
        """
        Sets TVB profile and do related initializations.
        """
        if selected_profile is not None:
            cls._load_framework_profiles(selected_profile)
            cls._build_profile_class(selected_profile, in_operation, run_init)

    @classmethod
    def _build_profile_class(cls,
                             selected_profile,
                             in_operation=False,
                             run_init=True):
        """
        :param selected_profile: Profile name to be loaded.
        """

        if selected_profile in cls.REGISTERED_PROFILES:
            current_class = cls.REGISTERED_PROFILES[selected_profile]

            cls.current = current_class()
            cls.CURRENT_PROFILE_NAME = selected_profile

            if in_operation:
                # set flags IN_OPERATION,  before initialize** calls, to avoid LoggingBuilder being created there
                cls.current.prepare_for_operation_mode()

            if cls.env.is_distribution():
                # initialize deployment first, because in case of a contributor setup this tried to reload
                # and initialize_profile loads already too many tvb modules,
                # making the reload difficult and prone to more failures
                cls.current.initialize_for_deployment()
            if run_init:
                cls.current.initialize_profile()

        else:
            msg = "Invalid profile name %r, expected one of %r"
            msg %= (selected_profile, cls.ALL)
            raise Exception(msg)

    @classmethod
    def _load_framework_profiles(cls, new_profile):

        from tvb.basic.config.profile_settings import LibrarySettingsProfile, TestLibraryProfile, MATLABLibraryProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.LIBRARY_PROFILE] = LibrarySettingsProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.TEST_LIBRARY_PROFILE] = TestLibraryProfile
        cls.REGISTERED_PROFILES[
            TvbProfile.MATLAB_PROFILE] = MATLABLibraryProfile

        if not cls.is_library_mode(new_profile):
            try:
                from tvb.config.profile_settings import CommandSettingsProfile, WebSettingsProfile
                from tvb.config.profile_settings import TestPostgresProfile, TestSQLiteProfile

                cls.REGISTERED_PROFILES[
                    TvbProfile.COMMAND_PROFILE] = CommandSettingsProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.WEB_PROFILE] = WebSettingsProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.TEST_POSTGRES_PROFILE] = TestPostgresProfile
                cls.REGISTERED_PROFILES[
                    TvbProfile.TEST_SQLITE_PROFILE] = TestSQLiteProfile

            except ImportError:
                pass

    @staticmethod
    def is_library_mode(new_profile=None):

        lib_profiles = [
            TvbProfile.LIBRARY_PROFILE, TvbProfile.TEST_LIBRARY_PROFILE
        ]
        result = (new_profile in lib_profiles
                  or (new_profile is None
                      and TvbProfile.CURRENT_PROFILE_NAME in lib_profiles)
                  or not TvbProfile.env.is_framework_present())

        # Make sure default settings are not failing because we are not finding some modules
        if (new_profile is None and TvbProfile.CURRENT_PROFILE_NAME is None
                and not TvbProfile.env.is_framework_present()):
            TvbProfile.set_profile(TvbProfile.LIBRARY_PROFILE)

        return result

    @staticmethod
    def is_first_run():

        return TvbProfile.current.manager.is_first_run()