blob: 73ada15468c2b4713633bb3eedd7b25e88231813 [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 Cutts0edf1572020-01-21 15:42:10 -08005from __future__ import print_function
6
Dennis Kempin351024d2013-02-06 11:18:21 -08007from gzip import GzipFile
Dennis Kempincee37612013-06-14 10:40:41 -07008from mtlib.feedback import FeedbackDownloader
Dennis Kempin351024d2013-02-06 11:18:21 -08009from StringIO import StringIO
Dennis Kempin60571e02014-01-09 12:00:15 -080010from cros_remote import CrOSRemote
Dennis Kempin351024d2013-02-06 11:18:21 -080011import base64
12import bz2
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -040013import imghdr
Dennis Kempin351024d2013-02-06 11:18:21 -080014import os
15import os.path
16import re
17import subprocess
18import sys
19import tarfile
20import tempfile
Dennis Kempinecd9a0c2013-03-27 16:03:46 -070021import zipfile
Dennis Kempin351024d2013-02-06 11:18:21 -080022
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040023# path for temporary screenshot
Dennis Kempin17766a62013-06-17 14:09:33 -070024screenshot_filepath = 'extension/screenshot.jpg'
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040025
Dennis Kempinaa7ce272014-04-18 11:50:59 -070026class Options:
27 def __init__(self, download=False, new=False, screenshot=False,
28 evdev=None):
29 self.download = download
30 self.new = new
31 self.screenshot = screenshot
32 self.evdev = evdev
33
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070034def Log(source=None, options=None, activity=None, evdev=None):
Dennis Kempin17766a62013-06-17 14:09:33 -070035 """ Load log from feedback url, device ip or local file path.
36
37 Returns a log object containg activity, evdev and system logs.
38 source can be either an ip address to a device from which to
39 download, a url pointing to a feedback report to pull or a filename.
Dennis Kempin351024d2013-02-06 11:18:21 -080040 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -070041 if not options:
42 options = Options()
43
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070044 if source:
45 if os.path.exists(source):
46 if source.endswith('.zip') or source.endswith('.bz2'):
Dennis Kempinaa7ce272014-04-18 11:50:59 -070047 return FeedbackLog(source)
48 return FileLog(source, options.evdev)
Dennis Kempin351024d2013-02-06 11:18:21 -080049 else:
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070050 match = re.search('Report/([0-9]+)', source)
51 if match:
Dennis Kempinaa7ce272014-04-18 11:50:59 -070052 return FeedbackLog(match.group(1), options.screenshot,
53 options.download)
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070054 else:
55 # todo add regex and error message
Dennis Kempinaa7ce272014-04-18 11:50:59 -070056 return DeviceLog(source, options.new)
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070057
58 if activity or evdev:
59 log = AbstractLog()
60 if activity:
61 if os.path.exists(activity):
62 log.activity = open(activity).read()
63 else:
64 log.activity = activity
65 if evdev:
66 if os.path.exists(evdev):
67 log.evdev = open(evdev).read()
68 else:
69 log.evdev = evdev
70 return log
Dennis Kempin351024d2013-02-06 11:18:21 -080071
72
73class AbstractLog(object):
Dennis Kempin17766a62013-06-17 14:09:33 -070074 """ Common functions for log files
75
76 A log file consists of the activity log, the evdev log, a system log, and
77 possibly a screenshot, which can be saved to disk all together.
Dennis Kempin351024d2013-02-06 11:18:21 -080078 """
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070079 def __init__(self):
Dennis Kempinaa7ce272014-04-18 11:50:59 -070080 self.activity = ''
81 self.evdev = ''
82 self.system = ''
Dennis Kempin1a8a5be2013-06-18 11:00:02 -070083 self.image = None
84
Dennis Kempin351024d2013-02-06 11:18:21 -080085 def SaveAs(self, filename):
Dennis Kempin17766a62013-06-17 14:09:33 -070086 open(filename, 'w').write(self.activity)
Dennis Kempin351024d2013-02-06 11:18:21 -080087 if self.evdev:
Dennis Kempin17766a62013-06-17 14:09:33 -070088 open(filename + '.evdev', 'w').write(self.evdev)
Dennis Kempin351024d2013-02-06 11:18:21 -080089 if self.system:
Dennis Kempin17766a62013-06-17 14:09:33 -070090 open(filename + '.system', 'w').write(self.system)
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040091 if self.image:
Dennis Kempin17766a62013-06-17 14:09:33 -070092 open(filename + '.jpg', 'w').write(self.image)
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040093
94 def CleanUp(self):
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040095 if os.path.exists(screenshot_filepath):
96 os.remove(screenshot_filepath)
Dennis Kempin351024d2013-02-06 11:18:21 -080097
98
99class FileLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700100 """ Loads log from file.
101
102 evdev or system logs are optional.
Dennis Kempin351024d2013-02-06 11:18:21 -0800103 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700104 def __init__(self, filename, evdev=None):
105 AbstractLog.__init__(self)
106
Dennis Kempin351024d2013-02-06 11:18:21 -0800107 self.activity = open(filename).read()
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700108 if evdev:
109 self.evdev = open(evdev).read()
Dennis Kempin17766a62013-06-17 14:09:33 -0700110 elif os.path.exists(filename + '.evdev'):
111 self.evdev = open(filename + '.evdev').read()
112 if os.path.exists(filename + '.system'):
113 self.system = open(filename + '.system').read()
114 if os.path.exists(filename + '.jpg'):
115 self.image = open(filename + '.jpg').read()
116 file(screenshot_filepath, 'w').write(self.image)
Dennis Kempin351024d2013-02-06 11:18:21 -0800117
118
119class FeedbackLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700120 """ Download log from feedback url.
121
122 Downloads logs and (possibly) screenshot from a feedback id or file name
Dennis Kempin351024d2013-02-06 11:18:21 -0800123 """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700124 def __init__(self, id_or_filename, screenshot=False, download=False,
Sean O'Brienfd204da2017-05-02 15:13:11 -0700125 force_latest=None, downloader=None):
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700126 AbstractLog.__init__(self)
Dennis Kempinb049d542013-06-13 13:55:18 -0700127 self.force_latest = force_latest
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700128 self.try_screenshot = screenshot
Dennis Kempin19e972b2013-06-20 13:21:38 -0700129 self.report = None
Sean O'Brienfd204da2017-05-02 15:13:11 -0700130 self.downloader = downloader
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400131
Dennis Kempin17766a62013-06-17 14:09:33 -0700132 if id_or_filename.endswith('.zip') or id_or_filename.endswith('.bz2'):
Dennis Kempin91e96df2013-03-29 14:21:42 -0700133 self.report = open(id_or_filename).read()
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400134 screenshot_filename = id_or_filename[:-4] + '.jpg'
Andrew de los Reyes1de4dcd2013-05-14 11:45:32 -0700135 try:
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400136 self.image = open(screenshot_filename).read()
Andrew de los Reyes1de4dcd2013-05-14 11:45:32 -0700137 except IOError:
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400138 # No corresponding screenshot available.
139 pass
140 else:
Sean O'Brienfd204da2017-05-02 15:13:11 -0700141 if not self.downloader:
142 self.downloader = FeedbackDownloader()
Dennis Kempincee37612013-06-14 10:40:41 -0700143 self.report = self.downloader.DownloadSystemLog(id_or_filename)
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400144 if self.try_screenshot:
Dennis Kempincee37612013-06-14 10:40:41 -0700145 self.image = self.downloader.DownloadScreenshot(id_or_filename)
Dennis Kempin351024d2013-02-06 11:18:21 -0800146
Dennis Kempin13d948e2014-04-18 11:23:32 -0700147 if self.report:
148 self._ExtractSystemLog()
149 if self.system:
150 self._ExtractLogFiles()
151
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400152 # Only write to screenshot.jpg if we will be viewing the screenshot
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700153 if not download and self.image:
Dennis Kempin17766a62013-06-17 14:09:33 -0700154 file(screenshot_filepath, 'w').write(self.image)
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400155
Dennis Kempin351024d2013-02-06 11:18:21 -0800156 def _GetLatestFile(self, match, tar):
157 # find file name containing match with latest timestamp
158 names = filter(lambda n: n.find(match) >= 0, tar.getnames())
159 names.sort()
Dennis Kempin765ad942013-03-26 13:58:46 -0700160 if not names:
Harry Cutts0edf1572020-01-21 15:42:10 -0800161 print('Cannot find files named %s in tar file' % match)
162 print('Tar file contents:', tar.getnames())
Dennis Kempin765ad942013-03-26 13:58:46 -0700163 sys.exit(-1)
Dennis Kempin351024d2013-02-06 11:18:21 -0800164 return tar.extractfile(names[-1])
165
Dennis Kempin91e96df2013-03-29 14:21:42 -0700166 def _ExtractSystemLog(self):
Dennis Kempin17766a62013-06-17 14:09:33 -0700167 if self.report[0:2] == 'BZ':
Dennis Kempin91e96df2013-03-29 14:21:42 -0700168 self.system = bz2.decompress(self.report)
Dennis Kempin17766a62013-06-17 14:09:33 -0700169 elif self.report[0:2] == 'PK':
Dennis Kempin91e96df2013-03-29 14:21:42 -0700170 io = StringIO(self.report)
Dennis Kempin17766a62013-06-17 14:09:33 -0700171 zip = zipfile.ZipFile(io, 'r')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700172 self.system = zip.read(zip.namelist()[0])
Dennis Kempin17766a62013-06-17 14:09:33 -0700173 zip.extractall('./')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700174 else:
Harry Cutts0edf1572020-01-21 15:42:10 -0800175 print('Cannot download logs file')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700176 sys.exit(-1)
Dennis Kempin351024d2013-02-06 11:18:21 -0800177
178 def _ExtractLogFiles(self):
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400179 # Find embedded and uuencoded activity.tar in system log
180
181 def ExtractByInterface(interface):
182 assert interface == 'pad' or interface == 'screen'
183
184 log_index = [
Dennis Kempin19e972b2013-06-20 13:21:38 -0700185 (('hack-33025-touch{0}_activity=\"\"\"\n' +
Dennis Kempin17766a62013-06-17 14:09:33 -0700186 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
187 '"""'),
Dennis Kempin19e972b2013-06-20 13:21:38 -0700188 (('touch{0}_activity=\"\"\"\n' +
Dennis Kempin17766a62013-06-17 14:09:33 -0700189 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
190 '"""'),
191 (('hack-33025-touch{0}_activity=<multiline>\n' +
192 '---------- START ----------\n' +
193 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
194 '---------- END ----------'),
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400195 ]
196
197 start_index = end_index = None
198 for start, end in log_index:
199 if start in self.system:
200 start_index = self.system.index(start) + len(start)
201 end_index = self.system.index(end, start_index)
202 break
203
204 if start_index is None:
205 return []
206
207 activity_tar_enc = self.system[start_index:end_index]
208
209 # base64 decode
210 activity_tar_data = base64.b64decode(activity_tar_enc)
211
Sean O'Brien0d2d0c42018-02-06 09:37:42 -0800212 if activity_tar_data == '':
Harry Cutts0edf1572020-01-21 15:42:10 -0800213 print('touch{0}_activity_log.tar found, but empty'.format(interface))
Sean O'Brien0d2d0c42018-02-06 09:37:42 -0800214 return []
215
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400216 # untar
217 activity_tar_file = tarfile.open(fileobj=StringIO(activity_tar_data))
218
219 def ExtractPadFiles(name):
220 # find matching evdev file
Sean O'Brien146a6842018-07-09 09:09:40 -0700221 evdev_name = name.replace('touchpad_activity', 'cmt_input_events')
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400222
223 activity_gz = activity_tar_file.extractfile(name)
224 evdev_gz = activity_tar_file.extractfile(evdev_name)
225
226 # decompress log files
227 return (GzipFile(fileobj=activity_gz).read(),
228 GzipFile(fileobj=evdev_gz).read())
229
230 def ExtractScreenFiles(name):
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400231 # Always try for a screenshot with touchscreen view
232 self.try_screenshot = True
233
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400234 evdev = activity_tar_file.extractfile(name)
235 return (evdev.read(), None)
236
237 extract_func = {
238 'pad': ExtractPadFiles,
239 'screen': ExtractScreenFiles,
240 }
241
242 # return latest log files, we don't include cmt files
243 return [(filename, extract_func[interface])
244 for filename in activity_tar_file.getnames()
245 if filename.find('cmt') == -1]
246
Dennis Kempin17766a62013-06-17 14:09:33 -0700247 if self.force_latest == 'pad':
Dennis Kempinb049d542013-06-13 13:55:18 -0700248 logs = ExtractByInterface('pad')
249 idx = 0
Dennis Kempin17766a62013-06-17 14:09:33 -0700250 elif self.force_latest == 'screen':
Dennis Kempinb049d542013-06-13 13:55:18 -0700251 logs = ExtractByInterface('screen')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700252 idx = 0
253 else:
Dennis Kempinb049d542013-06-13 13:55:18 -0700254 logs = ExtractByInterface('pad') + ExtractByInterface('screen')
255 if not logs:
Harry Cutts0edf1572020-01-21 15:42:10 -0800256 print('Cannot find touchpad_activity_log.tar or '
Dennis Kempin17766a62013-06-17 14:09:33 -0700257 'touchscreen_activity_log.tar in systems log file.')
Dennis Kempinb049d542013-06-13 13:55:18 -0700258 sys.exit(-1)
259
260 if len(logs) == 1:
261 idx = 0
262 else:
263 while True:
Harry Cutts0edf1572020-01-21 15:42:10 -0800264 print('Which log file would you like to use?')
Dennis Kempinb049d542013-06-13 13:55:18 -0700265 for i, (name, extract_func) in enumerate(logs):
266 if name.startswith('evdev'):
267 name = 'touchscreen log - ' + name
Harry Cutts0edf1572020-01-21 15:42:10 -0800268 print(i, name)
269 print('>')
Dennis Kempinb049d542013-06-13 13:55:18 -0700270 selection = sys.stdin.readline()
271 try:
272 idx = int(selection)
273 if idx < 0 or idx >= len(logs):
Harry Cutts0edf1572020-01-21 15:42:10 -0800274 print('Number out of range')
Dennis Kempinb049d542013-06-13 13:55:18 -0700275 else:
276 break
277 except:
Harry Cutts0edf1572020-01-21 15:42:10 -0800278 print('Not a number')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700279
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400280 name, extract_func = logs[idx]
281 self.activity, self.evdev = extract_func(name)
Dennis Kempin351024d2013-02-06 11:18:21 -0800282
283
284identity_message = """\
285In order to access devices in TPTool, you need to have password-less
286auth for chromebooks set up.
287Would you like tptool to run the following command for you?
288$ %s
289Yes/No? (Default: No)"""
290
291class DeviceLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700292 """ Downloads logs from a running chromebook via scp. """
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700293 def __init__(self, ip, new=False):
294 AbstractLog.__init__(self)
Dennis Kempin60571e02014-01-09 12:00:15 -0800295 self.remote = CrOSRemote(ip)
Dennis Kempin351024d2013-02-06 11:18:21 -0800296
Dennis Kempinaa7ce272014-04-18 11:50:59 -0700297 if new:
Dennis Kempin58d9a742014-05-08 18:34:59 -0700298 self.remote.SafeExecute('/opt/google/input/inputcontrol -t touchpad' +
299 ' --log')
Dennis Kempin60571e02014-01-09 12:00:15 -0800300 self.activity = self.remote.Read('/var/log/xorg/touchpad_activity_log.txt')
301 self.evdev = self.remote.Read('/var/log/xorg/cmt_input_events.dat')