-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostProLoop.py
More file actions
360 lines (325 loc) · 15.3 KB
/
postProLoop.py
File metadata and controls
360 lines (325 loc) · 15.3 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
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
#!/usr/bin/env python
# -*- coding: latin-1; -*-
# $Id: externalProgramPath.py 2645 2016-05-12 06:29:38Z boman $
#
#
# Define externals program path according to local configuration
#
from prmClasses import *
import os, os.path, distutils.spawn, time, sys, re, glob
import imp
#import importlib
import externalProgramPath
# Variable globale
import threading
matlock = threading.Lock() # un seul matlab a la fois (merdouille avec le R2009a sous windows)
#------------------------------------------------------------------------------------
class PostProLoop(PRMSet):
def __init__(self,_verb=False):
fname = 'postProLoop.cfg'
PRMSet.__init__(self, fname, _verb)
def __getitem__(self, key):
return self.pars[key].val
def loadPaths(self):
home = os.path.expanduser("~")
loc=os.path.abspath('.')
return [home,loc]
def savePath(self):
home = os.path.expanduser("~")
return home
def setDefaultPars(self):
if len(self.pars)!=0:
return
# load progs
progs = externalProgramPath.ExtProgs()
#
TextPRM (self.pars, 'DIRNAME', 'BaseDir on wich running Post-Operation', './workspace/MyTestDir')
YesNoPRM(self.pars, 'MULTITEST', 'Loop on all existing subDirectories', False)
TextPRM (self.pars, 'DIRWILDCARD', 'WildCard on subDirs Name', '*')
# Matlab
YesNoPRM(self.pars, 'MATLABRUN', 'Execute a Matlab function', False)
TextPRM (self.pars, 'MATLABEXE', 'Path To Matlab Exe', progs['MATLAB'])
TextPRM (self.pars, 'MATLABCMD', 'Matlab Command (script called)', '')
TextPRM (self.pars, 'MATLABPATH', 'Path to Matlab scripts', '')
TextPRM (self.pars, 'MATLABREQUEST', 'Requested file to run Matlab script', 'parameters.m') #'result.mat', ...
# GS
YesNoPRM(self.pars, 'GSRUN', 'Run GS on Eps Files', False)
TextPRM (self.pars, 'GSEXE', 'Path To Matlab Exe', progs['GHOSTSCRIPT'])
TextPRM (self.pars, 'GSFILTER', 'Input filter on eps files', "*.eps")
MultiPRM(self.pars, 'GSOUTPUTFORMAT', 'GS Output format', ["png256", "bmp256"], "png256")
TextPRM (self.pars, 'GSDEFINITION', 'GS Output definition', "300")
TextPRM (self.pars, 'GSREQUEST', 'Requested file to run GhostScript', "*.eps")
# Python
YesNoPRM(self.pars, 'PYTHONRUN', 'Execute a Python script', False)
TextPRM(self.pars, 'PYTHONMODULE', 'Module containing script', '')
TextPRM(self.pars, 'PYTHONSCRIPT', 'Script to call inside python', '')
TextPRM(self.pars, 'PYTHONREQUEST', 'Requested file to execute script', '')
# Latex
YesNoPRM(self.pars, 'LATEXRUN', 'Run the build latex file post-pro', False)
TextPRM(self.pars, 'LATEXEXE', 'Path To Latex Exe', progs['LATEX'])
TextPRM(self.pars, 'LATEXCMD', 'Latex command line', '')
TextPRM(self.pars, 'LATEXREQUEST', 'Script to call to build latex file', '')
#
PRMAction(self.actions, 'a', self.pars['DIRNAME'])
PRMAction(self.actions, 'b', self.pars['MULTITEST'])
PRMAction(self.actions, 'c', self.pars['DIRWILDCARD'])
# Matlab
NoAction(self.actions)
PRMAction(self.actions, 'f', self.pars['MATLABRUN'])
PRMAction(self.actions, 'g', self.pars['MATLABEXE'])
PRMAction(self.actions, 'h', self.pars['MATLABCMD'])
PRMAction(self.actions, 'i', self.pars['MATLABPATH'])
PRMAction(self.actions, 'j', self.pars['MATLABREQUEST'])
NoAction(self.actions)
# GhostScript
PRMAction(self.actions, 'k', self.pars['GSRUN'])
PRMAction(self.actions, 'l', self.pars['GSEXE'])
PRMAction(self.actions, 'm', self.pars['GSFILTER'])
PRMAction(self.actions, 'n', self.pars['GSOUTPUTFORMAT'])
PRMAction(self.actions, 'o', self.pars['GSDEFINITION'])
#PRMAction(self.actions, 'o', self.pars['GSREQUEST'])
NoAction(self.actions)
# Python
PRMAction(self.actions, 'p', self.pars['PYTHONRUN'])
PRMAction(self.actions, 'q', self.pars['PYTHONMODULE'])
PRMAction(self.actions, 'r', self.pars['PYTHONSCRIPT'])
PRMAction(self.actions, 's', self.pars['PYTHONREQUEST'])
NoAction(self.actions)
# Latex
PRMAction(self.actions, 't', self.pars['LATEXRUN'])
PRMAction(self.actions, 'u', self.pars['LATEXEXE'])
PRMAction(self.actions, 'v', self.pars['LATEXCMD'])
PRMAction(self.actions, 'w', self.pars['LATEXREQUEST'])
NoAction (self.actions)
GoAction (self.actions, 'G')
SaveAction(self.actions, 'S')
QuitAction(self.actions, 'Q')
def configActions(self):
self.pars['DIRWILDCARD'].enable(self.pars['MULTITEST'].val==True)
# Matlab
self.pars['MATLABCMD'].enable(self.pars['MATLABRUN'].val==True)
self.pars['MATLABEXE'].enable(self.pars['MATLABRUN'].val==True)
self.pars['MATLABPATH'].enable(self.pars['MATLABRUN'].val==True)
self.pars['MATLABREQUEST'].enable(self.pars['MATLABRUN'].val==True)
# GhostScript
self.pars['GSEXE'].enable(self.pars['GSRUN'].val==True)
self.pars['GSFILTER'].enable(self.pars['GSRUN'].val==True)
self.pars['GSOUTPUTFORMAT'].enable(self.pars['GSRUN'].val==True)
self.pars['GSDEFINITION'].enable(self.pars['GSRUN'].val==True)
self.pars['GSREQUEST'].enable(self.pars['GSRUN'].val==True)
# Python
self.pars['PYTHONMODULE'].enable(self.pars['PYTHONRUN'].val==True)
self.pars['PYTHONSCRIPT'].enable(self.pars['PYTHONRUN'].val==True)
self.pars['PYTHONREQUEST'].enable(self.pars['PYTHONRUN'].val==True)
# Latex
self.pars['LATEXEXE'].enable(self.pars['LATEXRUN'].val==True)
self.pars['LATEXCMD'].enable(self.pars['LATEXRUN'].val==True)
self.pars['LATEXREQUEST'].enable(self.pars['LATEXRUN'].val==True)
def checkValidity(self, key):
'''
if distutils.spawn.find_executable(os.path.splitext(self.pars[key].val)[0]):
return True
else:
print "%s is not found (%s)...."%self.pars[key].val
print "\t Check installation and accessibility..."
print "\t Use 'externalProgramPathGui' to define the full program path (recommanded)"
print "\t or add %s in your user path (not recommanded)"%key
return False
'''
return true
def go(self):
# change to workingDirectory
baseDir = os.getcwd()
os.chdir(self.pars['DIRNAME'].val)
if self.pars['MULTITEST'].val == True:
self.loopOnDirTree(os.path.abspath('.'))
else:
self.execInDir()
# back to source directory
os.chdir(baseDir)
def loopOnDirTree(self, wDir):
# change to workingDirectory
print "loopOnDirTree running on ", wDir
oldDir = os.getcwd()
os.chdir(wDir)
# loop on dir
ld = glob.glob(self.pars['DIRWILDCARD'].val)
for file in ld:
if os.path.isdir(file):
self.loopOnDirTree(file)
# finished loop on dir => execute ...
self.execInDir()
# back to source directory
os.chdir(oldDir)
def checkRequest(self, request):
if len(request) == 0:
return True
elif re.search('\*',request):
if len(glob.glob(request)) > 0:
return True
elif os.path.exists(request):
return True
else:
return False
def execInDir(self):
print "execInDir running in ", os.path.abspath('.')
if self.pars['MATLABRUN'].val and self.checkRequest(self.pars['MATLABREQUEST'].val):
print "launch matlab..."
out = execMatlabScript(self.pars['MATLABEXE'].val, self.pars['MATLABCMD'].val , self.pars['MATLABPATH'].val)
#out = matlab.execMatlabScript(self.pars['MATLABCMD'].val , self.pars['MATLABPATH'].val)
print "matlab done"
if self.pars['GSRUN'].val and self.checkRequest(self.pars['GSREQUEST'].val):
print "launch Ghostscript translation of eps files..."
#execGhostScriptPostPro(device='png256', outExt='png',res=300)
out = execGhostScript(self.pars['GSEXE'].val ,self.pars['GSFILTER'].val ,self.pars['GSOUTPUTFORMAT'].val , int(self.pars['GSDEFINITION'].val))
#out = ghostScript.execGhostScriptPostPro(self.pars['GSOUTPUTFORMAT'].val , 'png', int(self.pars['GSDEFINITION'].val))
print "Ghostscript done "
if self.pars['PYTHONRUN'].val and self.checkRequest(self.pars['PYTHONREQUEST'].val):
print "launch Python script..."
out = execPythonScript(self.pars['PYTHONMODULE'].val, self.pars['PYTHONSCRIPT'].val)
print "Python script done "
if self.pars['LATEXRUN'].val and self.checkRequest(self.pars['LATEXREQUEST'].val):
print "launch latex building file ..."
print "NOT IMPLEMENTED YIET "
#out = latex.execMatlabScript(self.pars['MATLABCMD'].val , self.pars['MATLABPATH'].val)
print "latex building file done "
#=================================================================================
#=================================================================================
def execMatlabScript(matlabExe, matlabCmd, mFilePath=None):
tim0 = time.time()
print "entering MatlabPostPro compute"
inirep = os.getcwd()
print "inirep = ", inirep
moutfile = os.path.abspath(os.path.join("matlab.log"))
print "moutfile = ",moutfile
mCmd = ""
if mFilePath:
print "mFilePath = ",mFilePath
mCmd = mCmd + "addpath('%s'); " %mFilePath
mCmd = mCmd + "cd '%s'; "%inirep
mCmd = mCmd + matlabCmd
print "mCmd = ", mCmd
if isUnix():
cmd = '"%s" -nodisplay -logfile "%s" -r "%s; quit" > pipo 2>&1' % (matlabExe, moutfile, mCmd)
else:
cmd = '"%s" -automation -noFigureWindows -nodesktop -wait -logfile "%s" -r "%s; quit"' % (matlabExe, moutfile, mCmd) # work
print 'complete Matlab subprocess command: ', cmd
# acquire matlock
matlock.acquire()
try:
print "Running Matlab..."
import subprocess
matlabProcess = subprocess.call(cmd, shell=True)
print "Running Matlab... Done"
a = 1.0
except:
# fonction objectif doit retourner un double
print "problem during matlab run"
a = 0.0
# release matlock
matlock.release()
print " matlab time = %d sec" %(time.time()-tim0)
return a
#=================================================================================
#=================================================================================
def execGhostScript(gsExe, inFilter = '*.eps', device='png256', res=300):
import subprocess, os, time
tim0 = time.time()
fileOut = open('gs.log', 'w')
if re.match('png', device):
outExt = 'png'
elif re.match('jpeg', device):
outExt = 'jpg'
elif re.match('bmp', device):
outExt = 'bmp'
elif re.match('tiff', device):
outExt = 'tiff'
else:
print "device extension not implented!!! => exit"
return
# fonction objectif doit retourner un double
a = 0
try:
ld = glob.glob(inFilter)
for file in ld:
[base,ext] = os.path.splitext(file)
cmd = '"%s" -dSAFER -dBATCH -dNOPAUSE -dEPSCrop -r%d -sDEVICE=%s -sOutputFile=%s.%s %s'%(gsExe, res, device, base, outExt, file)
ret = subprocess.call(cmd, stdout=fileOut, stderr=subprocess.STDOUT, shell=True)
a = a+1
'''
cmd = ''
for file in ld:
[base,ext] = os.path.splitext(file)
cmd = cmd+'"%s" -dSAFER -dBATCH -dNOPAUSE -dEPSCrop -r%d -sDEVICE=%s -sOutputFile=%s.%s %s\n'%(gsExe, res, device, base, outExt, file)
a = a+1
print "cmd = ", cmd
ret = subprocess.call(cmd, stdout=fileOut, stderr=subprocess.STDOUT, shell=True)
print "subprocess return = ", ret
'''
except:
print "problem during ghostScript run"
a = 0.0
fileOut.write("\nexecGhostScriptPostPro done in %d sec\n"%(time.time()-tim0))
fileOut.close()
print "execGhostScriptPostPro: %d files translated in %d sec"%(a, (time.time()-tim0))
return a
#------------------------------------------------------------------------------------
def execPythonScript(pythonModule, pythonCmd):
import subprocess, os, time, imp
tim0 = time.time()
fileOut = open('python.log', 'w')
try:
pName, fName = os.path.split(pythonModule)
[baseFName,extFName] = os.path.splitext(fName)
(file, pathName, description) = imp.find_module(baseFName, [pName])
module = imp.load_module(baseFName, file, pathName, description)
print "python module loaded: ", pythonModule
except:
print "execPythonScript Error: unable to find or load pythonModule: ", pythonModule
try:
print "method to search in module: ", pythonCmd
m = re.match('(.*)\((.*)\)', pythonCmd)
#print "re.match(...) = ", m
if m:
print "method ", repr(m.group(1)), " found... with arg " ,repr(m.group(2))
method = getattr(module, m.group(1))
print dir(method)
print "method ", m.group(1), " loaded..."
if len(m.group(2)) > 0:
args = m.group(2).split(',')
argsVals=[]
for arg in args:
argsVals.append(eval(arg))
method(*argsVals)
else:
method()
print "method ", m.group(1),'(',m.group(2),')', " successfully called..."
else:
method = getattr(module, pythonCmd)
method()
print "method ", pythonCmd,'()', " called..."
print "execPythonScript: compute done"
a=1.0
except:
print "problem during python cmd ",pythonCmd, " from module ", pythonModule, " run"
a = 0.0
fileOut.write("\execPythonScript done in %d sec\n"%(time.time()-tim0))
fileOut.close()
print "execPythonScript: done in %d sec"%(time.time()-tim0)
return a
#------------------------------------------------------------------------------------
#=================================================================================
#=================================================================================
#=================================================================================
#=================================================================================
def main():
postProLoop = PostProLoop() #verb=True)
postProLoop.configActions()
postProLoop.menu()
if __name__ == "__main__":
try:
import signal
signal.signal(signal.SIGBREAK, sigbreak);
except:
pass
main()