Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [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 | from gzip import GzipFile |
| 6 | from StringIO import StringIO |
| 7 | import base64 |
| 8 | import bz2 |
| 9 | import cookielib |
| 10 | import getpass |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 11 | import imghdr |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 12 | import os |
| 13 | import os.path |
| 14 | import re |
| 15 | import subprocess |
| 16 | import sys |
| 17 | import tarfile |
| 18 | import tempfile |
| 19 | import urllib2 |
Dennis Kempin | ecd9a0c | 2013-03-27 16:03:46 -0700 | [diff] [blame] | 20 | import zipfile |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 21 | |
| 22 | # path to current script directory |
| 23 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 24 | |
| 25 | # path to the cookies file used for storing the login cookies. |
Dennis Kempin | 136b732 | 2013-02-06 13:20:57 -0800 | [diff] [blame] | 26 | cookies_file = os.path.join(script_dir, "mtedit.cookies") |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 27 | |
Andrew de los Reyes | 1de4dcd | 2013-05-14 11:45:32 -0700 | [diff] [blame] | 28 | log_cache_dir = os.path.join(script_dir, "reports") |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 29 | |
WeiNan-Peter, Wen | 294408a | 2013-06-13 10:48:39 -0400 | [diff] [blame] | 30 | # path for temporary screenshot |
| 31 | screenshot_filepath = "extension/screenshot.jpg" |
| 32 | |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 33 | def Log(source, options): |
| 34 | """ |
| 35 | Returns a log object containg activity, evdev and system logs. |
| 36 | source can be either an ip address to a device from which to |
| 37 | download, a url pointing to a feedback report to pull or a filename. |
| 38 | """ |
| 39 | if os.path.exists(source): |
Dennis Kempin | 91e96df | 2013-03-29 14:21:42 -0700 | [diff] [blame] | 40 | if source.endswith(".zip") or source.endswith(".bz2"): |
WeiNan-Peter, Wen | 9aab26a | 2013-05-13 09:32:29 -0400 | [diff] [blame] | 41 | return FeedbackLog(source, options) |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 42 | return FileLog(source, options) |
| 43 | else: |
| 44 | match = re.search("Report/([0-9]+)", source) |
| 45 | if match: |
| 46 | return FeedbackLog(match.group(1), options) |
| 47 | else: |
| 48 | # todo add regex and error message |
| 49 | return DeviceLog(source, options) |
| 50 | |
| 51 | |
| 52 | class AbstractLog(object): |
| 53 | """ |
WeiNan-Peter, Wen | 9aab26a | 2013-05-13 09:32:29 -0400 | [diff] [blame] | 54 | A log file consists of the activity log, the evdev log, a system log, and |
| 55 | possibly a screenshot, which can be saved to disk all together. |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 56 | """ |
| 57 | def SaveAs(self, filename): |
| 58 | open(filename, "w").write(self.activity) |
| 59 | if self.evdev: |
| 60 | open(filename + ".evdev", "w").write(self.evdev) |
| 61 | if self.system: |
| 62 | open(filename + ".system", "w").write(self.system) |
WeiNan-Peter, Wen | 9aab26a | 2013-05-13 09:32:29 -0400 | [diff] [blame] | 63 | if self.image: |
| 64 | open(filename + ".jpg", "w").write(self.image) |
| 65 | |
| 66 | def CleanUp(self): |
WeiNan-Peter, Wen | 294408a | 2013-06-13 10:48:39 -0400 | [diff] [blame] | 67 | if os.path.exists(screenshot_filepath): |
| 68 | os.remove(screenshot_filepath) |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 69 | |
| 70 | |
| 71 | class FileLog(AbstractLog): |
| 72 | """ |
| 73 | Loads log from file. Does not contain evdev or system logs. |
| 74 | """ |
| 75 | def __init__(self, filename, options): |
| 76 | self.activity = open(filename).read() |
| 77 | self.evdev = "" |
| 78 | self.system = "" |
WeiNan-Peter, Wen | 9aab26a | 2013-05-13 09:32:29 -0400 | [diff] [blame] | 79 | self.image = None |
Dennis Kempin | 7b78327 | 2013-04-01 15:06:35 -0700 | [diff] [blame] | 80 | if options.evdev: |
| 81 | self.evdev = open(options.evdev).read() |
| 82 | elif os.path.exists(filename + ".evdev"): |
Dennis Kempin | fa6597c | 2013-02-20 14:05:19 -0800 | [diff] [blame] | 83 | self.evdev = open(filename + ".evdev").read() |
| 84 | if os.path.exists(filename + ".system"): |
| 85 | self.system = open(filename + ".system").read() |
WeiNan-Peter, Wen | 9aab26a | 2013-05-13 09:32:29 -0400 | [diff] [blame] | 86 | if os.path.exists(filename + ".jpg"): |
| 87 | self.image = open(filename + ".jpg").read() |
WeiNan-Peter, Wen | 294408a | 2013-06-13 10:48:39 -0400 | [diff] [blame] | 88 | file(screenshot_filepath, "w").write(self.image) |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 89 | |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 90 | class FeedbackDownloader(): |
| 91 | def __init__(self): |
| 92 | # setup cookies file and url opener |
| 93 | self.cookies = cookielib.MozillaCookieJar(cookies_file) |
| 94 | if os.path.exists(cookies_file): |
| 95 | self.cookies.load() |
| 96 | cookie_processor = urllib2.HTTPCookieProcessor(self.cookies) |
| 97 | self.opener = urllib2.build_opener(cookie_processor) |
| 98 | self._Login() |
| 99 | |
| 100 | def _Login(self): |
| 101 | username = getpass.getuser() |
| 102 | |
| 103 | # check if already logged in |
| 104 | url = "https://login.corp.google.com/" |
| 105 | data = self.opener.open(url).read() |
| 106 | if username in data: |
| 107 | return |
| 108 | |
| 109 | # ask for credentials |
| 110 | print "Login to corp.google.com:" |
| 111 | password = getpass.getpass() |
| 112 | sys.stdout.write("OTP: ") |
| 113 | otp = sys.stdin.readline().strip() |
| 114 | |
| 115 | # execute login by posting userdata to login.corp.google.com |
| 116 | url = "https://login.corp.google.com/login?ssoformat=CORP_SSO" |
| 117 | data = "u=%s&pw=%s&otp=%s" % (username, password, otp) |
| 118 | result = self.opener.open(url, data).read() |
| 119 | |
| 120 | # check if the result displays error |
| 121 | if result.find("error") >= 0: |
| 122 | print "Login failed" |
| 123 | exit(-1) |
| 124 | |
| 125 | # login was successful. save cookies to file to be reused later. |
| 126 | self.cookies.save() |
| 127 | |
| 128 | def DownloadFile(self, url): |
| 129 | try: |
| 130 | return self.opener.open(url).read() |
| 131 | except urllib2.URLError: |
| 132 | return None |
| 133 | |
| 134 | def DownloadSystemLog(self, id): |
| 135 | # First download the report.zip file |
| 136 | logfile = "report-%s-system_logs.zip" % id |
| 137 | url = ("https://feedback.corp.googleusercontent.com/binarydata/" + |
| 138 | "%s?id=%s&logIndex=0") % (logfile, id) |
| 139 | report = self.DownloadFile(url) |
| 140 | if not report or (report[0:2] != "BZ" and report[0:2] != "PK"): |
| 141 | print "Report does not include log files at %s" % url |
| 142 | return None |
| 143 | return report |
| 144 | |
| 145 | def DownloadScreenshot(self, id): |
| 146 | url = "https://feedback.corp.googleusercontent.com/screenshot?id=%s" % id |
| 147 | image = self.DownloadFile(url) |
| 148 | if not image or imghdr.what("", image) != 'jpeg': |
| 149 | print "No screenshots available at %s" % url |
| 150 | return None |
| 151 | return image |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 152 | |
| 153 | class FeedbackLog(AbstractLog): |
| 154 | """ |
WeiNan-Peter, Wen | 9aab26a | 2013-05-13 09:32:29 -0400 | [diff] [blame] | 155 | Downloads logs and (possibly) screenshot from a feedback id or file name |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 156 | """ |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 157 | def __init__(self, id_or_filename, options=None, force_latest=None): |
WeiNan-Peter, Wen | e539a18 | 2013-05-16 15:31:21 -0400 | [diff] [blame] | 158 | self.image = None |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 159 | self.force_latest = force_latest |
| 160 | self.try_screenshot = options and options.screenshot |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 161 | |
Dennis Kempin | 91e96df | 2013-03-29 14:21:42 -0700 | [diff] [blame] | 162 | if id_or_filename.endswith(".zip") or id_or_filename.endswith(".bz2"): |
| 163 | self.report = open(id_or_filename).read() |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 164 | screenshot_filename = id_or_filename[:-4] + '.jpg' |
Andrew de los Reyes | 1de4dcd | 2013-05-14 11:45:32 -0700 | [diff] [blame] | 165 | try: |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 166 | self.image = open(screenshot_filename).read() |
Andrew de los Reyes | 1de4dcd | 2013-05-14 11:45:32 -0700 | [diff] [blame] | 167 | except IOError: |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 168 | # No corresponding screenshot available. |
| 169 | pass |
| 170 | else: |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 171 | self.downloader = FeedbackDownloader() |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 172 | def GetAndUpdateCachedFile(cached_filename, downloader): |
| 173 | try: |
| 174 | return open(cached_filename).read() |
| 175 | except IOError: |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 176 | data = downloader(id_or_filename) |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 177 | # Cache the data on disk |
| 178 | if not os.path.exists(log_cache_dir): |
| 179 | os.makedirs(log_cache_dir) |
| 180 | file(cached_filename, "w").write(data) |
| 181 | return data |
| 182 | self.report = GetAndUpdateCachedFile( |
| 183 | os.path.join(log_cache_dir, id_or_filename + ".zip"), |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 184 | self.downloader.DownloadSystemLog) |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 185 | self._ExtractSystemLog() |
| 186 | self._ExtractLogFiles() |
| 187 | if self.try_screenshot: |
| 188 | self.image = GetAndUpdateCachedFile( |
| 189 | os.path.join(log_cache_dir, id_or_filename + ".jpg"), |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 190 | self.downloader.DownloadScreenshot) |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 191 | |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 192 | # Only write to screenshot.jpg if we will be viewing the screenshot |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 193 | if options and not options.download and self.image: |
WeiNan-Peter, Wen | 294408a | 2013-06-13 10:48:39 -0400 | [diff] [blame] | 194 | file(screenshot_filepath, "w").write(self.image) |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 195 | |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 196 | |
| 197 | def _GetLatestFile(self, match, tar): |
| 198 | # find file name containing match with latest timestamp |
| 199 | names = filter(lambda n: n.find(match) >= 0, tar.getnames()) |
| 200 | names.sort() |
Dennis Kempin | 765ad94 | 2013-03-26 13:58:46 -0700 | [diff] [blame] | 201 | if not names: |
| 202 | print "Cannot find files named %s in tar file" % match |
| 203 | print "Tar file contents:", tar.getnames() |
| 204 | sys.exit(-1) |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 205 | return tar.extractfile(names[-1]) |
| 206 | |
WeiNan-Peter, Wen | 9aab26a | 2013-05-13 09:32:29 -0400 | [diff] [blame] | 207 | |
Dennis Kempin | 91e96df | 2013-03-29 14:21:42 -0700 | [diff] [blame] | 208 | def _ExtractSystemLog(self): |
| 209 | if self.report[0:2] == "BZ": |
| 210 | self.system = bz2.decompress(self.report) |
| 211 | elif self.report[0:2] == "PK": |
| 212 | io = StringIO(self.report) |
Dennis Kempin | ecd9a0c | 2013-03-27 16:03:46 -0700 | [diff] [blame] | 213 | zip = zipfile.ZipFile(io, "r") |
| 214 | self.system = zip.read(zip.namelist()[0]) |
| 215 | zip.extractall("./") |
| 216 | else: |
| 217 | print "Cannot download logs file" |
| 218 | sys.exit(-1) |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 219 | |
| 220 | def _ExtractLogFiles(self): |
WeiNan-Peter, Wen | f3f4034 | 2013-05-15 11:26:09 -0400 | [diff] [blame] | 221 | # Find embedded and uuencoded activity.tar in system log |
| 222 | |
| 223 | def ExtractByInterface(interface): |
| 224 | assert interface == 'pad' or interface == 'screen' |
| 225 | |
| 226 | log_index = [ |
| 227 | (("hack-33025-touch{0}_activity=\"\"\"\n" + |
| 228 | "begin-base64 644 touch{0}_activity_log.tar\n").format(interface), |
| 229 | '"""'), |
| 230 | (("touch{0}_activity=\"\"\"\n" + |
| 231 | "begin-base64 644 touch{0}_activity_log.tar\n").format(interface), |
| 232 | '"""'), |
| 233 | (("hack-33025-touch{0}_activity=<multiline>\n" + |
| 234 | "---------- START ----------\n" + |
| 235 | "begin-base64 644 touch{0}_activity_log.tar\n").format(interface), |
| 236 | "---------- END ----------"), |
| 237 | ] |
| 238 | |
| 239 | start_index = end_index = None |
| 240 | for start, end in log_index: |
| 241 | if start in self.system: |
| 242 | start_index = self.system.index(start) + len(start) |
| 243 | end_index = self.system.index(end, start_index) |
| 244 | break |
| 245 | |
| 246 | if start_index is None: |
| 247 | return [] |
| 248 | |
| 249 | activity_tar_enc = self.system[start_index:end_index] |
| 250 | |
| 251 | # base64 decode |
| 252 | activity_tar_data = base64.b64decode(activity_tar_enc) |
| 253 | |
| 254 | # untar |
| 255 | activity_tar_file = tarfile.open(fileobj=StringIO(activity_tar_data)) |
| 256 | |
| 257 | def ExtractPadFiles(name): |
| 258 | # find matching evdev file |
| 259 | evdev_name = name[0:name.index('touchpad_activity')] |
| 260 | evdev_name = evdev_name + "cmt_input_events" |
| 261 | |
| 262 | for file_name in activity_tar_file.getnames(): |
| 263 | if file_name.startswith(evdev_name): |
| 264 | evdev_name = file_name |
| 265 | break |
| 266 | |
| 267 | activity_gz = activity_tar_file.extractfile(name) |
| 268 | evdev_gz = activity_tar_file.extractfile(evdev_name) |
| 269 | |
| 270 | # decompress log files |
| 271 | return (GzipFile(fileobj=activity_gz).read(), |
| 272 | GzipFile(fileobj=evdev_gz).read()) |
| 273 | |
| 274 | def ExtractScreenFiles(name): |
WeiNan-Peter, Wen | 4625886 | 2013-05-21 11:17:54 -0400 | [diff] [blame] | 275 | # Always try for a screenshot with touchscreen view |
| 276 | self.try_screenshot = True |
| 277 | |
WeiNan-Peter, Wen | f3f4034 | 2013-05-15 11:26:09 -0400 | [diff] [blame] | 278 | evdev = activity_tar_file.extractfile(name) |
| 279 | return (evdev.read(), None) |
| 280 | |
| 281 | extract_func = { |
| 282 | 'pad': ExtractPadFiles, |
| 283 | 'screen': ExtractScreenFiles, |
| 284 | } |
| 285 | |
| 286 | # return latest log files, we don't include cmt files |
| 287 | return [(filename, extract_func[interface]) |
| 288 | for filename in activity_tar_file.getnames() |
| 289 | if filename.find('cmt') == -1] |
| 290 | |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 291 | if self.force_latest == "pad": |
| 292 | logs = ExtractByInterface('pad') |
| 293 | idx = 0 |
| 294 | elif self.force_latest == "screen": |
| 295 | logs = ExtractByInterface('screen') |
Dennis Kempin | ecd9a0c | 2013-03-27 16:03:46 -0700 | [diff] [blame] | 296 | idx = 0 |
| 297 | else: |
Dennis Kempin | b049d54 | 2013-06-13 13:55:18 -0700 | [diff] [blame^] | 298 | logs = ExtractByInterface('pad') + ExtractByInterface('screen') |
| 299 | if not logs: |
| 300 | print ("Cannot find touchpad_activity_log.tar or " + |
| 301 | "touchscreen_activity_log.tar in systems log file.") |
| 302 | sys.exit(-1) |
| 303 | |
| 304 | if len(logs) == 1: |
| 305 | idx = 0 |
| 306 | else: |
| 307 | while True: |
| 308 | print "Which log file would you like to use?" |
| 309 | for i, (name, extract_func) in enumerate(logs): |
| 310 | if name.startswith('evdev'): |
| 311 | name = 'touchscreen log - ' + name |
| 312 | print i, name |
| 313 | print ">" |
| 314 | selection = sys.stdin.readline() |
| 315 | try: |
| 316 | idx = int(selection) |
| 317 | if idx < 0 or idx >= len(logs): |
| 318 | print "Number out of range" |
| 319 | else: |
| 320 | break |
| 321 | except: |
| 322 | print "Not a number" |
Dennis Kempin | ecd9a0c | 2013-03-27 16:03:46 -0700 | [diff] [blame] | 323 | |
WeiNan-Peter, Wen | f3f4034 | 2013-05-15 11:26:09 -0400 | [diff] [blame] | 324 | name, extract_func = logs[idx] |
| 325 | self.activity, self.evdev = extract_func(name) |
Dennis Kempin | 351024d | 2013-02-06 11:18:21 -0800 | [diff] [blame] | 326 | |
| 327 | |
| 328 | identity_message = """\ |
| 329 | In order to access devices in TPTool, you need to have password-less |
| 330 | auth for chromebooks set up. |
| 331 | Would you like tptool to run the following command for you? |
| 332 | $ %s |
| 333 | Yes/No? (Default: No)""" |
| 334 | |
| 335 | class DeviceLog(AbstractLog): |
| 336 | """ |
| 337 | Downloads logs from a running chromebook via scp. |
| 338 | """ |
| 339 | def __init__(self, ip, options): |
| 340 | self.id_path = os.path.expanduser("~/.ssh/identity") |
| 341 | if not os.path.exists(self.id_path): |
| 342 | self._SetupIdentity() |
| 343 | |
| 344 | if options.new: |
| 345 | self._GenerateLogs(ip) |
| 346 | |
| 347 | self.activity = self._Download(ip, "touchpad_activity_log.txt") |
| 348 | self.evdev = self._Download(ip, "cmt_input_events.dat") |
| 349 | self.system = "" |
| 350 | |
| 351 | def _GenerateLogs(self, ip): |
| 352 | cmd = ("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + |
| 353 | "-q root@%s /opt/google/touchpad/tpcontrol log") % ip |
| 354 | process = subprocess.Popen(cmd.split()) |
| 355 | process.wait() |
| 356 | |
| 357 | def _Download(self, ip, filename): |
| 358 | temp = tempfile.NamedTemporaryFile("r") |
| 359 | cmd = ("scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no " + |
| 360 | "-q root@%s:/var/log/%s %s") |
| 361 | cmd = cmd % (ip, filename, temp.name) |
| 362 | process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE) |
| 363 | process.wait() |
| 364 | temp.seek(0) |
| 365 | return temp.read() |
| 366 | |
| 367 | def _SetupIdentity(self): |
| 368 | source = os.path.expanduser("~/trunk/chromite/ssh_keys/testing_rsa") |
| 369 | command = "cp %s %s && chmod 600 %s" % (source, self.id_path, self.id_path) |
| 370 | print identity_message % command |
| 371 | response = sys.stdin.readline().strip() |
| 372 | if response in ("Yes", "yes", "Y", "y"): |
| 373 | print command |
| 374 | os.system(command) |