3737split = re .compile (r"^%%([^:]*):[ \t]*(.*)[ \t]*$" )
3838field = re .compile (r"^%[%!\w]([^:]*)[ \t]*$" )
3939
40+ gs_binary = None
4041gs_windows_binary = None
41- if sys .platform .startswith ("win" ):
42- import shutil
43-
44- for binary in ("gswin32c" , "gswin64c" , "gs" ):
45- if shutil .which (binary ) is not None :
46- gs_windows_binary = binary
47- break
48- else :
49- gs_windows_binary = False
5042
5143
5244def has_ghostscript ():
53- if gs_windows_binary :
54- return True
55- if not sys .platform .startswith ("win" ):
56- try :
57- subprocess .check_call (["gs" , "--version" ], stdout = subprocess .DEVNULL )
58- return True
59- except OSError :
60- # No Ghostscript
61- pass
62- 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
6365
6466
6567def Ghostscript (tile , size , fp , scale = 1 , transparency = False ):
6668 """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 )
6773
6874 # Unpack decoder tile
6975 decoder , tile , offset , data = tile [0 ]
@@ -113,7 +119,7 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
113119
114120 # Build Ghostscript command
115121 command = [
116- "gs" ,
122+ gs_binary ,
117123 "-q" , # quiet mode
118124 "-g%dx%d" % size , # set output geometry (pixels)
119125 "-r%fx%f" % res , # set input DPI (dots per inch)
@@ -132,19 +138,6 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False):
132138 "showpage" ,
133139 ]
134140
135- if gs_windows_binary is not None :
136- if not gs_windows_binary :
137- try :
138- os .unlink (outfile )
139- if infile_temp :
140- os .unlink (infile_temp )
141- except OSError :
142- pass
143-
144- msg = "Unable to locate Ghostscript on paths"
145- raise OSError (msg )
146- command [0 ] = gs_windows_binary
147-
148141 # push data through Ghostscript
149142 try :
150143 startupinfo = None
0 commit comments