Add fancy locking to oauth2client.

Reviewed in http://codereview.appspot.com/4919049/
diff --git a/oauth2client/tools.py b/oauth2client/tools.py
index f04d4c8..dc779b4 100644
--- a/oauth2client/tools.py
+++ b/oauth2client/tools.py
@@ -25,31 +25,30 @@
 
 import BaseHTTPServer
 import gflags
-import logging
 import socket
 import sys
 
 from client import FlowExchangeError
 
 try:
-    from urlparse import parse_qsl
+  from urlparse import parse_qsl
 except ImportError:
-    from cgi import parse_qsl
+  from cgi import parse_qsl
 
 
 FLAGS = gflags.FLAGS
 
 gflags.DEFINE_boolean('auth_local_webserver', True,
-                     ('Run a local web server to handle redirects during '
+                      ('Run a local web server to handle redirects during '
                        'OAuth authorization.'))
 
 gflags.DEFINE_string('auth_host_name', 'localhost',
                      ('Host name to use when running a local web server to '
-                       'handle redirects during OAuth authorization.'))
+                      'handle redirects during OAuth authorization.'))
 
 gflags.DEFINE_multi_int('auth_host_port', [8080, 8090],
-                     ('Port to use when running a local web server to '
-                       'handle redirects during OAuth authorization.'))
+                        ('Port to use when running a local web server to '
+                         'handle redirects during OAuth authorization.'))
 
 
 class ClientRedirectServer(BaseHTTPServer.HTTPServer):
@@ -69,7 +68,7 @@
   """
 
   def do_GET(s):
-    """Handle a GET request
+    """Handle a GET request.
 
     Parses the query parameters and prints a message
     if the flow has completed. Note that we can't detect
@@ -106,8 +105,8 @@
     for port in FLAGS.auth_host_port:
       port_number = port
       try:
-        httpd = BaseHTTPServer.HTTPServer((FLAGS.auth_host_name, port),
-            ClientRedirectHandler)
+        httpd = ClientRedirectServer((FLAGS.auth_host_name, port),
+                                     ClientRedirectHandler)
       except socket.error, e:
         pass
       else:
@@ -126,10 +125,10 @@
   print
   if FLAGS.auth_local_webserver:
     print 'If your browser is on a different machine then exit and re-run this'
-    print 'application with the command-line parameter --noauth_local_webserver.'
+    print 'application with the command-line parameter '
+    print '--noauth_local_webserver.'
     print
 
-
   if FLAGS.auth_local_webserver:
     httpd.handle_request()
     if 'error' in httpd.query_params:
@@ -137,18 +136,15 @@
     if 'code' in httpd.query_params:
       code = httpd.query_params['code']
   else:
-    accepted = 'n'
-    while accepted.lower() == 'n':
-      accepted = raw_input('Have you authorized me? (y/n) ')
-    code = raw_input('What is the verification code? ').strip()
+    code = raw_input('Enter verification code: ').strip()
 
   try:
-    credentials = flow.step2_exchange(code)
-  except FlowExchangeError:
-    sys.exit('The authentication has failed.')
+    credential = flow.step2_exchange(code)
+  except FlowExchangeError, e:
+    sys.exit('Authentication has failed: %s' % e)
 
-  storage.put(credentials)
-  credentials.set_store(storage.put)
-  print "You have successfully authenticated."
+  storage.put(credential)
+  credential.set_store(storage)
+  print 'Authentication successful.'
 
-  return credentials
+  return credential