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
12 print "Beginning keytest.py"
17 keycode=["\[Left]",'\[Right]','\[Down]','\[Up]','\[BackSpace]','\[Delete]','\[Escape]']
23 for k in range(97, 123):
26 for k in range(97, 123):
27 keycode[:0]=["\A"+chr(k)]
29 for k in range(97, 123):
30 keycode[:0]=["\A"+chr(k)]
32 for k in range(97, 123):
33 keycode[:0]=["\C"+chr(k)]
40 self.count=self.count+1;
41 #if self.count > self.count_max:
45 elif self.count > self.count_max:
50 keystr=keystr+self.keycode[random.randint(1,len(self.keycode))-1]
53 class CommandSourceFromFile(CommandSource):
54 def __init__(self,filename,p):
55 self.infile=open(filename,'r')
56 self.lines=self.infile.readlines()
61 #Now we start randomly dropping lines, which we hope are redundant
62 #p is the probability that any given line will be removed
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)
67 p=p-(1/len(self.lines))
72 if random.uniform(0,1) < self.p:
73 print "Randomly dropping line "+l+"\n"
82 if self.i >= len(self.lines):
83 if self.count >= 100 or self.loops>1:
87 self.loops=self.loops+1
90 line=self.lines[self.i]
91 print "Line read: <<"+line+">>\n"
92 self.count=self.count+1
96 def sendKeystring(keystr,LYX_PID):
97 print "sending keystring "+keystr+"\n"
98 if not re.match(".*\w.*", keystr):
99 print ("print ."+keystr+".\n")
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"
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")
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");
121 file_new_command=os.environ.get("FILE_NEW_COMMAND");
122 if file_new_command is None:
123 file_new_command="\Afn"
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"
130 if lyx_window_name is None:
131 lyx_window_name="LyX";
133 print("outfilename: "+outfilename+"\n")
134 print("max_drop: "+max_drop+"\n")
136 if infilename is None:
137 print("infilename is None\n")
139 print ("Using x=CommandSource\n");
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)
146 x=CommandSourceFromFile(infilename,probability_we_drop_a_command)
147 print ("Using x=CommandSourceFromFile\n");
149 outfile=open(outfilename,'w')
152 sendKeystring("\Afn",lyx_pid)
156 os.system("echo -n LOADAVG:; cat /proc/loadavg")
160 outfile=open(outfilename+'+','w')
161 print ("Now Looping")
162 outfile.writelines(c+'\n')
165 print ("Raising Lyx");
168 if os.path.exists("/proc/"+lyx_pid+"/status"):
169 sendKeystring(c[4:],lyx_pid)
171 os.system("killall lyx; sleep 2 ; killall -9 lyx")
172 print ("No path /proc/"+lyx_pid+"/status, exiting")
176 sendKeystring(ResetCommand,lyx_pid)
178 print ("Unrecognised Command '"+c+"'\n")