Пример #1
0
    def get_start_namespace(self):
        """Load all default and custom modules"""
        from aiida import load_dbenv, is_dbenv_loaded
        from aiida.backends import settings
        if not is_dbenv_loaded():
            load_dbenv(profile=settings.AIIDADB_PROFILE)

        from aiida.common.setup import get_property
        user_ns = {}
        # load default modules
        for app_mod, model_name, alias in default_modules_list:
            user_ns[alias] = getattr(__import__(app_mod, {}, {},
                                                model_name), model_name)

        # load custom modules
        custom_modules_list = [(str(e[0]), str(e[2])) for e in
                               [p.rpartition('.') for p in get_property(
                                   'verdishell.modules', default="").split(
                                   ':')]
                               if e[1] == '.']

        for app_mod, model_name in custom_modules_list:
            try:
                user_ns[model_name] = getattr(
                    __import__(app_mod, {}, {}, model_name), model_name)
            except AttributeError:
                # if the module does not exist, we ignore it
                pass

        return user_ns
Пример #2
0
def get_start_namespace():
    """Load all default and custom modules"""
    from aiida.cmdline.utils.shell import DEFAULT_MODULES_LIST
    from aiida.common.setup import get_property

    user_ns = {}
    # load default modules
    for app_mod, model_name, alias in DEFAULT_MODULES_LIST:
        user_ns[alias] = getattr(__import__(app_mod, {}, {}, model_name),
                                 model_name)

    # load custom modules
    custom_modules_list = [(str(e[0]), str(e[2])) for e in [
        p.rpartition('.')
        for p in get_property('verdishell.modules', default="").split(':')
    ] if e[1] == '.']

    for app_mod, model_name in custom_modules_list:
        try:
            user_ns[model_name] = getattr(
                __import__(app_mod, {}, {}, model_name), model_name)
        except AttributeError:
            # if the module does not exist, we ignore it
            pass

    return user_ns
Пример #3
0
    def run_listproperties(self, *args):
        """
        List all found global AiiDA properties.
        """
        import argparse

        from aiida.common.setup import (
            _property_table, exists_property, get_property)

        parser = argparse.ArgumentParser(
            prog=self.get_full_command_name(),
            description='List all custom properties stored in the user configuration file.')
        parser.add_argument('-a', '--all',
                            dest='all', action='store_true',
                            help="Show all properties, even if not explicitly defined, if they "
                                 "have a default value.")
        parser.set_defaults(all=False)
        parsed_args = parser.parse_args(args)

        show_all = parsed_args.all

        for prop in sorted(_property_table.keys()):
            try:
                # To enforce the generation of an exception, even if
                # there is a default value
                if show_all or exists_property(prop):
                    val = get_property(prop)
                    print "{} = {}".format(prop, val)
            except KeyError:
                pass
Пример #4
0
    def test_set_property(self):
        """Test the `verdi devel setproperty` command."""
        property_name = 'daemon.timeout'
        property_values = [10, 20]

        for property_value in property_values:
            options = [property_name, str(property_value)]
            result = self.runner.invoke(cmd_devel.devel_setproperty, options)

            self.assertIsNone(result.exception)
            self.assertEquals(get_property(property_name), property_value)
Пример #5
0
def devel_listproperties(all_entries):
    """List all the properties that are explicitly set for the current profile."""
    from aiida.common.setup import _property_table, exists_property, get_property

    for prop in sorted(_property_table.keys()):
        try:
            # To enforce the generation of an exception, even if there is a default value
            if all_entries or exists_property(prop):
                val = get_property(prop)
                echo.echo('{} = {}'.format(prop, val))
        except KeyError:
            pass
Пример #6
0
def devel_getproperty(prop):
    """Get the global PROPERTY from the configuration file."""
    from aiida.common.setup import get_property

    try:
        value = get_property(prop)
    except ValueError:
        echo.echo_critical('property {} not found'.format(prop))
    except Exception as exception:  # pylint: disable=broad-except
        echo.echo_critical('{} while getting the property: {}'.format(
            type(exception).__name__, exception.message))
    else:
        echo.echo('{}'.format(value))
Пример #7
0
    def run_getproperty(self, *args):
        """
        Get a global AiiDA property from the config file in .aiida.
        """
        from aiida.common.setup import get_property

        if len(args) != 1:
            print >> sys.stderr, ("usage: {} PROPERTYNAME".format(
                self.get_full_command_name()))
            sys.exit()

        try:
            value = get_property(args[0])
        except Exception as e:
            print >> sys.stderr, ("{} while getting the "
                                  "property: {}".format(type(e).__name__, e.message))
            sys.exit(1)
        print "{}".format(value)
Пример #8
0
import warnings

from aiida.common.log import configure_logging
from aiida.common.setup import get_property

__copyright__ = u"Copyright (c), This file is part of the AiiDA platform. For further information please visit http://www.aiida.net/. All rights reserved."
__license__ = "MIT license, see LICENSE.txt file."
__version__ = "1.0.0a2"
__authors__ = "The AiiDA team."
__paper__ = """G. Pizzi, A. Cepellotti, R. Sabatini, N. Marzari, and B. Kozinsky, "AiiDA: automated interactive infrastructure and database for computational science", Comp. Mat. Sci 111, 218-230 (2016); http://dx.doi.org/10.1016/j.commatsci.2015.09.013 - http://www.aiida.net."""
__paper_short__ = """G. Pizzi et al., Comp. Mat. Sci 111, 218 (2016)."""

# Configure the default logging
configure_logging()

if get_property("warnings.showdeprecations"):
    # print out the warnings coming from deprecation
    # in Python 2.7 it is suppressed by default
    warnings.simplefilter('default', DeprecationWarning)


def try_load_dbenv(*argc, **argv):
    """
    Run `load_dbenv` unless the dbenv has already been loaded.
    """
    if not is_dbenv_loaded():
        load_dbenv(*argc, **argv)
        return True
    return False

Пример #9
0
    def run_tests(self, *args):
        import unittest
        from aiida.common.setup import get_property
        from aiida.backends import settings
        from aiida.backends.djsite.settings import settings_profile

        db_test_list = []
        test_folders = []
        do_db = False
        if args:
            for arg in args:
                if arg in self.allowed_test_folders:
                    dbtests = self.allowed_test_folders[arg]
                    # Anything that has been added is a DB test
                    if dbtests is not None:
                        do_db = True
                        for dbtest in dbtests:
                            db_test_list.append(dbtest)
                    else:
                        test_folders.append(arg)
                else:
                    print >> sys.stderr, (
                        "{} is not a valid test. "
                        "Allowed test folders are:".format(arg))
                    print >> sys.stderr, "\n".join(
                        '  * {}'.format(a)
                        for a in sorted(self.allowed_test_folders.keys()))
                    sys.exit(1)
        else:
            # Without arguments, run all tests
            do_db = True
            for k, v in self.allowed_test_folders.iteritems():
                if v is None:
                    # Non-db tests
                    test_folders.append(k)
                else:
                    # DB test
                    for dbtest in v:
                        db_test_list.append(dbtest)

        for test_folder in test_folders:
            print "v" * 75
            print ">>> Tests for module {} <<<".format(test_folder.ljust(50))
            print "^" * 75
            testsuite = unittest.defaultTestLoader.discover(
                test_folder, top_level_dir=os.path.dirname(aiida.__file__))
            test_runner = unittest.TextTestRunner()
            test_runner.run(testsuite)
        if do_db:
            # As a first thing, I want to set the correct flags.
            # This allow to work on temporary DBs and a temporary repository.

            ## Setup a sqlite3 DB for tests (WAY faster, since it remains in-memory
            ## and not on disk.
            # if you define such a variable to False, it will use the same backend
            # that you have already configured also for tests. Otherwise,
            # Setup a sqlite3 DB for tests (WAY faster, since it remains in-memory)

            # The prefix is then checked inside get_profile_config and stripped
            # but it is needed to know if this is a test or not
            if get_property('tests.use_sqlite'):
                profile_prefix = 'testsqlite_'
            else:
                profile_prefix = 'test_'

            profile = "{}{}".format(
                profile_prefix, settings.AIIDADB_PROFILE
                if settings.AIIDADB_PROFILE is not None else 'default')
            settings_profile.aiida_test_list = db_test_list

            print "v" * 75
            print(
                ">>> Tests for django db application   "
                "                                  <<<")
            print "^" * 75
            pass_to_django_manage([execname, 'test', 'db'], profile=profile)
Пример #10
0
         'datefmt': '%m/%d/%Y %I:%M:%S %p',
     },
 },
 'handlers': {
     'console': {
         'level': 'DEBUG',
         'class': 'logging.StreamHandler',
         'formatter': 'halfverbose',
     },
     'dblogger': {
         # get_property takes the property from the config json file
         # The key used in the json, and the default value, are
         # specified in the _property_table inside aiida.common.setup
         # NOTE: To modify properties, use the 'verdi devel setproperty'
         #   command and similar ones (getproperty, describeproperties, ...)
         'level': get_property('logging.db_loglevel'),
         'class': 'aiida.utils.logger.DBLogHandler',
     },
 },
 'loggers': {
     'aiida': {
         'handlers': ['console', 'dblogger'],
         'level': get_property('logging.aiida_loglevel'),
         'propagate': False,
     },
     'paramiko': {
         'handlers': ['console'],
         'level': get_property('logging.paramiko_loglevel'),
         'propagate': False,
     },
 },
Пример #11
0
def deposit(what,
            type,
            author_name=None,
            author_email=None,
            url=None,
            title=None,
            username=None,
            password=False,
            user_email=None,
            code_label=default_options['code'],
            computer_name=None,
            replace=None,
            message=None,
            **kwargs):
    """
    Launches a
    :py:class:`aiida.orm.implementation.general.calculation.job.AbstractJobCalculation`
    to deposit data node to \*COD-type database.

    :return: launched :py:class:`aiida.orm.implementation.general.calculation.job.AbstractJobCalculation`
        instance.
    :raises ValueError: if any of the required parameters are not given.
    """
    from aiida.common.setup import get_property

    parameters = {}

    if not what:
        raise ValueError("Node to be deposited is not supplied")
    if not type:
        raise ValueError("Deposition type is not supplied. Should be "
                         "one of the following: 'published', "
                         "'prepublication' or 'personal'")
    if not username:
        username = get_property('tcod.depositor_username')
        if not username:
            raise ValueError("Depositor username is not supplied")
    if not password:
        parameters['password'] = get_property('tcod.depositor_password')
        if not parameters['password']:
            raise ValueError("Depositor password is not supplied")
    if not user_email:
        user_email = get_property('tcod.depositor_email')
        if not user_email:
            raise ValueError("Depositor email is not supplied")

    parameters['deposition-type'] = type
    parameters['username'] = username
    parameters['user_email'] = user_email

    if type == 'published':
        pass
    elif type in ['prepublication', 'personal']:
        if not author_name:
            author_name = get_property('tcod.depositor_author_name')
            if not author_name:
                raise ValueError("Author name is not supplied")
        if not author_email:
            author_email = get_property('tcod.depositor_author_email')
            if not author_email:
                raise ValueError("Author email is not supplied")
        if not title:
            raise ValueError("Publication title is not supplied")
    else:
        raise ValueError("Unknown deposition type '{}' -- should be "
                         "one of the following: 'published', "
                         "'prepublication' or 'personal'".format(type))

    if replace:
        if str(int(replace)) != replace or int(replace) < 10000000 \
            or int(replace) > 99999999:
            raise ValueError("ID of the replaced structure ({}) does not "
                             "seem to be valid TCOD ID: must be in "
                             "range [10000000,99999999]".format(replace))
    elif message:
        raise ValueError("Message is given while the structure is not "
                         "redeposited -- log message is relevant to "
                         "redeposition only")

    kwargs['additional_tags'] = {}
    if title:
        kwargs['additional_tags']['_publ_section_title'] = title
    if author_name:
        kwargs['additional_tags']['_publ_author_name'] = author_name
    if replace:
        kwargs['additional_tags']['_tcod_database_code'] = replace
        kwargs['datablock_names'] = [replace]

    cif = export_cifnode(what, store=True, **kwargs)

    from aiida.orm.code import Code
    from aiida.orm.computer import Computer
    from aiida.orm.data.parameter import ParameterData
    from aiida.common.exceptions import NotExistent

    code = Code.get_from_string(code_label)
    computer = None
    if computer_name:
        computer = Computer.get(computer_name)
    calc = code.new_calc(computer=computer)
    calc.set_resources({'num_machines': 1, 'num_mpiprocs_per_machine': 1})

    if password:
        import getpass
        parameters['password'] = getpass.getpass("Password: ")
    if author_name:
        parameters['author_name'] = author_name
    if author_email:
        parameters['author_email'] = author_email
    if url:
        parameters['url'] = url
    if replace:
        parameters['replace'] = True
    if message:
        parameters['log-message'] = str(message)
    pd = ParameterData(dict=parameters)

    calc.use_cif(cif)
    calc.use_parameters(pd)

    calc.store_all()
    calc.submit()

    return calc
Пример #12
0
    def calculation_list(self, *args):
        """
        Return a list of calculations on screen.
        """
        if not is_dbenv_loaded():
            load_dbenv()

        from aiida.common.datastructures import calc_states

        import argparse
        from aiida.orm.calculation.job import JobCalculation as C
        from aiida.common.setup import get_property

        parser = argparse.ArgumentParser(
            prog=self.get_full_command_name(),
            description='List AiiDA calculations.')
        # The default states are those that are shown if no option is given
        parser.add_argument('-s', '--states', nargs='+', type=str,
                            help="show only the AiiDA calculations with given state",
                            default=[calc_states.WITHSCHEDULER,
                                     calc_states.NEW,
                                     calc_states.TOSUBMIT,
                                     calc_states.SUBMITTING,
                                     calc_states.COMPUTED,
                                     calc_states.RETRIEVING,
                                     calc_states.PARSING,
                                     ])

        parser.add_argument('-p', '--past-days', metavar='N',
                            help="add a filter to show only calculations created in the past N days",
                            action='store', type=int)
        parser.add_argument('-g', '--group', '--group-name',
                            metavar='GROUPNAME',
                            help="add a filter to show only calculations within a given group",
                            action='store', type=str)
        parser.add_argument('-G', '--group-pk', metavar='GROUPPK',
                            help="add a filter to show only calculations within a given group",
                            action='store', type=int)
        parser.add_argument('pks', type=int, nargs='*',
                            help="a list of calculations to show. If empty, all running calculations are shown. If non-empty, ignores the -p and -r options.")
        parser.add_argument('-a', '--all-states',
                            dest='all_states', action='store_true',
                            help="Overwrite manual set of states if present, and look for calculations in every possible state")
        parser.set_defaults(all_states=False)
        parser.add_argument('-A', '--all-users',
                            dest='all_users', action='store_true',
                            help="Show calculations for all users, rather than only for the current user")
        parser.set_defaults(all_users=False)
        parser.add_argument('-t', '--absolute-time',
                            dest='relative_ctime', action='store_false', default=True,
                            help="Print the absolute creation time, rather than the relative creation time")
        parser.add_argument('-l', '--limit',
                            type=int, default=None,
                            help='set a limit to the number of rows returned')
        parser.add_argument('-o', '--order-by',
                            choices=['id', 'ctime'],
                            default='ctime',
                            help='order the results')
        parser.add_argument('--project',
                            choices=(
                                    'pk', 'state', 'ctime', 'sched', 'computer',
                                    'type', 'description', 'label', 'uuid',
                                    'mtime', 'user'
                                ),
                            nargs='+',
                            default=get_property("verdishell.calculation_list"),
                            #('pk', 'ctime', 'state', 'sched', 'computer', 'type', 'label'),
                            help="Define the list of properties to show"
                        )

        args = list(args)
        parsed_args = parser.parse_args(args)

        capital_states = [i.upper() for i in parsed_args.states]
        parsed_args.states = capital_states

        if parsed_args.all_states:
            parsed_args.states = None

        C._list_calculations(
            states=parsed_args.states,
            past_days=parsed_args.past_days,
            pks=parsed_args.pks,
            all_users=parsed_args.all_users,
            group=parsed_args.group,
            group_pk=parsed_args.group_pk,
            relative_ctime=parsed_args.relative_ctime,
            # with_scheduler_state=parsed_args.with_scheduler_state,
            order_by=parsed_args.order_by,
            limit=parsed_args.limit,
            projections=parsed_args.project,
        )
Пример #13
0
     }
 },
 'handlers': {
     'console': {
         'level': 'DEBUG',
         'class': 'logging.StreamHandler',
         'formatter': 'halfverbose',
         'filters': ['testing']
     },
     'dblogger': {
         # setup.get_property takes the property from the config json file
         # The key used in the json, and the default value, are
         # specified in the _property_table inside aiida.common.setup
         # NOTE: To modify properties, use the 'verdi devel setproperty'
         #   command and similar ones (getproperty, describeproperties, ...)
         'level': setup.get_property('logging.db_loglevel'),
         'class': 'aiida.common.log.DBLogHandler',
     },
 },
 'loggers': {
     'aiida': {
         'handlers': ['console', 'dblogger'],
         'level': setup.get_property('logging.aiida_loglevel'),
         'propagate': False,
     },
     'paramiko': {
         'handlers': ['console'],
         'level': setup.get_property('logging.paramiko_loglevel'),
         'propagate': False,
     },
     'alembic': {
Пример #14
0
 def __init__(self, profile_name=None):
     super(DaemonClient, self).__init__(profile_name)
     self._SOCKET_DIRECTORY = None
     self._DAEMON_TIMEOUT = get_property('daemon.timeout')
Пример #15
0
         'level': 'ERROR',
         'filters': ['require_debug_false'],
         'class': 'django.utils.log.AdminEmailHandler'
     },
     'console': {
         'level': 'DEBUG',
         'class': 'logging.StreamHandler',
         'formatter': 'halfverbose',
     },
     'dblogger': {
         # get_property takes the property from the config json file
         # The key used in the json, and the default value, are
         # specified in the _property_table inside aiida.common.setup
         # NOTE: To modify properties, use the 'verdi devel setproperty'
         #   command and similar ones (getproperty, describeproperties, ...)
         'level': get_property('logging.db_loglevel'),
         'class': 'aiida.backends.djsite.utils.DBLogHandler',
     },
 },
 'loggers': {
     'django.request': {
         'handlers': ['mail_admins'],
         'level': 'ERROR',
         'propagate': True,
     },
     'aiida': {
         'handlers': ['console', 'dblogger'],
         'level': get_property('logging.aiida_loglevel'),
         'propagate': False,
     },
     # ~ 'celery': {
Пример #16
0
#                                                                         #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida_core #
# For further information on the license, see the LICENSE.txt file        #
# For further information please visit http://www.aiida.net               #
###########################################################################
"""`verdi calculation` commands."""
import os

import click

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.cmdline.params import arguments, options, types
from aiida.cmdline.utils import decorators, echo
from aiida.common.setup import get_property

LIST_CMDLINE_PROJECT_DEFAULT = get_property('verdishell.calculation_list')
LIST_CMDLINE_PROJECT_CHOICES = ('pk', 'state', 'ctime', 'job_state',
                                'calculation_state', 'scheduler_state',
                                'computer', 'type', 'description', 'label',
                                'uuid', 'mtime', 'user', 'sealed')


@verdi.group('calculation')
def verdi_calculation():
    """Inspect and manage calculations."""
    pass


@verdi_calculation.command('gotocomputer')
@arguments.CALCULATION(
    type=types.CalculationParamType(sub_classes=('aiida.calculations:job',
Пример #17
0
 def setUp(self):
     self.init_alemb_log_level = get_property('logging.alembic_loglevel')
     set_property('logging.alembic_loglevel',
                  logging.getLevelName(logging.ERROR))
     self.migrate_db_with_non_testing_migrations("base")