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