From 97e77880748c3d08701a48e27c21043612538682 Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Thu, 31 Aug 2023 11:55:43 +0000 Subject: [PATCH 1/4] CompResourcesWidget: do not raise when computer test fails. --- .../computational_resources.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/aiidalab_widgets_base/computational_resources.py b/aiidalab_widgets_base/computational_resources.py index d4837f575..6b24bd57a 100644 --- a/aiidalab_widgets_base/computational_resources.py +++ b/aiidalab_widgets_base/computational_resources.py @@ -927,11 +927,20 @@ def on_setup_computer_success(self, function): def test(self, _=None): with self._test_out: clear_output() - print( - subprocess.check_output( - ["verdi", "computer", "test", "--print-traceback", self.label.value] - ).decode("utf-8") - ) + try: + print( + subprocess.check_output( + [ + "verdi", + "computer", + "test", + "--print-traceback", + self.label.value, + ] + ).decode("utf-8") + ) + except subprocess.CalledProcessError as error: + print(error) def _reset(self): self.label.value = "" From 5935dccde85b576d3389febd399cea4590873681 Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Wed, 6 Sep 2023 15:55:45 +0000 Subject: [PATCH 2/4] review --- .../computational_resources.py | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/aiidalab_widgets_base/computational_resources.py b/aiidalab_widgets_base/computational_resources.py index 69fbd0031..73be8a4da 100644 --- a/aiidalab_widgets_base/computational_resources.py +++ b/aiidalab_widgets_base/computational_resources.py @@ -799,7 +799,7 @@ def observe_memory_per_machine(change): self.setup_button.on_click(self.on_setup_computer) test_button = ipw.Button(description="Test computer") test_button.on_click(self.test) - self._test_out = ipw.Output(layout=LAYOUT) + self._test_out = ipw.HTML(layout=LAYOUT) # Organize the widgets children = [ @@ -884,14 +884,18 @@ def _configure_computer_local(self, computer: orm.Computer, user: orm.User): def _run_callbacks_if_computer_exists(self, label): """Run things on an existing computer""" - try: - orm.Computer.objects.get(label=label) + if self._computer_exists(label): for function in self._on_setup_computer_success: function() + return True + return False + + def _computer_exists(self, label): + try: + orm.Computer.objects.get(label=label) except common.NotExistent: return False - else: - return True + return True def _validate_computer_settings(self): if self.label.value == "": # check computer label @@ -963,22 +967,24 @@ def on_setup_computer_success(self, function): self._on_setup_computer_success.append(function) def test(self, _=None): - with self._test_out: - clear_output() - try: - print( - subprocess.check_output( - [ - "verdi", - "computer", - "test", - "--print-traceback", - self.label.value, - ] - ).decode("utf-8") - ) - except subprocess.CalledProcessError as error: - print(error) + if self.label.value == "": + self._test_out = "Please specify the computer name (for AiiDA)" + return False + elif not self._computer_exists(self.label.value): + self._test_out = f"A computer called {self.label.value} does not exist." + return False + + process_result = subprocess.run( + ["verdi", "computer", "test", "--print-traceback", self.label.value], + capture_output=True, + ) + + if process_result.returncode == 0: + self._test_out = process_result.stdout.decode("utf-8") + return True + else: + self._test_out = process_result.stderr.decode("utf-8") + return False def _reset(self): self.label.value = "" From cd0ab24278da726e584157dcfbec4025aa7cbb95 Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Wed, 6 Sep 2023 19:25:17 +0000 Subject: [PATCH 3/4] Small updates --- aiidalab_widgets_base/computational_resources.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/aiidalab_widgets_base/computational_resources.py b/aiidalab_widgets_base/computational_resources.py index 73be8a4da..60b113a97 100644 --- a/aiidalab_widgets_base/computational_resources.py +++ b/aiidalab_widgets_base/computational_resources.py @@ -968,10 +968,12 @@ def on_setup_computer_success(self, function): def test(self, _=None): if self.label.value == "": - self._test_out = "Please specify the computer name (for AiiDA)" + self._test_out.value = "Please specify the computer name (for AiiDA)." return False elif not self._computer_exists(self.label.value): - self._test_out = f"A computer called {self.label.value} does not exist." + self._test_out.value = ( + f"A computer called {self.label.value} does not exist." + ) return False process_result = subprocess.run( @@ -980,10 +982,14 @@ def test(self, _=None): ) if process_result.returncode == 0: - self._test_out = process_result.stdout.decode("utf-8") + self._test_out.value = process_result.stdout.decode("utf-8").replace( + "\n", "
" + ) return True else: - self._test_out = process_result.stderr.decode("utf-8") + self._test_out.value = process_result.stderr.decode("utf-8").replace( + "\n", "
" + ) return False def _reset(self): From 91e0887f0aa4f46fe526c1d98ee713de675fef28 Mon Sep 17 00:00:00 2001 From: Aliaksandr Yakutovich Date: Thu, 7 Sep 2023 21:07:39 +0200 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Daniel Hollas --- aiidalab_widgets_base/computational_resources.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aiidalab_widgets_base/computational_resources.py b/aiidalab_widgets_base/computational_resources.py index 60b113a97..9e6f60a69 100644 --- a/aiidalab_widgets_base/computational_resources.py +++ b/aiidalab_widgets_base/computational_resources.py @@ -892,7 +892,7 @@ def _run_callbacks_if_computer_exists(self, label): def _computer_exists(self, label): try: - orm.Computer.objects.get(label=label) + orm.load_computer(label=label) except common.NotExistent: return False return True @@ -976,6 +976,7 @@ def test(self, _=None): ) return False + self._test_out.value = '' process_result = subprocess.run( ["verdi", "computer", "test", "--print-traceback", self.label.value], capture_output=True,