예제 #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
예제 #2
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'))
예제 #3
0
    def get_code(entry_point,
                 executable,
                 computer=aiida_localhost,
                 label=None,
                 prepend_text=None,
                 append_text=None):
        """Get local code.

        Sets up code for given entry point on given computer.

        :param entry_point: Entry point of calculation plugin
        :param executable: name of executable; will be searched for in local system PATH.
        :param computer: (local) AiiDA computer
        :param prepend_text: a string of code that will be put in the scheduler script before the execution of the code.
        :param append_text: a string of code that will be put in the scheduler script after the execution of the code.
        :return: the `Code` either retrieved from the database or created if it did not yet exist.
        :rtype: :py:class:`aiida.orm.Code`
        """
        from aiida.common import exceptions
        from aiida.orm import Code, Computer, QueryBuilder

        if label is None:
            label = executable

        builder = QueryBuilder().append(Computer,
                                        filters={'uuid': computer.uuid},
                                        tag='computer')
        builder.append(Code,
                       filters={
                           'label': label,
                           'attributes.input_plugin': entry_point
                       },
                       with_computer='computer')

        try:
            code = builder.one()[0]
        except (exceptions.MultipleObjectsError, exceptions.NotExistent):
            code = None
        else:
            return code

        executable_path = shutil.which(executable)
        if not executable_path:
            raise ValueError(
                'The executable "{}" was not found in the $PATH.'.format(
                    executable))

        code = Code(input_plugin_name=entry_point,
                    remote_computer_exec=[computer, executable_path])
        code.label = label
        code.description = label

        if prepend_text is not None:
            code.set_prepend_text(prepend_text)

        if append_text is not None:
            code.set_append_text(append_text)

        return code.store()
예제 #4
0
def cstdn_code(computer):
    """
    Setup a new code object.
    """
    from aiida.orm import Code
    code = Code(remote_computer_exec=(computer, '/path/to/cstdn'))
    code.label = 'cstdncode'
    code.set_prepend_text('custodian code prepend')
    code.set_append_text('custodian code append')
    # do not store the code yet such that the default plugin can be set later
    yield code
예제 #5
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"))
예제 #6
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