aboutsummaryrefslogtreecommitdiff
path: root/oauth2client
diff options
context:
space:
mode:
authorPat Ferate <pferate@users.noreply.github.com>2016-08-01 09:49:02 -0700
committerJon Wayne Parrott <jonwayne@google.com>2016-08-01 09:49:02 -0700
commiteb019c2dadfbcefccfcaff4d58fc2112ed584825 (patch)
tree8d68cc57d33a47c8f73a395fb2562915280b1e3f /oauth2client
parentae73312942d3cf0e98f097dfbb40f136c2a7c463 (diff)
downloadoauth2client-eb019c2dadfbcefccfcaff4d58fc2112ed584825.tar.gz
Handle missing storage files (#576)
* Move `validate_file` to `oauth2client.util` * Warn user if storage file is missing * Raise an `IOError` exception if the given filename is a directory. * Raise an `IOError` exception if the given filename is a symbolic link. (Previously raised `CredentialsFileSymbolicLinkError`) * (test) Expanding single-letter variables * (test) `assertEqual(None, <obj>)` -> `assertIsNone(<obj>)` * (test) `assertNotEqual(None, <obj>)` -> `assertIsNotNone(<obj>)`
Diffstat (limited to 'oauth2client')
-rw-r--r--oauth2client/contrib/_fcntl_opener.py6
-rw-r--r--oauth2client/contrib/_win32_opener.py6
-rw-r--r--oauth2client/contrib/locked_file.py14
-rw-r--r--oauth2client/file.py18
-rw-r--r--oauth2client/util.py15
5 files changed, 28 insertions, 31 deletions
diff --git a/oauth2client/contrib/_fcntl_opener.py b/oauth2client/contrib/_fcntl_opener.py
index ae6c85b..c9777a9 100644
--- a/oauth2client/contrib/_fcntl_opener.py
+++ b/oauth2client/contrib/_fcntl_opener.py
@@ -16,6 +16,7 @@ import errno
import fcntl
import time
+from oauth2client import util
from oauth2client.contrib import locked_file
@@ -32,15 +33,14 @@ class _FcntlOpener(locked_file._Opener):
Raises:
AlreadyLockedException: if the lock is already acquired.
IOError: if the open fails.
- CredentialsFileSymbolicLinkError: if the file is a symbolic
- link.
+ IOError: if the file is a symbolic link.
"""
if self._locked:
raise locked_file.AlreadyLockedException(
'File {0} is already locked'.format(self._filename))
start_time = time.time()
- locked_file.validate_file(self._filename)
+ util.validate_file(self._filename)
try:
self._fh = open(self._filename, self._mode)
except IOError as e:
diff --git a/oauth2client/contrib/_win32_opener.py b/oauth2client/contrib/_win32_opener.py
index 34b4f48..6fa0196 100644
--- a/oauth2client/contrib/_win32_opener.py
+++ b/oauth2client/contrib/_win32_opener.py
@@ -19,6 +19,7 @@ import pywintypes
import win32con
import win32file
+from oauth2client import util
from oauth2client.contrib import locked_file
@@ -43,15 +44,14 @@ class _Win32Opener(locked_file._Opener):
Raises:
AlreadyLockedException: if the lock is already acquired.
IOError: if the open fails.
- CredentialsFileSymbolicLinkError: if the file is a symbolic
- link.
+ IOError: if the file is a symbolic link.
"""
if self._locked:
raise locked_file.AlreadyLockedException(
'File {0} is already locked'.format(self._filename))
start_time = time.time()
- locked_file.validate_file(self._filename)
+ util.validate_file(self._filename)
try:
self._fh = open(self._filename, self._mode)
except IOError as e:
diff --git a/oauth2client/contrib/locked_file.py b/oauth2client/contrib/locked_file.py
index 0d28ebb..9c880d7 100644
--- a/oauth2client/contrib/locked_file.py
+++ b/oauth2client/contrib/locked_file.py
@@ -45,21 +45,11 @@ __author__ = 'cache@google.com (David T McWherter)'
logger = logging.getLogger(__name__)
-class CredentialsFileSymbolicLinkError(Exception):
- """Credentials files must not be symbolic links."""
-
-
class AlreadyLockedException(Exception):
"""Trying to lock a file that has already been locked by the LockedFile."""
pass
-def validate_file(filename):
- if os.path.islink(filename):
- raise CredentialsFileSymbolicLinkError(
- 'File: {0} is a symbolic link.'.format(filename))
-
-
class _Opener(object):
"""Base class for different locking primitives."""
@@ -119,14 +109,14 @@ class _PosixOpener(_Opener):
Raises:
AlreadyLockedException: if the lock is already acquired.
IOError: if the open fails.
- CredentialsFileSymbolicLinkError if the file is a symbolic link.
+ IOError: if the file is a symbolic link.
"""
if self._locked:
raise AlreadyLockedException(
'File {0} is already locked'.format(self._filename))
self._locked = False
- validate_file(self._filename)
+ util.validate_file(self._filename)
try:
self._fh = open(self._filename, self._mode)
except IOError as e:
diff --git a/oauth2client/file.py b/oauth2client/file.py
index feede11..3d8e41a 100644
--- a/oauth2client/file.py
+++ b/oauth2client/file.py
@@ -22,15 +22,12 @@ import os
import threading
from oauth2client import client
+from oauth2client import util
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
-class CredentialsFileSymbolicLinkError(Exception):
- """Credentials files must not be symbolic links."""
-
-
class Storage(client.Storage):
"""Store and retrieve a single credential to and from a file."""
@@ -38,11 +35,6 @@ class Storage(client.Storage):
super(Storage, self).__init__(lock=threading.Lock())
self._filename = filename
- def _validate_file(self):
- if os.path.islink(self._filename):
- raise CredentialsFileSymbolicLinkError(
- 'File: {0} is a symbolic link.'.format(self._filename))
-
def locked_get(self):
"""Retrieve Credential from file.
@@ -50,10 +42,10 @@ class Storage(client.Storage):
oauth2client.client.Credentials
Raises:
- CredentialsFileSymbolicLinkError if the file is a symbolic link.
+ IOError if the file is a symbolic link.
"""
credentials = None
- self._validate_file()
+ util.validate_file(self._filename)
try:
f = open(self._filename, 'rb')
content = f.read()
@@ -89,10 +81,10 @@ class Storage(client.Storage):
credentials: Credentials, the credentials to store.
Raises:
- CredentialsFileSymbolicLinkError if the file is a symbolic link.
+ IOError if the file is a symbolic link.
"""
self._create_file_if_needed()
- self._validate_file()
+ util.validate_file(self._filename)
f = open(self._filename, 'w')
f.write(credentials.to_json())
f.close()
diff --git a/oauth2client/util.py b/oauth2client/util.py
index e3ba62b..18481c9 100644
--- a/oauth2client/util.py
+++ b/oauth2client/util.py
@@ -17,6 +17,8 @@
import functools
import inspect
import logging
+import os
+import warnings
import six
from six.moves import urllib
@@ -44,6 +46,10 @@ POSITIONAL_SET = frozenset([POSITIONAL_WARNING, POSITIONAL_EXCEPTION,
positional_parameters_enforcement = POSITIONAL_WARNING
+_SYM_LINK_MESSAGE = 'File: {0}: Is a symbolic link.'
+_IS_DIR_MESSAGE = '{0}: Is a directory'
+_MISSING_FILE_MESSAGE = 'Cannot access {0}: No such file or directory'
+
def positional(max_positional_args):
"""A decorator to declare that only the first N arguments my be positional.
@@ -204,3 +210,12 @@ def _add_query_parameter(url, name, value):
q[name] = value
parsed[4] = urllib.parse.urlencode(q)
return urllib.parse.urlunparse(parsed)
+
+
+def validate_file(filename):
+ if os.path.islink(filename):
+ raise IOError(_SYM_LINK_MESSAGE.format(filename))
+ elif os.path.isdir(filename):
+ raise IOError(_IS_DIR_MESSAGE.format(filename))
+ elif not os.path.isfile(filename):
+ warnings.warn(_MISSING_FILE_MESSAGE.format(filename))