Ejemplo n.º 1
0
    def create_profile(self):
        """
        Set AiiDA to use the tests config dir and create a default profile there

        Warning: the AiiDA dbenv must not be loaded when this is called!
        """
        from aiida.manage.configuration import settings, load_profile, Profile

        if not self._has_test_db:
            self.create_aiida_db()

        if not self.root_dir:
            self.root_dir = tempfile.mkdtemp()
        configuration.CONFIG = None
        settings.AIIDA_CONFIG_FOLDER = self.config_dir
        configuration.PROFILE = None
        create_instance_directories()
        profile_name = self.profile_info['name']
        config = configuration.get_config(create=True)
        profile = Profile(profile_name, self.profile_dictionary)
        config.add_profile(profile)
        config.set_default_profile(profile_name).store()
        self._profile = profile

        load_profile(profile_name)
        backend = manager.get_manager()._load_backend(schema_check=False)
        backend.migrate()

        self._select_db_test_case(backend=self._profile.database_backend)
        self.init_db()
Ejemplo n.º 2
0
 def setUpClass(cls, *args, **kwargs):
     """Setup a mock profile."""
     super().setUpClass(*args, **kwargs)
     cls.profile_name = 'test_profile'
     cls.profile_dictionary = {
         'default_user':
         '******',
         'database_engine':
         'postgresql_psycopg2',
         'database_backend':
         'django',
         'database_name':
         cls.profile_name,
         'database_port':
         '5432',
         'database_hostname':
         'localhost',
         'database_username':
         '******',
         'database_password':
         '******',
         'repository_uri':
         f"file:///{os.path.join('/some/path', f'repository_{cls.profile_name}')}",
     }
     cls.profile = Profile(cls.profile_name, cls.profile_dictionary)
Ejemplo n.º 3
0
def create_mock_profile(name, repository_dirpath=None, **kwargs):
    """Create mock profile for testing purposes.

    :param name: name of the profile
    :param repository_dirpath: optional absolute path to use as the base for the repository path
    """
    from aiida.manage.configuration import get_config, Profile
    from aiida.manage.external.postgres import DEFAULT_DBINFO

    if repository_dirpath is None:
        config = get_config()
        repository_dirpath = config.dirpath

    profile_dictionary = {
        'default_user':
        kwargs.pop('default_user', 'dummy@localhost'),
        'database_engine':
        kwargs.pop('database_engine', 'postgresql_psycopg2'),
        'database_backend':
        kwargs.pop('database_backend', 'django'),
        'database_hostname':
        kwargs.pop('database_hostname', DEFAULT_DBINFO['host']),
        'database_port':
        kwargs.pop('database_port', DEFAULT_DBINFO['port']),
        'database_name':
        kwargs.pop('database_name', name),
        'database_username':
        kwargs.pop('database_username', 'user'),
        'database_password':
        kwargs.pop('database_password', 'pass'),
        'repository_uri':
        'file:///' + os.path.join(repository_dirpath, 'repository_' + name),
    }

    return Profile(name, profile_dictionary)
Ejemplo n.º 4
0
    def _create_profile(name, **kwargs):

        repository_dirpath = kwargs.pop('repository_dirpath',
                                        get_config().dirpath)

        profile_dictionary = {
            'default_user':
            kwargs.pop('default_user', 'dummy@localhost'),
            'database_engine':
            kwargs.pop('database_engine', 'postgresql_psycopg2'),
            'database_backend':
            kwargs.pop('database_backend', 'django'),
            'database_hostname':
            kwargs.pop('database_hostname', 'localhost'),
            'database_port':
            kwargs.pop('database_port', 5432),
            'database_name':
            kwargs.pop('database_name', name),
            'database_username':
            kwargs.pop('database_username', 'user'),
            'database_password':
            kwargs.pop('database_password', 'pass'),
            'repository_uri':
            f"file:///{os.path.join(repository_dirpath, f'repository_{name}')}",
        }

        return Profile(name, profile_dictionary)
Ejemplo n.º 5
0
    def convert(self, value, param, ctx):
        """Attempt to match the given value to a valid profile."""
        from aiida.common.exceptions import MissingConfigurationError, ProfileConfigurationError
        from aiida.manage.configuration import get_config, load_profile, Profile

        value = super().convert(value, param, ctx)

        try:
            config = get_config(create=True)
            profile = config.get_profile(value)
        except (MissingConfigurationError,
                ProfileConfigurationError) as exception:
            if not self._cannot_exist:
                self.fail(str(exception))

            # Create a new empty profile
            profile = Profile(value, {})
        else:
            if self._cannot_exist:
                self.fail(str(f'the profile `{value}` already exists'))

        if self._load_profile:
            load_profile(profile.name)

        return profile