]> git.lyx.org Git - lyx.git/blob - development/keystest/keytest.py
Flags
[lyx.git] / development / keystest / keytest.py
1 #!/usr/bin/env python
2 #This script generated hundreds of random keypresses per second,
3 #  and sends them to the lyx window
4 #It requires xvkbd and wmctrl
5 #It generates a log of the KEYCODES it sends as development/keystest/out/KEYCODES
6
7 import random
8 import os 
9 import re
10 import sys
11
12 print "Beginning keytest.py"
13
14
15 class CommandSource:
16         def __init__(self):
17                 keycode=["\[Left]",'\[Right]','\[Down]','\[Up]','\[BackSpace]','\[Delete]','\[Escape]']
18                 keycode[:0]=keycode
19                 keycode[:0]=keycode
20
21                 keycode[:0]=['\\']
22
23                 for k in range(97, 123):
24                   keycode[:0]=chr(k)
25
26                 for k in range(97, 123):
27                   keycode[:0]=["\A"+chr(k)]
28
29                 for k in range(97, 123):
30                   keycode[:0]=["\A"+chr(k)]
31
32                 for k in range(97, 123):
33                   keycode[:0]=["\C"+chr(k)]
34
35                 self.keycode=keycode;
36                 self.count=0;
37                 self.count_max=1999;
38
39         def getCommand(self):
40                 self.count=self.count+1;
41                 #if self.count > self.count_max:
42                 if self.count%200==0:
43                         #self.count=0
44                         return ("RaiseLyx")
45                 elif self.count > self.count_max:
46                         os._exit(0)
47                 else:
48                         keystr=""
49                         for k in range(1,2):
50                                 keystr=keystr+self.keycode[random.randint(1,len(self.keycode))-1]
51                         return "KK: "+keystr
52
53 class CommandSourceFromFile(CommandSource):
54         def __init__(self,filename,p):
55                 self.infile=open(filename,'r')
56                 self.lines=self.infile.readlines()
57                 self.p=p
58                 self.i=0
59                 self.count=0
60                 self.loops=0
61                 #Now we start randomly dropping lines, which we hope are redundant
62                 #p is the probability that any given line will be removed
63                 if (p>0):
64                         #The next couple of lines are to ensure that at least one line is dropped
65                         drop=random.randint(0,len(self.lines)-1)
66                         del self.lines[drop]
67                         p=p-(1/len(self.lines))
68                         j=0
69                         origlines=self.lines
70                         self.lines=[];
71                         for l in origlines:
72                                 if random.uniform(0,1) < self.p:
73                                         print "Randomly dropping line "+l+"\n"
74                                 else:
75                                         self.lines.append(l)
76                         print "LINES\n"
77                         print self.lines
78                         sys.stdout.flush()
79                         os.system("sleep 2")
80                 
81         def getCommand(self):
82                 if self.i >= len(self.lines):
83                         if self.count >= 100 or self.loops>1:
84                                 os.system("sleep 1")
85                                 os._exit(0)
86                         else:
87                                 self.loops=self.loops+1
88                                 self.i=0
89                                 return("Loop")
90                 line=self.lines[self.i]
91                 print "Line read: <<"+line+">>\n"
92                 self.count=self.count+1
93                 self.i=self.i+1
94                 return(line.rstrip())
95
96 def sendKeystring(keystr,LYX_PID):
97         print "sending keystring "+keystr+"\n"
98         if not re.match(".*\w.*", keystr):
99                 print ("print ."+keystr+".\n")
100                 keystr="a"
101         os.system("while ( test -e /proc/$LYX_PID/status && ! grep 'tate.*[(]sleeping[)]' /proc/$LYX_PID/status); do echo -n . ; sleep 0.02; done")
102         cmd="xvkbd -xsendevent -text '"+keystr+"';sleep 0.03"
103         sys.stdout.flush()
104         os.system(cmd)
105         sys.stdout.flush()
106
107 def RaiseWindow():
108         os.system("echo x-session-manager PID: $X_PID.")
109         os.system("echo x-session-manager open files: `lsof -p $X_PID | grep ICE-unix | wc -l`")
110         os.system("wmctrl -l | ( grep '"+lyx_window_name+"' || ( killall lyx ; sleep 1 ; killall -9 lyx ))")
111         os.system("wmctrl -R '"+lyx_window_name+"' ;sleep 0.1")
112         
113                         
114 lyx_pid=os.environ.get("LYX_PID")
115 print("lyx_pid: "+lyx_pid+"\n");
116 infilename=os.environ.get("KEYTEST_INFILE")
117 outfilename=os.environ.get("KEYTEST_OUTFILE")
118 max_drop=os.environ.get("MAX_DROP")
119 lyx_window_name=os.environ.get("LYX_WINDOW_NAME");
120
121 file_new_command=os.environ.get("FILE_NEW_COMMAND");
122 if file_new_command is None:
123         file_new_command="\Afn"
124
125 ResetCommand=os.environ.get("RESET_COMMAND");
126 if ResetCommand is None:
127         ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]"+file_new_command
128         #ResetCommand="\[Escape]\[Escape]\[Escape]\[Escape]\Cw\Cw\Cw\Cw\Cw\Afn"
129
130 if lyx_window_name is None:
131         lyx_window_name="LyX";
132
133 print("outfilename: "+outfilename+"\n")
134 print("max_drop: "+max_drop+"\n")
135
136 if infilename is None:
137         print("infilename is None\n")
138         x=CommandSource()
139         print ("Using x=CommandSource\n");
140 else:
141         print("infilename: "+infilename+"\n")
142         probability_we_drop_a_command=random.uniform(0,float(max_drop))
143         print ("probability_we_drop_a_command: ")
144         print '%s'%(probability_we_drop_a_command)
145         print  "\n"
146         x=CommandSourceFromFile(infilename,probability_we_drop_a_command)
147         print ("Using x=CommandSourceFromFile\n");
148
149 outfile=open(outfilename,'w')
150
151 RaiseWindow()
152 sendKeystring("\Afn",lyx_pid)
153 write_commands=True;
154
155 while True:
156         os.system("echo -n LOADAVG:; cat /proc/loadavg")
157         c=x.getCommand()
158         if (c=="Loop"):
159                 outfile.close()
160                 outfile=open(outfilename+'+','w')
161                 print ("Now Looping")
162         outfile.writelines(c+'\n')
163         outfile.flush()
164         if c=="RaiseLyx":
165                 print ("Raising Lyx");
166                 RaiseWindow()
167         elif c[0:4]=="KK: ":
168                 if os.path.exists("/proc/"+lyx_pid+"/status"):
169                         sendKeystring(c[4:],lyx_pid)
170                 else:
171                         os.system("killall lyx; sleep 2 ; killall -9 lyx")
172                         print ("No path /proc/"+lyx_pid+"/status, exiting")
173                         os._exit(1)
174         elif (c=="Loop"):
175                 RaiseWindow()
176                 sendKeystring(ResetCommand,lyx_pid)
177         else:
178                 print ("Unrecognised Command '"+c+"'\n")