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 |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 6 | import datetime |
| 7 | import time |
Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 8 | import getpass |
| 9 | import imghdr |
| 10 | import os |
| 11 | import os.path |
Dennis Kempin | 13d948e | 2014-04-18 11:23:32 -0700 | [diff] [blame] | 12 | import re |
Charlie Mooney | d30322b | 2014-09-04 15:02:29 -0700 | [diff] [blame] | 13 | import subprocess |
Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 14 | import sys |
| 15 | import urllib |
| 16 | import urllib2 |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 17 | import StringIO |
| 18 | import multiprocessing |
| 19 | import random |
| 20 | import errno |
Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 21 | |
| 22 | # path to current script directory |
| 23 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 24 | cache_dir = os.path.realpath(os.path.join(script_dir, "..", "cache")) |
| 25 | if not os.path.exists(cache_dir): |
| 26 | os.mkdir(cache_dir) |
| 27 | |
| 28 | # path to the cookies file used for storing the login cookies. |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 29 | rpc_cred_file = os.path.join(cache_dir, "rpc_cred") |
Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 30 | |
| 31 | # path to folder where downloaded reports are cached |
| 32 | log_cache_dir = os.path.join(cache_dir, "reports") |
| 33 | if not os.path.exists(log_cache_dir): |
| 34 | os.mkdir(log_cache_dir) |
| 35 | |
| 36 | class FeedbackDownloader(): |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 37 | # Stubby command to download a feedback log: |
| 38 | # field_id - determines what data to download: |
| 39 | # 1: feedback log ID # |
| 40 | # 7: screenshot |
| 41 | # 8: system log |
| 42 | # resource_id - feedback log ID # |
| 43 | STUBBY_FILE_CMD = 'stubby --rpc_creds_file=' + rpc_cred_file + \ |
| 44 | ' call blade:feedback-export-api-prod ' \ |
| 45 | 'ReportService.Get ' \ |
| 46 | '\'result_mask <field <id:{field_id}>>, ' \ |
| 47 | 'resource_id: "{resource_id}"\'' |
| 48 | # Stubby command to download a list of feedback log IDs: |
| 49 | # product_id - 208 refers to ChromeOS |
| 50 | # submission_time_[start|end]_time_ms - feedback submission time range |
| 51 | # max_results - number of IDs to download |
| 52 | # token - page token |
| 53 | STUBBY_LIST_CMD = 'stubby --rpc_creds_file=' + rpc_cred_file + \ |
| 54 | ' call blade:feedback-export-api-prod ' \ |
| 55 | 'ReportService.List --proto2 ' \ |
| 56 | '\'result_mask <field <id:1>>, ' \ |
| 57 | 'product_id: "208", ' \ |
| 58 | 'submission_time_start_ms: 0, ' \ |
| 59 | 'submission_time_end_ms: {end_time}, ' \ |
| 60 | 'page_selection {{ max_results: {max_results} ' \ |
| 61 | 'token: "{token}" }}\'' |
| 62 | # Refer to go/touch-feedback-download for information on getting permission |
| 63 | # to download feedback logs. |
| 64 | GAIA_CMD = '/google/data/ro/projects/gaiamint/bin/get_mint --type=loas ' \ |
| 65 | '--text --scopes=40700 --endusercreds > ' + rpc_cred_file |
| 66 | SYSTEM_LOG_FIELD = 7 |
| 67 | SCREENSHOT_FIELD = 8 |
| 68 | sleep_sec = 4.0 |
| 69 | auth_lock = multiprocessing.Lock() |
Charlie Mooney | d30322b | 2014-09-04 15:02:29 -0700 | [diff] [blame] | 70 | |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 71 | def __init__(self, force_authenticate=False): |
| 72 | if force_authenticate: |
| 73 | self._Authenticate() |
Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 74 | |
Charlie Mooney | d30322b | 2014-09-04 15:02:29 -0700 | [diff] [blame] | 75 | def _OctetStreamToBinary(self, octetStream): |
| 76 | """ The zip files are returned in an octet-stream format that must |
| 77 | be decoded back into a binary. This function scans through the stream |
| 78 | and unescapes the special characters |
| 79 | """ |
| 80 | binary = '' |
| 81 | i = 0 |
| 82 | while i < len(octetStream): |
| 83 | if ord(octetStream[i]) is ord('\\'): |
| 84 | if re.match('\d\d\d', octetStream[i + 1:i + 4]): |
| 85 | binary += chr(int(octetStream[i + 1:i + 4], 8)) |
| 86 | i += 4 |
| 87 | else: |
| 88 | binary += octetStream[i:i + 2].decode("string-escape") |
| 89 | i += 2 |
| 90 | else: |
| 91 | binary += octetStream[i] |
| 92 | i += 1 |
| 93 | return binary |
| 94 | |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 95 | def _AuthenticateWithLock(self): |
| 96 | is_owner = self.auth_lock.acquire(False) |
| 97 | if is_owner: |
| 98 | self._Authenticate() |
| 99 | self.auth_lock.release() |
| 100 | else: |
| 101 | self.auth_lock.acquire() |
| 102 | self.auth_lock.release() |
| 103 | |
| 104 | def _StubbyCall(self, cmd): |
| 105 | if not os.path.exists(rpc_cred_file): |
| 106 | try: |
| 107 | os.mkdir(cache_dir) |
| 108 | except OSError as ex: |
| 109 | if ex.errno != errno.EEXIST: |
| 110 | raise |
| 111 | pass |
| 112 | self._AuthenticateWithLock() |
| 113 | while True: |
| 114 | process = subprocess.Popen(cmd, shell=True, |
| 115 | stdout=subprocess.PIPE, |
| 116 | stderr=subprocess.PIPE) |
| 117 | |
| 118 | output, errors = process.communicate() |
| 119 | errorcode = process.returncode |
| 120 | |
| 121 | if ('AUTH_FAIL' in errors or |
| 122 | 'CredentialsExpiredException' in output or |
| 123 | 'FORBIDDEN' in output): |
| 124 | self._AuthenticateWithLock() |
| 125 | continue |
| 126 | elif ('exceeds rate limit' in errors or |
| 127 | 'WaitUntilNonEmpty' in errors): |
| 128 | self._AuthenticateWithLock() |
| 129 | |
| 130 | sleep_time = self.sleep_sec + random.uniform(0.0, self.sleep_sec * 2) |
| 131 | time.sleep(sleep_time) |
| 132 | continue |
| 133 | elif errorcode != 0: |
| 134 | print errors |
| 135 | print 'default error' |
| 136 | print "An error (%d) occurred while downloading" % errorcode |
| 137 | sys.exit(errorcode) |
| 138 | return output |
| 139 | |
| 140 | def _DownloadAttachedFile(self, id, field): |
| 141 | cmd = FeedbackDownloader.STUBBY_FILE_CMD.format( |
| 142 | field_id=field, resource_id=id) |
| 143 | output = self._StubbyCall(cmd) |
| 144 | return output |
| 145 | |
| 146 | def _Authenticate(self): |
| 147 | cmd = FeedbackDownloader.GAIA_CMD |
Charlie Mooney | d30322b | 2014-09-04 15:02:29 -0700 | [diff] [blame] | 148 | process = subprocess.Popen(cmd, shell=True, |
| 149 | stdout=subprocess.PIPE, |
| 150 | stderr=subprocess.PIPE) |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 151 | print "Authenticating..." |
Charlie Mooney | d30322b | 2014-09-04 15:02:29 -0700 | [diff] [blame] | 152 | output, errors = process.communicate() |
| 153 | errorcode = process.returncode |
Charlie Mooney | d30322b | 2014-09-04 15:02:29 -0700 | [diff] [blame] | 154 | |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 155 | if errorcode != 0: |
| 156 | print errors |
| 157 | print "An error (%d) occurred while authenticating" % errorcode |
| 158 | if errorcode == 126: |
| 159 | print "You may need to run prodaccess" |
| 160 | sys.exit(errorcode) |
| 161 | return None |
| 162 | print "Done Authenticating" |
| 163 | |
| 164 | def DownloadIDs(self, num, end_time=None, page_token=''): |
| 165 | if not end_time: |
| 166 | dt = datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1) |
| 167 | end_time = (((dt.days * 24 * 60 * 60 + dt.seconds) * 1000) + |
| 168 | (dt.microseconds / 10)) |
| 169 | |
| 170 | cmd = FeedbackDownloader.STUBBY_LIST_CMD.format( |
| 171 | end_time=end_time, max_results=num, token=page_token) |
| 172 | output = self._StubbyCall(cmd) |
| 173 | |
| 174 | page_token = filter(lambda x: 'next_page_token' in x, output.split('\n'))[0] |
| 175 | page_token = page_token.split(" ")[-1][1:-1] |
| 176 | ids = filter(lambda x: 'id' in x, output.split('\n')) |
| 177 | ids = [x[7:-1] for x in ids] |
| 178 | return page_token, ids |
Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 179 | |
| 180 | def DownloadSystemLog(self, id): |
Charlie Mooney | d30322b | 2014-09-04 15:02:29 -0700 | [diff] [blame] | 181 | report = self._DownloadAttachedFile(id, |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 182 | FeedbackDownloader.SYSTEM_LOG_FIELD) |
| 183 | sleep_time = self.sleep_sec + random.uniform(0.0, self.sleep_sec * 2) |
| 184 | time.sleep(sleep_time) |
| 185 | data_line = None |
| 186 | system_log = None |
| 187 | for count, line in enumerate(StringIO.StringIO(report)): |
| 188 | if 'name: "system_logs.zip"' in line: |
| 189 | data_line = count + 2 |
| 190 | elif data_line and data_line == count: |
| 191 | system_log = re.search('data: "(.*)"\s*', line).group(1) |
Dennis Kempin | c6c981d | 2014-04-18 11:49:16 -0700 | [diff] [blame] | 192 | |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 193 | if not system_log or (system_log[0:2] != "BZ" and system_log[0:2] != "PK"): |
| 194 | print "Report " + id + " does not seem to include include log files..." |
Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 195 | return None |
Charlie Mooney | d30322b | 2014-09-04 15:02:29 -0700 | [diff] [blame] | 196 | |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 197 | return self._OctetStreamToBinary(system_log) |
Dennis Kempin | cee3761 | 2013-06-14 10:40:41 -0700 | [diff] [blame] | 198 | |
| 199 | def DownloadScreenshot(self, id): |
Sean O'Brien | fd204da | 2017-05-02 15:13:11 -0700 | [diff] [blame] | 200 | print "Downloading screenshot from %s..." % id |
| 201 | report = self._DownloadAttachedFile(id, |
| 202 | FeedbackDownloader.SCREENSHOT_FIELD) |
| 203 | data_line = None |
| 204 | screenshot = None |
| 205 | for count, line in enumerate(StringIO.StringIO(report)): |
| 206 | if 'screenshot <' in line: |
| 207 | data_line = count + 2 |
| 208 | elif data_line and data_line == count: |
| 209 | screenshot = re.search('content: "(.*)"\s*', line).group(1) |
| 210 | |
| 211 | if not screenshot: |
| 212 | print "Report does not seem to include include a screenshot..." |
| 213 | return None |
| 214 | |
| 215 | return self._OctetStreamToBinary(screenshot) |