-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotListener.py
More file actions
84 lines (69 loc) · 2.35 KB
/
RobotListener.py
File metadata and controls
84 lines (69 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
__author__ = 'Prasad'
#usage
#pybot --listener RobotListener:E;\\test1.txt
#The ; is used deliberately as the Listener interprets : as a delimiter between arguments.
import os
import sys
import time
class Constants():
FILE_ALREADY_LOCKED=1
FILE_SUCCESS_LOCKED=2
class RobotListener():
ROBOT_LISTENER_API_VERSION = 2
def __init__(self,fileName):
#Robot cannot accept a :, it interprets it as start of second arg, hence use ; which will be replaced here.
self.fileName=fileName.replace(";",":",1)
self.lockfileName="{0}.lock".format(self.fileName)
self.fd=0
self.delay=2
def lockfile(self):
try:
self.fd=os.open(self.lockfileName,os.O_CREAT|os.O_EXCL|os.O_RDWR)
return Constants.FILE_SUCCESS_LOCKED
except OSError as o:
#we except the control here when the file is already locked/created
return Constants.FILE_ALREADY_LOCKED
except :
print sys.exc_info()
print "File already locked"
def releaselock(self):
try:
if(os.path.exists(self.lockfileName) and self.fd!=0):
os.close(self.fd)
os.unlink(self.lockfileName)
else:
print "Lock file doesn't exist"
except:
print sys.exc_info()
def end_test(self,arg1,arg2):
iteration=0
timedout="FALSE"
try:
while(self.lockfile()!=Constants.FILE_SUCCESS_LOCKED):
if iteration>5:
timedout="TRUE"
break
iteration=iteration+1
time.sleep(self.delay)
if timedout=="FALSE":
wFile=open(self.fileName,"a+")
#wFile.writelines()
wFile.writelines("NAME:{0},STATUS:{1}\n".format(arg2["longname"],arg2["status"]))
self.releaselock()
except:
print sys.exc_info()
if __name__=="__main__":
fileName="E;\\op9.txt"
#ob=RobotListener(fileName)
#ob.lockfile()
#ob.releaselock()
#ob1=RobotListener(fileName)
#ob1.lockfile()
#ob1.releaselock()
#ob1=RobotListener(fileName)
#ob2=RobotListener(fileName)
#ob1.lockfile()
#ob2.releaselock()
#ob2.lockfile()
ob1=RobotListener(fileName)
ob1.end_test("prasad",{"longname":"prasad","status":"PASS"})