OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 # | 3 # |
4 # Copyright 2007 Google Inc. | 4 # Copyright 2007 Google Inc. |
5 # | 5 # |
6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
7 # you may not use this file except in compliance with the License. | 7 # you may not use this file except in compliance with the License. |
8 # You may obtain a copy of the License at | 8 # You may obtain a copy of the License at |
9 # | 9 # |
10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 139 |
140 """ | 140 """ |
141 last_email_file_name = os.path.expanduser("~/.last_codereview_email_address") | 141 last_email_file_name = os.path.expanduser("~/.last_codereview_email_address") |
142 last_email = "" | 142 last_email = "" |
143 if os.path.exists(last_email_file_name): | 143 if os.path.exists(last_email_file_name): |
144 try: | 144 try: |
145 last_email_file = open(last_email_file_name, "r") | 145 last_email_file = open(last_email_file_name, "r") |
146 last_email = last_email_file.readline().strip("\n") | 146 last_email = last_email_file.readline().strip("\n") |
147 last_email_file.close() | 147 last_email_file.close() |
148 prompt += " [%s]" % last_email | 148 prompt += " [%s]" % last_email |
149 except IOError, e: | 149 except IOError as e: |
150 pass | 150 pass |
151 email = raw_input(prompt + ": ").strip() | 151 email = raw_input(prompt + ": ").strip() |
152 if email: | 152 if email: |
153 try: | 153 try: |
154 last_email_file = open(last_email_file_name, "w") | 154 last_email_file = open(last_email_file_name, "w") |
155 last_email_file.write(email) | 155 last_email_file.write(email) |
156 last_email_file.close() | 156 last_email_file.close() |
157 except IOError, e: | 157 except IOError as e: |
158 pass | 158 pass |
159 else: | 159 else: |
160 email = last_email | 160 email = last_email |
161 return email | 161 return email |
162 | 162 |
163 | 163 |
164 def StatusUpdate(msg): | 164 def StatusUpdate(msg): |
165 """Print a status message to stdout. | 165 """Print a status message to stdout. |
166 | 166 |
167 If 'verbosity' is greater than 0, print the message. | 167 If 'verbosity' is greater than 0, print the message. |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 "source": "rietveld-codereview-upload", | 281 "source": "rietveld-codereview-upload", |
282 "accountType": account_type, | 282 "accountType": account_type, |
283 }), | 283 }), |
284 ) | 284 ) |
285 try: | 285 try: |
286 response = self.opener.open(req) | 286 response = self.opener.open(req) |
287 response_body = response.read() | 287 response_body = response.read() |
288 response_dict = dict(x.split("=") | 288 response_dict = dict(x.split("=") |
289 for x in response_body.split("\n") if x) | 289 for x in response_body.split("\n") if x) |
290 return response_dict["Auth"] | 290 return response_dict["Auth"] |
291 except urllib2.HTTPError, e: | 291 except urllib2.HTTPError as e: |
292 if e.code == 403: | 292 if e.code == 403: |
293 body = e.read() | 293 body = e.read() |
294 response_dict = dict(x.split("=", 1) for x in body.split("\n") if x) | 294 response_dict = dict(x.split("=", 1) for x in body.split("\n") if x) |
295 raise ClientLoginError(req.get_full_url(), e.code, e.msg, | 295 raise ClientLoginError(req.get_full_url(), e.code, e.msg, |
296 e.headers, response_dict) | 296 e.headers, response_dict) |
297 else: | 297 else: |
298 raise | 298 raise |
299 | 299 |
300 def _GetAuthCookie(self, auth_token): | 300 def _GetAuthCookie(self, auth_token): |
301 """Fetches authentication cookies for an authentication token. | 301 """Fetches authentication cookies for an authentication token. |
302 | 302 |
303 Args: | 303 Args: |
304 auth_token: The authentication token returned by ClientLogin. | 304 auth_token: The authentication token returned by ClientLogin. |
305 | 305 |
306 Raises: | 306 Raises: |
307 HTTPError: If there was an error fetching the authentication cookies. | 307 HTTPError: If there was an error fetching the authentication cookies. |
308 """ | 308 """ |
309 # This is a dummy value to allow us to identify when we're successful. | 309 # This is a dummy value to allow us to identify when we're successful. |
310 continue_location = "http://localhost/" | 310 continue_location = "http://localhost/" |
311 args = {"continue": continue_location, "auth": auth_token} | 311 args = {"continue": continue_location, "auth": auth_token} |
312 req = self._CreateRequest("%s/_ah/login?%s" % | 312 req = self._CreateRequest("%s/_ah/login?%s" % |
313 (self.host, urllib.urlencode(args))) | 313 (self.host, urllib.urlencode(args))) |
314 try: | 314 try: |
315 response = self.opener.open(req) | 315 response = self.opener.open(req) |
316 except urllib2.HTTPError, e: | 316 except urllib2.HTTPError as e: |
317 response = e | 317 response = e |
318 if (response.code != 302 or | 318 if (response.code != 302 or |
319 response.info()["location"] != continue_location): | 319 response.info()["location"] != continue_location): |
320 raise urllib2.HTTPError(req.get_full_url(), response.code, response.msg, | 320 raise urllib2.HTTPError(req.get_full_url(), response.code, response.msg, |
321 response.headers, response.fp) | 321 response.headers, response.fp) |
322 self.authenticated = True | 322 self.authenticated = True |
323 | 323 |
324 def _Authenticate(self, force_refresh): | 324 def _Authenticate(self, force_refresh): |
325 """Authenticates the user. | 325 """Authenticates the user. |
326 | 326 |
(...skipping 23 matching lines...) Expand all Loading... |
350 error_map = { | 350 error_map = { |
351 "badauth": "BadAuthentication", | 351 "badauth": "BadAuthentication", |
352 "cr": "CaptchaRequired", | 352 "cr": "CaptchaRequired", |
353 "adel": "AccountDeleted", | 353 "adel": "AccountDeleted", |
354 "adis": "AccountDisabled", | 354 "adis": "AccountDisabled", |
355 "sdis": "ServiceDisabled", | 355 "sdis": "ServiceDisabled", |
356 "ire": "ServiceUnavailable", | 356 "ire": "ServiceUnavailable", |
357 } | 357 } |
358 auth_token = self._GetAuthToken(credentials[0], credentials[1], | 358 auth_token = self._GetAuthToken(credentials[0], credentials[1], |
359 internal=True) | 359 internal=True) |
360 except ClientLoginError, exc: | 360 except ClientLoginError as exc: |
361 e = exc | 361 e = exc |
362 if e: | 362 if e: |
363 print('', file=sys.stderr) | 363 print('', file=sys.stderr) |
364 error_message = e.reason | 364 error_message = e.reason |
365 if error_map: | 365 if error_map: |
366 error_message = error_map.get(error_message, error_message) | 366 error_message = error_map.get(error_message, error_message) |
367 if error_message == "BadAuthentication": | 367 if error_message == "BadAuthentication": |
368 if e.info == "InvalidSecondFactor": | 368 if e.info == "InvalidSecondFactor": |
369 print >> sys.stderr, ( | 369 print >> sys.stderr, ( |
370 "Use an application-specific password instead " | 370 "Use an application-specific password instead " |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 req = self._CreateRequest(url=url, data=payload) | 443 req = self._CreateRequest(url=url, data=payload) |
444 req.add_header("Content-Type", content_type) | 444 req.add_header("Content-Type", content_type) |
445 if extra_headers: | 445 if extra_headers: |
446 for header, value in extra_headers.items(): | 446 for header, value in extra_headers.items(): |
447 req.add_header(header, value) | 447 req.add_header(header, value) |
448 try: | 448 try: |
449 f = self.opener.open(req, timeout=70) | 449 f = self.opener.open(req, timeout=70) |
450 response = f.read() | 450 response = f.read() |
451 f.close() | 451 f.close() |
452 return response | 452 return response |
453 except urllib2.HTTPError, e: | 453 except urllib2.HTTPError as e: |
454 if tries > 3: | 454 if tries > 3: |
455 raise | 455 raise |
456 elif e.code in (302, 401, 403): | 456 elif e.code in (302, 401, 403): |
457 if not self.auth_function: | 457 if not self.auth_function: |
458 raise | 458 raise |
459 # Already tried force refresh, didn't help -> give up with error. | 459 # Already tried force refresh, didn't help -> give up with error. |
460 if auth_attempted: | 460 if auth_attempted: |
461 raise auth.AuthenticationError( | 461 raise auth.AuthenticationError( |
462 'Access to %s is denied (server returned HTTP %d).' | 462 'Access to %s is denied (server returned HTTP %d).' |
463 % (self.host, e.code)) | 463 % (self.host, e.code)) |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
992 ("is_current", str(not is_base)), | 992 ("is_current", str(not is_base)), |
993 ] | 993 ] |
994 if file_too_large: | 994 if file_too_large: |
995 form_fields.append(("file_too_large", "1")) | 995 form_fields.append(("file_too_large", "1")) |
996 if options.email: | 996 if options.email: |
997 form_fields.append(("user", options.email)) | 997 form_fields.append(("user", options.email)) |
998 ctype, body = EncodeMultipartFormData(form_fields, | 998 ctype, body = EncodeMultipartFormData(form_fields, |
999 [("data", filename, content)]) | 999 [("data", filename, content)]) |
1000 try: | 1000 try: |
1001 response_body = rpc_server.Send(url, body, content_type=ctype) | 1001 response_body = rpc_server.Send(url, body, content_type=ctype) |
1002 except urllib2.HTTPError, e: | 1002 except urllib2.HTTPError as e: |
1003 response_body = ("Failed to upload file for %s. Got %d status code." % | 1003 response_body = ("Failed to upload file for %s. Got %d status code." % |
1004 (filename, e.code)) | 1004 (filename, e.code)) |
1005 | 1005 |
1006 if not response_body.startswith("OK"): | 1006 if not response_body.startswith("OK"): |
1007 StatusUpdate(" --> %s" % response_body) | 1007 StatusUpdate(" --> %s" % response_body) |
1008 sys.exit(1) | 1008 sys.exit(1) |
1009 | 1009 |
1010 return result | 1010 return result |
1011 | 1011 |
1012 patches = dict() | 1012 patches = dict() |
(...skipping 1049 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2062 def UploadFile(filename, data): | 2062 def UploadFile(filename, data): |
2063 form_fields = [("filename", filename)] | 2063 form_fields = [("filename", filename)] |
2064 if not options.download_base: | 2064 if not options.download_base: |
2065 form_fields.append(("content_upload", "1")) | 2065 form_fields.append(("content_upload", "1")) |
2066 files = [("data", "data.diff", data)] | 2066 files = [("data", "data.diff", data)] |
2067 ctype, body = EncodeMultipartFormData(form_fields, files) | 2067 ctype, body = EncodeMultipartFormData(form_fields, files) |
2068 url = "/%d/upload_patch/%d" % (int(issue), int(patchset)) | 2068 url = "/%d/upload_patch/%d" % (int(issue), int(patchset)) |
2069 | 2069 |
2070 try: | 2070 try: |
2071 response_body = rpc_server.Send(url, body, content_type=ctype) | 2071 response_body = rpc_server.Send(url, body, content_type=ctype) |
2072 except urllib2.HTTPError, e: | 2072 except urllib2.HTTPError as e: |
2073 response_body = ("Failed to upload patch for %s. Got %d status code." % | 2073 response_body = ("Failed to upload patch for %s. Got %d status code." % |
2074 (filename, e.code)) | 2074 (filename, e.code)) |
2075 | 2075 |
2076 lines = response_body.splitlines() | 2076 lines = response_body.splitlines() |
2077 if not lines or lines[0] != "OK": | 2077 if not lines or lines[0] != "OK": |
2078 StatusUpdate(" --> %s" % response_body) | 2078 StatusUpdate(" --> %s" % response_body) |
2079 sys.exit(1) | 2079 sys.exit(1) |
2080 return ("Uploaded patch for " + filename, [lines[1], filename]) | 2080 return ("Uploaded patch for " + filename, [lines[1], filename]) |
2081 | 2081 |
2082 threads = [] | 2082 threads = [] |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2126 def RunDetectCommand(vcs_type, command): | 2126 def RunDetectCommand(vcs_type, command): |
2127 """Helper to detect VCS by executing command. | 2127 """Helper to detect VCS by executing command. |
2128 | 2128 |
2129 Returns: | 2129 Returns: |
2130 A pair (vcs, output) or None. Throws exception on error. | 2130 A pair (vcs, output) or None. Throws exception on error. |
2131 """ | 2131 """ |
2132 try: | 2132 try: |
2133 out, returncode = RunShellWithReturnCode(command) | 2133 out, returncode = RunShellWithReturnCode(command) |
2134 if returncode == 0: | 2134 if returncode == 0: |
2135 return (vcs_type, out.strip()) | 2135 return (vcs_type, out.strip()) |
2136 except OSError, (errcode, message): | 2136 except OSError as e: |
2137 if errcode != errno.ENOENT: # command not found code | 2137 if e.errno != errno.ENOENT: # command not found code |
2138 raise | 2138 raise |
2139 | 2139 |
2140 # Mercurial has a command to get the base directory of a repository | 2140 # Mercurial has a command to get the base directory of a repository |
2141 # Try running it, but don't die if we don't have hg installed. | 2141 # Try running it, but don't die if we don't have hg installed. |
2142 # NOTE: we try Mercurial first as it can sit on top of an SVN working copy. | 2142 # NOTE: we try Mercurial first as it can sit on top of an SVN working copy. |
2143 res = RunDetectCommand(VCS_MERCURIAL, ["hg", "root"]) | 2143 res = RunDetectCommand(VCS_MERCURIAL, ["hg", "root"]) |
2144 if res != None: | 2144 if res != None: |
2145 return res | 2145 return res |
2146 | 2146 |
2147 # Subversion from 1.7 has a single centralized .svn folder | 2147 # Subversion from 1.7 has a single centralized .svn folder |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2546 print | 2546 print |
2547 StatusUpdate("Interrupted.") | 2547 StatusUpdate("Interrupted.") |
2548 sys.exit(1) | 2548 sys.exit(1) |
2549 except auth.AuthenticationError as e: | 2549 except auth.AuthenticationError as e: |
2550 print(e, file=sys.stderr) | 2550 print(e, file=sys.stderr) |
2551 sys.exit(1) | 2551 sys.exit(1) |
2552 | 2552 |
2553 | 2553 |
2554 if __name__ == "__main__": | 2554 if __name__ == "__main__": |
2555 main() | 2555 main() |
OLD | NEW |