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
8 # Adapted by Tommaso Cucinotta from the original MonKey Test by
11 from __future__ import print_function
17 #from subprocess import call
20 print('Beginning keytest.py')
22 FNULL = open('/dev/null', 'w')
43 for k in range(97, 123):
46 for k in range(97, 123):
47 keycode[:0] = ["\A" + chr(k)]
49 for k in range(97, 123):
50 keycode[:0] = ["\A" + chr(k)]
52 for k in range(97, 123):
53 keycode[:0] = ["\C" + chr(k)]
55 self.keycode = keycode
60 self.count = self.count + 1
61 if self.count % 200 == 0:
63 elif self.count > self.count_max:
68 keystr = keystr + self.keycode[random.randint(1,
69 len(self.keycode)) - 1]
70 return 'KK: ' + keystr
73 class CommandSourceFromFile(CommandSource):
75 def __init__(self, filename, p):
77 self.infile = open(filename, 'r')
78 self.lines = self.infile.readlines()
82 print(p, self.p, 'self.p')
87 # Now we start randomly dropping lines, which we hope are redundant
88 # p is the probability that any given line will be removed
91 if random.uniform(0, 1) < 0.5:
92 print('randomdrop_independant\n')
93 self.randomdrop_independant()
95 print('randomdrop_slice\n')
96 self.randomdrop_slice()
97 if screenshot_out is None:
101 self.max_count = max(len(self.lines) + 20, count_atleast)
102 if len(self.lines) < 1:
103 self.lines = linesbak
105 def randomdrop_independant(self):
108 # The next couple of lines are to ensure that at least one line is dropped
110 drop = random.randint(0, len(self.lines) - 1)
112 #p = p - 1 / len(self.lines)
113 origlines = self.lines
116 if random.uniform(0, 1) < self.p:
117 print('Randomly dropping line ' + l + '\n')
124 def randomdrop_slice(self):
126 if random.uniform(0, 1) < 0.4:
127 lines.append(lines[0])
129 num_lines = len(lines)
130 max_drop = max(5, num_lines / 5)
131 num_drop = random.randint(1, 5)
132 drop_mid = random.randint(0, num_lines)
133 drop_start = max(drop_mid - num_drop / 2, 0)
134 drop_end = min(drop_start + num_drop, num_lines)
135 print(drop_start, drop_mid, drop_end)
137 del lines[drop_start:drop_end]
141 def getCommand(self):
142 if self.count >= self.max_count:
144 if self.i >= len(self.lines):
145 self.loops = self.loops + 1
146 if self.loops >= int(max_loops):
150 line = self.lines[self.i].rstrip('\n')
151 self.count = self.count + 1
159 fname = '/proc/' + lyx_pid + '/status'
160 return os.path.exists(fname)
163 # Interruptible os.system()
164 def intr_system(cmd, ignore_err = False):
165 print("Executing " + cmd + "\n")
167 if os.WIFSIGNALED(ret):
168 raise KeyboardInterrupt
169 if ret != 0 and not ignore_err:
170 raise BaseException("command failed:" + cmd)
173 statreg = re.compile(r'^State:.*\(([a-z]+)\)')
178 fname = '/proc/' + pid + '/status'
182 m = statreg.match(line)
189 print("I/O error({0}): {1}".format(e.errno, e.strerror))
192 print("Unexpected error:", sys.exc_info()[0])
195 # Return true if LyX (identified via lyx_pid) is sleeping
197 return lyx_status(lyx_pid) == "sleeping"
199 # Return true if LyX (identified via lyx_pid) is zombie
201 return lyx_status(lyx_pid) == "zombie"
204 status = lyx_status(lyx_pid)
205 return (status == "dead") or (status == "zombie")
207 def sendKeystringLocal(keystr, LYX_PID):
209 #if not re.match(".*\w.*", keystr):
210 # print('print .' + keystr)
212 before_secs = time.time()
213 while lyx_exists() and not lyx_sleeping():
216 if time.time() - before_secs > 180:
217 print('Killing due to freeze (KILL_FREEZE)')
219 # Do profiling, but sysprof has no command line interface?
220 # intr_system("killall -KILL lyx")
223 if not screenshot_out is None:
224 while lyx_exists() and not lyx_sleeping():
227 print('Making Screenshot: ' + screenshot_out + ' OF ' + infilename)
229 intr_system('import -window root '+screenshot_out+str(x.count)+".png")
232 actual_delay = key_delay
233 if actual_delay == '':
234 actual_delay = def_delay
236 if qt_frontend == 'QT5':
237 xvpar.extend(["-no-jump-pointer"])
239 xvpar.extend(["-xsendevent"])
240 xvpar.extend(["-window", lyx_window_name, "-delay", actual_delay, "-text", keystr])
242 print("Sending \"" + keystr + "\"")
243 subprocess.call(xvpar, stdout = FNULL, stderr = FNULL)
245 Axreg = re.compile(r'^(.*)\\Ax([^\\]*)(.*)$')
246 returnreg = re.compile(r'\\\[Return\](.*)$')
248 # recursive wrapper around sendKeystringLocal()
249 # handling \Ax-entries
250 def sendKeystringAx(line, LYX_PID):
252 saved_delay = key_delay
253 m = Axreg.match(line)
259 # since (.*) is greedy, check prefix for '\Ax' again
260 sendKeystringAx(prefix, LYX_PID)
261 sendKeystringLocal('\Ax', LYX_PID)
263 m2 = returnreg.match(rest)
267 sendKeystringLocal(content + '\[Return]', LYX_PID)
268 key_delay = saved_delay
271 sendKeystringLocal(line, LYX_PID)
274 sendKeystringLocal(content, LYX_PID)
276 sendKeystringLocal(rest, LYX_PID)
279 sendKeystringLocal(line, LYX_PID)
281 controlkeyreg = re.compile(r'^(.*\\\[[A-Z][a-z]+\])(.*\\\[[A-Z][a-z]+\])(.*)$')
282 # Make sure, only one of \[Return], \[Tab], \[Down], \[Home] etc are in one sent line
283 # e.g. split the input line on each keysym
284 def sendKeystringRT(line, LYX_PID):
285 m = controlkeyreg.match(line)
290 sendKeystringRT(first, LYX_PID)
291 sendKeystringRT(second, LYX_PID)
293 sendKeystringRT(third, LYX_PID)
295 sendKeystringAx(line, LYX_PID)
297 def system_retry(num_retry, cmd):
299 rtn = intr_system(cmd)
300 while ( ( i < num_retry ) and ( rtn != 0) ):
302 rtn = intr_system(cmd)
305 print("Command Failed: "+cmd)
310 #intr_system("echo x-session-manager PID: $X_PID.")
311 #intr_system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
312 ####intr_system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
313 print("lyx_window_name = " + lyx_window_name + "\n")
314 intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
315 system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'")
318 lyx_pid = os.environ.get('LYX_PID')
319 print('lyx_pid: ' + str(lyx_pid) + '\n')
320 infilename = os.environ.get('KEYTEST_INFILE')
321 outfilename = os.environ.get('KEYTEST_OUTFILE')
322 max_drop = os.environ.get('MAX_DROP')
323 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
324 screenshot_out = os.environ.get('SCREENSHOT_OUT')
325 lyx_userdir = os.environ.get('LYX_USERDIR')
327 max_loops = os.environ.get('MAX_LOOPS')
328 if max_loops is None:
331 PACKAGE = os.environ.get('PACKAGE')
332 if not PACKAGE is None:
333 print("PACKAGE = " + PACKAGE + "\n")
335 PO_BUILD_DIR = os.environ.get('PO_BUILD_DIR')
336 if not PO_BUILD_DIR is None:
337 print("PO_BUILD_DIR = " + PO_BUILD_DIR + "\n")
339 lyx = os.environ.get('LYX')
343 lyx_exe = os.environ.get('LYX_EXE')
347 xvkbd_exe = os.environ.get('XVKBD_EXE')
348 if xvkbd_exe is None:
351 qt_frontend = os.environ.get('QT_FRONTEND')
352 if qt_frontend is None:
355 locale_dir = os.environ.get('LOCALE_DIR')
356 if locale_dir is None:
359 def_delay = os.environ.get('XVKBD_DELAY')
360 if def_delay is None:
363 file_new_command = os.environ.get('FILE_NEW_COMMAND')
364 if file_new_command is None:
365 file_new_command = "\Afn"
367 ResetCommand = os.environ.get('RESET_COMMAND')
368 if ResetCommand is None:
369 ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
370 #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
372 if lyx_window_name is None:
373 lyx_window_name = 'LyX'
375 print('outfilename: ' + outfilename + '\n')
376 print('max_drop: ' + max_drop + '\n')
378 if infilename is None:
379 print('infilename is None\n')
381 print('Using x=CommandSource\n')
383 print('infilename: ' + infilename + '\n')
384 probability_we_drop_a_command = random.uniform(0, float(max_drop))
385 print('probability_we_drop_a_command: ')
386 print('%s' % probability_we_drop_a_command)
388 x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
389 print('Using x=CommandSourceFromFile\n')
391 outfile = open(outfilename, 'w')
393 if not lyx_pid is None:
395 # Next command is language dependent
396 #sendKeystringRT("\Afn", lyx_pid)
398 write_commands = True
402 #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
408 outfile.writelines(c + '\n')
411 print("Ignoring comment line: " + c)
412 elif c[0:9] == 'TestBegin':
414 lyx_pid=os.popen("pidof " + lyx).read()
416 print("Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n")
417 intr_system("killall " + lyx, True)
419 intr_system("killall -KILL " + lyx, True)
421 print("Starting LyX . . .")
422 if lyx_userdir is None:
423 intr_system(lyx_exe + c[9:] + "&")
425 intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
428 old_lyx_window_name = "not set"
429 print("Waiting for LyX to show up . . .")
431 lyx_pid=os.popen("pidof " + lyx).read().rstrip()
432 if lyx_pid != old_lyx_pid:
433 print('lyx_pid=' + lyx_pid)
434 old_lyx_pid = lyx_pid
436 lyx_window_name=os.popen("wmctrl -l -p | grep ' " + str(lyx_pid) + " ' | cut -d ' ' -f 1").read().rstrip()
437 if old_lyx_window_name != lyx_window_name:
438 print('lyx_win=' + lyx_window_name, '\n')
439 old_lyx_window_name = lyx_window_name
440 if lyx_window_name != "":
446 print('Timeout: could not start ' + lyx_exe, '\n')
449 print('lyx_pid: ' + lyx_pid)
450 print('lyx_win: ' + lyx_window_name + '\n')
451 sendKeystringLocal("\C\[Home]", lyx_pid)
452 elif c[0:5] == 'Sleep':
453 print("Sleeping for " + c[6:] + " seconds\n")
454 time.sleep(float(c[6:]))
455 elif c[0:4] == 'Exec':
460 outfile = open(outfilename + '+', 'w')
462 elif c == 'RaiseLyx':
465 elif c[0:4] == 'KK: ':
467 sendKeystringRT(c[4:], lyx_pid)
469 ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
471 print('No path /proc/xxxx/status, exiting')
473 print('No path /proc/' + lyx_pid + '/status, exiting')
475 elif c[0:4] == 'KD: ':
476 key_delay = c[4:].rstrip('\n')
477 print('Setting DELAY to ' + key_delay + '.\n')
480 sendKeystringRT(ResetCommand, lyx_pid)
481 elif c[0:6] == 'Assert':
483 result = intr_system(cmd)
484 failed = failed or (result != 0)
485 print("result=" + str(result) + ", failed=" + str(failed))
486 elif c[0:7] == 'TestEnd':
489 print("LyX instance not found because of crash or assert !\n")
492 print(" ------------ Forcing quit of lyx instance: " + str(lyx_pid) + " ------------")
493 # \Ax Enter command line is sometimes blocked
494 # \[Escape] works after this
495 sendKeystringAx("\Ax\[Escape]", lyx_pid)
496 # now we should be outside any dialog
497 # and so the function lyx-quit should work
498 sendKeystringLocal("\Cq", lyx_pid)
501 # probably waiting for Save/Discard/Abort, we select 'Discard'
502 sendKeystringRT("\[Tab]\[Return]", lyx_pid)
506 while not lyx_dead():
509 print("LyX still up, killing process and waiting for it to die...\n")
510 intr_system("kill -9 " + str(lyx_pid), True);
513 print("Executing " + cmd)
514 result = intr_system(cmd)
515 failed = failed or (result != 0)
516 print("result=" + str(result) + ", failed=" + str(failed))
517 elif c[0:4] == 'Lang':
518 lang = c[5:].rstrip()
519 print("Setting LANG=" + lang + "\n")
520 os.environ['LANG'] = lang
521 os.environ['LC_ALL'] = lang
522 # If it doesn't exist, create a link <locale_dir>/<country-code>/LC_MESSAGES/lyx<version-suffix>.mo
523 # pointing to the corresponding .gmo file. Needed to let lyx find the right translation files.
524 # See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg165613.html
525 idx = lang.rfind(".")
531 print("Setting LANGUAGE=" + ccode + "\n")
532 os.environ['LANGUAGE'] = ccode
536 short_code = lang[0:idx]
539 lyx_dir = os.popen("dirname \"" + lyx_exe + "\"").read().rstrip()
541 # on cmake-build there is no Makefile in this directory
542 # so PACKAGE has to be provided
543 if os.path.exists(lyx_dir + "/Makefile"):
544 print("Executing: grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'")
545 lyx_name = os.popen("grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'").read().rstrip()
547 print('Could not determine PACKAGE name needed for translations\n')
551 intr_system("mkdir -p " + locale_dir + "/" + ccode + "/LC_MESSAGES")
552 intr_system("rm -f " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
553 if PO_BUILD_DIR is None:
554 if lyx_dir[0:3] == "../":
555 rel_dir = "../../" + lyx_dir
558 intr_system("ln -s " + rel_dir + "/../po/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
560 intr_system("ln -s " + PO_BUILD_DIR + "/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
562 print("Unrecognised Command '" + c + "'\n")
565 print("Test case terminated: ")