Merge lp:~roadmr/checkbox/1303941 into lp:checkbox

Proposed by Daniel Manrique
Status: Merged
Approved by: Zygmunt Krynicki
Approved revision: 3084
Merged at revision: 3084
Proposed branch: lp:~roadmr/checkbox/1303941
Merge into: lp:checkbox
Diff against target: 304 lines (+74/-68)
1 file modified
providers/plainbox-provider-checkbox/bin/sleep_test (+74/-68)
To merge this branch: bzr merge lp:~roadmr/checkbox/1303941
Reviewer Review Type Date Requested Status
Zygmunt Krynicki (community) Approve
Review via email: mp+223635@code.launchpad.net

Commit message

providers:plainbox: convert sleep_test to python3.

INcludes some pep8 fixes.

Note that I just did the overall conversion to ensure the script runs; however, since it depends on specific markers logged by the kernel in syslog, and those have changed in the past year, the script no longer works (it does suspend but then always outputs "fail"). Some more work is needed to identify marker strings and properly set state based on them.

Description of the change

providers:plainbox: convert sleep_test to python3.

INcludes some pep8 fixes.

Note that I just did the overall conversion to ensure the script runs; however, since it depends on specific markers logged by the kernel in syslog, and those have changed in the past year, the script no longer works (it does suspend but then always outputs "fail", at least on a 14.10 system). Some more work is needed to identify marker strings and properly set state based on them.

To post a comment you must log in.
Revision history for this message
Zygmunt Krynicki (zyga) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'providers/plainbox-provider-checkbox/bin/sleep_test'
2--- providers/plainbox-provider-checkbox/bin/sleep_test 2014-03-20 14:01:58 +0000
3+++ providers/plainbox-provider-checkbox/bin/sleep_test 2014-06-18 18:46:23 +0000
4@@ -1,11 +1,12 @@
5-#!/usr/bin/python
6+#!/usr/bin/python3
7 '''
8 Program to automate system entering and resuming from sleep states
9
10-Copyright (C) 2010,2011 Canonical Ltd.
11+Copyright (C) 2010-2014 Canonical Ltd.
12
13 Author:
14 Jeff Lane <jeffrey.lane@canonical.com>
15+ Daniel Manrique <roadmr@ubuntu.com>
16
17 This program is free software: you can redistribute it and/or modify
18 it under the terms of the GNU General Public License version 2,
19@@ -20,13 +21,14 @@
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 '''
22
23+import logging
24 import os
25+import re
26 import sys
27-import logging
28-import re
29+import syslog
30+
31+from optparse import OptionParser, OptionGroup
32 from subprocess import call
33-from optparse import OptionParser, OptionGroup
34-from syslog import *
35
36
37 class ListDictHandler(logging.StreamHandler):
38@@ -41,16 +43,20 @@
39 for msg in record.msg:
40 logger = logging.getLogger(record.name)
41 new_record = logger.makeRecord(record.name, record.levelno,
42- record.pathname, record.lineno, msg, record.args,
43- record.exc_info, record.funcName)
44+ record.pathname, record.lineno,
45+ msg, record.args,
46+ record.exc_info,
47+ record.funcName)
48 logging.StreamHandler.emit(self, new_record)
49 elif isinstance(record.msg, dict):
50- for key, val in record.msg.iteritems():
51+ for key, val in record.msg.items():
52 logger = logging.getLogger(record.name)
53 new_msg = '%s: %s' % (key, val)
54 new_record = logger.makeRecord(record.name, record.levelno,
55- record.pathname, record.lineno, new_msg, record.args,
56- record.exc_info, record.funcName)
57+ record.pathname, record.lineno,
58+ new_msg, record.args,
59+ record.exc_info,
60+ record.funcName)
61 logging.StreamHandler.emit(self, new_record)
62 else:
63 logging.StreamHandler.emit(self, record)
64@@ -74,9 +80,9 @@
65 future kernels.
66
67 '''
68- states_fh = open('/sys/power/state', 'r', 0)
69+ states_fh = open('/sys/power/state', 'rb', 0)
70 try:
71- states = states_fh.read().split()
72+ states = states_fh.read().decode('ascii').split()
73 finally:
74 states_fh.close()
75 logging.debug('The following sleep states were found:')
76@@ -89,9 +95,9 @@
77
78 def GetCurrentTime(self):
79
80- time_fh = open('/sys/class/rtc/rtc0/since_epoch', 'r', 0)
81+ time_fh = open('/sys/class/rtc/rtc0/since_epoch', 'rb', 0)
82 try:
83- time = int(time_fh.read())
84+ time = int(time_fh.read().decode('ascii'))
85 finally:
86 time_fh.close()
87 return time
88@@ -110,13 +116,13 @@
89 self.last_time = self.GetCurrentTime()
90 logging.debug('Current epoch time: %s' % self.last_time)
91
92- wakealarm_fh = open('/sys/class/rtc/rtc0/wakealarm', 'w', 0)
93+ wakealarm_fh = open('/sys/class/rtc/rtc0/wakealarm', 'wb', 0)
94
95 try:
96- wakealarm_fh.write('0\n')
97+ wakealarm_fh.write('0\n'.encode('ascii'))
98 wakealarm_fh.flush()
99
100- wakealarm_fh.write('+%s\n' % time)
101+ wakealarm_fh.write('+{}\n'.format(time).encode('ascii'))
102 wakealarm_fh.flush()
103 finally:
104 wakealarm_fh.close()
105@@ -153,10 +159,10 @@
106
107 def GetResults(self, mode, perf):
108 '''
109- This will parse /var/log/messages for our start and end markers. Then
110- it'll find a few key phrases that are part of the sleep and resume
111- process, grab their timestamps, Bob's your Uncle and return a
112- three-tuple consisting of: (PASS/FAIL,Sleep elapsed time, Resume
113+ This will parse /var/log/messages for our start and end markers. Then
114+ it'll find a few key phrases that are part of the sleep and resume
115+ process, grab their timestamps, Bob's your Uncle and return a
116+ three-tuple consisting of: (PASS/FAIL,Sleep elapsed time, Resume
117 elapsed time)
118 '''
119 # figure out our elapsed time
120@@ -185,7 +191,7 @@
121 sleep_end_time = re.split('[\[\]]',
122 loglist[idx - 1])[1].strip()
123 logging.debug('Sleep ended at %s' % sleep_end_time)
124- resume_start_time = re.split('[\[\]]',
125+ resume_start_time = re.split('[\[\]]',
126 loglist[idx])[1].strip()
127 logging.debug('Resume started at %s' % resume_start_time)
128 idx += 1
129@@ -194,7 +200,7 @@
130 loglist[idx])[1].strip()
131 logging.debug('Resume ended at %s' % resume_end_time)
132 if self.end_marker in loglist[idx]:
133- logging.debug('End Marker found, run appears to '
134+ logging.debug('End Marker found, run appears to '
135 'have completed')
136 run_complete = 'Pass'
137 break
138@@ -206,7 +212,7 @@
139 else:
140 if self.end_marker in loglist:
141 logging.debug('End Marker found, '
142- 'run appears to have completed')
143+ 'run appears to have completed')
144 run_complete = 'Pass'
145 sleep_elapsed = None
146 resume_elapsed = None
147@@ -218,7 +224,7 @@
148 Write a stamped marker to syslogd (will appear in /var/log/messages).
149 This is used to calculate the elapsed time for each iteration.
150 '''
151- syslog(LOG_INFO, '---' + marker + '---')
152+ syslog(syslog.LOG_INFO, '---' + marker + '---')
153
154 def CheckAlarm(self, mode):
155 '''
156@@ -229,10 +235,10 @@
157 the system did not wake by alarm IRQ, but by some other means.
158 '''
159 rtc = {}
160- rtc_fh = open('/proc/driver/rtc', 'r', 0)
161- alarm_fh = open('/sys/class/rtc/rtc0/wakealarm', 'r', 0)
162+ rtc_fh = open('/proc/driver/rtc', 'rb', 0)
163+ alarm_fh = open('/sys/class/rtc/rtc0/wakealarm', 'rb', 0)
164 try:
165- rtc_data = rtc_fh.read().splitlines()
166+ rtc_data = rtc_fh.read().decode('ascii').splitlines()
167 for item in rtc_data:
168 rtc_entry = item.partition(':')
169 rtc[rtc_entry[0].strip()] = rtc_entry[2].strip()
170@@ -240,7 +246,7 @@
171 rtc_fh.close()
172
173 try:
174- alarm = int(alarm_fh.read())
175+ alarm = int(alarm_fh.read().decode('ascii'))
176 except ValueError:
177 alarm = None
178 finally:
179@@ -275,44 +281,44 @@
180 def main():
181 usage = 'Usage: %prog [OPTIONS]'
182 parser = OptionParser(usage)
183- group = OptionGroup(parser, 'This will not work for hibernat testing due' \
184- ' to a kernel timestamp bug when doing an S4 ' \
185+ group = OptionGroup(parser, 'This will not work for hibernat testing due'
186+ ' to a kernel timestamp bug when doing an S4 '
187 '(hibernate/resume) sleep cycle')
188 group.add_option('-p', '--perf',
189- action='store_true',
190- default=False,
191- help='Add some output that tells you how long it ' \
192- 'takes to enter a sleep state and how long it ' \
193- 'takes to resume.')
194+ action='store_true',
195+ default=False,
196+ help='Add some output that tells you how long it '
197+ 'takes to enter a sleep state and how long it '
198+ 'takes to resume.')
199 parser.add_option('-i', '--iterations',
200- action='store',
201- type='int',
202- metavar='NUM',
203- default=1,
204- help='The number of times to run the suspend/resume \
205- loop. Default is %default')
206+ action='store',
207+ type='int',
208+ metavar='NUM',
209+ default=1,
210+ help='The number of times to run the suspend/resume '
211+ 'loop. Default is %default')
212 parser.add_option('-w', '--wake-in',
213- action='store',
214- type='int',
215- metavar='NUM',
216- default=60,
217- dest='wake_time',
218- help='Sets wake up time (in seconds) in the future \
219- from now. Default is %default.')
220+ action='store',
221+ type='int',
222+ metavar='NUM',
223+ default=60,
224+ dest='wake_time',
225+ help='Sets wake up time (in seconds) in the future '
226+ 'from now. Default is %default.')
227 parser.add_option('-s', '--sleep-state',
228- action='store',
229- default='mem',
230- metavar='MODE',
231- dest='mode',
232- help='Sets the sleep state to test. Passing mem will \
233- set the sleep state to Suspend-To-Ram or S3. Passing \
234- disk will set the sleep state to Suspend-To-Disk or S4\
235- (hibernate). Default sleep state is %default')
236+ action='store',
237+ default='mem',
238+ metavar='MODE',
239+ dest='mode',
240+ help='Sets the sleep state to test. Passing mem will '
241+ 'set the sleep state to Suspend-To-Ram or S3. Passing '
242+ 'disk will set the sleep state to Suspend-To-Disk or S4 '
243+ '(hibernate). Default sleep state is %default')
244 parser.add_option('-d', '--debug',
245- action='store_true',
246- default=False,
247- help='Choose this to add verbose output for debug \
248- purposes')
249+ action='store_true',
250+ default=False,
251+ help='Choose this to add verbose output for debug \
252+ purposes')
253 parser.add_option_group(group)
254 (options, args) = parser.parse_args()
255 options_dict = vars(options)
256@@ -330,10 +336,10 @@
257 # Set up the logger
258 logger = logging.getLogger()
259 logger.setLevel(logging.DEBUG)
260-
261+
262 if options.debug:
263 handler.setLevel(logging.DEBUG)
264-
265+
266 logger.addHandler(handler)
267 logging.debug('Running with these options')
268 logging.debug(options_dict)
269@@ -349,7 +355,7 @@
270 return 1
271 else:
272 logging.debug('%s sleep state supported, continuing test'
273- % options.mode)
274+ % options.mode)
275
276 # We run the following for the number of iterations requested
277 for iteration in range(0, options.iterations):
278@@ -358,7 +364,7 @@
279 suspender.DoSuspend(options.mode)
280 run_count += 1
281 run_result[run_count] = suspender.GetResults(options.mode,
282- options.perf)
283+ options.perf)
284 if suspender.CheckAlarm(options.mode):
285 logging.debug('The alarm is still set')
286
287@@ -367,7 +373,7 @@
288 resume_total = 0.0
289 logging.info('=' * 20 + ' Test Results ' + '=' * 20)
290 logging.info(run_result)
291-
292+
293 for k in run_result.iterkeys():
294 sleep_total += run_result[k][1]
295 resume_total += run_result[k][2]
296@@ -381,7 +387,7 @@
297
298 if fail_count > 0:
299 logging.error('%s sleep/resume test cycles failed' %
300- fail_count)
301+ fail_count)
302 logging.error(run_result)
303 return 1
304 else:

Subscribers

People subscribed via source and target branches