2 # -*- coding: utf-8 -*-
3 # This script generates hundreds of random keypresses per second,
4 # and sends them to the lyx window
5 # It requires xvkbd and wmctrl
6 # It generates a log of the KEYCODES it sends as development/keystest/out/KEYCODES
13 #from subprocess import call
16 print 'Beginning keytest.py'
37 for k in range(97, 123):
40 for k in range(97, 123):
41 keycode[:0] = ["\A" + chr(k)]
43 for k in range(97, 123):
44 keycode[:0] = ["\A" + chr(k)]
46 for k in range(97, 123):
47 keycode[:0] = ["\C" + chr(k)]
49 self.keycode = keycode
54 self.count = self.count + 1
55 if self.count % 200 == 0:
57 elif self.count > self.count_max:
62 keystr = keystr + self.keycode[random.randint(1,
63 len(self.keycode)) - 1]
64 return 'KK: ' + keystr
67 class CommandSourceFromFile(CommandSource):
69 def __init__(self, filename, p):
71 self.infile = open(filename, 'r')
72 self.lines = self.infile.readlines()
76 print p, self.p, 'self.p'
81 # Now we start randomly dropping lines, which we hope are redundant
82 # p is the probability that any given line will be removed
85 if random.uniform(0, 1) < 0.5:
86 print 'randomdrop_independant\n'
87 self.randomdrop_independant()
89 print 'randomdrop_slice\n'
90 self.randomdrop_slice()
91 if screenshot_out is None:
95 self.max_count = max(len(self.lines) + 20, count_atleast)
96 if len(self.lines) < 1:
99 def randomdrop_independant(self):
102 # The next couple of lines are to ensure that at least one line is dropped
104 drop = random.randint(0, len(self.lines) - 1)
106 p = p - 1 / len(self.lines)
107 origlines = self.lines
110 if random.uniform(0, 1) < self.p:
111 print 'Randomly dropping line ' + l + '\n'
118 def randomdrop_slice(self):
120 if random.uniform(0, 1) < 0.4:
121 lines.append(lines[0])
123 num_lines = len(lines)
124 max_drop = max(5, num_lines / 5)
125 num_drop = random.randint(1, 5)
126 drop_mid = random.randint(0, num_lines)
127 drop_start = max(drop_mid - num_drop / 2, 0)
128 drop_end = min(drop_start + num_drop, num_lines)
129 print drop_start, drop_mid, drop_end
131 del lines[drop_start:drop_end]
135 def getCommand(self):
136 if self.count >= self.max_count:
138 if self.i >= len(self.lines):
139 self.loops = self.loops + 1
142 line = self.lines[self.i]
143 self.count = self.count + 1
145 print 'Line read: <<' + line + '>>\n'
146 return line.rstrip('\n').rstrip()
150 fname = '/proc/' + lyx_pid + '/status'
151 if not os.path.exists(fname):
154 lines = f.readlines()
155 sleeping = lines[1].find('(sleeping)') > 0
157 # print 'LYX_STATE', lines[1] , 'SLEEPING=', sleeping
162 def sendKeystring(keystr, LYX_PID):
164 # print "sending keystring "+keystr+"\n"
166 if not re.match(".*\w.*", keystr):
167 print 'print .' + keystr + '.\n'
169 before_secs = time.time()
170 while not lyx_sleeping():
173 if time.time() - before_secs > 180:
174 print 'Killing due to freeze (KILL_FREEZE)'
176 # Do profiling, but sysprof has no command line interface?
177 # os.system("killall -KILL lyx")
180 if not screenshot_out is None:
181 while not lyx_sleeping():
184 print 'Making Screenshot: ' + screenshot_out
186 os.system('import -window root '+screenshot_out+str(x.count)+".png")
189 subprocess.call(["xvkbd", "-xsendevent", "-delay", DELAY, "-text", keystr])
192 os.system("echo x-session-manager PID: $X_PID.")
193 os.system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
194 os.system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
195 os.system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
198 lyx_pid = os.environ.get('LYX_PID')
199 print 'lyx_pid: ' + lyx_pid + '\n'
200 infilename = os.environ.get('KEYTEST_INFILE')
201 outfilename = os.environ.get('KEYTEST_OUTFILE')
202 max_drop = os.environ.get('MAX_DROP')
203 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
204 screenshot_out = os.environ.get('SCREENSHOT_OUT')
206 file_new_command = os.environ.get('FILE_NEW_COMMAND')
207 if file_new_command is None:
208 file_new_command = "\Afn"
210 ResetCommand = os.environ.get('RESET_COMMAND')
211 if ResetCommand is None:
212 ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
213 #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
215 if lyx_window_name is None:
216 lyx_window_name = 'LyX'
218 print 'outfilename: ' + outfilename + '\n'
219 print 'max_drop: ' + max_drop + '\n'
221 if infilename is None:
222 print 'infilename is None\n'
224 print 'Using x=CommandSource\n'
226 print 'infilename: ' + infilename + '\n'
227 probability_we_drop_a_command = random.uniform(0, float(max_drop))
228 print 'probability_we_drop_a_command: '
229 print '%s' % probability_we_drop_a_command
231 x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
232 print 'Using x=CommandSourceFromFile\n'
234 outfile = open(outfilename, 'w')
237 sendKeystring("\Afn", lyx_pid)
238 write_commands = True
241 os.system('echo -n LOADAVG:; cat /proc/loadavg')
245 outfile = open(outfilename + '+', 'w')
247 outfile.writelines(c + '\n')
252 elif c[0:4] == 'KK: ':
253 if os.path.exists('/proc/' + lyx_pid + '/status'):
254 sendKeystring(c[4:], lyx_pid)
256 os.system('killall lyx; sleep 2 ; killall -9 lyx')
257 print 'No path /proc/' + lyx_pid + '/status, exiting'
259 elif c[0:4] == 'KD: ':
260 DELAY = c[4:].rstrip('\n')
261 print 'Setting DELAY to ' + DELAY + '.'
264 sendKeystring(ResetCommand, lyx_pid)
266 print "Unrecognised Command '" + c + "'\n"