blob: a5c62df87f178ce34f8bfb98917c678b7a35d284 [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
5from gzip import GzipFile
Dennis Kempincee37612013-06-14 10:40:41 -07006from mtlib.feedback import FeedbackDownloader
Dennis Kempin351024d2013-02-06 11:18:21 -08007from StringIO import StringIO
8import base64
9import bz2
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -040010import imghdr
Dennis Kempin351024d2013-02-06 11:18:21 -080011import os
12import os.path
13import re
14import subprocess
15import sys
16import tarfile
17import tempfile
Dennis Kempinecd9a0c2013-03-27 16:03:46 -070018import zipfile
Dennis Kempin351024d2013-02-06 11:18:21 -080019
Dennis Kempin351024d2013-02-06 11:18:21 -080020
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040021# path for temporary screenshot
Dennis Kempin17766a62013-06-17 14:09:33 -070022screenshot_filepath = 'extension/screenshot.jpg'
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040023
Dennis Kempin351024d2013-02-06 11:18:21 -080024def Log(source, options):
Dennis Kempin17766a62013-06-17 14:09:33 -070025 """ Load log from feedback url, device ip or local file path.
26
27 Returns a log object containg activity, evdev and system logs.
28 source can be either an ip address to a device from which to
29 download, a url pointing to a feedback report to pull or a filename.
Dennis Kempin351024d2013-02-06 11:18:21 -080030 """
31 if os.path.exists(source):
Dennis Kempin17766a62013-06-17 14:09:33 -070032 if source.endswith('.zip') or source.endswith('.bz2'):
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040033 return FeedbackLog(source, options)
Dennis Kempin351024d2013-02-06 11:18:21 -080034 return FileLog(source, options)
35 else:
Dennis Kempin17766a62013-06-17 14:09:33 -070036 match = re.search('Report/([0-9]+)', source)
Dennis Kempin351024d2013-02-06 11:18:21 -080037 if match:
38 return FeedbackLog(match.group(1), options)
39 else:
40 # todo add regex and error message
41 return DeviceLog(source, options)
42
43
44class AbstractLog(object):
Dennis Kempin17766a62013-06-17 14:09:33 -070045 """ Common functions for log files
46
47 A log file consists of the activity log, the evdev log, a system log, and
48 possibly a screenshot, which can be saved to disk all together.
Dennis Kempin351024d2013-02-06 11:18:21 -080049 """
50 def SaveAs(self, filename):
Dennis Kempin17766a62013-06-17 14:09:33 -070051 open(filename, 'w').write(self.activity)
Dennis Kempin351024d2013-02-06 11:18:21 -080052 if self.evdev:
Dennis Kempin17766a62013-06-17 14:09:33 -070053 open(filename + '.evdev', 'w').write(self.evdev)
Dennis Kempin351024d2013-02-06 11:18:21 -080054 if self.system:
Dennis Kempin17766a62013-06-17 14:09:33 -070055 open(filename + '.system', 'w').write(self.system)
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040056 if self.image:
Dennis Kempin17766a62013-06-17 14:09:33 -070057 open(filename + '.jpg', 'w').write(self.image)
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040058
59 def CleanUp(self):
WeiNan-Peter, Wen294408a2013-06-13 10:48:39 -040060 if os.path.exists(screenshot_filepath):
61 os.remove(screenshot_filepath)
Dennis Kempin351024d2013-02-06 11:18:21 -080062
63
64class FileLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -070065 """ Loads log from file.
66
67 evdev or system logs are optional.
Dennis Kempin351024d2013-02-06 11:18:21 -080068 """
69 def __init__(self, filename, options):
70 self.activity = open(filename).read()
Dennis Kempin17766a62013-06-17 14:09:33 -070071 self.evdev = ''
72 self.system = ''
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -040073 self.image = None
Dennis Kempin7b783272013-04-01 15:06:35 -070074 if options.evdev:
75 self.evdev = open(options.evdev).read()
Dennis Kempin17766a62013-06-17 14:09:33 -070076 elif os.path.exists(filename + '.evdev'):
77 self.evdev = open(filename + '.evdev').read()
78 if os.path.exists(filename + '.system'):
79 self.system = open(filename + '.system').read()
80 if os.path.exists(filename + '.jpg'):
81 self.image = open(filename + '.jpg').read()
82 file(screenshot_filepath, 'w').write(self.image)
Dennis Kempin351024d2013-02-06 11:18:21 -080083
84
85class FeedbackLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -070086 """ Download log from feedback url.
87
88 Downloads logs and (possibly) screenshot from a feedback id or file name
Dennis Kempin351024d2013-02-06 11:18:21 -080089 """
Dennis Kempinb049d542013-06-13 13:55:18 -070090 def __init__(self, id_or_filename, options=None, force_latest=None):
WeiNan-Peter, Wene539a182013-05-16 15:31:21 -040091 self.image = None
Dennis Kempinb049d542013-06-13 13:55:18 -070092 self.force_latest = force_latest
93 self.try_screenshot = options and options.screenshot
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -040094
Dennis Kempin17766a62013-06-17 14:09:33 -070095 if id_or_filename.endswith('.zip') or id_or_filename.endswith('.bz2'):
Dennis Kempin91e96df2013-03-29 14:21:42 -070096 self.report = open(id_or_filename).read()
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -040097 screenshot_filename = id_or_filename[:-4] + '.jpg'
Andrew de los Reyes1de4dcd2013-05-14 11:45:32 -070098 try:
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -040099 self.image = open(screenshot_filename).read()
Andrew de los Reyes1de4dcd2013-05-14 11:45:32 -0700100 except IOError:
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400101 # No corresponding screenshot available.
102 pass
103 else:
Dennis Kempinb049d542013-06-13 13:55:18 -0700104 self.downloader = FeedbackDownloader()
Dennis Kempincee37612013-06-14 10:40:41 -0700105 self.report = self.downloader.DownloadSystemLog(id_or_filename)
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400106 self._ExtractSystemLog()
107 self._ExtractLogFiles()
108 if self.try_screenshot:
Dennis Kempincee37612013-06-14 10:40:41 -0700109 self.image = self.downloader.DownloadScreenshot(id_or_filename)
Dennis Kempin351024d2013-02-06 11:18:21 -0800110
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400111 # Only write to screenshot.jpg if we will be viewing the screenshot
Dennis Kempinb049d542013-06-13 13:55:18 -0700112 if options and not options.download and self.image:
Dennis Kempin17766a62013-06-17 14:09:33 -0700113 file(screenshot_filepath, 'w').write(self.image)
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400114
Dennis Kempin351024d2013-02-06 11:18:21 -0800115
116 def _GetLatestFile(self, match, tar):
117 # find file name containing match with latest timestamp
118 names = filter(lambda n: n.find(match) >= 0, tar.getnames())
119 names.sort()
Dennis Kempin765ad942013-03-26 13:58:46 -0700120 if not names:
Dennis Kempin17766a62013-06-17 14:09:33 -0700121 print 'Cannot find files named %s in tar file' % match
122 print 'Tar file contents:', tar.getnames()
Dennis Kempin765ad942013-03-26 13:58:46 -0700123 sys.exit(-1)
Dennis Kempin351024d2013-02-06 11:18:21 -0800124 return tar.extractfile(names[-1])
125
WeiNan-Peter, Wen9aab26a2013-05-13 09:32:29 -0400126
Dennis Kempin91e96df2013-03-29 14:21:42 -0700127 def _ExtractSystemLog(self):
Dennis Kempin17766a62013-06-17 14:09:33 -0700128 if self.report[0:2] == 'BZ':
Dennis Kempin91e96df2013-03-29 14:21:42 -0700129 self.system = bz2.decompress(self.report)
Dennis Kempin17766a62013-06-17 14:09:33 -0700130 elif self.report[0:2] == 'PK':
Dennis Kempin91e96df2013-03-29 14:21:42 -0700131 io = StringIO(self.report)
Dennis Kempin17766a62013-06-17 14:09:33 -0700132 zip = zipfile.ZipFile(io, 'r')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700133 self.system = zip.read(zip.namelist()[0])
Dennis Kempin17766a62013-06-17 14:09:33 -0700134 zip.extractall('./')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700135 else:
Dennis Kempin17766a62013-06-17 14:09:33 -0700136 print 'Cannot download logs file'
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700137 sys.exit(-1)
Dennis Kempin351024d2013-02-06 11:18:21 -0800138
139 def _ExtractLogFiles(self):
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400140 # Find embedded and uuencoded activity.tar in system log
141
142 def ExtractByInterface(interface):
143 assert interface == 'pad' or interface == 'screen'
144
145 log_index = [
Dennis Kempin17766a62013-06-17 14:09:33 -0700146 (('hack-33025-touch{0}_activity=\'\'\'\n' +
147 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
148 '"""'),
149 (('touch{0}_activity=\'\'\'\n' +
150 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
151 '"""'),
152 (('hack-33025-touch{0}_activity=<multiline>\n' +
153 '---------- START ----------\n' +
154 'begin-base64 644 touch{0}_activity_log.tar\n').format(interface),
155 '---------- END ----------'),
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400156 ]
157
158 start_index = end_index = None
159 for start, end in log_index:
160 if start in self.system:
161 start_index = self.system.index(start) + len(start)
162 end_index = self.system.index(end, start_index)
163 break
164
165 if start_index is None:
166 return []
167
168 activity_tar_enc = self.system[start_index:end_index]
169
170 # base64 decode
171 activity_tar_data = base64.b64decode(activity_tar_enc)
172
173 # untar
174 activity_tar_file = tarfile.open(fileobj=StringIO(activity_tar_data))
175
176 def ExtractPadFiles(name):
177 # find matching evdev file
178 evdev_name = name[0:name.index('touchpad_activity')]
Dennis Kempin17766a62013-06-17 14:09:33 -0700179 evdev_name = evdev_name + 'cmt_input_events'
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400180
181 for file_name in activity_tar_file.getnames():
182 if file_name.startswith(evdev_name):
183 evdev_name = file_name
184 break
185
186 activity_gz = activity_tar_file.extractfile(name)
187 evdev_gz = activity_tar_file.extractfile(evdev_name)
188
189 # decompress log files
190 return (GzipFile(fileobj=activity_gz).read(),
191 GzipFile(fileobj=evdev_gz).read())
192
193 def ExtractScreenFiles(name):
WeiNan-Peter, Wen46258862013-05-21 11:17:54 -0400194 # Always try for a screenshot with touchscreen view
195 self.try_screenshot = True
196
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400197 evdev = activity_tar_file.extractfile(name)
198 return (evdev.read(), None)
199
200 extract_func = {
201 'pad': ExtractPadFiles,
202 'screen': ExtractScreenFiles,
203 }
204
205 # return latest log files, we don't include cmt files
206 return [(filename, extract_func[interface])
207 for filename in activity_tar_file.getnames()
208 if filename.find('cmt') == -1]
209
Dennis Kempin17766a62013-06-17 14:09:33 -0700210 if self.force_latest == 'pad':
Dennis Kempinb049d542013-06-13 13:55:18 -0700211 logs = ExtractByInterface('pad')
212 idx = 0
Dennis Kempin17766a62013-06-17 14:09:33 -0700213 elif self.force_latest == 'screen':
Dennis Kempinb049d542013-06-13 13:55:18 -0700214 logs = ExtractByInterface('screen')
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700215 idx = 0
216 else:
Dennis Kempinb049d542013-06-13 13:55:18 -0700217 logs = ExtractByInterface('pad') + ExtractByInterface('screen')
218 if not logs:
Dennis Kempin17766a62013-06-17 14:09:33 -0700219 print ('Cannot find touchpad_activity_log.tar or ' +
220 'touchscreen_activity_log.tar in systems log file.')
Dennis Kempinb049d542013-06-13 13:55:18 -0700221 sys.exit(-1)
222
223 if len(logs) == 1:
224 idx = 0
225 else:
226 while True:
Dennis Kempin17766a62013-06-17 14:09:33 -0700227 print 'Which log file would you like to use?'
Dennis Kempinb049d542013-06-13 13:55:18 -0700228 for i, (name, extract_func) in enumerate(logs):
229 if name.startswith('evdev'):
230 name = 'touchscreen log - ' + name
231 print i, name
Dennis Kempin17766a62013-06-17 14:09:33 -0700232 print '>'
Dennis Kempinb049d542013-06-13 13:55:18 -0700233 selection = sys.stdin.readline()
234 try:
235 idx = int(selection)
236 if idx < 0 or idx >= len(logs):
Dennis Kempin17766a62013-06-17 14:09:33 -0700237 print 'Number out of range'
Dennis Kempinb049d542013-06-13 13:55:18 -0700238 else:
239 break
240 except:
Dennis Kempin17766a62013-06-17 14:09:33 -0700241 print 'Not a number'
Dennis Kempinecd9a0c2013-03-27 16:03:46 -0700242
WeiNan-Peter, Wenf3f40342013-05-15 11:26:09 -0400243 name, extract_func = logs[idx]
244 self.activity, self.evdev = extract_func(name)
Dennis Kempin351024d2013-02-06 11:18:21 -0800245
246
247identity_message = """\
248In order to access devices in TPTool, you need to have password-less
249auth for chromebooks set up.
250Would you like tptool to run the following command for you?
251$ %s
252Yes/No? (Default: No)"""
253
254class DeviceLog(AbstractLog):
Dennis Kempin17766a62013-06-17 14:09:33 -0700255 """ Downloads logs from a running chromebook via scp. """
Dennis Kempin351024d2013-02-06 11:18:21 -0800256 def __init__(self, ip, options):
Dennis Kempin17766a62013-06-17 14:09:33 -0700257 self.id_path = os.path.expanduser('~/.ssh/identity')
Dennis Kempin351024d2013-02-06 11:18:21 -0800258 if not os.path.exists(self.id_path):
259 self._SetupIdentity()
260
261 if options.new:
262 self._GenerateLogs(ip)
263
Dennis Kempin17766a62013-06-17 14:09:33 -0700264 self.activity = self._Download(ip, 'touchpad_activity_log.txt')
265 self.evdev = self._Download(ip, 'cmt_input_events.dat')
266 self.system = ''
Dennis Kempin351024d2013-02-06 11:18:21 -0800267
268 def _GenerateLogs(self, ip):
Dennis Kempin17766a62013-06-17 14:09:33 -0700269 cmd = ('ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' +
270 '-q root@%s /opt/google/touchpad/tpcontrol log') % ip
Dennis Kempin351024d2013-02-06 11:18:21 -0800271 process = subprocess.Popen(cmd.split())
272 process.wait()
273
274 def _Download(self, ip, filename):
Dennis Kempin17766a62013-06-17 14:09:33 -0700275 temp = tempfile.NamedTemporaryFile('r')
276 cmd = ('scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' +
277 '-q root@%s:/var/log/%s %s')
Dennis Kempin351024d2013-02-06 11:18:21 -0800278 cmd = cmd % (ip, filename, temp.name)
279 process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
280 process.wait()
281 temp.seek(0)
282 return temp.read()
283
284 def _SetupIdentity(self):
Dennis Kempin17766a62013-06-17 14:09:33 -0700285 source = os.path.expanduser('~/trunk/chromite/ssh_keys/testing_rsa')
286 command = 'cp %s %s && chmod 600 %s' % (source, self.id_path, self.id_path)
Dennis Kempin351024d2013-02-06 11:18:21 -0800287 print identity_message % command
288 response = sys.stdin.readline().strip()
Dennis Kempin17766a62013-06-17 14:09:33 -0700289 if response in ('Yes', 'yes', 'Y', 'y'):
Dennis Kempin351024d2013-02-06 11:18:21 -0800290 print command
291 os.system(command)