Example #1
0
    def create_code(self):
        """
        Create a code with the information contained in this class,
        BUT DOES NOT STORE IT.
        """
        import os.path
        from aiida.orm import Code as AiidaOrmCode

        if self.is_local:
            file_list = [
                os.path.realpath(os.path.join(self.folder_with_code, f))
                for f in os.listdir(self.folder_with_code)
            ]
            code = AiidaOrmCode(local_executable=self.local_rel_path,
                                files=file_list)
        else:
            code = AiidaOrmCode(remote_computer_exec=(self.computer,
                                                      self.remote_abs_path))

        code.label = self.label
        code.description = self.description
        code.set_input_plugin_name(self.input_plugin)
        code.set_prepend_text(self.prepend_text)
        code.set_append_text(self.append_text)

        return code
Example #2
0
def prepare_code(codename, codelocation, computername, workdir):
    """."""
    # first create or read computer
    comp = prepare_computer(computername, workdir)
    # now decide which code to add
    if codename == 'kkrhost':
        execname = 'kkr.x'
        pluginname = 'kkr.kkr'
    elif codename == 'voronoi':
        execname = 'voronoi.exe'
        pluginname = 'kkr.voro'
    elif codename == 'kkrimp':
        execname = 'kkrflex.exe'
        pluginname = 'kkr.kkrimp'
    else:
        raise ValueError('unknown codename')
    # then get code from database or create a new code
    from aiida.orm import Code
    from aiida.common.exceptions import NotExistent
    try:
        code = Code.get_from_string(codename + '@' + computername)
    except NotExistent as exception:
        code = Code()
        code.label = codename
        code.description = ''
        code.set_remote_computer_exec((comp, codelocation + execname))
        code.set_input_plugin_name(pluginname)
        if codename == 'voronoi':
            code.set_prepend_text('ln -s ' + codelocation +
                                  'ElementDataBase .')
        code.store()
Example #3
0
    def test_code_get_builder(self):
        """
        Test that the get_builder method of Code returns a builder
        where the code is already set.
        """
        from aiida.orm import Code

        code1 = Code()
        # This also sets the code as a remote code
        code1.set_remote_computer_exec((self.computer, '/bin/true'))
        code1.label = 'test_code1'
        code1.set_input_plugin_name('simpleplugins.templatereplacer')
        code1.store()

        # Check that I can get a builder
        builder = code1.get_builder()
        self.assertEquals(builder.code.pk, code1.pk)

        # Check that I can set the parameters
        builder.parameters = ParameterData(dict={})

        # Check that it complains for an unknown input
        with self.assertRaises(AttributeError):
            builder.unknown_parameter = 3

        # Check that it complains if the type is not the correct one
        # (for the simpleplugins.templatereplacer, it should be a
        # ParameterData)
        with self.assertRaises(ValueError):
            builder.parameters = Int(3)
Example #4
0
def mock_vasp(fresh_aiida_env, localhost):
    """Points to a mock-up of a VASP executable."""
    from aiida.orm import Code
    from aiida.orm.querybuilder import QueryBuilder
    query_builder = QueryBuilder()
    query_builder.append(Code, tag='code')
    query_builder.add_filter('code', {'label': {'==': 'mock-vasp'}})
    query_results = query_builder.all()
    if query_results:
        code = query_results[0][0]
    else:
        os_env = os.environ.copy()
        if not localhost.pk:
            localhost.store()
        # returns unicode
        mock_vasp_path = sp.check_output(['which', 'mock-vasp'],
                                         env=os_env,
                                         universal_newlines=True).strip()
        code = Code()
        code.label = 'mock-vasp'
        code.description = 'Mock VASP for tests'
        code.set_remote_computer_exec((localhost, mock_vasp_path))
        code.set_input_plugin_name('vasp.vasp')
        aiidapath = py_path.local(
            fresh_aiida_env._manager.root_dir).join('.aiida')
        code.set_prepend_text('export AIIDA_PATH={}'.format(aiidapath))

    return code
Example #5
0
 def _setup_code(self, change=None):  # pylint: disable=unused-argument
     """Setup an AiiDA code."""
     with self._setup_code_out:
         clear_output()
         if self.label is None:
             print("You did not specify code label")
             return
         if not self.exec_path:
             print("You did not specify absolute path to the executable")
             return
         if self.exists():
             print("Code {}@{} already exists".format(
                 self.label, self.computer.name))
             return
         code = Code(remote_computer_exec=(self.computer, self.exec_path))
         code.label = self.label
         code.description = self.description
         code.set_input_plugin_name(self.plugin)
         code.set_prepend_text(self.prepend_text)
         code.set_append_text(self.append_text)
         code.store()
         code.reveal()
         full_string = "{}@{}".format(self.label, self.computer.name)
         print(
             check_output(['verdi', 'code', 'show',
                           full_string]).decode('utf-8'))
Example #6
0
 def code_echo(self):
     """Fixture of a code that just echos"""
     code = Code()
     code.set_remote_computer_exec((self.localhost, "/bin/echo"))
     code.set_input_plugin_name("castep.castep")
     code.store()
     return code
Example #7
0
 def setUp(self):
     # Create some code nodes
     code = Code()
     code.set_remote_computer_exec((self.computer, '/bin/true'))
     code.label = self.CODE_LABEL
     code.set_input_plugin_name(self.INPUT_PLUGIN_NAME)
     code.store()
Example #8
0
 def code_mock_factory(self, overide=None):
     """Mock calculation, can overide by prepend path"""
     code = Code()
     exec_path = this_folder.parent.parent / 'utils/mock.py'
     code.set_remote_computer_exec((self.localhost, str(exec_path)))
     code.set_input_plugin_name('castep.castep')
     if overide:
         code.set_prepend_text('export MOCK_CALC={}'.format(overide))
     return code
Example #9
0
def vasp_code(localhost):
    """Fixture for a vasp code, the executable it points to does not exist"""
    from aiida.orm import Code
    localhost.store()
    code = Code()
    code.label = 'vasp'
    code.description = 'VASP code'
    code.set_remote_computer_exec((localhost, '/usr/local/bin/vasp'))
    code.set_input_plugin_name('vasp.vasp')
    return code
Example #10
0
def test_properties_code(test_computer):
    from aiida.orm import Code
    if not test_computer.pk:
        test_computer.store()
    code = Code()
    code.label = 'properties'
    code.description = 'CRYSTAL properties code'
    mock_exec = os.path.join(TEST_DIR, 'mock', 'crystal')
    code.set_remote_computer_exec((test_computer, mock_exec))
    code.set_input_plugin_name('crystal_dft.properties')
    return code
Example #11
0
def test_crystal_code(test_computer):
    from aiida.orm import Code
    if not test_computer.pk:
        test_computer.store()
    code = Code()
    code.label = 'crystal'
    code.description = 'CRYSTAL code'
    mock_exec = os.path.join(MOCK_DIR, 'crystal')
    code.set_remote_computer_exec((test_computer, mock_exec))
    code.set_input_plugin_name('crystal_dft.serial')
    return code
Example #12
0
 def _setup_code(self, _=None):
     """Setup an AiiDA code."""
     with self._setup_code_out:
         clear_output()
         if self.label is None:
             print("You did not specify code label.")
             return
         if not self.remote_abs_path:
             print("You did not specify absolute path to the executable.")
             return
         if not self.inp_computer.selected_computer:
             print(
                 "Please specify a computer that is configured in your AiiDA profile."
             )
             return False
         if not self.input_plugin:
             print(
                 "Please specify an input plugin that is installed in your AiiDA environment."
             )
             return False
         if self.exists():
             print(
                 f"Code {self.label}@{self.inp_computer.selected_computer.label} already exists."
             )
             return
         code = Code(remote_computer_exec=(
             self.inp_computer.selected_computer,
             self.remote_abs_path,
         ))
         code.label = self.label
         code.description = self.description
         code.set_input_plugin_name(self.input_plugin)
         code.set_prepend_text(self.prepend_text)
         code.set_append_text(self.append_text)
         code.store()
         code.reveal()
         full_string = f"{self.label}@{self.inp_computer.selected_computer.label}"
         print(
             check_output(["verdi", "code", "show",
                           full_string]).decode("utf-8"))
Example #13
0
    def new(self):
        """Build and return a new code instance (not stored)"""
        self.validate()

        from aiida.orm import Code

        # Will be used at the end to check if all keys are known (those that are not None)
        passed_keys = set([
            k for k in self._code_spec.keys() if self._code_spec[k] is not None
        ])
        used = set()

        if self._get_and_count('code_type',
                               used) == self.CodeType.STORE_AND_UPLOAD:
            file_list = [
                os.path.realpath(os.path.join(self.code_folder, f))
                for f in os.listdir(self._get_and_count('code_folder', used))
            ]
            code = Code(local_executable=self._get_and_count(
                'code_rel_path', used),
                        files=file_list)
        else:
            code = Code(remote_computer_exec=(
                self._get_and_count('computer', used),
                self._get_and_count('remote_abs_path', used)))

        code.label = self._get_and_count('label', used)
        code.description = self._get_and_count('description', used)
        code.set_input_plugin_name(
            self._get_and_count('input_plugin', used).name)
        code.set_prepend_text(self._get_and_count('prepend_text', used))
        code.set_append_text(self._get_and_count('append_text', used))

        # Complain if there are keys that are passed but not used
        if passed_keys - used:
            raise self.CodeValidationError(
                'Unknown parameters passed to the CodeBuilder: {}'.format(
                    ", ".join(sorted(passed_keys - used))))

        return code