blob: 2835d11087e637c94d42f2250cdc7cd3287f148a [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 Cutts69fc2be2020-01-22 18:03:21 -080011try:
12 from StringIO import StringIO
13except ImportError:
14 from io import StringIO
15from .cros_remote import CrOSRemote
Dennis Kempin351024d2013-02-06 11:18:21 -080016import base64
17import bz2
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -040018import imghdr
Dennis Kempin351024d2013-02-06 11:18:21 -080019import os
20import os.path
21import re
22import subprocess
23import sys
24import tarfile
25import tempfile
Dennis Kempinecd9a0c2013-03-27 16:03:46 -070026import zipfile
Dennis Kempin351024d2013-02-06 11:18:21 -080027
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040028# path for temporary screenshot
Dennis Kempin17766a62013-06-17 14:09:33 -070029screenshot_filepath = 'extension/screenshot.jpg'
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040030
Dennis Kempinaa7ce272014-04-18 11:50:59 -070031class Options:
32 def __init__(self, download=False, new=False, screenshot=False,
33 evdev=None):
34 self.download = download
35 self.new = new
36 self.screenshot = screenshot
37 self.evdev = evdev
38
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070039def Log(source=None, options=None, activity=None, evdev=None):
Dennis Kempin17766a62013-06-17 14:09:33 -070040 """ Load log from feedback url, device ip or local file path.
41
42 Returns a log object containg activity, evdev and system logs.
43 source can be either an ip address to a device from which to
44 download, a url pointing to a feedback report to pull or a filename.
Dennis Kempin351024d2013-02-06 11:18:21 -080045 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -070046 if not options:
47 options = Options()
48
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070049 if source:
50 if os.path.exists(source):
51 if source.endswith('.zip') or source.endswith('.bz2'):
Dennis Kempinaa7ce272014-04-18 11:50:59 -070052 return FeedbackLog(source)
53 return FileLog(source, options.evdev)
Dennis Kempin351024d2013-02-06 11:18:21 -080054 else:
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070055 match = re.search('Report/([0-9]+)', source)
56 if match:
Dennis Kempinaa7ce272014-04-18 11:50:59 -070057 return FeedbackLog(match.group(1), options.screenshot,
58 options.download)
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070059 else:
60 # todo add regex and error message
Dennis Kempinaa7ce272014-04-18 11:50:59 -070061 return DeviceLog(source, options.new)
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070062
63 if activity or evdev:
64 log = AbstractLog()
65 if activity:
66 if os.path.exists(activity):
67 log.activity = open(activity).read()
68 else:
69 log.activity = activity
70 if evdev:
71 if os.path.exists(evdev):
72 log.evdev = open(evdev).read()
73 else:
74 log.evdev = evdev
75 return log
Dennis Kempin351024d2013-02-06 11:18:21 -080076
77
78class AbstractLog(object):
Dennis Kempin17766a62013-06-17 14:09:33 -070079 """ Common functions for log files
80
81 A log file consists of the activity log, the evdev log, a system log, and
82 possibly a screenshot, which can be saved to disk all together.
Dennis Kempin351024d2013-02-06 11:18:21 -080083 """
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070084 def __init__(self):
Dennis Kempinaa7ce272014-04-18 11:50:59 -070085 self.activity = ''
86 self.evdev = ''
87 self.system = ''
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070088 self.image = None
89
Dennis Kempin351024d2013-02-06 11:18:21 -080090 def SaveAs(self, filename):
Dennis Kempin17766a62013-06-17 14:09:33 -070091 open(filename, 'w').write(self.activity)
Dennis Kempin351024d2013-02-06 11:18:21 -080092 if self.evdev:
Dennis Kempin17766a62013-06-17 14:09:33 -070093 open(filename + '.evdev', 'w').write(self.evdev)
Dennis Kempin351024d2013-02-06 11:18:21 -080094 if self.system:
Dennis Kempin17766a62013-06-17 14:09:33 -070095 open(filename + '.system', 'w').write(self.system)
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040096 if self.image:
Dennis Kempin17766a62013-06-17 14:09:33 -070097 open(filename + '.jpg', 'w').write(self.image)
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040098
99 def CleanUp(self):
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -0400100 if os.path.exists(screenshot_filepath):
101 os.remove(screenshot_filepath)
Dennis Kempin351024d2013-02-06 11:18:21 -0800102
103
104class FileLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700105 """ Loads log from file.
106
107 evdev or system logs are optional.
Dennis Kempin351024d2013-02-06 11:18:21 -0800108 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700109 def __init__(self, filename, evdev=None):
110 AbstractLog.__init__(self)
111
Dennis Kempin351024d2013-02-06 11:18:21 -0800112 self.activity = open(filename).read()
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700113 if evdev:
114 self.evdev = open(evdev).read()
Dennis Kempin17766a62013-06-17 14:09:33 -0700115 elif os.path.exists(filename + '.evdev'):
116 self.evdev = open(filename + '.evdev').read()
117 if os.path.exists(filename + '.system'):
118 self.system = open(filename + '.system').read()
119 if os.path.exists(filename + '.jpg'):
120 self.image = open(filename + '.jpg').read()
121 file(screenshot_filepath, 'w').write(self.image)
Dennis Kempin351024d2013-02-06 11:18:21 -0800122
123
124class FeedbackLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700125 """ Download log from feedback url.
126
127 Downloads logs and (possibly) screenshot from a feedback id or file name
Dennis Kempin351024d2013-02-06 11:18:21 -0800128 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700129 def __init__(self, id_or_filename, screenshot=False, download=False,
Sean O'Brienfd204da2017-05-02 15:13:11 -0700130 force_latest=None, downloader=None):
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700131 AbstractLog.__init__(self)
Dennis Kempinb049d542013-06-13 13:55:18 -0700132 self.force_latest = force_latest
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700133 self.try_screenshot = screenshot
Dennis Kempin19e972b2013-06-20 13:21:38 -0700134 self.report = None
Sean O'Brienfd204da2017-05-02 15:13:11 -0700135 self.downloader = downloader
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400136
Dennis Kempin17766a62013-06-17 14:09:33 -0700137 if id_or_filename.endswith('.zip') or id_or_filename.endswith('.bz2'):
Dennis Kempin91e96df2013-03-29 14:21:42 -0700138 self.report = open(id_or_filename).read()
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400139 screenshot_filename = id_or_filename[:-4] + '.jpg'
Andrew de los Reyes1de4dcd2013-05-14 11:45:32 -0700140 try:
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400141 self.image = open(screenshot_filename).read()
Andrew de los Reyes1de4dcd2013-05-14 11:45:32 -0700142 except IOError:
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400143 # No corresponding screenshot available.
144 pass
145 else:
Sean O'Brienfd204da2017-05-02 15:13:11 -0700146 if not self.downloader:
147 self.downloader = FeedbackDownloader()
Dennis Kempincee37612013-06-14 10:40:41 -0700148 self.report = self.downloader.DownloadSystemLog(id_or_filename)
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400149 if self.try_screenshot:
Dennis Kempincee37612013-06-14 10:40:41 -0700150 self.image = self.downloader.DownloadScreenshot(id_or_filename)
Dennis Kempin351024d2013-02-06 11:18:21 -0800151
Dennis Kempin13d948e2014-04-18 11:23:32 -0700152 if self.report:
153 self._ExtractSystemLog()
154 if self.system:
155 self._ExtractLogFiles()
156
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400157 # Only write to screenshot.jpg if we will be viewing the screenshot
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700158 if not download and self.image:
Dennis Kempin17766a62013-06-17 14:09:33 -0700159 file(screenshot_filepath, 'w').write(self.image)
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400160
Dennis Kempin351024d2013-02-06 11:18:21 -0800161 def _GetLatestFile(self, match, tar):
162 # find file name containing match with latest timestamp
163 names = filter(lambda n: n.find(match) >= 0, tar.getnames())
164 names.sort()
Dennis Kempin765ad942013-03-26 13:58:46 -0700165 if not names:
Harry Cutts0edf1572020-01-21 15:42:10 -0800166 print('Cannot find files named %s in tar file' % match)
167 print('Tar file contents:', tar.getnames())
Dennis Kempin765ad942013-03-26 13:58:46 -0700168 sys.exit(-1)
Dennis Kempin351024d2013-02-06 11:18:21 -0800169 return tar.extractfile(names[-1])
170
Dennis Kempin91e96df2013-03-29 14:21:42 -0700171 def _ExtractSystemLog(self):
Dennis Kempin17766a62013-06-17 14:09:33 -0700172 if self.report[0:2] == 'BZ':
Dennis Kempin91e96df2013-03-29 14:21:42 -0700173 self.system = bz2.decompress(self.report)
Dennis Kempin17766a62013-06-17 14:09:33 -0700174 elif self.report[0:2] == 'PK':
Dennis Kempin91e96df2013-03-29 14:21:42 -0700175 io = StringIO(self.report)
Dennis Kempin17766a62013-06-17 14:09:33 -0700176 zip = zipfile.ZipFile(io, 'r')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700177 self.system = zip.read(zip.namelist()[0])
Dennis Kempin17766a62013-06-17 14:09:33 -0700178 zip.extractall('./')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700179 else:
Harry Cutts0edf1572020-01-21 15:42:10 -0800180 print('Cannot download logs file')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700181 sys.exit(-1)
Dennis Kempin351024d2013-02-06 11:18:21 -0800182
183 def _ExtractLogFiles(self):
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400184 # Find embedded and uuencoded activity.tar in system log
185
186 def ExtractByInterface(interface):
187 assert interface == 'pad' or interface == 'screen'
188
189 log_index = [
Dennis Kempin19e972b2013-06-20 13:21:38 -0700190 (('hack-33025-touch{0}_activity=\"\"\"\n' +
Dennis Kempin17766a62013-06-17 14:09:33 -0700191 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
192 '"""'),
Dennis Kempin19e972b2013-06-20 13:21:38 -0700193 (('touch{0}_activity=\"\"\"\n' +
Dennis Kempin17766a62013-06-17 14:09:33 -0700194 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
195 '"""'),
196 (('hack-33025-touch{0}_activity=<multiline>\n' +
197 '---------- START ----------\n' +
198 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
199 '---------- END ----------'),
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400200 ]
201
202 start_index = end_index = None
203 for start, end in log_index:
204 if start in self.system:
205 start_index = self.system.index(start) + len(start)
206 end_index = self.system.index(end, start_index)
207 break
208
209 if start_index is None:
210 return []
211
212 activity_tar_enc = self.system[start_index:end_index]
213
214 # base64 decode
215 activity_tar_data = base64.b64decode(activity_tar_enc)
216
Sean O'Brien0d2d0c42018-02-06 09:37:42 -0800217 if activity_tar_data == '':
Harry Cutts0edf1572020-01-21 15:42:10 -0800218 print('touch{0}_activity_log.tar found, but empty'.format(interface))
Sean O'Brien0d2d0c42018-02-06 09:37:42 -0800219 return []
220
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400221 # untar
222 activity_tar_file = tarfile.open(fileobj=StringIO(activity_tar_data))
223
224 def ExtractPadFiles(name):
225 # find matching evdev file
Sean O'Brien146a6842018-07-09 09:09:40 -0700226 evdev_name = name.replace('touchpad_activity', 'cmt_input_events')
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400227
228 activity_gz = activity_tar_file.extractfile(name)
229 evdev_gz = activity_tar_file.extractfile(evdev_name)
230
231 # decompress log files
232 return (GzipFile(fileobj=activity_gz).read(),
233 GzipFile(fileobj=evdev_gz).read())
234
235 def ExtractScreenFiles(name):
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400236 # Always try for a screenshot with touchscreen view
237 self.try_screenshot = True
238
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400239 evdev = activity_tar_file.extractfile(name)
240 return (evdev.read(), None)
241
242 extract_func = {
243 'pad': ExtractPadFiles,
244 'screen': ExtractScreenFiles,
245 }
246
247 # return latest log files, we don't include cmt files
248 return [(filename, extract_func[interface])
249 for filename in activity_tar_file.getnames()
250 if filename.find('cmt') == -1]
251
Dennis Kempin17766a62013-06-17 14:09:33 -0700252 if self.force_latest == 'pad':
Dennis Kempinb049d542013-06-13 13:55:18 -0700253 logs = ExtractByInterface('pad')
254 idx = 0
Dennis Kempin17766a62013-06-17 14:09:33 -0700255 elif self.force_latest == 'screen':
Dennis Kempinb049d542013-06-13 13:55:18 -0700256 logs = ExtractByInterface('screen')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700257 idx = 0
258 else:
Dennis Kempinb049d542013-06-13 13:55:18 -0700259 logs = ExtractByInterface('pad') + ExtractByInterface('screen')
260 if not logs:
Harry Cutts0edf1572020-01-21 15:42:10 -0800261 print('Cannot find touchpad_activity_log.tar or '
Dennis Kempin17766a62013-06-17 14:09:33 -0700262 'touchscreen_activity_log.tar in systems log file.')
Dennis Kempinb049d542013-06-13 13:55:18 -0700263 sys.exit(-1)
264
265 if len(logs) == 1:
266 idx = 0
267 else:
268 while True:
Harry Cutts0edf1572020-01-21 15:42:10 -0800269 print('Which log file would you like to use?')
Dennis Kempinb049d542013-06-13 13:55:18 -0700270 for i, (name, extract_func) in enumerate(logs):
271 if name.startswith('evdev'):
272 name = 'touchscreen log - ' + name
Harry Cutts0edf1572020-01-21 15:42:10 -0800273 print(i, name)
274 print('>')
Dennis Kempinb049d542013-06-13 13:55:18 -0700275 selection = sys.stdin.readline()
276 try:
277 idx = int(selection)
278 if idx < 0 or idx >= len(logs):
Harry Cutts0edf1572020-01-21 15:42:10 -0800279 print('Number out of range')
Dennis Kempinb049d542013-06-13 13:55:18 -0700280 else:
281 break
282 except:
Harry Cutts0edf1572020-01-21 15:42:10 -0800283 print('Not a number')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700284
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400285 name, extract_func = logs[idx]
286 self.activity, self.evdev = extract_func(name)
Dennis Kempin351024d2013-02-06 11:18:21 -0800287
288
289identity_message = """\
290In order to access devices in TPTool, you need to have password-less
291auth for chromebooks set up.
292Would you like tptool to run the following command for you?
293$ %s
294Yes/No? (Default: No)"""
295
296class DeviceLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700297 """ Downloads logs from a running chromebook via scp. """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700298 def __init__(self, ip, new=False):
299 AbstractLog.__init__(self)
Dennis Kempin60571e02014-01-09 12:00:15 -0800300 self.remote = CrOSRemote(ip)
Dennis Kempin351024d2013-02-06 11:18:21 -0800301
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700302 if new:
Dennis Kempin58d9a742014-05-08 18:34:59 -0700303 self.remote.SafeExecute('/opt/google/input/inputcontrol -t touchpad' +
304 ' --log')
Dennis Kempin60571e02014-01-09 12:00:15 -0800305 self.activity = self.remote.Read('/var/log/xorg/touchpad_activity_log.txt')
306 self.evdev = self.remote.Read('/var/log/xorg/cmt_input_events.dat')