def test_code_list(self): # set up second code 'code2' from aiida.orm import Code try: code = Code.get_from_string('code2') except NotExistent: code = Code( input_plugin_name='simpleplugins.templatereplacer', remote_computer_exec=[self.comp, '/remote/abs/path'], ) code.label = 'code2' code.store() options = [ '-A', '-a', '-o', '--input-plugin=simpleplugins.arithmetic.add', '--computer={}'.format(self.comp.name) ] result = self.runner.invoke(code_list, options) self.assertIsNone(result.exception) self.assertTrue( str(self.code.pk) in result.output, 'PK of first code should be included') self.assertTrue('code2' not in result.output, 'label of second code should not be included') self.assertTrue('comp' in result.output, 'computer name should be included')
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()
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)
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()
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'))
def get_code(entry_point, computer_name='localhost'): """Get local code. Sets up code for given entry point on given computer. :param entry_point: Entry point of calculation plugin :param computer_name: Name of (local) computer :return: The code node :rtype: :py:class:`aiida.orm.Code` """ from aiida.orm import Code from aiida.common.exceptions import NotExistent computer = get_computer(computer_name) try: executable = executables[entry_point] except KeyError: raise KeyError( "Entry point {} not recognized. Allowed values: {}".format( entry_point, executables.keys())) try: code = Code.get_from_string('{}@{}'.format(executable, computer_name)) except NotExistent: path = get_path_to_executable(executable) code = Code( input_plugin_name=entry_point, remote_computer_exec=[computer, path], ) code.label = executable code.store() return code
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
def test_load_code(self): """Test the functionality of load_code.""" from aiida.orm import Code label = 'compy' code = Code() code.label = label code.set_remote_computer_exec((self.computer, '/x.x')) code.store() # Load through full label loaded_code = load_code(code.full_label) self.assertEquals(loaded_code.uuid, code.uuid) # Load through label loaded_code = load_code(code.label) self.assertEquals(loaded_code.uuid, code.uuid) # Load through uuid loaded_code = load_code(code.uuid) self.assertEquals(loaded_code.uuid, code.uuid) # Load through pk loaded_code = load_code(code.pk) self.assertEquals(loaded_code.uuid, code.uuid) # Load through full label explicitly loaded_code = load_code(label=code.full_label) self.assertEquals(loaded_code.uuid, code.uuid) # Load through label explicitly loaded_code = load_code(label=code.label) self.assertEquals(loaded_code.uuid, code.uuid) # Load through uuid explicitly loaded_code = load_code(uuid=code.uuid) self.assertEquals(loaded_code.uuid, code.uuid) # Load through pk explicitly loaded_code = load_code(pk=code.pk) self.assertEquals(loaded_code.uuid, code.uuid) # Load through partial uuid without a dash loaded_code = load_code(uuid=code.uuid[:8]) self.assertEquals(loaded_code.uuid, code.uuid) # Load through partial uuid including a dash loaded_code = load_code(uuid=code.uuid[:10]) self.assertEquals(loaded_code.uuid, code.uuid) with self.assertRaises(NotExistent): load_code('non-existent-uuid')
def test_remote(self): """Test remote code.""" import tempfile from aiida.orm import Code from aiida.common.exceptions import ValidationError with self.assertRaises(ValueError): # remote_computer_exec has length 2 but is not a list or tuple Code(remote_computer_exec='ab') # invalid code path with self.assertRaises(ValueError): Code(remote_computer_exec=(self.computer, '')) # Relative path is invalid for remote code with self.assertRaises(ValueError): Code(remote_computer_exec=(self.computer, 'subdir/run.exe')) # first argument should be a computer, not a string with self.assertRaises(TypeError): Code(remote_computer_exec=('localhost', '/bin/ls')) code = Code(remote_computer_exec=(self.computer, '/bin/ls')) with tempfile.NamedTemporaryFile(mode='w+') as fhandle: fhandle.write('#/bin/bash\n\necho test run\n') fhandle.flush() code.put_object_from_filelike(fhandle, 'test.sh') with self.assertRaises(ValidationError): # There are files inside code.store() # If there are no files, I can store code.delete_object('test.sh') code.store() self.assertEqual(code.get_remote_computer().pk, self.computer.pk) # pylint: disable=no-member self.assertEqual(code.get_remote_exec_path(), '/bin/ls') self.assertEqual(code.get_execname(), '/bin/ls') self.assertTrue(code.can_run_on(self.computer)) othercomputer = orm.Computer( label='another_localhost', hostname='localhost', transport_type='local', scheduler_type='pbspro', workdir='/tmp/aiida' ).store() self.assertFalse(code.can_run_on(othercomputer))
def get_code(entry_point, computer): """Get local code. Sets up code for given entry point on given computer. :param entry_point: Entry point of calculation plugin :param computer: (local) AiiDA computer :return: The code node :rtype: :py:class:`aiida.orm.Code` """ from aiida.orm import Code try: executable = executables[entry_point] except KeyError: raise KeyError( "Entry point '{}' not recognized. Allowed values: {}".format( entry_point, list(executables.keys()))) codes = Code.objects.find(filters={'label': executable}) # pylint: disable=no-member if codes: return codes[0] path = get_path_to_executable(executable) code = Code( input_plugin_name=entry_point, remote_computer_exec=[computer, path], ) code.label = executable return code.store()
def setUp(self): from aiida.orm import Computer, Code self.comp = Computer.get('comp') try: code = Code.get_from_string('code') except NotExistent: code = Code( input_plugin_name='simpleplugins.arithmetic.add', remote_computer_exec=[self.comp, '/remote/abs/path'], ) code.label = 'code' code.store() self.code = code self.runner = CliRunner()
def get_code(entry_point, executable, computer=aiida_localhost): """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 :return: The code node :rtype: :py:class:`aiida.orm.Code` """ from aiida.orm import Code codes = Code.objects.find(filters={'label': executable}) # pylint: disable=no-member if codes: return codes[0] 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 = executable return code.store()
def get_code(entry_point, computer): """Get local code. Sets up code for given entry point on given computer. :param entry_point: Entry point of calculation plugin :param computer: (local) AiiDA computer :return: The code node :rtype: :py:class:`aiida.orm.Code` """ from aiida.orm import Code, QueryBuilder, Computer try: executable = EXECUTABLE[entry_point] except KeyError: raise KeyError( "Entry point '{}' not recognized. Allowed values: {}".format( entry_point, list(EXECUTABLE.keys()))) qbuilder = QueryBuilder() qbuilder.append(Computer, filters={'id': computer.pk}) qbuilder.append(Code, with_computer=Computer, filters={'label': executable}) codes = [_[0] for _ in qbuilder.all()] if codes: return codes[0] path = get_path_to_executable(executable) code = Code( input_plugin_name=entry_point, remote_computer_exec=[computer, path], ) code.label = executable return code.store()
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()
def get_or_create_code(entry_point, computer, executable, exec_path=None): """Setup code on localhost computer""" if isinstance(computer, str): computer = Computer.objects.get(label=computer) try: code = Code.objects.get( # pylint: disable=no-member label=f'{entry_point}-{executable}-{computer.label}') except NotExistent: if exec_path is None: exec_path = get_path_to_executable(executable) code = Code(input_plugin_name=entry_point, remote_computer_exec=[computer, exec_path]) code.label = f'{entry_point}-{executable}-{computer.label}' code.store() return code
def get_code(entry_point, computer_name='localhost-test'): """Set up code on provided computer""" executable = EXECUTABLES[entry_point] try: codes = Code.objects.find(filters={'label': executable}) # pylint: disable=no-member code = codes[0] except IndexError: path = get_path_to_executable(executable) code = Code( input_plugin_name=entry_point, remote_computer_exec=[get_computer(computer_name), path], ) code.label = executable code.store() return code
def _get_mock_code(label: str, entry_point: str, data_dir_abspath: ty.Union[str, pathlib.Path], ignore_files: ty.Iterable[str] = ('_aiidasubmit.sh', )): """ Creates a mock AiiDA code. If the same inputs have been run previously, the results are copied over from the corresponding sub-directory of the ``data_dir_abspath``. Otherwise, the code is executed if an executable is specified in the configuration, or fails if it is not. Parameters ---------- label : Label by which the code is identified in the configuration file. entry_point : The AiiDA calculation entry point for the default calculation of the code. data_dir_abspath : Absolute path of the directory where the code results are stored. ignore_files : A list of files which are not copied to the results directory when the code is executed. """ from aiida.orm import Code # we want to set a custom prepend_text, which is why the code # can not be reused. code_label = f'mock-{label}-{uuid.uuid4()}' executable_path = shutil.which('aiida-mock-code') code = Code(input_plugin_name=entry_point, remote_computer_exec=[aiida_localhost, executable_path]) code.label = code_label code.set_prepend_text( inspect.cleandoc(f""" export {EnvKeys.LABEL.value}={label} export {EnvKeys.DATA_DIR.value}={data_dir_abspath} export {EnvKeys.EXECUTABLE_PATH.value}={config.get(label, '')} export {EnvKeys.IGNORE_FILES.value}={':'.join(ignore_files)} """)) code.store() return code
def get_code(entry_point, computer_name='localhost'): """Setup code on localhost computer""" from aiida.orm import Code from aiida.common.exceptions import NotExistent computer = get_computer(computer_name) executable = executables[entry_point] try: code = Code.get_from_string('{}@{}'.format(executable, computer_name)) except NotExistent: path = get_path_to_executable(executable) code = Code( input_plugin_name=entry_point, remote_computer_exec=[computer, path], ) code.label = executable code.store() return code
def test_code_local(self): import tempfile from aiida.orm import Code from aiida.common.exceptions import ValidationError code = Code(local_executable='test.sh') with self.assertRaises(ValidationError): # No file with name test.sh code.store() with tempfile.NamedTemporaryFile(mode='w+') as fhandle: fhandle.write('#/bin/bash\n\necho test run\n') fhandle.flush() code.put_object_from_filelike(fhandle, 'test.sh') code.store() self.assertTrue(code.can_run_on(self.computer)) self.assertTrue(code.get_local_executable(), 'test.sh') self.assertTrue(code.get_execname(), 'stest.sh')
def get_or_create_code(entry_point, computer, executable, exec_path=None): """Setup code on localhost computer""" from aiida.common import NotExistent from aiida.orm import Code, Computer if isinstance(computer, str): computer = Computer.objects.get(label=computer) try: code = Code.objects.get( label="{}-{}-{}".format(entry_point, executable, computer.label)) except NotExistent: if exec_path is None: exec_path = get_path_to_executable(executable) code = Code(input_plugin_name=entry_point, remote_computer_exec=[computer, exec_path]) code.label = "{}-{}-{}".format(entry_point, executable, computer.label) code.store() return code
def get_code(entry_point, computer_name='localhost-test'): """Set up code on provided computer""" from aiida.orm import Code from aiida.common import NotExistent computer = get_computer(computer_name) executable = executables[entry_point] try: code = Code.objects.get( # pylint: disable=no-member label='{}@{}'.format(executable, computer_name)) except NotExistent: path = get_path_to_executable(executable) code = Code( input_plugin_name=entry_point, remote_computer_exec=[computer, path], ) code.label = executable code.store() return code
def test_process_type_with_entry_point(self): """ For a process with a registered entry point, the process_type will be its formatted entry point string """ from aiida.orm import Code from aiida.plugins import CalculationFactory code = Code() code.set_remote_computer_exec((self.computer, '/bin/true')) code.store() parameters = orm.Dict(dict={}) template = orm.Dict(dict={}) options = { 'resources': { 'num_machines': 1, 'tot_num_mpiprocs': 1 }, 'max_wallclock_seconds': 1, } inputs = { 'code': code, 'parameters': parameters, 'template': template, 'metadata': { 'options': options, } } entry_point = 'templatereplacer' process_class = CalculationFactory(entry_point) process = process_class(inputs=inputs) expected_process_type = 'aiida.calculations:{}'.format(entry_point) self.assertEqual(process.node.process_type, expected_process_type) # Verify that process_class on the calculation node returns the original entry point class recovered_process = process.node.process_class self.assertEqual(recovered_process, process_class)
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"))
def get_code(entry_point, computer): """Get local code. Sets up code for given entry point on a given computer. :param entry_point: Entry point of calculation plugin :param computer: computer :return: The code node :rtype: :py:class:`aiida.orm.Code` """ from aiida.orm import Code from aiida.common.exceptions import NotExistent if os.environ.get(MOCK_GLOBAL_VAR, False): print("NB: using mock executable") exec_lookup = mock_executables else: exec_lookup = executables try: executable = exec_lookup[entry_point] except KeyError: raise KeyError("Entry point {} not recognized. Allowed values: {}" .format(entry_point, exec_lookup.keys())) try: code = Code.get_from_string('{}-{}@{}'.format(entry_point, executable, computer.get_name())) except NotExistent: path = get_path_to_executable(executable) code = Code( input_plugin_name=entry_point, remote_computer_exec=[computer, path], ) code.label = '{}-{}'.format(entry_point, executable) code.store() return code
def test_cif_structure_roundtrip(self): from aiida.tools.dbexporters.tcod import export_cif, export_values from aiida.orm import Code from aiida.orm import JobCalculation from aiida.orm.data.cif import CifData from aiida.orm.data.parameter import ParameterData from aiida.orm.data.upf import UpfData from aiida.orm.data.folder import FolderData from aiida.common.folders import SandboxFolder from aiida.common.datastructures import calc_states import tempfile with tempfile.NamedTemporaryFile() as f: f.write(''' data_test _cell_length_a 10 _cell_length_b 10 _cell_length_c 10 _cell_angle_alpha 90 _cell_angle_beta 90 _cell_angle_gamma 90 loop_ _atom_site_label _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z C 0 0 0 O 0.5 0.5 0.5 ''') f.flush() a = CifData(file=f.name) c = a._get_aiida_structure() c.store() pd = ParameterData() code = Code(local_executable='test.sh') with tempfile.NamedTemporaryFile() as f: f.write("#/bin/bash\n\necho test run\n") f.flush() code.add_path(f.name, 'test.sh') code.store() calc = JobCalculation(computer=self.computer) calc.set_resources({'num_machines': 1, 'num_mpiprocs_per_machine': 1}) calc.add_link_from(code, "code") calc.set_environment_variables({ 'PATH': '/dev/null', 'USER': '******' }) with tempfile.NamedTemporaryFile(prefix="Fe") as f: f.write("<UPF version=\"2.0.1\">\nelement=\"Fe\"\n") f.flush() upf = UpfData(file=f.name) upf.store() calc.add_link_from(upf, "upf") with tempfile.NamedTemporaryFile() as f: f.write("data_test") f.flush() cif = CifData(file=f.name) cif.store() calc.add_link_from(cif, "cif") calc.store() calc._set_state(calc_states.SUBMITTING) with SandboxFolder() as f: calc._store_raw_input_folder(f.abspath) fd = FolderData() with open( fd._get_folder_pathsubfolder.get_abs_path( calc._SCHED_OUTPUT_FILE), 'w') as f: f.write("standard output") f.flush() with open( fd._get_folder_pathsubfolder.get_abs_path( calc._SCHED_ERROR_FILE), 'w') as f: f.write("standard error") f.flush() fd.store() fd.add_link_from(calc, calc._get_linkname_retrieved(), LinkType.CREATE) pd.add_link_from(calc, "calc", LinkType.CREATE) pd.store() with self.assertRaises(ValueError): export_cif(c, parameters=pd) c.add_link_from(calc, "calc", LinkType.CREATE) export_cif(c, parameters=pd) values = export_values(c, parameters=pd) values = values['0'] self.assertEquals(values['_tcod_computation_environment'], ['PATH=/dev/null\nUSER=unknown']) self.assertEquals(values['_tcod_computation_command'], ['cd 1; ./_aiidasubmit.sh'])