-
Notifications
You must be signed in to change notification settings - Fork 2
/
configIOC.py
executable file
·396 lines (322 loc) · 15.3 KB
/
configIOC.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python3
#
# A script to configure an xxx IOC in the style of makeIOC.sh
#
import os
import sys
import argparse
def iocDirCheck(pwd, iocName):
iocDir = True
shouldExist = ('configure', '{}App'.format(iocName),
'iocBoot', 'Makefile')
for thing in shouldExist:
path = "{}/{}".format(pwd, thing)
if not os.path.exists(thing):
iocDir = False
return iocDir
def remove_file(path):
global quiet
if os.path.isfile(path):
vprint("> git rm -f {}".format(path))
if quiet:
os.system("git rm -qf {}".format(path))
else:
os.system("git rm -f {}".format(path))
else:
vprint("{} is not a file".format(path))
def remove_files(fileList):
for fp in fileList:
remove_file(fp)
def remove_dir(path, saveList=None):
global quiet
deleteEntireDir = True
if saveList != None:
for fp in saveList:
if path == os.path.dirname(fp):
deleteEntireDir = False
break
if deleteEntireDir:
if os.path.isdir(path):
vprint("> git rm -rf {}".format(path))
if quiet:
os.system("git rm -qrf {}".format(path))
else:
os.system("git rm -rf {}".format(path))
else:
vprint("{} is not a dir".format(path))
else:
for fn in os.listdir(path):
fp = "{}/{}".format(path, fn)
if fp in saveList:
vprint("saving {}".format(fp))
else:
remove_file(fp)
def remove_dirs(dirList, saveList=None):
for dp in dirList:
remove_dir(dp, saveList)
def make_dir(path):
if not os.path.exists(path):
os.mkdir(path)
def modifyFile(inFileName, outFileName=None, patternsToExclude=None, lineSubstitutions=None):
if os.path.exists(inFileName):
if outFileName == None:
# Overwrite the input file if no output file is specified
outFileName = inFileName
# Use a .new extension for testing
#outFileName = "{}.new".format(inFileName)
# Read the contents of the file
with open(inFileName, 'r') as fh:
contents = fh.readlines()
# Determine which lines need to be replaced
if lineSubstitutions == None:
linesToReplace = None
else:
linesToReplace = lineSubstitutions.keys()
# Write the modified contents to the output file
with open(outFileName, 'w') as fh:
for line in contents:
if linesToReplace != None and line in linesToReplace:
if type(lineSubstitutions[line]) == list:
fh.write("".join(lineSubstitutions[line]))
else:
fh.write(lineSubstitutions[line])
else:
ignore = False
if patternsToExclude != None:
for pattern in patternsToExclude:
if pattern in line:
ignore = True
break
if not ignore:
fh.write(line)
# Stage the modified file to be committed
vprint('> git add {}'.format(outFileName))
os.system('git add {}'.format(outFileName))
else:
vprint("{} does not exist".format(inFileName))
def createMotorIocsh(iocName):
#
newDir = 'iocBoot/ioc{}/iocsh'.format(iocName)
make_dir(newDir)
inFileName = 'iocBoot/ioc{}/examples/motors.iocsh'.format(iocName)
outFileName = '{}/motors.iocsh'.format(newDir)
patternsToExclude = ['traj', 'pseudo']
lineSubstitutions = {'dbLoadTemplate("substitutions/motor.substitutions", "P=$(PREFIX)")\n': '#dbLoadTemplate("substitutions/motor.substitutions", "P=$(PREFIX)")\n'}
modifyFile(inFileName, outFileName, patternsToExclude, lineSubstitutions)
def deleteCommonFiles(iocName):
filesToDelete = ['LICENSE', 'README', 'README.md', 'start_putrecorder',
'iocBoot/accessSecurity.acf',
'iocBoot/ioc{}/README'.format(iocName),
'iocBoot/ioc{}/SGMenu.req'.format(iocName),
'iocBoot/ioc{}/bootParms'.format(iocName),
'iocBoot/ioc{}/softioc/in-screen.sh'.format(iocName),
'iocBoot/ioc{}/softioc/run'.format(iocName),
'iocBoot/ioc{}/st.cmd.Cygwin'.format(iocName),
'{}App/Db/Security_Control.db'.format(iocName),
'{}App/Db/Security_Control_settings.req'.format(iocName),
'{}App/Db/streamExample.db'.format(iocName),
]
dirsToDelete = ['documentation',
'iocBoot/ioc{}/examples'.format(iocName),
'iocBoot/ioc{}/substitutions'.format(iocName),
'{}App/op/bob/autoconvert'.format(iocName),
'{}App/op/edl/autoconvert'.format(iocName),
'{}App/op/opi'.format(iocName),
'{}App/op/python'.format(iocName),
'{}App/op/burt'.format(iocName),
]
filesToKeep = ['iocBoot/ioc{}/substitutions/motor.substitutions'.format(iocName),
'iocBoot/ioc{}/substitutions/motorSim.substitutions'.format(iocName),
'iocBoot/ioc{}/substitutions/softMotor.substitutions'.format(iocName),
]
remove_files(filesToDelete)
remove_dirs(dirsToDelete, filesToKeep)
def patchCommonIocsh(iocName):
#
inFileName = 'iocBoot/ioc{}/common.iocsh'.format(iocName)
lineSubstitutions = {
'iocshLoad("$(AUTOSAVE)/iocsh/autosave_settings.iocsh", "PREFIX=$(PREFIX), SAVE_PATH=$(TOP)/iocBoot/$(IOC)")\n' : 'iocshLoad("$(AUTOSAVE)/iocsh/autosave_settings.iocsh", "PREFIX=$(PREFIX), SAVE_PATH=$(TOP)/iocBoot/$(IOC), NUM_SEQ=12, SEQ_PERIOD=43200")\n',
'set_requestfile_path("$(TOP)/db")\n' : [ 'set_requestfile_path("$(TOP)/db")\n', 'set_requestfile_path("$(TOP)/{}App/Db")\n'.format(iocName)],
'dbLoadRecords("$(ALIVE)/aliveApp/Db/aliveMSGCalc.db", "P=$(PREFIX)")\n' : ['dbLoadRecords("$(ALIVE)/aliveApp/Db/aliveMSGCalc.db", "P=$(PREFIX)")\n',
'\n',
'# Miscellaneous PV\'s, such as burtResult\n',
'dbLoadRecords("$(STD)/stdApp/Db/misc.db","P=$(PREFIX)")\n',
'\n']
}
modifyFile(inFileName, lineSubstitutions=lineSubstitutions)
def configureLinux(iocName):
#
filesToDelete = [ 'iocBoot/nfsCommands',
'iocBoot/ioc{}/cdCommands.vxWorks-ppc32'.format(iocName),
'iocBoot/ioc{}/cdCommands.vxWorks-ppc32sf'.format(iocName),
'iocBoot/ioc{}/st.cmd.Win32'.format(iocName),
'iocBoot/ioc{}/st.cmd.Win64'.format(iocName),
'iocBoot/ioc{}/st.cmd.vxWorks'.format(iocName),
'iocBoot/ioc{}/st.cmd.vxWorks-local'.format(iocName),
]
remove_files(filesToDelete)
inFileName = 'iocBoot/ioc{}/Makefile'.format(iocName)
patternsToExclude = ['win', 'vxWorks']
lineSubstitutions = {'ARCH = linux-x86_64\n' : ['ARCH = linux-x86_64\n', '#ARCH = linux-x86_64-debug\n'],
'TARGETS = cdCommands envPaths dllPath.bat modules.lua\n' : 'TARGETS = envPaths modules.lua\n',
}
modifyFile(inFileName, patternsToExclude=patternsToExclude, lineSubstitutions=lineSubstitutions)
inFileName = 'configure/CONFIG_SITE'
lineSubstitutions = {'#CROSS_COMPILER_TARGET_ARCHS = vxWorks-68040\n' : 'CROSS_COMPILER_TARGET_ARCHS = \n'}
modifyFile(inFileName, lineSubstitutions=lineSubstitutions)
def configureVxWorks(iocName):
#
filesToDelete = [ 'iocBoot/ioc{}/st.cmd.Win32'.format(iocName),
'iocBoot/ioc{}/st.cmd.Win64'.format(iocName),
'iocBoot/ioc{}/st.cmd.Linux'.format(iocName),
]
dirsToDelete = ['iocBoot/ioc{}/softioc'.format(iocName), ]
#
remove_files(filesToDelete)
remove_dirs(dirsToDelete)
# Re-enable building for Vxworks
inFileName = 'configure/CONFIG_SITE'
lineSubstitutions = {'#CROSS_COMPILER_TARGET_ARCHS = vxWorks-68040\n' : 'CROSS_COMPILER_TARGET_ARCHS = vxWorks-ppc32 vxWorks-ppc32-debug vxWorks-ppc32sf vxWorks-ppc32sf-debug\n'}
modifyFile(inFileName, lineSubstitutions=lineSubstitutions)
# Update nfsCommands
inFileName = 'iocBoot/nfsCommands'
patternsToExclude = ['oxygen', 'mooney']
lineSubstitutions = { 'nfsMount("s100dserv","/APSshare","/APSshare")\n' : [
'nfsMount("s100dserv","/APSshare","/APSshare")\n',
'nfsMount("s100dserv","/xorApps","/xorApps")\n',
'nfsMount("s100dserv","/xorApps","/net/s100dserv/xorApps")\n',
'#!nfsMount("s100dserv","/export/beams","/home/beams")\n',
'\n',
'#!hostAdd(("aquila","164.54.100.16")\n',
'#!nfsMount("aquila","/export/beams3","/home/beams3")\n']
}
modifyFile(inFileName, patternsToExclude=patternsToExclude, lineSubstitutions=lineSubstitutions)
# Update Makefile
inFileName = 'iocBoot/ioc{}/Makefile'.format(iocName)
patternsToExclude = ['win', 'linux', 'rhel']
lineSubstitutions = { '#ARCH = vxWorks-ppc32\n' : ['ARCH = vxWorks-ppc32\n', '#ARCH = vxWorks-ppc32-debug\n'],
'#TARGETS = cdCommands\n' : 'TARGETS = cdCommands\n',
'TARGETS = envPaths\n' : '#TARGETS = envPaths\n',
'TARGETS = cdCommands envPaths dllPath.bat modules.lua\n' : 'TARGETS = cdCommands modules.lua\n',
}
modifyFile(inFileName, patternsToExclude=patternsToExclude, lineSubstitutions=lineSubstitutions)
# Update st.cmd.vxWorks
inFileName = 'iocBoot/ioc{}/st.cmd.vxWorks'.format(iocName)
patternsToExclude = []
lineSubstitutions = { '< cdCommands.vxWorks-ppc32sf\n' : ['#-< cdCommands.vxWorks-ppc32sf\n', '< cdCommands\n'],
'local_startup = "/enter/startup/directory/here/"\n' : '#-local_startup = "/enter/startup/directory/here/"\n',
'cd local_startup\n' : 'cd startup\n',
'epicsEnvSet("STARTUP", local_startup)\n' : '#-epicsEnvSet("STARTUP", local_startup)\n',
'epicsEnvSet("TOP", "$(STARTUP)/../..")\n' : '#-epicsEnvSet("TOP", "$(STARTUP)/../..")\n',
}
modifyFile(inFileName, patternsToExclude=patternsToExclude, lineSubstitutions=lineSubstitutions)
# Undo changes that allow running from the xxx build on APSshare
os.system("sed -i -e 's/xxxbin/topbin/g' -e 's/XXX/TOP/g' -e 's/xxx/{}/g' iocBoot/ioc{}/st.cmd.vxWorks".format(iocName, iocName))
def configureWindows(iocName):
#
filesToDelete = [ 'iocBoot/nfsCommands'.format(iocName),
'iocBoot/ioc{}/cdCommands.vxWorks-ppc32'.format(iocName),
'iocBoot/ioc{}/cdCommands.vxWorks-ppc32sf'.format(iocName),
'iocBoot/ioc{}/st.cmd.vxWorks'.format(iocName),
'iocBoot/ioc{}/st.cmd.Linux'.format(iocName),
'iocBoot/ioc{}/st.cmd.vxWorks-local'.format(iocName),
]
dirsToDelete = ['iocBoot/ioc{}/softioc'.format(iocName), ]
#
remove_files(filesToDelete)
remove_dirs(dirsToDelete)
# Create batch files
batchFileSkeleton = """@echo OFF
SETLOCAL
REM This should match an existing subdirectory of the IOC's bin dir
set EPICS_HOST_ARCH={}
REM set EPICS_CA_MAX_ARRAY_BYTES=100000000
REM Add caRepeater to the PATH
REM set PATH=%PATH%;D:/epics/base-3.15.5/bin/%EPICS_HOST_ARCH%
REM Go to the startup directory
cd %~dp0
REM dlls to the PATH
call dllPath.bat
REM start the IOC
..\\..\\bin\\%EPICS_HOST_ARCH%\\xxx st.cmd.{}
pause
ENDLOCAL"""
fn = 'iocBoot/ioc{}/start_ioc.Win32.bat'.format(iocName)
with open(fn, 'w') as fh:
fh.write(batchFileSkeleton.format("win32-x86-static", "Win32"))
vprint('> git add {}'.format(fn))
os.system('git add {}'.format(fn))
fn = 'iocBoot/ioc{}/start_ioc.Win64.bat'.format(iocName)
with open(fn, 'w') as fh:
fh.write(batchFileSkeleton.format("windows-x64-static", "Win64"))
vprint('> git add {}'.format(fn))
os.system('git add {}'.format(fn))
# Modify Makefile
inFileName = 'iocBoot/ioc{}/Makefile'.format(iocName)
patternsToExclude = ['vxWorks', 'linux', 'rhel', 'cyg' ]
lineSubstitutions = { '#ARCH = windows-x64-static\n' : 'ARCH = windows-x64-static\n',
'#ARCH = win32-x86\n' : ['#ARCH = win32-x86-static\n',
'#ARCH = win32-x86-debug\n',
'#ARCH = win32-x86\n'],
'TARGETS = envPaths\n' : '#TARGETS = envPaths\n',
'#TARGETS = envPaths dllPath.bat\n' : 'TARGETS = envPaths dllPath.bat\n',
'TARGETS = cdCommands envPaths dllPath.bat modules.lua\n' : 'TARGETS = envPaths dllPath.bat modules.lua\n',
}
modifyFile(inFileName, patternsToExclude=patternsToExclude, lineSubstitutions=lineSubstitutions)
def includeMotorIocsh(iocName):
#
lineSubstitutions = { '< common.iocsh\n' : ['< common.iocsh\n',
'\n',
'< iocsh/motors.iocsh\n']}
startupDir = 'iocBoot/ioc{}'.format(iocName)
for fn in os.listdir(startupDir):
if 'st.cmd' in fn:
inFileName = "{}/{}".format(startupDir, fn)
modifyFile(inFileName, lineSubstitutions=lineSubstitutions)
def vprint(message):
global verbose
if verbose == True:
print(message)
verbose = None
quiet = None
def main(options):
global verbose
verbose = options.verbose
global quiet
quiet = options.quiet
vprint(options)
cwd = os.getcwd()
# Assume the following:
# 1. The name of the IOC's top-level directory is the name of the IOC
iocName = os.path.basename(cwd)
if iocDirCheck(cwd, iocName):
#
createMotorIocsh(iocName)
#
deleteCommonFiles(iocName)
#
patchCommonIocsh(iocName)
#
if (options.os == 'linux'):
configureLinux(iocName)
elif (options.os == 'windows'):
configureWindows(iocName)
elif (options.os == 'vxWorks'):
configureVxWorks(iocName)
#
includeMotorIocsh(iocName)
#
vprint('> git commit -m "Configured {} for {} with configIOC.py"'.format(iocName, options.os))
if quiet:
os.system('git commit -q -m "Configured {} for {} with configIOC.py"'.format(iocName, options.os))
else:
os.system('git commit -m "Configured {} for {} with configIOC.py"'.format(iocName, options.os))
else:
print("Error: {} is not an IOC dir".format(cwd))
if __name__ == '__main__':
parser = argparse.ArgumentParser("configIOC.py")
parser.add_argument('os', choices=('linux', 'vxWorks', 'windows'))
parser.add_argument('-v', '--verbose', dest='verbose', action="store_true", help="Print commands that are executed")
parser.add_argument('-q', '--quiet', dest='quiet', action="store_true", help="Pass the quiet flag to git commands")
options = parser.parse_args(sys.argv[1:])
main(options)