Skip to content

Commit 1cc2681

Browse files
authored
ruff: Fix violations of ruff's S607 rule (#2888)
1 parent d8a9b0a commit 1cc2681

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

pygmt/clib/loading.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import ctypes
88
import os
9+
import shutil
910
import subprocess as sp
1011
import sys
1112
from ctypes.util import find_library
@@ -117,17 +118,17 @@ def clib_full_names(env=None):
117118

118119
# 2. Search for the library returned by command "gmt --show-library"
119120
# Use `str(Path(realpath))` to avoid mixture of separators "\\" and "/"
120-
try:
121-
libfullpath = Path(
122-
sp.check_output(["gmt", "--show-library"], encoding="utf-8").rstrip("\n")
123-
)
124-
if libfullpath.exists():
125-
yield str(libfullpath)
126-
except (FileNotFoundError, sp.CalledProcessError):
127-
# the 'gmt' executable is not found
128-
# the gmt library is not found
129-
# the 'gmt' executable is broken
130-
pass
121+
if (gmtbin := shutil.which("gmt")) is not None:
122+
try:
123+
libfullpath = Path(
124+
sp.check_output([gmtbin, "--show-library"], encoding="utf-8").rstrip(
125+
"\n"
126+
)
127+
)
128+
if libfullpath.exists():
129+
yield str(libfullpath)
130+
except sp.CalledProcessError: # the 'gmt' executable is broken
131+
pass
131132

132133
# 3. Search for DLLs in PATH by calling find_library() (Windows only)
133134
if sys.platform == "win32":

pygmt/helpers/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,12 @@ def launch_external_viewer(fname, waiting=0):
494494
# Open the file with the default viewer.
495495
# Fall back to the browser if can't recognize the operating system.
496496
os_name = sys.platform
497-
if os_name.startswith(("linux", "freebsd")) and shutil.which("xdg-open"):
498-
subprocess.run(["xdg-open", fname], check=False, **run_args)
497+
if os_name.startswith(("linux", "freebsd")) and (
498+
xdgopen := shutil.which("xdg-open")
499+
):
500+
subprocess.run([xdgopen, fname], check=False, **run_args)
499501
elif os_name == "darwin": # Darwin is macOS
500-
subprocess.run(["open", fname], check=False, **run_args)
502+
subprocess.run([shutil.which("open"), fname], check=False, **run_args)
501503
elif os_name == "win32":
502504
os.startfile(fname)
503505
else:

pygmt/tests/test_clib_loading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def fixture_gmt_lib_realpath():
224224
Return the real path of the GMT library.
225225
"""
226226
lib_realpath = subprocess.check_output(
227-
["gmt", "--show-library"], encoding="utf-8"
227+
[shutil.which("gmt"), "--show-library"], encoding="utf-8"
228228
).rstrip("\n")
229229
# On Windows, clib_full_names() returns paths with separator "\\",
230230
# but "gmt --show-library" returns paths with separator "/".

0 commit comments

Comments
 (0)