summaryrefslogtreecommitdiffstats
path: root/codereview/proto_client.py
diff options
context:
space:
mode:
Diffstat (limited to 'codereview/proto_client.py')
-rwxr-xr-xcodereview/proto_client.py58
1 files changed, 39 insertions, 19 deletions
diff --git a/codereview/proto_client.py b/codereview/proto_client.py
index a51fcd06..b58cf6a8 100755
--- a/codereview/proto_client.py
+++ b/codereview/proto_client.py
@@ -20,6 +20,7 @@ import md5
20import os 20import os
21import random 21import random
22import socket 22import socket
23import sys
23import time 24import time
24import urllib 25import urllib
25import urllib2 26import urllib2
@@ -29,6 +30,38 @@ from froofle.protobuf.service import RpcChannel
29from froofle.protobuf.service import RpcController 30from froofle.protobuf.service import RpcController
30from need_retry_pb2 import RetryRequestLaterResponse; 31from need_retry_pb2 import RetryRequestLaterResponse;
31 32
33_cookie_jars = {}
34
35def _open_jar(path):
36 auth = False
37
38 if path is None:
39 c = cookielib.CookieJar()
40 else:
41 c = _cookie_jars.get(path)
42 if c is None:
43 c = cookielib.MozillaCookieJar(path)
44
45 if os.path.exists(path):
46 try:
47 c.load()
48 auth = True
49 except (cookielib.LoadError, IOError):
50 pass
51
52 if auth:
53 print >>sys.stderr, \
54 'Loaded authentication cookies from %s' \
55 % path
56 else:
57 os.close(os.open(path, os.O_CREAT, 0600))
58 os.chmod(path, 0600)
59 _cookie_jars[path] = c
60 else:
61 auth = True
62 return c, auth
63
64
32class ClientLoginError(urllib2.HTTPError): 65class ClientLoginError(urllib2.HTTPError):
33 """Raised to indicate an error authenticating with ClientLogin.""" 66 """Raised to indicate an error authenticating with ClientLogin."""
34 67
@@ -269,6 +302,9 @@ class HttpRpc(RpcChannel):
269 self._GetAuthCookie(auth_token) 302 self._GetAuthCookie(auth_token)
270 self.authenticated = True 303 self.authenticated = True
271 if self.cookie_file is not None: 304 if self.cookie_file is not None:
305 print >>sys.stderr, \
306 'Saving authentication cookies to %s' \
307 % self.cookie_file
272 self.cookie_jar.save() 308 self.cookie_jar.save()
273 return 309 return
274 310
@@ -337,24 +373,8 @@ class HttpRpc(RpcChannel):
337 opener.add_handler(urllib2.HTTPDefaultErrorHandler()) 373 opener.add_handler(urllib2.HTTPDefaultErrorHandler())
338 opener.add_handler(urllib2.HTTPSHandler()) 374 opener.add_handler(urllib2.HTTPSHandler())
339 opener.add_handler(urllib2.HTTPErrorProcessor()) 375 opener.add_handler(urllib2.HTTPErrorProcessor())
340 if self.cookie_file is not None: 376
341 self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file) 377 self.cookie_jar, \
342 if os.path.exists(self.cookie_file): 378 self.authenticated = _open_jar(self.cookie_file)
343 try:
344 self.cookie_jar.load()
345 self.authenticated = True
346 except (cookielib.LoadError, IOError):
347 # Failed to load cookies - just ignore them.
348 pass
349 else:
350 # Create an empty cookie file with mode 600
351 fd = os.open(self.cookie_file, os.O_CREAT, 0600)
352 os.close(fd)
353 # Always chmod the cookie file
354 os.chmod(self.cookie_file, 0600)
355 else:
356 # Don't save cookies across runs of update.py.
357 self.cookie_jar = cookielib.CookieJar()
358 opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar)) 379 opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar))
359 return opener 380 return opener
360