From a178d5c72bc8c668d3178aacdff2febce435bbc7 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 16 Jan 2022 21:48:01 +0100 Subject: [PATCH 1/7] Improve error handling in python script --- buildres/linux/jabrefHost.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/buildres/linux/jabrefHost.py b/buildres/linux/jabrefHost.py index bbe02decfc6..d89b558c65c 100755 --- a/buildres/linux/jabrefHost.py +++ b/buildres/linux/jabrefHost.py @@ -75,12 +75,8 @@ def add_jabref_entry(data): """Send string via cli as literal to preserve special characters""" cmd = str(JABREF_PATH).split() + ["--importBibtex", r"{}".format(data)] logging.info("Try to execute command {}".format(cmd)) - try: - response = subprocess.check_output(cmd, stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as exc: - logging.error("Failed to call JabRef: {} {}".format(exc.returncode, exc.output)) - else: - logging.info("Called JabRef and got: {}".format(response)) + response = subprocess.check_output(cmd, stderr=subprocess.STDOUT) + logging.info("Called JabRef and got: {}".format(response)) return response @@ -104,5 +100,9 @@ def add_jabref_entry(data): send_message({"message": "jarFound"}) else: entry = message["text"] - output = add_jabref_entry(entry) - send_message({"message": "ok", "output": str(output)}) + try: + output = add_jabref_entry(entry) + send_message({"message": "ok", "output": str(output)}) + except subprocess.CalledProcessError as exc: + logging.error("Failed to call JabRef: {} {}".format(exc.returncode, exc.output)) + send_message({"message": "error", "output": str(exc.output)}) From e1633ccebeb4596b255032ce750a5eb623f9c5da Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 23 Jan 2022 00:47:55 +0100 Subject: [PATCH 2/7] First try pwsh before powershell to run ps script --- buildres/windows/JabRefHost.bat | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/buildres/windows/JabRefHost.bat b/buildres/windows/JabRefHost.bat index d44d94f282c..8561a8dedd1 100644 --- a/buildres/windows/JabRefHost.bat +++ b/buildres/windows/JabRefHost.bat @@ -1,3 +1,18 @@ @echo off pushd %~dp0 -@powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File ".\JabRefHost.ps1" + +:: Test if pwsh exists +where /q pwsh.exe +if %errorlevel%==0 ( + @pwsh.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File ".\JabRefHost.ps1" +) else ( + :: If not, test if powershell exists + where /q powershell.exe + if %errorlevel%==0 ( + @powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File ".\JabRefHost.ps1" + ) else ( + echo "Could not find pwsh.exe or powershell.exe" 1>&2 + echo "Please install PowerShell and try again." 1>&2 + exit /b 1 + ) +) From 07a52bf40d89e730cfdc3d2df621fc6359b8aa15 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 23 Jan 2022 00:48:25 +0100 Subject: [PATCH 3/7] Improve error message in powershell script --- buildres/windows/JabRefHost.ps1 | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/buildres/windows/JabRefHost.ps1 b/buildres/windows/JabRefHost.ps1 index dc90a582921..bebcf34e2ba 100644 --- a/buildres/windows/JabRefHost.ps1 +++ b/buildres/windows/JabRefHost.ps1 @@ -28,15 +28,9 @@ try { } if (-not (Test-Path $jabRefExe)) { - $wshell = New-Object -ComObject Wscript.Shell - $popup = "Unable to locate '$jabRefExe'." - $wshell.Popup($popup,0,"JabRef", 0x0 + 0x30) - return + return Respond @{message="jarNotFound"; output="Unable to locate '$jabRefExe'."} } - #$wshell = New-Object -ComObject Wscript.Shell - #$wshell.Popup($message.Text,0,"JabRef", 0x0 + 0x30) - $messageText = $message.Text.replace("`n"," ").replace("`r"," ") $tempfile = New-TemporaryFile # WriteAllLines should write the file as UTF-8 without BOM @@ -44,10 +38,18 @@ try { [IO.File]::WriteAllLines($tempfile, $messageText) $output = & $jabRefExe -importToOpen $tempfile *>&1 Remove-Item $tempfile + # For debugging: uncomment the following lines to get the output of JabRef be displayed as a popup #$output = "$messageText" #$wshell = New-Object -ComObject Wscript.Shell #$wshell.Popup($output,0,"JabRef", 0x0 + 0x30) - return Respond @{message="ok";output="$output"} -} finally { + return Respond @{message="ok"; output="$output"} +} +catch { + $err = $PSItem + Respond @{message="error"; output="$($err.Exception.Message)"; stacktrace="$($err.ScriptStackTrace)"} + # Still print the error to the console so that it is picked up by the browser in addition + throw $err +} +finally { $reader.Dispose() } From 373b7f00692521d26db7878fcf2ea24042599be4 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 23 Jan 2022 14:11:04 +0100 Subject: [PATCH 4/7] Improve error handling in python script on macos --- buildres/mac/jabrefHost.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/buildres/mac/jabrefHost.py b/buildres/mac/jabrefHost.py index 51727f9d81c..331858ee661 100755 --- a/buildres/mac/jabrefHost.py +++ b/buildres/mac/jabrefHost.py @@ -2,6 +2,7 @@ import json import logging +import os import platform import shlex import shutil @@ -19,6 +20,7 @@ if not JABREF_PATH.exists(): logging.error("Could not determine JABREF_PATH") + send_message({"message": "error", "output": "Could not find JabRef. Please check that it is installed correctly.}) sys.exit(-1) logging_dir = Path.home() / ".mozilla/native-messaging-hosts/" @@ -61,14 +63,10 @@ def send_message(message): def add_jabref_entry(data): """Send string via cli as literal to preserve special characters""" - cmd = [str(JABREF_PATH), "--importBibtex", r"{}".format(data)] + cmd = str(JABREF_PATH).split() + ["--importBibtex", r"{}".format(data)] logging.info("Try to execute command {}".format(cmd)) - try: - response = subprocess.check_output(cmd, stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as exc: - logging.error("Failed to call JabRef: {} {}".format(exc.returncode, exc.output)) - else: - logging.info("Called JabRef and got: {}".format(response)) + response = subprocess.check_output(cmd, stderr=subprocess.STDOUT) + logging.info("Called JabRef and got: {}".format(response)) return response @@ -81,7 +79,7 @@ def add_jabref_entry(data): logging.info(str(message)) if "status" in message and message["status"] == "validate": - cmd = [JABREF_PATH, "--version"] + cmd = str(JABREF_PATH).split() + ["--version"] try: response = subprocess.check_output(cmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as exc: @@ -92,5 +90,9 @@ def add_jabref_entry(data): send_message({"message": "jarFound"}) else: entry = message["text"] - output = add_jabref_entry(entry) - send_message({"message": "ok", "output": str(output)}) + try: + output = add_jabref_entry(entry) + send_message({"message": "ok", "output": str(output)}) + except subprocess.CalledProcessError as exc: + logging.error("Failed to call JabRef: {} {}".format(exc.returncode, exc.output)) + send_message({"message": "error", "output": str(exc.output)}) From a47e30a18d8425a82687cb1b970293dd3bfe6ffa Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 23 Jan 2022 14:11:31 +0100 Subject: [PATCH 5/7] Send better error message back if JabRef couldn't be found --- buildres/linux/jabrefHost.py | 1 + 1 file changed, 1 insertion(+) diff --git a/buildres/linux/jabrefHost.py b/buildres/linux/jabrefHost.py index d89b558c65c..4bb1b8208fe 100755 --- a/buildres/linux/jabrefHost.py +++ b/buildres/linux/jabrefHost.py @@ -31,6 +31,7 @@ JABREF_PATH = "flatpak run org.jabref.jabref" else: logging.error("Could not determine JABREF_PATH") + send_message({"message": "error", "output": "Could not find JabRef. Please check that it is installed correctly.}) sys.exit(-1) logging_dir = Path.home() / ".mozilla/native-messaging-hosts/" From 626b2e1bfab1faeabf0404b3bfe01c6c0c4dfab6 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 23 Jan 2022 14:12:04 +0100 Subject: [PATCH 6/7] Fix typo --- buildres/linux/jabrefHost.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildres/linux/jabrefHost.py b/buildres/linux/jabrefHost.py index 4bb1b8208fe..aedf2d81580 100755 --- a/buildres/linux/jabrefHost.py +++ b/buildres/linux/jabrefHost.py @@ -31,7 +31,7 @@ JABREF_PATH = "flatpak run org.jabref.jabref" else: logging.error("Could not determine JABREF_PATH") - send_message({"message": "error", "output": "Could not find JabRef. Please check that it is installed correctly.}) + send_message({"message": "error", "output": "Could not find JabRef. Please check that it is installed correctly."}) sys.exit(-1) logging_dir = Path.home() / ".mozilla/native-messaging-hosts/" From 87ad419224d364ba6a51f4de9db3f2dd942d404b Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 23 Jan 2022 14:12:46 +0100 Subject: [PATCH 7/7] Fix typo --- buildres/mac/jabrefHost.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/buildres/mac/jabrefHost.py b/buildres/mac/jabrefHost.py index 331858ee661..21d6296e533 100755 --- a/buildres/mac/jabrefHost.py +++ b/buildres/mac/jabrefHost.py @@ -2,7 +2,6 @@ import json import logging -import os import platform import shlex import shutil @@ -20,7 +19,7 @@ if not JABREF_PATH.exists(): logging.error("Could not determine JABREF_PATH") - send_message({"message": "error", "output": "Could not find JabRef. Please check that it is installed correctly.}) + send_message({"message": "error", "output": "Could not find JabRef. Please check that it is installed correctly."}) sys.exit(-1) logging_dir = Path.home() / ".mozilla/native-messaging-hosts/"