Ejemplo n.º 1
0
def load_backend_if_not_loaded():
    """Load the current profile if necessary while running the spinner to show command hasn't crashed."""
    from aiida.manage.configuration import load_profile
    from aiida.manage.manager import get_manager
    with spinner():
        load_profile()
        get_manager().get_backend()
Ejemplo n.º 2
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
Ejemplo n.º 3
0
    def aiida(self, line='', local_ns=None):
        """Load AiiDA in ipython (checking if it was already loaded), and
        inserts in the namespace the main AiiDA classes (the same that are
        loaded in ``verdi shell``.

        Usage::

            %aiida [optional parameters]

        .. todo:: implement parameters, e.g. for the profile to load.
        """
        # pylint: disable=unused-argument,attribute-defined-outside-init
        from aiida.manage.configuration import load_profile
        from aiida.cmdline.utils.shell import get_start_namespace

        self.is_warning = False
        lcontent = line.strip()
        if lcontent:
            profile = load_profile(lcontent)
        else:
            profile = load_profile()

        self.current_state = f'Loaded AiiDA DB environment - profile name: {profile.name}.'

        user_ns = get_start_namespace()
        for key, value in user_ns.items():
            add_to_ns(local_ns, key, value)

        return self
Ejemplo n.º 4
0
def alembic_cli(profile):
    """Simple wrapper around the alembic command line tool that first loads an AiiDA profile."""
    from aiida.manage.configuration import load_profile
    from aiida.manage.manager import get_manager

    load_profile(profile=profile.name)
    manager = get_manager()
    manager._load_backend(schema_check=False)  # pylint: disable=protected-access
Ejemplo n.º 5
0
def test_merge_deprecated_yaml(tmp_path):
    """Test that an existing 'cache_config.yml' is correctly merged into the main config.

    An AiidaDeprecationWarning should also be raised.
    """
    from aiida.common.warnings import AiidaDeprecationWarning
    from aiida.manage import configuration
    from aiida.manage.configuration import settings, load_profile, reset_profile, get_config_option

    # Store the current configuration instance and config directory path
    current_config = configuration.CONFIG
    current_config_path = current_config.dirpath
    current_profile_name = configuration.PROFILE.name

    try:
        reset_profile()
        configuration.CONFIG = None

        # Create a temporary folder, set it as the current config directory path
        settings.AIIDA_CONFIG_FOLDER = str(tmp_path)
        config_dictionary = json.loads(
            Path(__file__).parent.joinpath(
                'configuration/migrations/test_samples/reference/5.json').
            read_text())
        config_dictionary['profiles']['default'][
            'AIIDADB_REPOSITORY_URI'] = f"file:///{tmp_path/'repo'}"
        cache_dictionary = {
            'default': {
                'default': True,
                'enabled': ['aiida.calculations:quantumespresso.pw'],
                'disabled': ['aiida.calculations:templatereplacer']
            }
        }
        tmp_path.joinpath('config.json').write_text(
            json.dumps(config_dictionary))
        tmp_path.joinpath('cache_config.yml').write_text(
            yaml.dump(cache_dictionary))
        with pytest.warns(AiidaDeprecationWarning, match='cache_config.yml'):
            configuration.CONFIG = configuration.load_config()
        load_profile('default')

        assert get_config_option('caching.default_enabled') is True
        assert get_config_option('caching.enabled_for') == [
            'aiida.calculations:quantumespresso.pw'
        ]
        assert get_config_option('caching.disabled_for') == [
            'aiida.calculations:templatereplacer'
        ]
        # should have now been moved to cache_config.yml.<DATETIME>
        assert not tmp_path.joinpath('cache_config.yml').exists()
    finally:
        # Reset the config folder path and the config instance. Note this will always be executed after the yield no
        # matter what happened in the test that used this fixture.
        reset_profile()
        settings.AIIDA_CONFIG_FOLDER = current_config_path
        configuration.CONFIG = current_config
        load_profile(current_profile_name)
Ejemplo n.º 6
0
def setup(
    non_interactive, profile, email, first_name, last_name, institution, db_engine, db_backend, db_host, db_port,
    db_name, db_username, db_password, repository
):
    """Setup a new profile."""
    # pylint: disable=too-many-arguments,too-many-locals,unused-argument
    from aiida import orm
    from aiida.manage.configuration import get_config

    profile.database_engine = db_engine
    profile.database_backend = db_backend
    profile.database_name = db_name
    profile.database_port = db_port
    profile.database_hostname = db_host
    profile.database_username = db_username
    profile.database_password = db_password
    profile.repository_uri = 'file://' + repository

    config = get_config()

    # Creating the profile
    config.add_profile(profile)
    config.set_default_profile(profile.name)

    # Load the profile
    load_profile(profile.name)
    echo.echo_success('created new profile `{}`.'.format(profile.name))

    # Migrate the database
    echo.echo_info('migrating the database.')
    backend = get_manager()._load_backend(schema_check=False)  # pylint: disable=protected-access

    try:
        backend.migrate()
    except Exception as exception:  # pylint: disable=broad-except
        echo.echo_critical(
            'database migration failed, probably because connection details are incorrect:\n{}'.format(exception)
        )
    else:
        echo.echo_success('database migration completed.')

    # Optionally setting configuration default user settings
    config.set_option('user.email', email, override=False)
    config.set_option('user.first_name', first_name, override=False)
    config.set_option('user.last_name', last_name, override=False)
    config.set_option('user.institution', institution, override=False)

    # Create the user if it does not yet exist
    created, user = orm.User.objects.get_or_create(
        email=email, first_name=first_name, last_name=last_name, institution=institution
    )
    if created:
        user.store()
    profile.default_user = user.email
    config.update_profile(profile)
    config.store()
def temporary_config_instance():
    """Create a temporary AiiDA instance."""
    current_config = None
    current_config_path = None
    current_profile_name = None
    temporary_config_directory = None

    from aiida.common.utils import Capturing
    from aiida.manage import configuration
    from aiida.manage.configuration import settings, load_profile, reset_profile

    try:
        from aiida.manage.configuration.settings import create_instance_directories

        # Store the current configuration instance and config directory path
        current_config = configuration.CONFIG
        current_config_path = current_config.dirpath
        current_profile_name = configuration.PROFILE.name

        reset_profile()
        configuration.CONFIG = None

        # Create a temporary folder, set it as the current config directory path and reset the loaded configuration
        profile_name = 'test_profile_1234'
        temporary_config_directory = tempfile.mkdtemp()
        settings.AIIDA_CONFIG_FOLDER = temporary_config_directory

        # Create the instance base directory structure, the config file and a dummy profile
        create_instance_directories()

        # The constructor of `Config` called by `load_config` will print warning messages about migrating it
        with Capturing():
            configuration.CONFIG = configuration.load_config(create=True)

        profile = create_mock_profile(
            name=profile_name, repository_dirpath=temporary_config_directory)

        # Add the created profile and set it as the default
        configuration.CONFIG.add_profile(profile)
        configuration.CONFIG.set_default_profile(profile_name, overwrite=True)
        configuration.CONFIG.store()
        load_profile()

        yield configuration.CONFIG
    finally:
        # Reset the config folder path and the config instance
        reset_profile()
        settings.AIIDA_CONFIG_FOLDER = current_config_path
        configuration.CONFIG = current_config
        load_profile(current_profile_name)

        # Destroy the temporary instance directory
        if temporary_config_directory and os.path.isdir(
                temporary_config_directory):
            shutil.rmtree(temporary_config_directory)
Ejemplo n.º 8
0
    def initialize(self):
        """Set internal attributes of the class.

        Includes importing the process class.
        """
        # pylint: disable=attribute-defined-outside-init
        from aiida.manage.configuration import load_profile
        load_profile()

        self.class_name = self.arguments[0].split('(')[0]
        self.module_name = self.options['module']
        self.process_name = self.module_name + '.' + self.class_name
        self.process = get_object_from_string(self.process_name)
        self.process_spec = self.process.spec()
Ejemplo n.º 9
0
def load_backend_if_not_loaded():
    """Load the database backend environment for the currently loaded profile.

    If no profile has been loaded yet, the default profile will be loaded first. A spinner will be shown during both
    actions to indicate that the function is working and has not crashed, since loading can take a second.
    """
    from aiida.manage.configuration import get_profile, load_profile
    from aiida.manage.manager import get_manager

    manager = get_manager()

    if get_profile() is None or not manager.backend_loaded:
        with spinner():
            load_profile()  # This will load the default profile if no profile has already been loaded
            manager.get_backend()  # This will load the backend of the loaded profile, if not already loaded
Ejemplo n.º 10
0
def create_host_plots_clean():

    pn.extension('mathjax')

    # load aiida if necessary
    profile = load_profile(profile_name)

    ase_atoms = prepare_plotting_structure()
    strucview = create_structure_plot(ase_atoms, static_plot=True)

    # create bandstructure plot
    bandstruc_plot = plot_bandstruc()

    # make DOS plot
    dos_plot, ymax = plot_dos()
    # add ef lines to bandstructure plot (needs to be done here since some values are otherwise not defined)
    add_ef_lines_bandstruc(bandstruc_plot, ymax)

    # put bandstructure and DOS plots together
    layout = pn.Row(bandstruc_plot, dos_plot)

    #struc_title = pn.pane.LaTeX("Thin film (6QL) of Sb$_2$Te$_3$")
    #host_plots = pn.Row(pn.Column(struc_title, strucview), layout)
    host_plots = pn.Row(strucview, layout)

    return host_plots
Ejemplo n.º 11
0
def main(profile, command):
    """Simple wrapper around the Django command line tool that first loads an AiiDA profile."""
    from django.core.management import execute_from_command_line

    # Load the general load_dbenv.
    from aiida.manage.configuration import load_profile
    from aiida.manage.manager import get_manager

    load_profile(profile=profile.name)
    manager = get_manager()
    manager._load_backend(schema_check=False)

    # The `execute_from_command` expects a list of command line arguments where the first is the program name that one
    # would normally call directly. Since this is now replaced by our `click` command we just spoof a random name.
    argv = ['basename'] + list(command)
    execute_from_command_line(argv)
Ejemplo n.º 12
0
def create_db(profile):
    """Create PostgreSQL database, if missing."""
    dbinfo_su = {
        'host': profile.database_hostname,
        'port': profile.database_port,
        'user': os.getenv("AIIDADB_SUPER_USER"),
        'password': os.getenv("AIIDADB_SUPER_PASS"),
        'database': 'template1',
    }
    postgres = Postgres(interactive=False, quiet=False,
                        dbinfo=dbinfo_su)  #, determine_setup=False)

    if not postgres.is_connected:
        raise ConnectionError("Unable to connect as super user")

    try:
        if not postgres.dbuser_exists(dbuser=profile.database_username):
            postgres.create_dbuser(dbuser=profile.database_username,
                                   dbpass=profile.database_password)

        if not postgres.db_exists(dbname=profile.database_name):
            postgres.create_db(profile.database_username,
                               profile.database_name)

            # Fill DB with vanilla content
            load_profile(profile.name)
            backend = get_manager()._load_backend(schema_check=False)  # pylint: disable=protected-access

            try:
                backend.migrate()
            except Exception as exception:  # pylint: disable=broad-except
                print(
                    'database migration failed, probably because connection details are incorrect:\n{}'
                    .format(exception))

            # Create the user if it does not yet exist
            created, user = orm.User.objects.get_or_create(
                email=profile.default_user,
                first_name='AiiDA',
                last_name='EXPLORER',
                institution='WEBSERVICE')
            if created:
                user.store()
            profile.default_user = user.email

    except:
        print(traceback.format_exc())
Ejemplo n.º 13
0
def load_dbenv(profile=None):
    """Alias for `load_dbenv` from `aiida.backends.utils`

    :param profile: name of the profile to load
    :type profile: str

    .. deprecated:: 1.0.0
        Will be removed in `v2.0.0`, use :func:`aiida.manage.configuration.load_profile` instead.
    """
    warnings.warn('function is deprecated, use `load_profile` instead', AiidaDeprecationWarning)  # pylint: disable=no-member
    current_profile = get_profile()
    from aiida.common import InvalidOperation

    if current_profile:
        raise InvalidOperation('You cannot call load_dbenv multiple times!')

    load_profile(profile)
Ejemplo n.º 14
0
    def initialize(self):
        """Set internal attributes of the class.

        Includes importing the process class.
        """
        # pylint: disable=attribute-defined-outside-init
        load_profile()

        self.class_name = self.arguments[0].split('(')[0]
        self.module_name = self.options['module']
        self.process_name = f'{self.module_name}.{self.class_name}'
        self.process = get_object_from_string(self.process_name)

        try:
            self.process_spec = self.process.spec()
        except Exception as exc:
            raise RuntimeError(
                f"Error while building the spec for process '{self.process_name}': '{exc!r}.'"
            ) from exc
Ejemplo n.º 15
0
    def initialize(self):
        """Set internal attributes of the class.

        Includes importing the process class.
        """
        # pylint: disable=attribute-defined-outside-init
        from aiida.manage.configuration import load_profile
        load_profile()

        self.class_name = self.arguments[0].split('(')[0]
        self.module_name = self.options['module']
        self.process_name = self.module_name + '.' + self.class_name
        self.process = get_object_from_string(self.process_name)

        try:
            self.process_spec = self.process.spec()
        except Exception as exc:
            raise RuntimeError(
                "Error while building the spec for process '{}': '{!r}.'".
                format(self.process_name, exc)) from exc
Ejemplo n.º 16
0
def empty_config(tmp_path) -> Config:
    """Create a temporary configuration instance.

    This creates a temporary directory with a clean `.aiida` folder and basic configuration file. The currently loaded
    configuration and profile are stored in memory and are automatically restored at the end of this context manager.

    :return: a new empty config instance.
    """
    from aiida.common.utils import Capturing
    from aiida.manage import configuration
    from aiida.manage.configuration import settings, reset_profile

    # Store the current configuration instance and config directory path
    current_config = configuration.CONFIG
    current_config_path = current_config.dirpath
    current_profile_name = configuration.PROFILE.name

    reset_profile()
    configuration.CONFIG = None

    # Create a temporary folder, set it as the current config directory path and reset the loaded configuration
    settings.AIIDA_CONFIG_FOLDER = str(tmp_path)

    # Create the instance base directory structure, the config file and a dummy profile
    settings.create_instance_directories()

    # The constructor of `Config` called by `load_config` will print warning messages about migrating it
    with Capturing():
        configuration.CONFIG = configuration.load_config(create=True)

    yield get_config()

    # Reset the config folder path and the config instance. Note this will always be executed after the yield no
    # matter what happened in the test that used this fixture.
    reset_profile()
    settings.AIIDA_CONFIG_FOLDER = current_config_path
    configuration.CONFIG = current_config
    load_profile(current_profile_name)
Ejemplo n.º 17
0
    def _config_with_profile_factory(set_as_default=True, load=True, name='default', **kwargs):
        """Create a temporary configuration instance with one profile.

        :param set_as_default: whether to set the one profile as the default.
        :param load: whether to load the profile.
        :param name: the profile name
        :param kwargs: parameters that are forwarded to the `Profile` constructor.

        :return: a config instance with a configured profile.
        """
        profile = profile_factory(name=name, **kwargs)
        config = empty_config
        config.add_profile(profile)

        if set_as_default:
            config.set_default_profile(profile.name, overwrite=True)

        config.store()

        if load:
            load_profile(profile.name)

        return config
Ejemplo n.º 18
0
# Enable rtd mode via `export READTHEDOCS=True`
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'

if not on_rtd:  # only import and set the theme if we're building docs locally
    try:
        import sphinx_rtd_theme
        html_theme = 'sphinx_rtd_theme'
        html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
    except ImportError:
        # No sphinx_rtd_theme installed
        pass
    # Load the database environment by first loading the profile and then loading the backend through the manager
    from aiida.manage.configuration import get_config, load_profile
    from aiida.manage.manager import get_manager
    config = get_config()
    load_profile(config.default_profile_name)
    get_manager().get_backend()
else:
    # Back-end settings for readthedocs online documentation.
    from aiida.manage import configuration
    configuration.IN_RT_DOC_MODE = True
    configuration.BACKEND = "django"

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
needs_sphinx = '1.5'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
Ejemplo n.º 19
0
# documentation root, use os.path.abspath to make it absolute, like shown here.

# Enable rtd mode via `export READTHEDOCS=True`
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'

if not on_rtd:  # only import and set the theme if we're building docs locally
    import sphinx_rtd_theme
    html_theme = 'sphinx_rtd_theme'
    html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]

# Back-end settings for readthedocs online documentation.
configuration.IN_RT_DOC_MODE = True
configuration.BACKEND = "django"

configuration.reset_config()  # empty config was created when importing aiida
configuration.load_profile()  # load dummy config for RTD
# load DB backend (no schema check since no DB)
get_manager()._load_backend(schema_check=False)  # pylint: disable=protected-access

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
needs_sphinx = '1.5'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.mathjax',
    'sphinx.ext.intersphinx',
Ejemplo n.º 20
0
                email=profile.default_user,
                first_name='AiiDA',
                last_name='EXPLORER',
                institution='WEBSERVICE')
            if created:
                user.store()
            profile.default_user = user.email

    except:
        print(traceback.format_exc())


if os.getenv("AIIDADB_SUPER_USER"):
    create_db(profile)
    config.update_profile(profile)

load_profile(profile_name)

CONFIG_DIR = os.path.join(
    os.path.split(os.path.abspath(aiida.restapi.__file__))[0], 'common')

(app, api) = run_api(api.App,
                     api.AiidaApi,
                     hostname="localhost",
                     port=5000,
                     config=CONFIG_DIR,
                     debug=False,
                     wsgi_profile=False,
                     hookup=False,
                     catch_internal_server=False)
Ejemplo n.º 21
0
import numpy as np
from aiida.manage.configuration import load_profile
from aiida.orm import Bool, Str, Code, Int, Float
from aiida.plugins import DataFactory, WorkflowFactory
from aiida.engine import submit

load_profile()

Dict = DataFactory('dict')
KpointsData = DataFactory("array.kpoints")


def launch_aiida_bulk_modulus(structure, code_string, resources,
                              label="AlN VASP relax calculation"):
    incar_dict = {'incar': {
        'PREC': 'Accurate',
        'EDIFF': 1e-8,
        'NELMIN': 5,
        'NELM': 100,
        'ENCUT': 500,
        'IALGO': 38,
        'ISMEAR': 0,
        'SIGMA': 0.01,
        'GGA': 'PS',
        'LREAL': False,
        'LCHARG': False,
        'LWAVE': False,}
    }

    kpoints = KpointsData()
    kpoints.set_kpoints_mesh([6, 6, 4], offset=[0, 0, 0.5])
Ejemplo n.º 22
0
# documentation root, use os.path.abspath to make it absolute, like shown here.

# Enable rtd mode via `export READTHEDOCS=True`
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'

if not on_rtd:  # only import and set the theme if we're building docs locally
    try:
        import sphinx_rtd_theme
        html_theme = 'sphinx_rtd_theme'
        html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
    except ImportError:
        # No sphinx_rtd_theme installed
        pass
    # Load the database environment by first loading the profile and then loading the backend through the manager
    config = configuration.get_config()
    configuration.load_profile(config.default_profile_name)
    get_manager().get_backend()
else:
    # Back-end settings for readthedocs online documentation.
    configuration.IN_RT_DOC_MODE = True
    configuration.BACKEND = "django"

    configuration.reset_config()  # empty config was created when importing aiida
    configuration.load_profile()  # load dummy config for RTD
    # load DB backend (no schema check since no DB)
    get_manager()._load_backend(schema_check=False)  # pylint: disable=protected-access

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
needs_sphinx = '1.5'