Skip to content

Commit c7f426a

Browse files
authored
Merge pull request #32 from ChannelFinder/pyprojecttoml
Clean up package and tests
2 parents e03429f + ce51f03 commit c7f426a

17 files changed

+397
-279
lines changed

.github/workflows/ci.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3+
4+
name: Python package
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
pull_request:
10+
branches: [ "master" ]
11+
12+
jobs:
13+
test:
14+
15+
runs-on: ubuntu-20.04
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
20+
21+
steps:
22+
- uses: actions/checkout@v4
23+
- name: Set up Python ${{ matrix.python-version }}
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
- name: Install dependencies
28+
run: |
29+
python -m pip install --upgrade pip
30+
python -m pip install '.[test]'
31+
python -m pip install .
32+
- name: Clear existing docker image cache
33+
shell: bash
34+
run: docker image prune -af
35+
- name: Test with pytest
36+
run: |
37+
python -m unittest discover -v -s test -p "test*.py"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.pyc
33
build/
44
*.egg-info/
5+
.vscode

.project

Lines changed: 0 additions & 17 deletions
This file was deleted.

.pydevproject

Lines changed: 0 additions & 11 deletions
This file was deleted.

MANIFEST

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# pyCFClient
2-
A python client library
32

3+
A python client library for ChannelFinder.
44

5-
### configuration for pyCFClient
5+
## Configuration
66

77
The python channelfinder client library can be configured by setting up a `channelfinderapi.conf` file in the following locations
88

@@ -11,6 +11,7 @@ The python channelfinder client library can be configured by setting up a `chann
1111
`channelfinderapi.conf`
1212

1313
The example preferences:
14+
1415
```
1516
cat ~/channelfinderapi.conf
1617
[DEFAULT]
@@ -19,3 +20,22 @@ username=MyUserName
1920
password=MyPassword
2021
```
2122

23+
## Development
24+
25+
To install with dependancies for testing.
26+
27+
```bash
28+
python -m pip install --upgrade pip
29+
python -m pip install '.[test]'
30+
python -m pip install .
31+
```
32+
33+
### Testing
34+
35+
Some of the tests use docker to run a test ChannelFinderService, so a working docker installation needs to available for tests to be successful.
36+
37+
To run all tests:
38+
39+
```bash
40+
python -m unittest discover -v -s test -p "test*.py"
41+
```

cf-update-ioc

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 131 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,131 @@
1-
#!/usr/bin/python
2-
'''
3-
Created on Mar 19, 2012
4-
5-
A python script to ensure that the cf-update-ioc is working correctly.
6-
The script requires the setup of two files
7-
e.g.
8-
>cat nagios01.host01.dbl
9-
>test:cf-update-daemon{test}
10-
11-
>cat nagios02.host02.dbl
12-
>test:cf-update-daemon{test}
13-
14-
The script will touch each file, with a short delay and will check that
15-
Channelfinder has been appropriately updated.
16-
17-
python cf-monitor-test /complete/path/to/daemon/dir -i initialFile -f finalFile
18-
19-
@author: shroffk
20-
'''
21-
import sys
22-
import os
23-
import re
24-
from optparse import OptionParser
25-
from time import sleep
26-
27-
from channelfinder import ChannelFinderClient
28-
29-
SEVR = {0:'OK ',
30-
1:'Minor ',
31-
2:'Major '}
32-
33-
34-
def main():
35-
requiredOpts = ['initial-file', 'final-file']
36-
usage = "usage: %prog -i initial-file -f final-file directory "
37-
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')
44-
opts, args = parser.parse_args()
45-
if args == None or len(args) == 0 :
46-
parser.error('Please specify a directory')
47-
if not opts.initialFile:
48-
parser.error('Please specify a initial test files')
49-
if not opts.finalFile:
50-
parser.error('Please specify a final test files')
51-
mainRun(opts, args)
52-
53-
54-
def mainRun(opts, args):
55-
for directory in args:
56-
initialFile = os.path.normpath(directory + '/' + opts.initialFile)
57-
iHostName, iIocName = getArgsFromFilename(initialFile)
58-
finalFile = os.path.normpath(directory + '/' + opts.finalFile)
59-
fHostName, fIocName = getArgsFromFilename(finalFile)
60-
if getPVNames(initialFile) != getPVNames(finalFile):
61-
sys.exit(1)
62-
pvNames = getPVNames(initialFile)
63-
if len(pvNames) == 0:
64-
sys.exit(1)
65-
'''
66-
Touch the initial file and check channelfinder
67-
'''
68-
touch(initialFile)
69-
sleep(2)
70-
check(pvNames, iHostName, iIocName)
71-
'''
72-
Touch the final file and check channelfinder
73-
'''
74-
touch(finalFile)
75-
sleep(2)
76-
check(pvNames, fHostName, fIocName)
77-
sys.exit
78-
79-
80-
def check(pvNames, hostName, iocName):
81-
try:
82-
client = ChannelFinderClient()
83-
except:
84-
raise RuntimeError('Unable to create a valid webResourceClient')
85-
channels = client.find(property=[('hostName', hostName), ('iocName', iocName)])
86-
if channels and len(pvNames) == len(channels):
87-
for channel in channels:
88-
if channel.Name not in pvNames:
89-
sys.exit(2)
90-
else:
91-
sys.exit(2)
92-
93-
94-
def touch(fname, times=None):
95-
with open(fname, 'a'):
96-
os.utime(fname, times)
97-
98-
99-
def getArgsFromFilename(completeFilePath):
100-
fileName = os.path.split(os.path.normpath(completeFilePath))[1]
101-
pattern4Hostname = '(\S+?)\.\S+'
102-
match = re.search(pattern4Hostname, fileName)
103-
if match:
104-
hostName = match.group(1)
105-
else:
106-
hostName = None
107-
pattern4Iocname = '\S+?\.(\S+?)\.\S+'
108-
match = re.search(pattern4Iocname, fileName)
109-
if match:
110-
iocName = match.group(1)
111-
else:
112-
iocName = None
113-
return hostName, iocName
114-
115-
def getPVNames(completeFilePath, pattern=None):
116-
try:
117-
f = open(completeFilePath)
118-
pvNames = f.read().splitlines()
119-
pvNames = map(lambda x: x.strip(), pvNames)
120-
pvNames = filter(lambda x: len(x) > 0, pvNames)
121-
if pattern:
122-
pvNames = [ re.match(pattern, pvName).group() for pvName in pvNames if re.match(pattern, pvName) ]
123-
return pvNames
124-
except IOError:
125-
return None
126-
finally:
127-
f.close()
128-
129-
if __name__ == '__main__':
130-
main()
131-
pass
1+
#!/usr/bin/python
2+
'''
3+
Created on Mar 19, 2012
4+
5+
A python script to ensure that the cf-update-ioc is working correctly.
6+
The script requires the setup of two files
7+
e.g.
8+
>cat nagios01.host01.dbl
9+
>test:cf-update-daemon{test}
10+
11+
>cat nagios02.host02.dbl
12+
>test:cf-update-daemon{test}
13+
14+
The script will touch each file, with a short delay and will check that
15+
Channelfinder has been appropriately updated.
16+
17+
python cf-monitor-test /complete/path/to/daemon/dir -i initialFile -f finalFile
18+
19+
@author: shroffk
20+
'''
21+
import sys
22+
import os
23+
import re
24+
from optparse import OptionParser
25+
from time import sleep
26+
27+
from channelfinder import ChannelFinderClient
28+
29+
SEVR = {0:'OK ',
30+
1:'Minor ',
31+
2:'Major '}
32+
33+
34+
def main():
35+
requiredOpts = ['initial-file', 'final-file']
36+
usage = "usage: %prog -i initial-file -f final-file directory "
37+
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')
44+
opts, args = parser.parse_args()
45+
if args == None or len(args) == 0 :
46+
parser.error('Please specify a directory')
47+
if not opts.initialFile:
48+
parser.error('Please specify a initial test files')
49+
if not opts.finalFile:
50+
parser.error('Please specify a final test files')
51+
mainRun(opts, args)
52+
53+
54+
def mainRun(opts, args):
55+
for directory in args:
56+
initialFile = os.path.normpath(directory + '/' + opts.initialFile)
57+
iHostName, iIocName = getArgsFromFilename(initialFile)
58+
finalFile = os.path.normpath(directory + '/' + opts.finalFile)
59+
fHostName, fIocName = getArgsFromFilename(finalFile)
60+
if getPVNames(initialFile) != getPVNames(finalFile):
61+
sys.exit(1)
62+
pvNames = getPVNames(initialFile)
63+
if len(pvNames) == 0:
64+
sys.exit(1)
65+
'''
66+
Touch the initial file and check channelfinder
67+
'''
68+
touch(initialFile)
69+
sleep(2)
70+
check(pvNames, iHostName, iIocName)
71+
'''
72+
Touch the final file and check channelfinder
73+
'''
74+
touch(finalFile)
75+
sleep(2)
76+
check(pvNames, fHostName, fIocName)
77+
sys.exit
78+
79+
80+
def check(pvNames, hostName, iocName):
81+
try:
82+
client = ChannelFinderClient()
83+
except:
84+
raise RuntimeError('Unable to create a valid webResourceClient')
85+
channels = client.find(property=[('hostName', hostName), ('iocName', iocName)])
86+
if channels and len(pvNames) == len(channels):
87+
for channel in channels:
88+
if channel.Name not in pvNames:
89+
sys.exit(2)
90+
else:
91+
sys.exit(2)
92+
93+
94+
def touch(fname, times=None):
95+
with open(fname, 'a'):
96+
os.utime(fname, times)
97+
98+
99+
def getArgsFromFilename(completeFilePath):
100+
fileName = os.path.split(os.path.normpath(completeFilePath))[1]
101+
pattern4Hostname = '(\S+?)\.\S+'
102+
match = re.search(pattern4Hostname, fileName)
103+
if match:
104+
hostName = match.group(1)
105+
else:
106+
hostName = None
107+
pattern4Iocname = '\S+?\.(\S+?)\.\S+'
108+
match = re.search(pattern4Iocname, fileName)
109+
if match:
110+
iocName = match.group(1)
111+
else:
112+
iocName = None
113+
return hostName, iocName
114+
115+
def getPVNames(completeFilePath, pattern=None):
116+
try:
117+
f = open(completeFilePath)
118+
pvNames = f.read().splitlines()
119+
pvNames = map(lambda x: x.strip(), pvNames)
120+
pvNames = filter(lambda x: len(x) > 0, pvNames)
121+
if pattern:
122+
pvNames = [ re.match(pattern, pvName).group() for pvName in pvNames if re.match(pattern, pvName) ]
123+
return pvNames
124+
except IOError:
125+
return None
126+
finally:
127+
f.close()
128+
129+
if __name__ == '__main__':
130+
main()
131+
pass

0 commit comments

Comments
 (0)