Ejemplo n.º 1
0
    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.
    """
        if _EnsureFileExists(cache_filename):
            locked_file = oauth2client.locked_file.LockedFile(
                cache_filename, 'r+b', 'rb')
            try:
                locked_file.open_and_lock()
                if locked_file.is_locked():
                    creds = {  # Credentials metadata dict.
                        'scopes': sorted(list(scopes)),
                        'svc_acct_name': self.__service_account_name
                    }
                    locked_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:
                locked_file.unlock_and_close()
Ejemplo n.º 2
0
    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.
        """
        if _EnsureFileExists(cache_filename):
            locked_file = oauth2client.locked_file.LockedFile(
                cache_filename, 'r+b', 'rb')
            try:
                locked_file.open_and_lock()
                if locked_file.is_locked():
                    creds = {  # Credentials metadata dict.
                        'scopes': sorted(list(scopes)),
                        'svc_acct_name': self.__service_account_name}
                    locked_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:
                locked_file.unlock_and_close()
Ejemplo n.º 3
0
  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}
    if _EnsureFileExists(cache_filename):
      locked_file = oauth2client.locked_file.LockedFile(
          cache_filename, 'r+b', 'rb')
      try:
        locked_file.open_and_lock()
        cached_creds_str = locked_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'] and
              (creds['scopes'] is None or
               creds['scopes'] == cached_creds['scopes'])):
            scopes = cached_creds['scopes']
      finally:
        locked_file.unlock_and_close()
    return scopes
Ejemplo n.º 4
0
    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
        }
        if _EnsureFileExists(cache_filename):
            locked_file = oauth2client.locked_file.LockedFile(
                cache_filename, 'r+b', 'rb')
            try:
                locked_file.open_and_lock()
                cached_creds_str = locked_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']
                            and
                        (creds['scopes'] is None
                         or creds['scopes'] == cached_creds['scopes'])):
                        scopes = cached_creds['scopes']
            finally:
                locked_file.unlock_and_close()
        return scopes
Ejemplo n.º 5
0
    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):
                locked_file = oauth2client.locked_file.LockedFile(cache_filename, "r+b", "rb")
                try:
                    locked_file.open_and_lock()
                    cached_creds_str = locked_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:
                    locked_file.unlock_and_close()
        return scopes