Skip to content

Commit d42b2ac

Browse files
authored
Improve error messages when trying to validate orca (#1135)
Display the error message or invalid help result at the end of the message
1 parent 72ad0e4 commit d42b2ac

File tree

1 file changed

+88
-31
lines changed

1 file changed

+88
-31
lines changed

Diff for: plotly/io/_orca.py

+88-31
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,8 @@ def validate_executable():
887887
>>> plotly.io.orca.config.save()
888888
889889
If you're still having trouble, feel free to ask for help on the forums at
890-
https://community.plot.ly/c/api/python"""
890+
https://community.plot.ly/c/api/python
891+
"""
891892

892893
# Try to find an executable
893894
# -------------------------
@@ -914,50 +915,96 @@ def validate_executable():
914915
# ---------------------------------------------------
915916
invalid_executable_msg = """
916917
The orca executable is required in order to export figures as static images,
917-
but the executable that was found at '{executable}' does not seem to be a
918-
valid plotly orca executable.
918+
but the executable that was found at '{executable}'
919+
does not seem to be a valid plotly orca executable. Please refer to the end of
920+
this message for details on what went wrong.
919921
920922
{instructions}""".format(
921923
executable=executable,
922924
instructions=install_location_instructions)
923925

924-
try:
925-
help_result = subprocess.check_output([executable, '--help'])
926-
except subprocess.CalledProcessError:
927-
raise ValueError(invalid_executable_msg)
926+
# ### Run with Popen so we get access to stdout and stderr
927+
p = subprocess.Popen(
928+
[executable, '--help'],
929+
stdout=subprocess.PIPE,
930+
stderr=subprocess.PIPE)
931+
932+
help_result, help_error = p.communicate()
933+
934+
if p.returncode != 0:
935+
err_msg = invalid_executable_msg + """
936+
Here is the error that was returned by the command
937+
$ {executable} --help
938+
939+
[Return code: {returncode}]
940+
{err_msg}
941+
""".format(executable=executable,
942+
err_msg=help_error.decode('utf-8'),
943+
returncode=p.returncode)
944+
945+
# Check for Linux without X installed.
946+
if (sys.platform.startswith('linux') and
947+
not os.environ.get('DISPLAY')):
948+
949+
err_msg += """\
950+
Note: When used on Linux, orca requires an X11 display server, but none was
951+
detected. Please install X11, or configure your system with Xvfb. See
952+
the orca README (https://github.com/plotly/orca) for instructions on using
953+
orca with Xvfb.
954+
"""
955+
raise ValueError(err_msg)
928956

929957
if not help_result:
930-
raise ValueError(invalid_executable_msg)
958+
raise ValueError(invalid_executable_msg + """
959+
The error encountered is that no output was returned by the command
960+
$ {executable} --help
961+
""".format(executable=executable))
931962

932963
if ("Plotly's image-exporting utilities" not in
933964
help_result.decode('utf-8')):
934-
raise ValueError(invalid_executable_msg)
965+
raise ValueError(invalid_executable_msg + """
966+
The error encountered is that unexpected output was returned by the command
967+
$ {executable} --help
968+
969+
{help_result}
970+
""".format(executable=executable, help_result=help_result))
935971

936972
# Get orca version
937973
# ----------------
938-
try:
939-
orca_version = subprocess.check_output([executable, '--version'])
940-
except subprocess.CalledProcessError:
941-
raise ValueError("""
942-
An error occurred while trying to get the version of the orca executable.
943-
Here is the command that plotly.py ran to request the version:
974+
# ### Run with Popen so we get access to stdout and stderr
975+
p = subprocess.Popen(
976+
[executable, '--version'],
977+
stdout=subprocess.PIPE,
978+
stderr=subprocess.PIPE)
944979

980+
version_result, version_error = p.communicate()
981+
982+
if p.returncode != 0:
983+
raise ValueError(invalid_executable_msg + """
984+
An error occurred while trying to get the version of the orca executable.
985+
Here is the command that plotly.py ran to request the version
945986
$ {executable} --version
946-
""".format(executable=executable))
947987
948-
if not orca_version:
949-
raise ValueError("""
950-
No version was reported by the orca executable.
988+
This command returned the following error:
989+
990+
[Return code: {returncode}]
991+
{err_msg}
992+
""".format(executable=executable,
993+
err_msg=version_error.decode('utf-8'),
994+
returncode=p.returncode))
951995

996+
if not version_result:
997+
raise ValueError(invalid_executable_msg + """
998+
The error encountered is that no version was reported by the orca executable.
952999
Here is the command that plotly.py ran to request the version:
9531000
9541001
$ {executable} --version
9551002
""".format(executable=executable))
9561003
else:
957-
orca_version = orca_version.decode()
1004+
version_result = version_result.decode()
9581005

9591006
status._props['executable'] = executable
960-
status._props['version'] = orca_version.strip()
1007+
status._props['version'] = version_result.strip()
9611008
status._props['state'] = 'validated'
9621009

9631010

@@ -1012,18 +1059,28 @@ def shutdown_server():
10121059
# process. This prevents any zombie processes from being
10131060
# left over, and it saves us from needing to write
10141061
# OS-specific process management code here.
1062+
10151063
parent = psutil.Process(orca_state['proc'].pid)
10161064
for child in parent.children(recursive=True):
1017-
child.terminate()
1018-
1019-
# Kill parent process
1020-
orca_state['proc'].terminate()
1021-
1022-
# Retrieve standard out and standard error to avoid warnings
1023-
output, err = orca_state['proc'].communicate()
1024-
1025-
# Wait for the process to shutdown
1026-
child_status = orca_state['proc'].wait()
1065+
try:
1066+
child.terminate()
1067+
except:
1068+
# We tried, move on
1069+
pass
1070+
1071+
try:
1072+
# Kill parent process
1073+
orca_state['proc'].terminate()
1074+
1075+
# Retrieve standard out and standard error to avoid
1076+
# warnings
1077+
output, err = orca_state['proc'].communicate()
1078+
1079+
# Wait for the process to shutdown
1080+
child_status = orca_state['proc'].wait()
1081+
except:
1082+
# We tried, move on
1083+
pass
10271084

10281085
# Update our internal process management state
10291086
orca_state['proc'] = None

0 commit comments

Comments
 (0)