|
1 | 1 | import abc
|
2 | 2 | import datetime
|
| 3 | +import errno |
3 | 4 | import itertools
|
4 | 5 | from collections import namedtuple
|
5 | 6 | import logging
|
@@ -300,7 +301,8 @@ def _lock_file_path(self):
|
300 | 301 | memo_cache={},
|
301 | 302 | persist_path=os.path.join(
|
302 | 303 | tempfile.gettempdir(),
|
303 |
| - 'qn-py-sdk-regions-cache.jsonl' |
| 304 | + 'qn-py-sdk', |
| 305 | + 'regions-cache.jsonl' |
304 | 306 | ),
|
305 | 307 | last_shrink_at=datetime.datetime.fromtimestamp(0),
|
306 | 308 | shrink_interval=datetime.timedelta(days=1),
|
@@ -520,8 +522,23 @@ def __init__(
|
520 | 522 | persist_path = kwargs.get('persist_path', None)
|
521 | 523 | last_shrink_at = datetime.datetime.fromtimestamp(0)
|
522 | 524 | if persist_path is None:
|
523 |
| - persist_path = _global_cache_scope.persist_path |
524 |
| - last_shrink_at = _global_cache_scope.last_shrink_at |
| 525 | + cache_dir = os.path.dirname(_global_cache_scope.persist_path) |
| 526 | + try: |
| 527 | + # make sure the cache dir is available for all users. |
| 528 | + # we can not use the '/tmp' dir directly on linux, |
| 529 | + # because the permission is 0o1777 |
| 530 | + if not os.path.exists(cache_dir): |
| 531 | + # os.makedirs have no exists_ok parameter in python 2.7 |
| 532 | + os.makedirs(cache_dir, mode=0o777) |
| 533 | + persist_path = _global_cache_scope.persist_path |
| 534 | + last_shrink_at = _global_cache_scope.last_shrink_at |
| 535 | + except Exception as err: |
| 536 | + if isinstance(err, OSError) and err.errno == errno.EEXIST: |
| 537 | + persist_path = _global_cache_scope.persist_path |
| 538 | + last_shrink_at = _global_cache_scope.last_shrink_at |
| 539 | + else: |
| 540 | + logging.warning( |
| 541 | + 'failed to create cache dir %s. error: %s', cache_dir, err) |
525 | 542 |
|
526 | 543 | shrink_interval = kwargs.get('shrink_interval', None)
|
527 | 544 | if shrink_interval is None:
|
|
0 commit comments