]> git.lyx.org Git - lyx.git/blob - development/autotests/keytest.py
Rename the minted 'lang' external template option as 'language'
[lyx.git] / development / autotests / keytest.py
1 #!/usr/bin/python
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
7 #
8 # Adapted by Tommaso Cucinotta from the original MonKey Test by
9 # John McCabe-Dansted.
10
11 from __future__ import print_function
12 import random
13 import os
14 import re
15 import sys
16 import time
17 import tempfile
18 import shutil
19
20 #from subprocess import call
21 import subprocess
22
23 print('Beginning keytest.py')
24
25 FNULL = open('/dev/null', 'w')
26
27 key_delay = ''
28
29 # Ignore status == "dead" if this is set. Used at the last commands after "\Cq"
30 dead_expected = False
31
32 def die(excode, text):
33     if text != "":
34         print(text)
35     sys.stdout.flush()
36     os._exit(excode)
37
38 class CommandSource:
39
40     def __init__(self):
41         keycode = [
42             "\[Left]",
43             '\[Right]',
44             '\[Down]',
45             '\[Up]',
46             '\[BackSpace]',
47             '\[Delete]',
48             '\[Escape]',
49             ]
50         keycode[:0] = keycode
51         keycode[:0] = keycode
52
53         keycode[:0] = ['\\']
54
55         for k in range(97, 123):
56             keycode[:0] = chr(k)
57
58         for k in range(97, 123):
59             keycode[:0] = ["\A" + chr(k)]
60
61         for k in range(97, 123):
62             keycode[:0] = ["\A" + chr(k)]
63
64         for k in range(97, 123):
65             keycode[:0] = ["\C" + chr(k)]
66
67         self.keycode = keycode
68         self.count = 0
69         self.count_max = 1999
70
71     def getCommand(self):
72         self.count = self.count + 1
73         if self.count % 200 == 0:
74             return 'RaiseLyx'
75         elif self.count > self.count_max:
76             die(0, "")
77         else:
78             keystr = ''
79             for k in range(1, 2):
80                 keystr = keystr + self.keycode[random.randint(1,
81                         len(self.keycode)) - 1]
82             return 'KK: ' + keystr
83
84
85 class CommandSourceFromFile(CommandSource):
86
87     def __init__(self, filename, p):
88
89         self.infile = open(filename, 'r')
90         self.lines = self.infile.readlines()
91         self.infile.close()
92         linesbak = self.lines
93         self.p = p
94         print(p, self.p, 'self.p')
95         self.i = 0
96         self.count = 0
97         self.loops = 0
98
99         # Now we start randomly dropping lines, which we hope are redundant
100         # p is the probability that any given line will be removed
101
102         if p > 0.001:
103             if random.uniform(0, 1) < 0.5:
104                 print('randomdrop_independant\n')
105                 self.randomdrop_independant()
106             else:
107                 print('randomdrop_slice\n')
108                 self.randomdrop_slice()
109         if screenshot_out is None:
110             count_atleast = 100
111         else:
112             count_atleast = 1
113         self.max_count = max(len(self.lines) + 20, count_atleast)
114         if len(self.lines) < 1:
115             self.lines = linesbak
116
117     def randomdrop_independant(self):
118         p = self.p
119
120         # The next couple of lines are to ensure that at least one line is dropped
121
122         drop = random.randint(0, len(self.lines) - 1)
123         del self.lines[drop]
124         #p = p - 1 / len(self.lines)
125         origlines = self.lines
126         self.lines = []
127         for l in origlines:
128             if random.uniform(0, 1) < self.p:
129                 print('Randomly dropping line ' + l + '\n')
130             else:
131                 self.lines.append(l)
132         print('LINES\n')
133         print(self.lines)
134         sys.stdout.flush()
135
136     def randomdrop_slice(self):
137         lines = self.lines
138         if random.uniform(0, 1) < 0.4:
139             lines.append(lines[0])
140             del lines[0]
141         num_lines = len(lines)
142         max_drop = max(5, num_lines / 5)
143         num_drop = random.randint(1, 5)
144         drop_mid = random.randint(0, num_lines)
145         drop_start = max(drop_mid - num_drop / 2, 0)
146         drop_end = min(drop_start + num_drop, num_lines)
147         print(drop_start, drop_mid, drop_end)
148         print(lines)
149         del lines[drop_start:drop_end]
150         print(lines)
151         self.lines = lines
152
153     def getCommand(self):
154         if self.count >= self.max_count:
155             die(0, "")
156         if self.i >= len(self.lines):
157             self.loops = self.loops + 1
158             if self.loops >= int(max_loops):
159                 return None
160             self.i = 0
161             return 'Loop'
162         line = self.lines[self.i].rstrip('\n')
163         self.count = self.count + 1
164         self.i = self.i + 1
165         return line
166
167 class ControlFile:
168
169     def __init__(self):
170         self.control = re.compile(r'^(C[ONPpRrC]):\s*(.*)$')
171         self.fileformat = re.compile(r'^((\>\>?)[,\s]\s*)?([^\s]+)\s*$')
172         self.cntrname = None
173         self.cntrfile = None
174
175     def open(self, filename):
176         if not self.cntrfile is None:
177             self.cntrfile.close()
178             self.cntrfile = None
179             self.cntrname = None
180         m = self.fileformat.match(filename)
181         if m:
182             type = m.group(2)
183             filename = m.group(3)
184             if type == '>>':
185                 append = True
186             else:
187                 append = False
188         else:
189             append = False
190         self.cntrname = filename
191         if append:
192             self.cntrfile = open(filename, 'a')
193         else:
194             self.cntrfile = open(filename, 'w')
195
196     def close(self):
197         if not self.cntrfile is None:
198             self.cntrfile.close()
199             self.cntrfile = None
200             self.cntrname = None
201     # make the method below 'private'
202     def __addline(self, pat):
203         self.cntrfile.writelines(pat + "\n")
204
205     def getfname(self):
206         return self.cntrname
207
208     def dispatch(self, c):
209         m = self.control.match(c)
210         if not m:
211             return False
212         command = m.group(1)
213         text = m.group(2)
214         if command == "CO":
215             self.open(text);
216         elif command == "CC":
217             self.close()
218         else:
219             if not self.cntrfile is None:
220                 if command == "CN":
221                     self.__addline("Comment: " + text)
222                 elif command == "CP":
223                     self.__addline("Simple: " + text)
224                 elif command == "Cp":
225                     self.__addline("ErrSimple: " + text)
226                 elif command == "CR":
227                     self.__addline("Regex: " + text)
228                 elif command == "Cr":
229                     self.__addline("ErrRegex: " + text)
230                 else:
231                     die(1,"Error, Unrecognised Command '" + command + "'")
232         return True
233
234
235 def get_proc_pid(proc_name):
236     pid=os.popen("pidof " + proc_name).read().rstrip()
237     return pid
238
239 wlistreg = re.compile(r'^(0x[0-9a-f]{5,9})\s+[^\s]+\s+([0-9]+)\s.*$')
240 def get_proc_win_id(pid, ignoreid):
241     nlist = os.popen("wmctrl -l -p").read()
242     wlist = nlist.split("\n")
243     for item in wlist:
244         m = wlistreg.match(item)
245         if m:
246             win_id = m.group(1)
247             win_pid = m.group(2)
248             if win_pid == pid:
249                 if win_id != ignoreid:
250                     return win_id
251     return None
252
253 def lyx_exists():
254     if lyx_pid is None:
255         return False
256     fname = '/proc/' + lyx_pid + '/status'
257     return os.path.exists(fname)
258
259
260 # Interruptible os.system()
261 def intr_system(cmd, ignore_err = False):
262     print("Executing " + cmd)
263     # Assure the output of cmd does not overhaul
264     sys.stdout.flush()
265     ret = os.system(cmd)
266     if os.WIFSIGNALED(ret):
267         raise KeyboardInterrupt
268     if ret != 0 and not ignore_err:
269         raise BaseException("command failed:" + cmd)
270     return ret
271
272 statreg = re.compile(r'^State:.*\(([a-z]+)\)')
273
274 resstatus = []
275 def printresstatus():
276     for line in resstatus:
277         line = line.rstrip()
278         print("    " + line.rstrip())
279     print('End of /proc-lines')
280
281 def lyx_status_retry(pid):
282     resstatus = []
283     if pid is None:
284         print('Pid is None')
285         return "dead"
286     fname = '/proc/' + pid + '/status'
287     status = "dead"
288     try:
289         f = open(fname)
290         found = False
291         for line in f:
292             resstatus.extend([line])
293             m = statreg.match(line)
294             if m:
295                 status = m.group(1)
296                 found = True
297         f.close()
298         if not found:
299             return "retry"
300         return status
301     except IOError as e:
302         print("I/O error({0}): {1}".format(e.errno, e.strerror))
303         return "dead"
304     except:
305         print("Unexpected error:", sys.exc_info()[0])
306         return "dead"
307     print('This should not happen')
308     return status
309
310 def lyx_status(pid):
311     count = 0
312     while 1:
313         status = lyx_status_retry(pid)
314         if status != "retry":
315             break
316         if count == 0:
317             print('Retrying check for status')
318         count += 1
319         time.sleep(0.01)
320     if count > 1:
321         print('Retried to read status ' + str(count) + ' times')
322     #print('lys_status() returning ' + status)
323     return status
324
325 # Return true if LyX (identified via lyx_pid) is sleeping
326 def lyx_sleeping(LYX_PID):
327     return lyx_status(LYX_PID) == "sleeping"
328
329 # Return true if LyX (identified via lyx_pid) is zombie
330 def lyx_zombie(LYX_PID):
331     return lyx_status(LYX_PID) == "zombie"
332
333 def lyx_dead(LYX_PID):
334     status = lyx_status(LYX_PID)
335     return (status == "dead") or (status == "zombie")
336
337 def wait_until_lyx_sleeping(LYX_PID):
338     before_secs = time.time()
339     while True:
340         status = lyx_status(LYX_PID)
341         if status == "sleeping":
342             return True
343         if (status == "dead") or (status == "zombie"):
344             printresstatus()
345             if dead_expected:
346                 print('Lyx died while waiting for status == sleeping')
347                 return False
348             else:
349                 die(1,"Lyx is dead, exiting")
350         if time.time() - before_secs > 180:
351             # Do profiling, but sysprof has no command line interface?
352             # intr_system("killall -KILL lyx")
353             printresstatus()
354             die(1,"Killing due to freeze (KILL_FREEZE)")
355         time.sleep(0.02)
356     # Should be never reached
357     print('Wait for sleeping ends unexpectedly')
358     return False
359
360 def sendKeystringLocal(keystr, LYX_PID):
361     is_sleeping = wait_until_lyx_sleeping(LYX_PID)
362     if not is_sleeping:
363         print("Not sending \"" + keystr + "\"")
364         return
365     if not screenshot_out is None:
366         print('Making Screenshot: ' + screenshot_out + ' OF ' + infilename)
367         time.sleep(0.2)
368         intr_system('import -window root '+screenshot_out+str(x.count)+".png")
369         time.sleep(0.1)
370     actual_delay = key_delay
371     if actual_delay == '':
372         actual_delay = def_delay
373     xvpar = [xvkbd_exe]
374     if qt_frontend == 'QT5':
375         xvpar.extend(["-jump-pointer", "-no-back-pointer"])
376     else:
377         xvpar.extend(["-xsendevent"])
378     if lyx_other_window_name is None:
379         xvpar.extend(["-window", lyx_window_name])
380     else:
381         xvpar.extend(["-window", lyx_other_window_name])
382     xvpar.extend(["-delay", actual_delay, "-text", keystr])
383     print("Sending \"" + keystr + "\"")
384     subprocess.call(xvpar, stdout = FNULL, stderr = FNULL)
385     sys.stdout.flush()
386
387 def extractmultiple(line, regex):
388     #print("extractmultiple " + line)
389     res = ["", ""]
390     m = regex.match(line)
391     if m:
392         chr = m.group(1)
393         if m.group(2) == "":
394             res[0] = chr
395             res[1] = ""
396         else:
397             norm = extractmultiple(m.group(2), regex)
398             res[0] = chr + norm[0]
399             res[1] = norm[1]
400     else:
401         res[0] = ""
402         res[1] = line
403     return res
404
405 normal_re = re.compile(r'^([^\\]|\\\\)(.*)$')
406 def extractnormal(line):
407     # collect non-special chars from start of line
408     return extractmultiple(line, normal_re)
409
410 modifier_re = re.compile(r'^(\\[CAS])(.+)$')
411 def extractmodifiers(line):
412     # collect modifiers like '\\A' at start of line
413     return extractmultiple(line, modifier_re)
414
415 special_re = re.compile(r'^(\\\[[A-Z][a-z0-9]+\])(.*)$')
416 def extractsingle(line):
417     # check for single key following a modifier
418     # either ascii like 'a'
419     # or special like '\[Return]'
420     res = [False, "", ""]
421     m = normal_re.match(line)
422     if m:
423         res[0] = False
424         res[1] = m.group(1)
425         res[2] = m.group(2)
426     else:
427         m = special_re.match(line)
428         if m:
429             res[0] = True
430             res[1] = m.group(1)
431             res[2] = m.group(2)
432         else:
433             die(1, "Undecodable key for line \'" + line + "\"")
434     return res
435
436 def sendKeystring(line, LYX_PID):
437     if line == "":
438         return
439     normalchars = extractnormal(line)
440     line = normalchars[1]
441     if normalchars[0] != "":
442         sendKeystringLocal(normalchars[0], LYX_PID)
443     if line == "":
444         return
445     modchars = extractmodifiers(line)
446     line = modchars[1]
447     if line == "":
448         die(1, "Missing modified key")
449     modifiedchar = extractsingle(line)
450     line = modifiedchar[2]
451     special = modchars[0] != "" or modifiedchar[0]
452     sendKeystringLocal(modchars[0] + modifiedchar[1], LYX_PID)
453     if special:
454         # give the os time to update the status info (in /proc)
455         time.sleep(controlkey_delay)
456     sendKeystring(line, LYX_PID)
457
458 def system_retry(num_retry, cmd):
459     i = 0
460     rtn = intr_system(cmd, True)
461     while ( ( i < num_retry ) and ( rtn != 0) ):
462         i = i + 1
463         rtn = intr_system(cmd, True)
464         time.sleep(1)
465     if ( rtn != 0 ):
466         print("Command Failed: "+cmd)
467         die(1," EXITING!")
468
469 def RaiseWindow():
470     #intr_system("echo x-session-manager PID: $X_PID.")
471     #intr_system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
472     ####intr_system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
473     print("lyx_window_name = " + lyx_window_name + "\n")
474     intr_system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
475     system_retry(30, "wmctrl -i -a '"+lyx_window_name+"'")
476
477 class Shortcuts:
478
479     def __init__(self):
480         self.shortcut_entry = re.compile(r'^\s*"([^"]+)"\s*\"([^"]+)\"')
481         self.bindings = {}
482         self.bind = re.compile(r'^\s*\\bind\s+"([^"]+)"')
483         if lyx_userdir_ver is None:
484             self.dir = lyx_userdir
485         else:
486             self.dir = lyx_userdir_ver
487
488     def __UseShortcut(self, c):
489         m = self.shortcut_entry.match(c)
490         if m:
491             sh = m.group(1)
492             fkt = m.group(2)
493             self.bindings[sh] = fkt
494         else:
495             die(1, "cad shortcut spec(" + c + ")")
496
497     def __PrepareShortcuts(self):
498         if not self.dir is None:
499             tmp = tempfile.NamedTemporaryFile(suffix='.bind', delete=False)
500             try:
501                 old = open(self.dir + '/bind/user.bind', 'r')
502             except IOError as e:
503                 old = None
504             if not old is None:
505                 lines = old.read().split("\n")
506                 old.close()
507                 bindfound = False
508                 for line in lines:
509                     m = self.bind.match(line)
510                     if m:
511                         bindfound = True
512                         val = m.group(1)
513                         if val in self.bindings:
514                             if self.bindings[val] != "":
515                                 tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
516                                 self.bindings[val] = ""
517                         else:
518                             tmp.write(line + '\n')
519                     elif not bindfound:
520                         tmp.write(line + '\n')
521             else:
522                 tmp.writelines(
523                     '## This file is used for keytests only\n\n' +
524                     'Format 4\n\n'
525                 )
526             for val in self.bindings:
527                 if not self.bindings[val] is None:
528                     if  self.bindings[val] != "":
529                         tmp.write("\\bind \"" + val + "\" \"" + self.bindings[val] + "\"\n")
530                         self.bindings[val] = ""
531             tmp.close()
532             shutil.move(tmp.name, self.dir + '/bind/user.bind')
533         else:
534             print("User dir not specified")
535
536     def dispatch(self, c):
537         if c[0:12] == 'UseShortcut ':
538             self.__UseShortcut(c[12:])
539         elif c == 'PrepareShortcuts':
540             print('Preparing usefull sortcuts for tests')
541             self.__PrepareShortcuts()
542         else:
543             return False
544         return True
545
546 lyx_pid = os.environ.get('LYX_PID')
547 print('lyx_pid: ' + str(lyx_pid) + '\n')
548 infilename = os.environ.get('KEYTEST_INFILE')
549 outfilename = os.environ.get('KEYTEST_OUTFILE')
550 max_drop = os.environ.get('MAX_DROP')
551 lyx_window_name = os.environ.get('LYX_WINDOW_NAME')
552 lyx_other_window_name = None
553 screenshot_out = os.environ.get('SCREENSHOT_OUT')
554 lyx_userdir = os.environ.get('LYX_USERDIR')
555 lyx_userdir_ver = os.environ.get('LYX_USERDIR_23x')
556
557 max_loops = os.environ.get('MAX_LOOPS')
558 if max_loops is None:
559     max_loops = 3
560
561 extra_path = os.environ.get('EXTRA_PATH')
562 if not extra_path is None:
563   os.environ['PATH'] = extra_path + os.pathsep + os.environ['PATH']
564   print("Added " + extra_path + " to path")
565   print(os.environ['PATH'])
566
567 PACKAGE = os.environ.get('PACKAGE')
568 if not PACKAGE is None:
569   print("PACKAGE = " + PACKAGE + "\n")
570
571 PO_BUILD_DIR = os.environ.get('PO_BUILD_DIR')
572 if not PO_BUILD_DIR is None:
573   print("PO_BUILD_DIR = " + PO_BUILD_DIR + "\n")
574
575 lyx = os.environ.get('LYX')
576 if lyx is None:
577     lyx = "lyx"
578
579 lyx_exe = os.environ.get('LYX_EXE')
580 if lyx_exe is None:
581     lyx_exe = lyx
582
583 xvkbd_exe = os.environ.get('XVKBD_EXE')
584 if xvkbd_exe is None:
585     xvkbd_exe = "xvkbd"
586
587 qt_frontend = os.environ.get('QT_FRONTEND')
588 if qt_frontend is None:
589     qt_frontend = 'QT4'
590 if qt_frontend == 'QT5':
591     controlkey_delay = 0.01
592 else:
593     controlkey_delay = 0.4
594
595 locale_dir = os.environ.get('LOCALE_DIR')
596 if locale_dir is None:
597     locale_dir = '.'
598
599 def_delay = os.environ.get('XVKBD_DELAY')
600 if def_delay is None:
601     if qt_frontend == 'QT5':
602         def_delay = '1'
603     else:
604         def_delay = '1'
605
606 file_new_command = os.environ.get('FILE_NEW_COMMAND')
607 if file_new_command is None:
608     file_new_command = "\Afn"
609
610 ResetCommand = os.environ.get('RESET_COMMAND')
611 if ResetCommand is None:
612     ResetCommand = "\[Escape]\[Escape]\[Escape]\[Escape]" + file_new_command
613     #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
614
615 if lyx_window_name is None:
616     lyx_window_name = 'LyX'
617
618 print('outfilename: ' + outfilename + '\n')
619 print('max_drop: ' + max_drop + '\n')
620
621 if infilename is None:
622     print('infilename is None\n')
623     x = CommandSource()
624     print('Using x=CommandSource\n')
625 else:
626     print('infilename: ' + infilename + '\n')
627     probability_we_drop_a_command = random.uniform(0, float(max_drop))
628     print('probability_we_drop_a_command: ')
629     print('%s' % probability_we_drop_a_command)
630     print('\n')
631     x = CommandSourceFromFile(infilename, probability_we_drop_a_command)
632     print('Using x=CommandSourceFromFile\n')
633
634 outfile = open(outfilename, 'w')
635
636 if not lyx_pid is None:
637     RaiseWindow()
638     # Next command is language dependent
639     #sendKeystring("\Afn", lyx_pid)
640
641 write_commands = True
642 failed = False
643 lineempty = re.compile(r'^\s*$')
644 marked = ControlFile()
645 shortcuts = Shortcuts()
646 while not failed:
647     #intr_system('echo -n LOADAVG:; cat /proc/loadavg')
648     c = x.getCommand()
649     if c is None:
650         break
651
652     # Do not strip trailing spaces, only check for 'empty' lines
653     if lineempty.match(c):
654         continue
655     outfile.writelines(c + '\n')
656     outfile.flush()
657     if marked.dispatch(c):
658         continue
659     elif shortcuts.dispatch(c):
660         continue
661     if c[0] == '#':
662         print("Ignoring comment line: " + c)
663     elif c[0:9] == 'TestBegin':
664         print("\n")
665         lyx_pid=get_proc_pid(lyx)
666         if lyx_pid != "":
667             print("Found running instance(s) of LyX: " + lyx_pid + ": killing them all\n")
668             intr_system("killall " + lyx, True)
669             time.sleep(0.5)
670             intr_system("killall -KILL " + lyx, True)
671             time.sleep(0.2)
672         print("Starting LyX . . .")
673         if lyx_userdir is None:
674             intr_system(lyx_exe + c[9:] + "&")
675         else:
676             intr_system(lyx_exe + " -userdir " + lyx_userdir + " " + c[9:] + "&")
677         count = 10
678         old_lyx_pid = "-7"
679         old_lyx_window_name = None
680         print("Waiting for LyX to show up . . .")
681         while count > 0:
682             lyx_pid=get_proc_pid(lyx)
683             if lyx_pid != old_lyx_pid:
684                 print('lyx_pid=' + lyx_pid)
685                 old_lyx_pid = lyx_pid
686             if lyx_pid != "":
687                 lyx_window_name=get_proc_win_id(lyx_pid, "")
688                 if not lyx_window_name is None:
689                     if old_lyx_window_name != lyx_window_name:
690                         print('lyx_win=' + lyx_window_name, '\n')
691                         old_lyx_window_name = lyx_window_name
692                     break
693             else:
694                 count = count - 1
695             time.sleep(0.5)
696         if count <= 0:
697             print('Timeout: could not start ' + lyx_exe, '\n')
698             sys.stdout.flush()
699             failed = True
700         else:
701             print('lyx_pid: ' + lyx_pid)
702             print('lyx_win: ' + lyx_window_name)
703             dead_expected = False
704             sendKeystring("\C\[Home]", lyx_pid)
705     elif c[0:5] == 'Sleep':
706         print("Sleeping for " + c[6:] + " seconds")
707         time.sleep(float(c[6:]))
708     elif c[0:4] == 'Exec':
709         cmd = c[5:].rstrip()
710         intr_system(cmd)
711     elif c == 'Loop':
712         outfile.close()
713         outfile = open(outfilename + '+', 'w')
714         print('Now Looping')
715     elif c == 'RaiseLyx':
716         print('Raising Lyx')
717         RaiseWindow()
718     elif c[0:4] == 'KK: ':
719         if lyx_exists():
720             sendKeystring(c[4:], lyx_pid)
721         else:
722             ##intr_system('killall lyx; sleep 2 ; killall -9 lyx')
723             if lyx_pid is None:
724               die(1, 'No path /proc/xxxx/status, exiting')
725             else:
726               die(1, 'No path /proc/' + lyx_pid + '/status, exiting')
727     elif c[0:4] == 'KD: ':
728         key_delay = c[4:].rstrip('\n')
729         print('Setting DELAY to ' + key_delay)
730     elif c == 'Loop':
731         RaiseWindow()
732         sendKeystring(ResetCommand, lyx_pid)
733     elif c[0:6] == 'Assert':
734         cmd = c[7:].rstrip()
735         result = intr_system(cmd, True)
736         failed = failed or (result != 0)
737         print("result=" + str(result) + ", failed=" + str(failed))
738     elif c[0:15] == 'TestEndWithKill':
739         marked.close()
740         cmd = c[16:].rstrip()
741         if lyx_dead(lyx_pid):
742             print("LyX instance not found because of crash or assert !\n")
743             failed = True
744         else:
745             print("    ------------    Forcing kill of lyx instance: " + str(lyx_pid) + "    ------------")
746             # This line below is there only to allow lyx to update its log-file
747             sendKeystring("\[Escape]", lyx_pid)
748             dead_expected = True
749             while not lyx_dead(lyx_pid):
750                 intr_system("kill -9 " + str(lyx_pid), True);
751                 time.sleep(0.5)
752             if cmd != "":
753                 print("Executing " + cmd)
754                 result = intr_system(cmd, True)
755                 failed = failed or (result != 0)
756                 print("result=" + str(result) + ", failed=" + str(failed))
757             else:
758                 print("failed=" + str(failed))
759     elif c[0:7] == 'TestEnd':
760          #lyx_other_window_name = None
761         if lyx_dead(lyx_pid):
762             print("LyX instance not found because of crash or assert !\n")
763             marked.close()
764             failed = True
765         else:
766             print("    ------------    Forcing quit of lyx instance: " + str(lyx_pid) + "    ------------")
767             # \[Escape]+ should work as RESET focus to main window
768             sendKeystring("\[Escape]\[Escape]\[Escape]\[Escape]", lyx_pid)
769             # now we should be outside any dialog
770             # and so the function lyx-quit should work
771             sendKeystring("\Cq", lyx_pid)
772             marked.dispatch('CP: action=lyx-quit')
773             marked.close()
774             time.sleep(0.5)
775             dead_expected = True
776             is_sleeping = wait_until_lyx_sleeping(lyx_pid)
777             if is_sleeping:
778                 print('wait_until_lyx_sleeping() indicated "sleeping"')
779                 # For a short time lyx-status is 'sleeping', even if it is nearly dead.
780                 # Without the wait below, the \[Tab]-char is sent to nirvana
781                 # causing a 'beep'
782                 time.sleep(0.5)
783                 # probably waiting for Save/Discard/Abort, we select 'Discard'
784                 sendKeystring("\[Tab]\[Return]", lyx_pid)
785                 lcount = 0
786             else:
787                 lcount = 1
788             while not lyx_dead(lyx_pid):
789                 lcount = lcount + 1
790                 if lcount > 20:
791                     print("LyX still up, killing process and waiting for it to die...\n")
792                     intr_system("kill -9 " + str(lyx_pid), True);
793                 time.sleep(0.5)
794         cmd = c[8:].rstrip()
795         if cmd != "":
796             print("Executing " + cmd)
797             result = intr_system(cmd, True)
798             failed = failed or (result != 0)
799             print("result=" + str(result) + ", failed=" + str(failed))
800         else:
801             print("failed=" + str(failed))
802     elif c[0:4] == 'Lang':
803         lang = c[5:].rstrip()
804         print("Setting LANG=" + lang)
805         os.environ['LANG'] = lang
806         os.environ['LC_ALL'] = lang
807 # If it doesn't exist, create a link <locale_dir>/<country-code>/LC_MESSAGES/lyx<version-suffix>.mo
808 # pointing to the corresponding .gmo file. Needed to let lyx find the right translation files.
809 # See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg165613.html
810         idx = lang.rfind(".")
811         if idx != -1:
812             ccode = lang[0:idx]
813         else:
814             ccode = lang
815
816         print("Setting LANGUAGE=" + ccode)
817         os.environ['LANGUAGE'] = ccode
818
819         idx = lang.find("_")
820         if idx != -1:
821             short_code = lang[0:idx]
822         else:
823             short_code = ccode
824         lyx_dir = os.popen("dirname \"" + lyx_exe + "\"").read().rstrip()
825         if PACKAGE is None:
826           # on cmake-build there is no Makefile in this directory
827           # so PACKAGE has to be provided
828           if os.path.exists(lyx_dir + "/Makefile"):
829             print("Executing: grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'")
830             lyx_name = os.popen("grep 'PACKAGE =' " + lyx_dir + "/Makefile | sed -e 's/PACKAGE = \(.*\)/\\1/'").read().rstrip()
831           else:
832             print('Could not determine PACKAGE name needed for translations\n')
833             failed = True
834         else:
835           lyx_name = PACKAGE
836         intr_system("mkdir -p " + locale_dir + "/" + ccode + "/LC_MESSAGES")
837         intr_system("rm -f " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
838         if PO_BUILD_DIR is None:
839             if lyx_dir[0:3] == "../":
840                 rel_dir = "../../" + lyx_dir
841             else:
842                 rel_dir = lyx_dir
843             intr_system("ln -s " + rel_dir + "/../po/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
844         else:
845             intr_system("ln -s " + PO_BUILD_DIR + "/" + short_code + ".gmo " + locale_dir + "/" + ccode + "/LC_MESSAGES/" + lyx_name + ".mo")
846     else:
847         print("Unrecognised Command '" + c + "'\n")
848         failed = True
849
850 print("Test case terminated: ", end = '')
851 if failed:
852     die(1,"FAIL")
853 else:
854     die(0, "Ok")