blob: de0db30125f17a37627594bc3b85169a251d9a0a [file] [log] [blame]
Dennis Kempin351024d2013-02-06 11:18:21 -08001# 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
Harry Cutts69fc2be2020-01-22 18:03:21 -08005from __future__ import absolute_import
6from __future__ import division
Harry Cutts0edf1572020-01-21 15:42:10 -08007from __future__ import print_function
8
Dennis Kempin351024d2013-02-06 11:18:21 -08009from gzip import GzipFile
Dennis Kempincee37612013-06-14 10:40:41 -070010from mtlib.feedback import FeedbackDownloader
Harry Cuttsd77ab7a2020-01-28 11:09:26 -080011from io import BytesIO
Harry Cutts69fc2be2020-01-22 18:03:21 -080012from .cros_remote import CrOSRemote
Dennis Kempin351024d2013-02-06 11:18:21 -080013import base64
14import bz2
Dennis Kempin351024d2013-02-06 11:18:21 -080015import os
16import os.path
17import re
Dennis Kempin351024d2013-02-06 11:18:21 -080018import sys
19import tarfile
Dennis Kempinecd9a0c2013-03-27 16:03:46 -070020import zipfile
Dennis Kempin351024d2013-02-06 11:18:21 -080021
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040022# path for temporary screenshot
Dennis Kempin17766a62013-06-17 14:09:33 -070023screenshot_filepath = 'extension/screenshot.jpg'
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040024
Dennis Kempinaa7ce272014-04-18 11:50:59 -070025class Options:
26 def __init__(self, download=False, new=False, screenshot=False,
27 evdev=None):
28 self.download = download
29 self.new = new
30 self.screenshot = screenshot
31 self.evdev = evdev
32
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070033def Log(source=None, options=None, activity=None, evdev=None):
Dennis Kempin17766a62013-06-17 14:09:33 -070034 """ Load log from feedback url, device ip or local file path.
35
36 Returns a log object containg activity, evdev and system logs.
37 source can be either an ip address to a device from which to
38 download, a url pointing to a feedback report to pull or a filename.
Dennis Kempin351024d2013-02-06 11:18:21 -080039 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -070040 if not options:
41 options = Options()
42
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070043 if source:
44 if os.path.exists(source):
45 if source.endswith('.zip') or source.endswith('.bz2'):
Dennis Kempinaa7ce272014-04-18 11:50:59 -070046 return FeedbackLog(source)
47 return FileLog(source, options.evdev)
Dennis Kempin351024d2013-02-06 11:18:21 -080048 else:
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070049 match = re.search('Report/([0-9]+)', source)
50 if match:
Dennis Kempinaa7ce272014-04-18 11:50:59 -070051 return FeedbackLog(match.group(1), options.screenshot,
52 options.download)
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070053 else:
54 # todo add regex and error message
Dennis Kempinaa7ce272014-04-18 11:50:59 -070055 return DeviceLog(source, options.new)
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070056
57 if activity or evdev:
58 log = AbstractLog()
59 if activity:
60 if os.path.exists(activity):
61 log.activity = open(activity).read()
62 else:
63 log.activity = activity
64 if evdev:
65 if os.path.exists(evdev):
66 log.evdev = open(evdev).read()
67 else:
68 log.evdev = evdev
69 return log
Dennis Kempin351024d2013-02-06 11:18:21 -080070
71
72class AbstractLog(object):
Dennis Kempin17766a62013-06-17 14:09:33 -070073 """ Common functions for log files
74
75 A log file consists of the activity log, the evdev log, a system log, and
76 possibly a screenshot, which can be saved to disk all together.
Dennis Kempin351024d2013-02-06 11:18:21 -080077 """
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070078 def __init__(self):
Dennis Kempinaa7ce272014-04-18 11:50:59 -070079 self.activity = ''
80 self.evdev = ''
81 self.system = ''
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070082 self.image = None
83
Dennis Kempin351024d2013-02-06 11:18:21 -080084 def SaveAs(self, filename):
Dennis Kempin17766a62013-06-17 14:09:33 -070085 open(filename, 'w').write(self.activity)
Dennis Kempin351024d2013-02-06 11:18:21 -080086 if self.evdev:
Dennis Kempin17766a62013-06-17 14:09:33 -070087 open(filename + '.evdev', 'w').write(self.evdev)
Dennis Kempin351024d2013-02-06 11:18:21 -080088 if self.system:
Dennis Kempin17766a62013-06-17 14:09:33 -070089 open(filename + '.system', 'w').write(self.system)
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040090 if self.image:
Dennis Kempin17766a62013-06-17 14:09:33 -070091 open(filename + '.jpg', 'w').write(self.image)
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040092
93 def CleanUp(self):
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040094 if os.path.exists(screenshot_filepath):
95 os.remove(screenshot_filepath)
Dennis Kempin351024d2013-02-06 11:18:21 -080096
97
98class FileLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -070099 """ Loads log from file.
100
101 evdev or system logs are optional.
Dennis Kempin351024d2013-02-06 11:18:21 -0800102 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700103 def __init__(self, filename, evdev=None):
104 AbstractLog.__init__(self)
105
Dennis Kempin351024d2013-02-06 11:18:21 -0800106 self.activity = open(filename).read()
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700107 if evdev:
108 self.evdev = open(evdev).read()
Dennis Kempin17766a62013-06-17 14:09:33 -0700109 elif os.path.exists(filename + '.evdev'):
110 self.evdev = open(filename + '.evdev').read()
111 if os.path.exists(filename + '.system'):
112 self.system = open(filename + '.system').read()
113 if os.path.exists(filename + '.jpg'):
114 self.image = open(filename + '.jpg').read()
115 file(screenshot_filepath, 'w').write(self.image)
Dennis Kempin351024d2013-02-06 11:18:21 -0800116
117
118class FeedbackLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700119 """ Download log from feedback url.
120
121 Downloads logs and (possibly) screenshot from a feedback id or file name
Dennis Kempin351024d2013-02-06 11:18:21 -0800122 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700123 def __init__(self, id_or_filename, screenshot=False, download=False,
Sean O'Brienfd204da2017-05-02 15:13:11 -0700124 force_latest=None, downloader=None):
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700125 AbstractLog.__init__(self)
Dennis Kempinb049d542013-06-13 13:55:18 -0700126 self.force_latest = force_latest
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700127 self.try_screenshot = screenshot
Dennis Kempin19e972b2013-06-20 13:21:38 -0700128 self.report = None
Sean O'Brienfd204da2017-05-02 15:13:11 -0700129 self.downloader = downloader
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400130
Dennis Kempin17766a62013-06-17 14:09:33 -0700131 if id_or_filename.endswith('.zip') or id_or_filename.endswith('.bz2'):
Harry Cuttsd77ab7a2020-01-28 11:09:26 -0800132 self.report = open(id_or_filename, 'rb').read()
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400133 screenshot_filename = id_or_filename[:-4] + '.jpg'
Andrew de los Reyes1de4dcd2013-05-14 11:45:32 -0700134 try:
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400135 self.image = open(screenshot_filename).read()
Andrew de los Reyes1de4dcd2013-05-14 11:45:32 -0700136 except IOError:
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400137 # No corresponding screenshot available.
138 pass
139 else:
Sean O'Brienfd204da2017-05-02 15:13:11 -0700140 if not self.downloader:
141 self.downloader = FeedbackDownloader()
Dennis Kempincee37612013-06-14 10:40:41 -0700142 self.report = self.downloader.DownloadSystemLog(id_or_filename)
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400143 if self.try_screenshot:
Dennis Kempincee37612013-06-14 10:40:41 -0700144 self.image = self.downloader.DownloadScreenshot(id_or_filename)
Dennis Kempin351024d2013-02-06 11:18:21 -0800145
Dennis Kempin13d948e2014-04-18 11:23:32 -0700146 if self.report:
147 self._ExtractSystemLog()
148 if self.system:
149 self._ExtractLogFiles()
150
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400151 # Only write to screenshot.jpg if we will be viewing the screenshot
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700152 if not download and self.image:
Dennis Kempin17766a62013-06-17 14:09:33 -0700153 file(screenshot_filepath, 'w').write(self.image)
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400154
Dennis Kempin351024d2013-02-06 11:18:21 -0800155 def _GetLatestFile(self, match, tar):
156 # find file name containing match with latest timestamp
157 names = filter(lambda n: n.find(match) >= 0, tar.getnames())
158 names.sort()
Dennis Kempin765ad942013-03-26 13:58:46 -0700159 if not names:
Harry Cutts0edf1572020-01-21 15:42:10 -0800160 print('Cannot find files named %s in tar file' % match)
161 print('Tar file contents:', tar.getnames())
Dennis Kempin765ad942013-03-26 13:58:46 -0700162 sys.exit(-1)
Dennis Kempin351024d2013-02-06 11:18:21 -0800163 return tar.extractfile(names[-1])
164
Dennis Kempin91e96df2013-03-29 14:21:42 -0700165 def _ExtractSystemLog(self):
Harry Cuttsd77ab7a2020-01-28 11:09:26 -0800166 if self.report[0:2] == b'BZ':
167 system_log = bz2.decompress(self.report)
168 elif self.report[0:2] == b'PK':
169 bytes_io = BytesIO(self.report)
170 zip = zipfile.ZipFile(bytes_io, 'r')
171 system_log = zip.read(zip.namelist()[0])
Dennis Kempin17766a62013-06-17 14:09:33 -0700172 zip.extractall('./')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700173 else:
Harry Cutts0edf1572020-01-21 15:42:10 -0800174 print('Cannot download logs file')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700175 sys.exit(-1)
Dennis Kempin351024d2013-02-06 11:18:21 -0800176
Harry Cuttsd77ab7a2020-01-28 11:09:26 -0800177 self.system = system_log.decode('utf-8', 'strict')
178
Dennis Kempin351024d2013-02-06 11:18:21 -0800179 def _ExtractLogFiles(self):
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400180 # Find embedded and uuencoded activity.tar in system log
181
182 def ExtractByInterface(interface):
183 assert interface == 'pad' or interface == 'screen'
184
185 log_index = [
Dennis Kempin19e972b2013-06-20 13:21:38 -0700186 (('hack-33025-touch{0}_activity=\"\"\"\n' +
Dennis Kempin17766a62013-06-17 14:09:33 -0700187 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
188 '"""'),
Dennis Kempin19e972b2013-06-20 13:21:38 -0700189 (('touch{0}_activity=\"\"\"\n' +
Dennis Kempin17766a62013-06-17 14:09:33 -0700190 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
191 '"""'),
192 (('hack-33025-touch{0}_activity=<multiline>\n' +
193 '---------- START ----------\n' +
194 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
195 '---------- END ----------'),
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400196 ]
197
198 start_index = end_index = None
199 for start, end in log_index:
200 if start in self.system:
201 start_index = self.system.index(start) + len(start)
202 end_index = self.system.index(end, start_index)
203 break
204
205 if start_index is None:
206 return []
207
208 activity_tar_enc = self.system[start_index:end_index]
209
210 # base64 decode
211 activity_tar_data = base64.b64decode(activity_tar_enc)
212
Sean O'Brien0d2d0c42018-02-06 09:37:42 -0800213 if activity_tar_data == '':
Harry Cutts0edf1572020-01-21 15:42:10 -0800214 print('touch{0}_activity_log.tar found, but empty'.format(interface))
Sean O'Brien0d2d0c42018-02-06 09:37:42 -0800215 return []
216
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400217 # untar
Harry Cuttsd77ab7a2020-01-28 11:09:26 -0800218 activity_tar_file = tarfile.open(fileobj=BytesIO(activity_tar_data))
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400219
220 def ExtractPadFiles(name):
221 # find matching evdev file
Sean O'Brien146a6842018-07-09 09:09:40 -0700222 evdev_name = name.replace('touchpad_activity', 'cmt_input_events')
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400223
224 activity_gz = activity_tar_file.extractfile(name)
225 evdev_gz = activity_tar_file.extractfile(evdev_name)
226
227 # decompress log files
228 return (GzipFile(fileobj=activity_gz).read(),
229 GzipFile(fileobj=evdev_gz).read())
230
231 def ExtractScreenFiles(name):
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400232 # Always try for a screenshot with touchscreen view
233 self.try_screenshot = True
234
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400235 evdev = activity_tar_file.extractfile(name)
236 return (evdev.read(), None)
237
238 extract_func = {
239 'pad': ExtractPadFiles,
240 'screen': ExtractScreenFiles,
241 }
242
243 # return latest log files, we don't include cmt files
244 return [(filename, extract_func[interface])
245 for filename in activity_tar_file.getnames()
246 if filename.find('cmt') == -1]
247
Dennis Kempin17766a62013-06-17 14:09:33 -0700248 if self.force_latest == 'pad':
Dennis Kempinb049d542013-06-13 13:55:18 -0700249 logs = ExtractByInterface('pad')
250 idx = 0
Dennis Kempin17766a62013-06-17 14:09:33 -0700251 elif self.force_latest == 'screen':
Dennis Kempinb049d542013-06-13 13:55:18 -0700252 logs = ExtractByInterface('screen')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700253 idx = 0
254 else:
Dennis Kempinb049d542013-06-13 13:55:18 -0700255 logs = ExtractByInterface('pad') + ExtractByInterface('screen')
256 if not logs:
Harry Cutts0edf1572020-01-21 15:42:10 -0800257 print('Cannot find touchpad_activity_log.tar or '
Dennis Kempin17766a62013-06-17 14:09:33 -0700258 'touchscreen_activity_log.tar in systems log file.')
Dennis Kempinb049d542013-06-13 13:55:18 -0700259 sys.exit(-1)
260
261 if len(logs) == 1:
262 idx = 0
263 else:
264 while True:
Harry Cutts0edf1572020-01-21 15:42:10 -0800265 print('Which log file would you like to use?')
Dennis Kempinb049d542013-06-13 13:55:18 -0700266 for i, (name, extract_func) in enumerate(logs):
267 if name.startswith('evdev'):
268 name = 'touchscreen log - ' + name
Harry Cutts0edf1572020-01-21 15:42:10 -0800269 print(i, name)
270 print('>')
Dennis Kempinb049d542013-06-13 13:55:18 -0700271 selection = sys.stdin.readline()
272 try:
273 idx = int(selection)
274 if idx < 0 or idx >= len(logs):
Harry Cutts0edf1572020-01-21 15:42:10 -0800275 print('Number out of range')
Dennis Kempinb049d542013-06-13 13:55:18 -0700276 else:
277 break
278 except:
Harry Cutts0edf1572020-01-21 15:42:10 -0800279 print('Not a number')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700280
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400281 name, extract_func = logs[idx]
Harry Cutts87cc6002020-02-04 15:50:20 -0800282 activity, evdev = extract_func(name)
283 self.activity = activity.decode('utf-8', 'strict')
284 self.evdev = evdev.decode('utf-8', 'strict')
Dennis Kempin351024d2013-02-06 11:18:21 -0800285
286
287identity_message = """\
288In order to access devices in TPTool, you need to have password-less
289auth for chromebooks set up.
290Would you like tptool to run the following command for you?
291$ %s
292Yes/No? (Default: No)"""
293
294class DeviceLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700295 """ Downloads logs from a running chromebook via scp. """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700296 def __init__(self, ip, new=False):
297 AbstractLog.__init__(self)
Dennis Kempin60571e02014-01-09 12:00:15 -0800298 self.remote = CrOSRemote(ip)
Dennis Kempin351024d2013-02-06 11:18:21 -0800299
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700300 if new:
Dennis Kempin58d9a742014-05-08 18:34:59 -0700301 self.remote.SafeExecute('/opt/google/input/inputcontrol -t touchpad' +
302 ' --log')
Dennis Kempin60571e02014-01-09 12:00:15 -0800303 self.activity = self.remote.Read('/var/log/xorg/touchpad_activity_log.txt')
304 self.evdev = self.remote.Read('/var/log/xorg/cmt_input_events.dat')