Only search for Ghostscript Windows binary when needed

This commit is contained in:
Andrew Murray 2023-09-12 19:08:30 +10:00
parent e1889544cd
commit f225130ee5

View File

@ -38,7 +38,14 @@ split = re.compile(r"^%%([^:]*):[ \t]*(.*)[ \t]*$")
field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$") field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$")
gs_windows_binary = None gs_windows_binary = None
if sys.platform.startswith("win"):
def _get_windows_binary():
global gs_windows_binary
if gs_windows_binary is not None:
return gs_windows_binary
if not sys.platform.startswith("win"):
return
import shutil import shutil
for binary in ("gswin32c", "gswin64c", "gs"): for binary in ("gswin32c", "gswin64c", "gs"):
@ -47,19 +54,19 @@ if sys.platform.startswith("win"):
break break
else: else:
gs_windows_binary = False gs_windows_binary = False
return gs_windows_binary
def has_ghostscript(): def has_ghostscript():
if gs_windows_binary: gs_windows_binary = _get_windows_binary()
if gs_windows_binary is not None:
return gs_windows_binary is not False
try:
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
return True return True
if not sys.platform.startswith("win"): except OSError:
try: # No Ghostscript
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL) return False
return True
except OSError:
# No Ghostscript
pass
return False
def Ghostscript(tile, size, fp, scale=1, transparency=False): def Ghostscript(tile, size, fp, scale=1, transparency=False):
@ -132,17 +139,18 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
"showpage", "showpage",
] ]
if gs_windows_binary is not None: gs_windows_binary = _get_windows_binary()
if not gs_windows_binary: if gs_windows_binary is False:
try: try:
os.unlink(outfile) os.unlink(outfile)
if infile_temp: if infile_temp:
os.unlink(infile_temp) os.unlink(infile_temp)
except OSError: except OSError:
pass pass
msg = "Unable to locate Ghostscript on paths" msg = "Unable to locate Ghostscript on paths"
raise OSError(msg) raise OSError(msg)
elif gs_windows_binary is not None:
command[0] = gs_windows_binary command[0] = gs_windows_binary
# push data through Ghostscript # push data through Ghostscript