15
15
:license: BSD.
16
16
"""
17
17
from os import path , listdir
18
+ import os
18
19
import sys
20
+ import errno
19
21
import marshal
20
22
import tempfile
21
23
import fnmatch
@@ -189,7 +191,9 @@ class FileSystemBytecodeCache(BytecodeCache):
189
191
two arguments: The directory where the cache items are stored and a
190
192
pattern string that is used to build the filename.
191
193
192
- If no directory is specified the system temporary items folder is used.
194
+ If no directory is specified a default cache directory is selected. On
195
+ Windows the user's temp directory is used, on UNIX systems a directory
196
+ is created for the user in the system temp directory.
193
197
194
198
The pattern can be used to have multiple separate caches operate on the
195
199
same directory. The default pattern is ``'__jinja2_%s.cache'``. ``%s``
@@ -202,10 +206,31 @@ class FileSystemBytecodeCache(BytecodeCache):
202
206
203
207
def __init__ (self , directory = None , pattern = '__jinja2_%s.cache' ):
204
208
if directory is None :
205
- directory = tempfile . gettempdir ()
209
+ directory = self . _get_default_cache_dir ()
206
210
self .directory = directory
207
211
self .pattern = pattern
208
212
213
+ def _get_default_cache_dir (self ):
214
+ tmpdir = tempfile .gettempdir ()
215
+
216
+ # On windows the temporary directory is used specific unless
217
+ # explicitly forced otherwise. We can just use that.
218
+ if os .name == 'n' :
219
+ return tmpdir
220
+ if not hasattr (os , 'getuid' ):
221
+ raise RuntimeError ('Cannot determine safe temp directory. You '
222
+ 'need to explicitly provide one.' )
223
+
224
+ dirname = '_jinja2-cache-%d' % os .getuid ()
225
+ actual_dir = os .path .join (tmpdir , dirname )
226
+ try :
227
+ os .mkdir (actual_dir , 0700 )
228
+ except OSError as e :
229
+ if e .errno != errno .EEXIST :
230
+ raise
231
+
232
+ return actual_dir
233
+
209
234
def _get_cache_filename (self , bucket ):
210
235
return path .join (self .directory , self .pattern % bucket .key )
211
236
0 commit comments