Make sure that writing auth token from a file and reading it from a file is not fatal if a token file is corrupted or similar.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/2f02aece Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/2f02aece Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/2f02aece Branch: refs/heads/trunk Commit: 2f02aecebc4ba371b1239378961675a477ab9086 Parents: 43eeed6 Author: Tomaz Muraus <[email protected]> Authored: Sat Jul 30 13:02:30 2016 +0200 Committer: Tomaz Muraus <[email protected]> Committed: Sat Jul 30 13:02:30 2016 +0200 ---------------------------------------------------------------------- libcloud/common/google.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/2f02aece/libcloud/common/google.py ---------------------------------------------------------------------- diff --git a/libcloud/common/google.py b/libcloud/common/google.py index 68aa77c..4b896c6 100644 --- a/libcloud/common/google.py +++ b/libcloud/common/google.py @@ -677,7 +677,9 @@ class GoogleOAuth2Credential(object): with open(filename, 'r') as f: data = f.read() token = json.loads(data) - except IOError: + except (IOError, ValueError): + # Note: File related errors (IOError) and errors related to json + # parsing of the data (ValueError) are not fatal. pass return token @@ -686,11 +688,19 @@ class GoogleOAuth2Credential(object): Write token to credential file. Mocked in libcloud.test.common.google.GoogleTestCase. """ - filename = os.path.realpath(os.path.expanduser(self.credential_file)) - data = json.dumps(self.token) - with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, - int('600', 8)), 'w') as f: - f.write(data) + try: + filename = os.path.expanduser(self.credential_file) + filename = os.path.realpath(filename) + data = json.dumps(self.token) + write_flags = os.O_CREAT | os.O_WRONLY | os.O_TRUNC + with os.fdopen(os.open(filename, write_flags, + int('600', 8)), 'w') as f: + f.write(data) + except: + # Note: Failed to write (cache) token in a file is not fatal. It + # simply means degraded performance since we will need to acquire a + # new token each time script runs. + pass class GoogleBaseConnection(ConnectionUserAndKey, PollingConnection):
