11#!/usr/bin/python
2- '''
2+ """
33Created on Mar 19, 2012
44
55A python script to ensure that the cf-update-ioc is working correctly.
1717python cf-monitor-test /complete/path/to/daemon/dir -i initialFile -f finalFile
1818
1919@author: shroffk
20- '''
20+ """
21+
2122import sys
2223import os
2324import re
2627
2728from channelfinder import ChannelFinderClient
2829
29- SEVR = {0 :'OK ' ,
30- 1 :'Minor ' ,
31- 2 :'Major ' }
30+ SEVR = {0 : "OK " , 1 : "Minor " , 2 : "Major " }
3231
3332
3433def main ():
35- requiredOpts = [' initial-file' , ' final-file' ]
34+ requiredOpts = [" initial-file" , " final-file" ]
3635 usage = "usage: %prog -i initial-file -f final-file directory "
3736 parser = OptionParser (usage = usage )
38- parser .add_option ('-i' , '--initial-file' , \
39- action = 'store' , type = 'string' , dest = 'initialFile' , \
40- help = 'the initial-file' )
41- parser .add_option ('-f' , '--final-file' , \
42- action = 'store' , type = 'string' , dest = 'finalFile' , \
43- help = 'the --final-file' )
37+ parser .add_option (
38+ "-i" ,
39+ "--initial-file" ,
40+ action = "store" ,
41+ type = "string" ,
42+ dest = "initialFile" ,
43+ help = "the initial-file" ,
44+ )
45+ parser .add_option (
46+ "-f" ,
47+ "--final-file" ,
48+ action = "store" ,
49+ type = "string" ,
50+ dest = "finalFile" ,
51+ help = "the --final-file" ,
52+ )
4453 opts , args = parser .parse_args ()
45- if args == None or len (args ) == 0 :
46- parser .error (' Please specify a directory' )
54+ if args == None or len (args ) == 0 :
55+ parser .error (" Please specify a directory" )
4756 if not opts .initialFile :
48- parser .error (' Please specify a initial test files' )
57+ parser .error (" Please specify a initial test files" )
4958 if not opts .finalFile :
50- parser .error (' Please specify a final test files' )
59+ parser .error (" Please specify a final test files" )
5160 mainRun (opts , args )
5261
5362
5463def mainRun (opts , args ):
5564 for directory in args :
56- initialFile = os .path .normpath (directory + '/' + opts .initialFile )
65+ initialFile = os .path .normpath (directory + "/" + opts .initialFile )
5766 iHostName , iIocName = getArgsFromFilename (initialFile )
58- finalFile = os .path .normpath (directory + '/' + opts .finalFile )
67+ finalFile = os .path .normpath (directory + "/" + opts .finalFile )
5968 fHostName , fIocName = getArgsFromFilename (finalFile )
6069 if getPVNames (initialFile ) != getPVNames (finalFile ):
6170 sys .exit (1 )
6271 pvNames = getPVNames (initialFile )
6372 if len (pvNames ) == 0 :
6473 sys .exit (1 )
65- '''
74+ """
6675 Touch the initial file and check channelfinder
67- '''
76+ """
6877 touch (initialFile )
6978 sleep (2 )
7079 check (pvNames , iHostName , iIocName )
71- '''
80+ """
7281 Touch the final file and check channelfinder
73- '''
82+ """
7483 touch (finalFile )
7584 sleep (2 )
7685 check (pvNames , fHostName , fIocName )
@@ -81,8 +90,8 @@ def check(pvNames, hostName, iocName):
8190 try :
8291 client = ChannelFinderClient ()
8392 except :
84- raise RuntimeError (' Unable to create a valid webResourceClient' )
85- channels = client .find (property = [(' hostName' , hostName ), (' iocName' , iocName )])
93+ raise RuntimeError (" Unable to create a valid webResourceClient" )
94+ channels = client .find (property = [(" hostName" , hostName ), (" iocName" , iocName )])
8695 if channels and len (pvNames ) == len (channels ):
8796 for channel in channels :
8897 if channel .Name not in pvNames :
@@ -92,40 +101,46 @@ def check(pvNames, hostName, iocName):
92101
93102
94103def touch (fname , times = None ):
95- with open (fname , 'a' ):
104+ with open (fname , "a" ):
96105 os .utime (fname , times )
97106
98107
99108def getArgsFromFilename (completeFilePath ):
100109 fileName = os .path .split (os .path .normpath (completeFilePath ))[1 ]
101- pattern4Hostname = ' (\S+?)\.\S+'
110+ pattern4Hostname = " (\S+?)\.\S+"
102111 match = re .search (pattern4Hostname , fileName )
103112 if match :
104113 hostName = match .group (1 )
105114 else :
106115 hostName = None
107- pattern4Iocname = ' \S+?\.(\S+?)\.\S+'
116+ pattern4Iocname = " \S+?\.(\S+?)\.\S+"
108117 match = re .search (pattern4Iocname , fileName )
109118 if match :
110119 iocName = match .group (1 )
111120 else :
112121 iocName = None
113122 return hostName , iocName
114123
124+
115125def getPVNames (completeFilePath , pattern = None ):
116126 try :
117127 f = open (completeFilePath )
118128 pvNames = f .read ().splitlines ()
119129 pvNames = map (lambda x : x .strip (), pvNames )
120130 pvNames = filter (lambda x : len (x ) > 0 , pvNames )
121131 if pattern :
122- pvNames = [ re .match (pattern , pvName ).group () for pvName in pvNames if re .match (pattern , pvName ) ]
132+ pvNames = [
133+ re .match (pattern , pvName ).group ()
134+ for pvName in pvNames
135+ if re .match (pattern , pvName )
136+ ]
123137 return pvNames
124138 except IOError :
125139 return None
126140 finally :
127141 f .close ()
128142
129- if __name__ == '__main__' :
143+
144+ if __name__ == "__main__" :
130145 main ()
131146 pass
0 commit comments