-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeforms.py
292 lines (215 loc) · 10.5 KB
/
makeforms.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
import os
import sys
import subprocess
import shutil
import glob
import configparser as c_parser
import argparse
import builder.loader_xlsx as loader_xlsx
import builder.loader_redcap as loader_redcap
import builder.schema as handle_schema
import builder.handle_vm as handle_vm
import builder.handle_csv as handle_csv
import builder.handle_file_turbine as handle_file_turbine
import builder.handle_file_plugin as handle_file_plugin
test = False
def getConfigSettings(config):
out = dict()
abs_path = os.path.abspath(__file__)
parent_dir = os.path.dirname(os.getcwd())
configFilePath = os.getcwd()
config.read('xnat.cfg')
out['version'] = config.get('xnat','xnat_version').replace('.','')
if '18' in out['version']:
out['version'] = '176'
out['path_plugin_builder'] = os.path.dirname(abs_path)
out['path_builder_templates'] = os.path.join(out['path_plugin_builder'], 'builder_templates',out['version'])
out['path_builder_plugins'] = os.path.join(out['path_plugin_builder'], 'plugins')
out['host'] = config.get('xnat','xnat_host')
out['path_xnat_plugins'] = config.get('xnat','xnat_home') + '/plugins'
return out
#start some pluginSettings
if sys.version_info < (3, 0):
config = c_parser.RawConfigParser()
else:
config = c_parser.ConfigParser()
pluginSettings = getConfigSettings(config)
def printArray(msg,data):
for i in data:
if sys.version_info < (3, 0):
print ('{} : {}'.format(msg,i))
else:
print('{} : {}'.format(msg,i))
def printHR():
print('\n ----- \n')
for k in pluginSettings:
print (' ', k, ' : ',pluginSettings[k])
if __name__ == "__main__":
"""
Create schema and install datatype.
Import data if data exists.
Reads a template xls file that contains all schema and form data.
Runs on linux only
"""
sequence = 10 #for actions box
parsar = argparse.ArgumentParser(description='extract data from XNAT')
parsar.add_argument('-i', default='all in folder', help='Input Spreadhseet')
args = parsar.parse_args()
sub=False
Loader = loader_xlsx.LoaderXlsx(pluginSettings)
LoaderRedcap = loader_redcap.LoaderRedcap(pluginSettings)
pathLoadFiles = ('{}/xlsx'.format(pluginSettings['path_plugin_builder']))
chooseFilesExcell = Loader.filesList(pathLoadFiles)
pathLoadFilesRC = ('{}/redcap_dict'.format(pluginSettings['path_plugin_builder']))
LoaderRedcap.dos2unix(pathLoadFilesRC)
chooseFilesRedcap = LoaderRedcap.filesList(pathLoadFilesRC)
chooseFiles = chooseFilesExcell + chooseFilesRedcap
# APPEND CHOOSE FILES HERE WITH REDCAP
print(chooseFiles)
if len(chooseFiles) == 0:
print('No xlsx or redcap dictionary Files Found !')
exit()
print( '#############################')
print ('Files reading: {}'.format(chooseFiles))
for loadFile in chooseFiles:
pluginSettings['workbook'] = loadFile
if 'xlsx' in loadFile:
Loader = loader_xlsx.LoaderXlsx(pluginSettings)
cleanData = Loader.loadExcel(pathLoadFiles,loadFile)
#check here for errors
if Loader.hasError():
print('############### Errors in ', loadFile)
printArray('Errors VM ', Loader.errors)
exit()
elif 'csv' in loadFile:
LoaderRedcap = loader_redcap.LoaderRedcap(pluginSettings)
cleanData = LoaderRedcap.loadRedcap(pathLoadFilesRC, loadFile)
# check here for errors
if LoaderRedcap.hasError():
print('############### Errors in ', loadFile)
printArray('Errors VM ', LoaderRedcap.errors)
exit()
else:
print('Error: No files found')
exit()
schemaName = cleanData['schema']
pluginSettings['schemaName'] = cleanData['schema']
#restrict action boxes so only visible in certain projects:
pluginSettings['restrict_to_project'] = cleanData['restrict_to_project']
# copy gradle files to plugin folder
templateDir = ('{}/{}/{}').format(pluginSettings['path_plugin_builder'],'gradle_templates', pluginSettings['version'])
pluginDir = ('{}/{}').format(pluginSettings['path_builder_plugins'],schemaName)
if os.path.exists(pluginDir):
shutil.rmtree(pluginDir)
shutil.copytree( templateDir, pluginDir , symlinks=False, ignore=None)
#if it a new build need to empty the dirs
deleteExistingFiles = True
handleXsd = handle_schema.SchemaHandler(pluginSettings)
handleVm = handle_vm.VMHandler(pluginSettings,deleteExistingFiles)
handleTurbine = handle_file_turbine.TurbineHandler(pluginSettings,deleteExistingFiles)
handlePlugin = handle_file_plugin.PluginHandler(pluginSettings,deleteExistingFiles)
handleCsv = handle_csv.CSVHandler(pluginSettings, deleteExistingFiles)
# create schema all forms are in one xsd
data_xsd = handleXsd.all_data_xsd(cleanData)
if handleXsd.hasError():
printArray('Errors Loader ', handleVm.errors)
handleXsd.write_schema_xsd(schemaName,data_xsd)
printArray('log schema ', handleXsd.log)
printHR()
print( '#############################')
sequence = 10
# #velocity templates, one for each form
for f in cleanData['forms']:
print( '#############################')
print ('Form: {}-{}'.format(f, cleanData['forms'][f]['name']))
#print '{}'.format(cleanData['forms'][f])
print( '########## MAKING EDIT FORM ################')
handleVm.formEdit(cleanData['forms'][f])
if handleVm.hasError():
printArray('Errors VM ', handleVm.errors)
print( '########## MAKING REPORT ###################')
handleVm.formReport(cleanData['forms'][f])
if handleVm.hasError():
printArray('Errors VM ', handleVm.errors)
if 'subform' not in cleanData['forms'][f]['ext']:
print ('########## CREATING ACTIONS BOX ###################')
#handleVm.make_actionsbox(cleanData['forms'][f],sequence)
handleVm.add_to_actionsbox(cleanData['forms'][f],sequence, pluginSettings['version'])
if handleVm.hasError():
printArray('Errors VM ', handleVm.errors)
handleCsv.writeToFile(cleanData['forms'][f])
if handleCsv.hasError():
printArray('Errors CSV ', handleVm.errors)
sequence += 1
#printArray('log VM ', handleVm.log)
printHR()
print ('########## CREATING TURBINE FILES ###################')
handleTurbine.formEdit(cleanData['forms'][f])
if handleTurbine.hasError():
printArray('Errors Turbine ', handleTurbine.errors)
#if v1.8, copy TemplateSceen.vm/java
if '18' in pluginSettings['version']:
handleVm.copyTemplateScreeen()
if handleVm.hasError():
printArray('Errors VM ', handleVm.errors)
### repatsed.... can change so only does once.... but pain as a lot more code
handleTurbine.copyTemplateScreeen(cleanData['forms'][f])
if handleTurbine.hasError():
printArray('Errors Turbine ', handleTurbine.errors)
handleTurbine.copyModifyTemplateScreeen(cleanData['forms'][f])
if handleTurbine.hasError():
printArray('Errors Turbine ', handleTurbine.errors)
#printArray('log Turbine ', handleTurbine.log)
printHR()
if 'subform' in cleanData['forms'][f]['ext']:
handleCsv.write_subforms(cleanData['forms'][f])
if handleCsv.hasError():
printArray('Errors CSV ', handleVm.errors)
# exit()
#write plugin file
handlePlugin.createFiles(cleanData['forms'])
printArray('log plugin files ', handlePlugin.log)
printHR()
#change gradle builde settings.gradle
gsf = ('{}/settings.gradle').format(templateDir)
gsfTo = ('{}/settings.gradle').format(pluginDir)
fgsf = open(gsf,"r")
lines = fgsf.readlines()
fgsf.close()
fgsf = open(gsfTo,"w")
for line in lines:
fgsf.write( line.replace('[HERE_SCHEMANAME]',schemaName ) )
fgsf.close()
print(' run build ')
gradlew = 'gradlew'
#need to restart gradlew in order to clear cache so changes in schema do not break it!!!
stop_gradlew = '{}/{} --stop'.format(pluginDir, gradlew)
clean_plugin = '{}/{} clean'.format(pluginDir,gradlew)
build_plugin = '{}/{} --no-build-cache xnatDataBuilder xnatPluginJar'.format(pluginDir,gradlew )
if '18' not in pluginSettings['version']:
build_plugin = '{}/{} --no-build-cache clean jar'.format(pluginDir,gradlew)
print (' build plugin ',build_plugin)
try:
suc2 = subprocess.check_call(stop_gradlew, shell=True, cwd=pluginDir)
suc1 = subprocess.check_call(clean_plugin, shell=True, cwd=pluginDir)
suc = subprocess.check_call(build_plugin, shell=True, cwd=pluginDir)
if suc == 0:
print (' okay, build ')
fromLoc = '{}/build/libs/{}-plugin*'.format(pluginDir, schemaName)
for file in glob.glob(r'{}/build/libs/{}*.jar'.format(pluginDir, schemaName)):
if 'javadoc' not in file and 'sources' not in file and 'beans' not in file:
fromLoc = file
# in 1.8 different nameing....
shutil.copy(fromLoc, pluginSettings['path_builder_plugins'])
toLoc = ('{}/').format(pluginSettings['path_xnat_plugins'])
if not os.path.isdir(toLoc):
print ('The plugins directory does not exist, the plugins will not be copied')
else:
shutil.copy(fromLoc, toLoc)
# need to change owner to xnat owner?
print ('##############################')
print ('COMPLETED, PLEASE RESTART TOMCAT')
print ('##############################')
except Exception as detail:
print ('failed, build',detail)
sys.exit(0)