3737split = re .compile (r"^%%([^:]*):[ \t]*(.*)[ \t]*$" )
3838field = re .compile (r"^%[%!\w]([^:]*)[ \t]*$" )
3939
40+ gs_binary = None
4041gs_windows_binary = None
4142
4243
43- def _get_windows_binary ():
44- global gs_windows_binary
45- if gs_windows_binary is not None :
46- return gs_windows_binary
47- if not sys .platform .startswith ("win" ):
48- return
49- import shutil
50-
51- for binary in ("gswin32c" , "gswin64c" , "gs" ):
52- if shutil .which (binary ) is not None :
53- gs_windows_binary = binary
54- break
55- else :
56- gs_windows_binary = False
57- return gs_windows_binary
58-
59-
6044def has_ghostscript ():
61- gs_windows_binary = _get_windows_binary ()
62- if gs_windows_binary is not None :
63- return gs_windows_binary is not False
64- try :
65- subprocess .check_call (["gs" , "--version" ], stdout = subprocess .DEVNULL )
66- return True
67- except OSError :
68- # No Ghostscript
69- return False
45+ global gs_binary , gs_windows_binary
46+ if gs_binary is None :
47+ if sys .platform .startswith ("win" ):
48+ if gs_windows_binary is None :
49+ import shutil
50+
51+ for binary in ("gswin32c" , "gswin64c" , "gs" ):
52+ if shutil .which (binary ) is not None :
53+ gs_windows_binary = binary
54+ break
55+ else :
56+ gs_windows_binary = False
57+ gs_binary = gs_windows_binary
58+ else :
59+ try :
60+ subprocess .check_call (["gs" , "--version" ], stdout = subprocess .DEVNULL )
61+ gs_binary = "gs"
62+ except OSError :
63+ gs_binary = False
64+ return gs_binary is not False
7065
7166
7267def Ghostscript (tile , size , fp , scale = 1 , transparency = False ):
7368 """Render an image using Ghostscript"""
69+ global gs_binary
70+ if not has_ghostscript ():
71+ msg = "Unable to locate Ghostscript on paths"
72+ raise OSError (msg )
7473
7574 # Unpack decoder tile
7675 decoder , tile , offset , data = tile [0 ]
@@ -120,7 +119,7 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
120119
121120 # Build Ghostscript command
122121 command = [
123- "gs" ,
122+ gs_binary ,
124123 "-q" , # quiet mode
125124 "-g%dx%d" % size , # set output geometry (pixels)
126125 "-r%fx%f" % res , # set input DPI (dots per inch)
@@ -139,20 +138,6 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
139138 "showpage" ,
140139 ]
141140
142- gs_windows_binary = _get_windows_binary ()
143- if gs_windows_binary is False :
144- try :
145- os .unlink (outfile )
146- if infile_temp :
147- os .unlink (infile_temp )
148- except OSError :
149- pass
150-
151- msg = "Unable to locate Ghostscript on paths"
152- raise OSError (msg )
153- elif gs_windows_binary is not None :
154- command [0 ] = gs_windows_binary
155-
156141 # push data through Ghostscript
157142 try :
158143 startupinfo = None
0 commit comments