| 
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,24 @@ 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)  | 
 | 533 | +                    os.chmod(cache_dir, 0o777)  | 
 | 534 | +                persist_path = _global_cache_scope.persist_path  | 
 | 535 | +                last_shrink_at = _global_cache_scope.last_shrink_at  | 
 | 536 | +            except Exception as err:  | 
 | 537 | +                if isinstance(err, OSError) and err.errno == errno.EEXIST:  | 
 | 538 | +                    persist_path = _global_cache_scope.persist_path  | 
 | 539 | +                    last_shrink_at = _global_cache_scope.last_shrink_at  | 
 | 540 | +                else:  | 
 | 541 | +                    logging.warning(  | 
 | 542 | +                        'failed to create cache dir %s. error: %s', cache_dir, err)  | 
525 | 543 | 
 
  | 
526 | 544 |         shrink_interval = kwargs.get('shrink_interval', None)  | 
527 | 545 |         if shrink_interval is None:  | 
 | 
0 commit comments