Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import cookielib |
| 6 | import getpass |
| 7 | import imghdr |
| 8 | import os |
| 9 | import os.path |
| 10 | import sys |
| 11 | import urllib |
| 12 | import urllib2 |
| 13 | |
| 14 | # path to current script directory |
| 15 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 16 | cache_dir = os.path.realpath(os.path.join(script_dir, "..", "cache")) |
| 17 | if not os.path.exists(cache_dir): |
| 18 | os.mkdir(cache_dir) |
| 19 | |
| 20 | # path to the cookies file used for storing the login cookies. |
| 21 | cookies_file = os.path.join(cache_dir, "cookies") |
| 22 | |
| 23 | # path to folder where downloaded reports are cached |
| 24 | log_cache_dir = os.path.join(cache_dir, "reports") |
| 25 | if not os.path.exists(log_cache_dir): |
| 26 | os.mkdir(log_cache_dir) |
| 27 | |
| 28 | class FeedbackDownloader(): |
| 29 | def __init__(self): |
| 30 | # setup cookies file and url opener |
| 31 | self.cookies = cookielib.MozillaCookieJar(cookies_file) |
| 32 | if os.path.exists(cookies_file): |
| 33 | self.cookies.load() |
| 34 | cookie_processor = urllib2.HTTPCookieProcessor(self.cookies) |
| 35 | self.opener = urllib2.build_opener(cookie_processor) |
| 36 | self._Login() |
| 37 | |
| 38 | def _Login(self): |
| 39 | username = getpass.getuser() |
| 40 | |
| 41 | # check if already logged in |
| 42 | url = "https://login.corp.google.com/" |
| 43 | data = self.opener.open(url).read() |
| 44 | if username in data: |
| 45 | return |
| 46 | |
| 47 | # ask for credentials |
| 48 | print "Login to corp.google.com:" |
| 49 | password = getpass.getpass() |
| 50 | sys.stdout.write("OTP: ") |
| 51 | otp = sys.stdin.readline().strip() |
| 52 | |
| 53 | # execute login by posting userdata to login.corp.google.com |
| 54 | values = {'u': username, 'pw': password, 'otp': otp} |
| 55 | query = urllib.urlencode(values) |
| 56 | url = "https://login.corp.google.com/login?ssoformat=CORP_SSO" |
| 57 | result = self.opener.open(url, query).read() |
| 58 | |
| 59 | # check if the result displays error |
| 60 | if "error" in result >= 0: |
| 61 | print "Login failed" |
| 62 | exit(-1) |
| 63 | |
| 64 | # login was successful. save cookies to file to be reused later. |
| 65 | self.cookies.save() |
| 66 | |
| 67 | def DownloadFile(self, url): |
| 68 | try: |
| 69 | return self.opener.open(url).read() |
| 70 | except urllib2.URLError: |
| 71 | return None |
| 72 | |
| 73 | def _GetAndUpdateCachedFile(self, filename, url): |
| 74 | cached_filename = os.path.join(log_cache_dir, filename) |
| 75 | try: |
| 76 | return open(cached_filename).read() |
| 77 | except IOError: |
| 78 | data = self.DownloadFile(url) |
| 79 | if not data: |
| 80 | return None |
| 81 | # Cache the data on disk |
| 82 | file(cached_filename, "w").write(data) |
| 83 | return data |
| 84 | |
| 85 | def DownloadSystemLog(self, id): |
| 86 | # First download the report.zip file |
| 87 | logfile = urllib.quote("report-%s-system_logs.zip" % id) |
| 88 | query = urllib.urlencode({"id": id, "logIndex": "0"}) |
| 89 | url = ("https://feedback.corp.googleusercontent.com/binarydata/%s?%s" % |
| 90 | (logfile, query)) |
| 91 | report = self._GetAndUpdateCachedFile(id + ".zip", url) |
| 92 | if not report or (report[0:2] != "BZ" and report[0:2] != "PK"): |
| 93 | print "Report does not include log files at %s" % url |
| 94 | return None |
| 95 | return report |
| 96 | |
| 97 | def DownloadScreenshot(self, id): |
| 98 | query = urllib.urlencode({"id": id}) |
| 99 | url = "https://feedback.corp.googleusercontent.com/screenshot?%s" % query |
| 100 | image = self._GetAndUpdateCachedFile(id + ".jpg", url) |
| 101 | if not image or imghdr.what("", image) != 'jpeg': |
| 102 | print "No screenshots available at %s" % url |
| 103 | return None |
| 104 | return image |