From dbf8b03cf922081b17cdd48aa807814978e70db3 Mon Sep 17 00:00:00 2001 From: superstar54 Date: Thu, 7 Nov 2024 07:38:21 +0000 Subject: [PATCH 1/7] Use try/except to catch error --- src/aiidalab_qe/setup/codes.py | 45 +++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/aiidalab_qe/setup/codes.py b/src/aiidalab_qe/setup/codes.py index c63a76bb7..d62b80438 100644 --- a/src/aiidalab_qe/setup/codes.py +++ b/src/aiidalab_qe/setup/codes.py @@ -1,4 +1,5 @@ import subprocess +import json from pathlib import Path from shutil import which @@ -44,18 +45,40 @@ def get_qe_env(): def qe_installed(): - import json - - env_exist = get_qe_env().exists() - proc = subprocess.run( - ["conda", "list", "-n", f"{get_qe_env().name}", "--json", "--full-name", "qe"], - check=True, - capture_output=True, - ) - - info = json.loads(str(proc.stdout.decode()))[0] + """Check if Quantum Espresso (QE) is installed in the specified conda environment. + + Returns: + bool: True if the environment exists and QE is installed; False otherwise. + """ + try: + # Verify if the specified conda environment exists + env_exist = get_qe_env().exists() + + if not env_exist: + return False + + # Run the conda list command to check for the QE package + proc = subprocess.run( + ["conda", "list", "-n", f"{get_qe_env().name}", "--json", "--full-name", "qe"], + check=True, + capture_output=True, + ) + + # Load and interpret the JSON output + info = json.loads(proc.stdout.decode()) + + # Check if 'qe' is listed in the environment + for package in info: + if package.get("name") == "qe": + return True + return False - return env_exist and "qe" == info["name"] + except subprocess.CalledProcessError as e: + # Handle cases where the conda list command fails + return False + except (json.JSONDecodeError, IndexError) as e: + # Handle cases where the JSON output is invalid or missing expected data + return False def install_qe(): From adb454ceb83ccb269ca176fb9d5c050dc09f941f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Nov 2024 07:38:44 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/aiidalab_qe/setup/codes.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/aiidalab_qe/setup/codes.py b/src/aiidalab_qe/setup/codes.py index d62b80438..802760198 100644 --- a/src/aiidalab_qe/setup/codes.py +++ b/src/aiidalab_qe/setup/codes.py @@ -1,5 +1,5 @@ -import subprocess import json +import subprocess from pathlib import Path from shutil import which @@ -46,37 +46,45 @@ def get_qe_env(): def qe_installed(): """Check if Quantum Espresso (QE) is installed in the specified conda environment. - + Returns: bool: True if the environment exists and QE is installed; False otherwise. """ try: # Verify if the specified conda environment exists env_exist = get_qe_env().exists() - + if not env_exist: return False - + # Run the conda list command to check for the QE package proc = subprocess.run( - ["conda", "list", "-n", f"{get_qe_env().name}", "--json", "--full-name", "qe"], + [ + "conda", + "list", + "-n", + f"{get_qe_env().name}", + "--json", + "--full-name", + "qe", + ], check=True, capture_output=True, ) - + # Load and interpret the JSON output info = json.loads(proc.stdout.decode()) - + # Check if 'qe' is listed in the environment for package in info: if package.get("name") == "qe": return True return False - except subprocess.CalledProcessError as e: + except subprocess.CalledProcessError: # Handle cases where the conda list command fails return False - except (json.JSONDecodeError, IndexError) as e: + except (json.JSONDecodeError, IndexError): # Handle cases where the JSON output is invalid or missing expected data return False From 2e98568a73db876f45ca76a6acdfe0c819c3bcf7 Mon Sep 17 00:00:00 2001 From: superstar54 Date: Thu, 7 Nov 2024 07:51:44 +0000 Subject: [PATCH 3/7] Fix ruff check --- src/aiidalab_qe/setup/codes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/aiidalab_qe/setup/codes.py b/src/aiidalab_qe/setup/codes.py index 802760198..db6286b09 100644 --- a/src/aiidalab_qe/setup/codes.py +++ b/src/aiidalab_qe/setup/codes.py @@ -79,7 +79,8 @@ def qe_installed(): for package in info: if package.get("name") == "qe": return True - return False + else: + return False except subprocess.CalledProcessError: # Handle cases where the conda list command fails From bf06b6d6c5cc9b89a4b4ad1b682f06f9c1285373 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Nov 2024 07:51:58 +0000 Subject: [PATCH 4/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/aiidalab_qe/setup/codes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/aiidalab_qe/setup/codes.py b/src/aiidalab_qe/setup/codes.py index db6286b09..802760198 100644 --- a/src/aiidalab_qe/setup/codes.py +++ b/src/aiidalab_qe/setup/codes.py @@ -79,8 +79,7 @@ def qe_installed(): for package in info: if package.get("name") == "qe": return True - else: - return False + return False except subprocess.CalledProcessError: # Handle cases where the conda list command fails From e2746e2b44f8bad9c564942a584a0acedce1df1d Mon Sep 17 00:00:00 2001 From: superstar54 Date: Thu, 7 Nov 2024 07:56:50 +0000 Subject: [PATCH 5/7] skip ruff check --- src/aiidalab_qe/setup/codes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aiidalab_qe/setup/codes.py b/src/aiidalab_qe/setup/codes.py index 802760198..af3db44a3 100644 --- a/src/aiidalab_qe/setup/codes.py +++ b/src/aiidalab_qe/setup/codes.py @@ -79,7 +79,7 @@ def qe_installed(): for package in info: if package.get("name") == "qe": return True - return False + return False # noqa: TRY300 except subprocess.CalledProcessError: # Handle cases where the conda list command fails From 4f6885545e4bd67bb429fd948ae25306dd50846c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Nov 2024 07:58:12 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/aiidalab_qe/setup/codes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aiidalab_qe/setup/codes.py b/src/aiidalab_qe/setup/codes.py index af3db44a3..c8744dc7c 100644 --- a/src/aiidalab_qe/setup/codes.py +++ b/src/aiidalab_qe/setup/codes.py @@ -79,7 +79,7 @@ def qe_installed(): for package in info: if package.get("name") == "qe": return True - return False # noqa: TRY300 + return False # noqa: TRY300 except subprocess.CalledProcessError: # Handle cases where the conda list command fails From 3b654d199078b2d66669cc87c50c1dca8415a6f8 Mon Sep 17 00:00:00 2001 From: superstar54 Date: Fri, 8 Nov 2024 16:37:57 +0000 Subject: [PATCH 7/7] Raise if the subprocess run fail --- src/aiidalab_qe/setup/codes.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/aiidalab_qe/setup/codes.py b/src/aiidalab_qe/setup/codes.py index c8744dc7c..984575c6c 100644 --- a/src/aiidalab_qe/setup/codes.py +++ b/src/aiidalab_qe/setup/codes.py @@ -80,13 +80,10 @@ def qe_installed(): if package.get("name") == "qe": return True return False # noqa: TRY300 - - except subprocess.CalledProcessError: - # Handle cases where the conda list command fails - return False - except (json.JSONDecodeError, IndexError): - # Handle cases where the JSON output is invalid or missing expected data - return False + except Exception as error: + raise RuntimeError( + "Failed to check if Quantum Espresso is installed." + ) from error def install_qe():