def _WriteCacheFile(self, cache_filename, scopes): """Writes the credential metadata to the cache file. This does not save the credentials themselves (CredentialStore class optionally handles that after this class is initialized). Args: cache_filename: Cache filename to check. scopes: Scopes for the desired credentials. """ with cache_file_lock: if _EnsureFileExists(cache_filename): cache_file = locked_file.LockedFile(cache_filename, 'r+b', 'rb') try: cache_file.open_and_lock() if cache_file.is_locked(): creds = { # Credentials metadata dict. 'scopes': sorted(list(scopes)), 'svc_acct_name': self.__service_account_name } cache_file.file_handle().write( json.dumps(creds, encoding='ascii')) # If it's not locked, the locking process will # write the same data to the file, so just # continue. finally: cache_file.unlock_and_close()
def _make_one(self, opener_ctor_mock): opener_mock = mock.Mock() opener_ctor_mock.return_value = opener_mock return locked_file.LockedFile('a_file', 'r+', 'r', use_native_locking=False), opener_mock
def _CheckCacheFileForMatch(self, cache_filename, scopes): """Checks the cache file to see if it matches the given credentials. Args: cache_filename: Cache filename to check. scopes: Scopes for the desired credentials. Returns: List of scopes (if cache matches) or None. """ creds = { # Credentials metadata dict. 'scopes': sorted(list(scopes)) if scopes else None, 'svc_acct_name': self.__service_account_name, } with cache_file_lock: if _EnsureFileExists(cache_filename): cache_file = locked_file.LockedFile(cache_filename, 'r+b', 'rb') try: cache_file.open_and_lock() cached_creds_str = cache_file.file_handle().read() if cached_creds_str: # Cached credentials metadata dict. cached_creds = json.loads(cached_creds_str) if (creds['svc_acct_name'] == cached_creds['svc_acct_name']): if (creds['scopes'] in (None, cached_creds['scopes'])): scopes = cached_creds['scopes'] finally: cache_file.unlock_and_close() return scopes
def __init__(self, filename, warn_on_readonly=True): """Initialize the class. This will create the file if necessary. """ self._file = locked_file.LockedFile(filename, 'r+', 'r') self._thread_lock = threading.Lock() self._read_only = False self._warn_on_readonly = warn_on_readonly self._create_file_if_needed() # Cache of deserialized store. This is only valid after the # _MultiStore is locked or _refresh_data_cache is called. This is # of the form of: # # ((key, value), (key, value)...) -> OAuth2Credential # # If this is None, then the store hasn't been read yet. self._data = None
def test_ctor_native_posix_fallback(self, opener_mock): locked_file.LockedFile( 'a_file', 'r+', 'r', use_native_locking=True) opener_mock.assert_called_with('a_file', 'r+', 'r')
def test_ctor_native_fcntl(self): _fnctl_opener_mock = sys.modules['oauth2client.contrib._fcntl_opener'] locked_file.LockedFile( 'a_file', 'r+', 'r', use_native_locking=True) _fnctl_opener_mock._FcntlOpener.assert_called_with('a_file', 'r+', 'r')
def test_ctor_native_win32(self): _win32_opener_mock = sys.modules['oauth2client.contrib._win32_opener'] locked_file.LockedFile( 'a_file', 'r+', 'r', use_native_locking=True) _win32_opener_mock._Win32Opener.assert_called_with('a_file', 'r+', 'r')
def test_ctor_minimal(self, opener_mock): locked_file.LockedFile( 'a_file', 'r+', 'r', use_native_locking=False) opener_mock.assert_called_with('a_file', 'r+', 'r')