forked from piberger/pxar2POS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpxar2POS.py
executable file
·362 lines (315 loc) · 16.8 KB
/
pxar2POS.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
#!/usr/bin/env python
import argparse
import ConfigParser
import os
import shutil
import glob
import fnmatch
from POSWriter.POSWriter import POSWriter
PXAR2POSVERSION = '0.1'
try:
from pxar2POSConverter import pxar2POSConverter as pxar2POSConverter
except Exception as e:
print "\x1b[31mcould not load module: 'pxar2POSConverter', make sure the file exists.\n" + "%r"%e +"\x1b[0m"
# show splash screen
print "-"*80
print '''
222 $$$$$$$\ $$$$$$\ $$$$$$\\
____ ____ 2 22 $$ __$$\ $$ __$$\ $$ __$$\\
|_ _||_ _| 22 $$ | $$ |$$ / $$ |$$ / \__|
_ .--. \ \ / / ,--. _ .--. 22222 $$$$$$$ |$$ | $$ |\$$$$$$
[ '/'`\ \ > `' < `'_\ : [ `/'`\] $$ ____/ $$ | $$ | \____$$\\
| \__/ | _/ /'`\ \_ // | |, | | $$ | $$ | $$ |$$\ $$ |
| ;.__/ |____||____| `-;__/ [___] $$ | $$$$$$ |\$$$$$$ |
[__| \__| \______/ \______/
'''
try:
import subprocess
print "version:", "%s-%s"%(PXAR2POSVERSION,subprocess.check_output(["git", "describe", "--always"]).replace('\n', ''))
except:
print "version:", "%s" % PXAR2POSVERSION
print "-"*80
# **********************************************************************************************************************
# initialization
# **********************************************************************************************************************
# create user configuration (= not under version control) - if not existing
if not os.path.isfile('UserConfiguration.ini'):
if os.path.isfile('DefaultConfiguration.ini'):
try:
shutil.copy('DefaultConfiguration.ini', 'UserConfiguration.ini')
except:
print "\x1b[31mERROR: can't create UserConfiguration.ini file!\x1b[0m"
else:
print "\x1b[31mERROR: can't find DefaultConfiguration.ini file!\x1b[0m"
# load default configuration first and then overwrite with user configuration
config = ConfigParser.SafeConfigParser()
# make config parser case sensitive
config.optionxform = str
# load configuration
# UserConfiguration (not under version control) always overwrites DefaultConfiguration (under version control)
try:
config.read('DefaultConfiguration.ini')
except:
print "\x1b[31mERROR: can't load DefaultConfiguration.ini file!\x1b[0m"
try:
config.read('UserConfiguration.ini')
except:
print "\x1b[31mERROR: can't load UserConfiguration.ini file!\x1b[0m"
# read arguments from command line
parser = argparse.ArgumentParser(description='converter for pxar data from database to pixel online software (POS) format, see README.md for some general examples how to use it')
parser.add_argument('-m','--module',dest='module',
help='module ID, e.g. M1234 or list of modules separated by comma, e.g. M1234,M2345,M3456',
default='')
parser.add_argument('-T', '--trim', dest='trim',
help='trim value, e.g. 35, -1 for untrimmed',
default=config.get('Global', 'DefaultTrim'))
parser.add_argument('-t', '--temp', dest='temp',
help='temperature',
default=config.get('Global', 'DefaultTemperature'))
parser.add_argument('-o', '--output', dest='output',
help='output folder',
default=config.get('Paths', 'Output'))
parser.add_argument('-p', '--positions', dest='positions',
help='module positions file',
default=config.get('Paths', 'ModuleList'))
parser.add_argument('-s', '--source', dest='source',
help='data source. default: pisa DB, database url (http://...), local path to Fulltest folders, \'default\' to have pXar default configuration....',
default=config.get('Global', 'Database'))
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
help='verbose output',
default=config.get('Global', 'Verbose') == 1)
parser.add_argument('-i', '--configuration-id', dest='configuration_id',
help='configuration ID',
default=config.get('Global', 'ConfigurationId'))
parser.add_argument('-d', '--do', dest='do',
help='command to run',
default='')
parser.add_argument('-w', '--what', dest='what',
help='what parameters to extract, (comma separated): dac,iana,mask,tbm,trim',
default='dac,iana,mask,tbm,trim')
args = parser.parse_args()
# check given options, --do can not be used with -m at the same time
if len(args.module.strip()) < 1 and len(args.do) < 1:
print "no module specified. show help with -h"
exit(0)
elif len(args.do) > 0 and len(args.module) > 0:
print "running commands for certain modules only is not implemented yet, omit -m option to run it for all!"
exit(0)
# **********************************************************************************************************************
# work with already existing files
# this can be used to change DACs and save it as a new configuration
# **********************************************************************************************************************
if len(args.do) > 0:
# --do needs a config ID specified
configurationID = -1
try:
configurationID = int(args.configuration_id)
if configurationID < 0:
raise Exception("no configuration ID specified!")
except:
print "to run a command, please specify configuration ID with -i or in UserConfiguration.ini"
exit(0)
# find next free config ID
posWriter = POSWriter(outputPath=args.output, configurationID=configurationID)
inputFileNames = posWriter.getOutputFileNames()
configurations = []
for dataType, dataSubfolder in inputFileNames.items():
outputFolder = '/'.join(dataSubfolder.split('/')[:-2])
globPath = args.output + '/' + outputFolder + '/*/'
try:
configurations += [int(x.strip('/').split('/')[-1]) for x in glob.glob(globPath)]
except:
pass
newConfigurationID = max(configurations) + 1
# commands are separated by ;
runCommands = [x.strip().split(':') for x in args.do.split(';')]
# ask user confirmation
print '+%s+'%('-'*78)
print '|%s|'%((' copy configuration ID %d -> %d ?'%(configurationID, newConfigurationID)).ljust(78))
print '|%s|'%(' '*78)
print '|%s|'%((' and run the following commands on config ID %d:'%newConfigurationID).ljust(78))
for runCommand in runCommands:
print '| %s|'%(('%r'%runCommand).ljust(77))
print '|%s|'%(' '*78)
print '+%s+'%('-'*78)
answer = raw_input('ENTER/y to continue, q to quit: ')
if answer.lower() == 'y' or len(answer.strip()) < 1:
# copy configuration to new ID
posWriterOutput = POSWriter(outputPath=args.output, configurationID=newConfigurationID, createFoldersOnInitialization=False)
outputFileNames = posWriterOutput.getOutputFileNames()
print "copy configuration..."
for dataType, dataSubfolder in inputFileNames.items():
copyFrom = args.output + '/' + '/'.join(dataSubfolder.split('/')[:-1])
copyTo = args.output + '/' + '/'.join(outputFileNames[dataType].split('/')[:-1])
if args.verbose:
print "copy:", copyFrom, " --> ", copyTo
shutil.copytree(copyFrom, copyTo)
print " -> done."
# run commands
print "run commands..."
# examples: enable PKAM counter, but only for L4 modules: tbm:*_LYR4_*?set:TBMADisablePKAMCounter:0
# dac:set:Vdd:8
# dac:incr8bit:Vana:20
# dac:incr4bit:Vdd:1
# tbm:and:TBMADelay:128
# loop over list of commands separated by ';'
for runCommand in runCommands:
conditional = ''
# check if directory (first argument) exists
if runCommand[0] in outputFileNames:
# extract condition and instruction
if args.verbose:
print "found! ", outputFileNames[runCommand[0]]
if '?' in runCommand[1]:
conditional = runCommand[1].split('?')[0]
instruction = runCommand[1].split('?')[1].lower()
else:
instruction = runCommand[1].lower()
# loop over ALL files
datFileNamesMask = args.output + '/' + outputFileNames[runCommand[0]].format(Position='*')
datFileNames = glob.glob(datFileNamesMask)
for datFileName in datFileNames:
conditionalMet = False
if len(conditional) < 1:
conditionalMet = True
if args.verbose:
print datFileName
with open(datFileName, 'r') as datFile:
datFileLines = datFile.readlines()
# loop over all lines of the dat file
changesMade = False
for i in range(len(datFileLines)):
if conditionalMet:
try:
valueString = datFileLines[i].split(':')[1]
paddingSpaces = len(valueString) - len(valueString.lstrip())
if paddingSpaces < 1:
paddingSpaces = 1
except:
paddingSpaces = 1
lineParts = [x.strip() for x in datFileLines[i].split(':')]
if instruction == 'set':
if lineParts[0] == runCommand[2]:
lineParts[1] = runCommand[3].strip()
datFileLines[i] = '%s:%s%s\n'%(lineParts[0],' '*paddingSpaces, lineParts[1])
# reset condition and mark the file as changed
if len(conditional) > 0:
conditionalMet = False
changesMade = True
# syntax: limit:dacname:[min,max]
elif instruction == 'limit':
if lineParts[0] == runCommand[2]:
value = int(lineParts[1])
limits = [int(x) for x in runCommand[3].replace('[','').replace(']','').replace('(','').replace(')','').split(',')]
# if only 1 argument given, take it as maximum
if len(limits) == 1:
limits = [0, limits[0]]
if value > limits[1]:
value = limits[1]
elif value < limits[0]:
value = limits[0]
lineParts[1] = '%d'%value
datFileLines[i] = '%s:%s%s\n'%(lineParts[0],' '*paddingSpaces, lineParts[1])
# reset condition and mark the file as changed
if len(conditional) > 0:
conditionalMet = False
changesMade = True
elif instruction == 'and':
if lineParts[0] == runCommand[2]:
lineParts[1] = '%d'%(int(lineParts[1]) & int(runCommand[3]))
datFileLines[i] = '%s:%s%s\n'%(lineParts[0],' '*paddingSpaces, lineParts[1])
# reset condition and mark the file as changed
if len(conditional) > 0:
conditionalMet = False
changesMade = True
elif instruction == 'or':
if lineParts[0] == runCommand[2]:
lineParts[1] = '%d'%(int(lineParts[1]) | int(runCommand[3]))
datFileLines[i] = '%s:%s%s\n'%(lineParts[0],' '*paddingSpaces, lineParts[1])
# reset condition and mark the file as changed
if len(conditional) > 0:
conditionalMet = False
changesMade = True
elif instruction.startswith('incr') and (instruction.endswith('bit') or instruction.endswith('bits')):
nBits = int(instruction.replace('bits','').replace('bit','').replace('incr',''))
if nBits < 1:
print "\x1b[31mERROR: incr#bit operator needs # >= 1! defaulting to 8 bit!\x1b[0m"
nBits = 8
if lineParts[0] == runCommand[2]:
value = int(lineParts[1]) + int(runCommand[3])
if value > (2**nBits)-1:
value = (2**nBits)-1
elif value < 0:
value = 0
lineParts[1] = '%d'%(value)
datFileLines[i] = '%s:%s%s\n'%(lineParts[0],' '*paddingSpaces, lineParts[1])
# reset condition and mark the file as changed
if len(conditional) > 0:
conditionalMet = False
changesMade = True
else:
# check if condition is fulfilled
if fnmatch.fnmatch(datFileLines[i].strip(), conditional):
if args.verbose:
print " -> condition met:", datFileLines[i], conditional
conditionalMet = True
# write file back, if changes were made
if changesMade:
if args.verbose:
print "update file: '\x1b[34m{datFileName}\x1b[0m'".format(datFileName=datFileName)
with open(datFileName, 'w') as datFile:
datFile.writelines(datFileLines)
elif runCommand[0] == 'exit':
break
else:
print "\x1b[31mERROR: command type not found:",runCommand[0],"\x1b[0m"
print " -> done."
print " -> done."
else:
print " \x1b[31m-> aborted!\x1b[0m"
# **********************************************************************************************************************
# obtain data from DB
# **********************************************************************************************************************
else:
# show summary of parameters
if args.configuration_id < 0:
print " -> NO configuration id specified (with -i), saving all files directly into the output folder"
else:
print " -> configuration ID: ", args.configuration_id
print " -> module:", args.module
print " -> trim:", args.trim
print " -> temperature:", args.temp
print " -> module positions from:", args.positions
print " -> module data from:", args.source
print " -> save data in:", args.output
if args.verbose:
print " -> verbose output is turned ON"
# initialize converter
print "initialize converter..."
converter = pxar2POSConverter(
options={
'ModulePositionTable': args.positions,
'DataSource': args.source,
'OutputPath': args.output,
'Verbose': args.verbose,
'ConfigurationID': args.configuration_id,
'ExtractParameters': args.what,
}
)
# select which Fulltest of FullQualification to use
testOptions = {'Test': '*ulltest*_' + args.temp, 'tempnominal': args.temp, 'TrimValue': args.trim, 'Transformations': {}}
# additional transformations of values
if config.has_section('DACs'):
testOptions['Transformations']['DACs'] = {}
for dac, value in config.items('DACs'):
testOptions['Transformations']['DACs'][dac] = value
# convert files
moduleIDList = args.module.replace(';',',').split(',')
moduleIDListLength = len(moduleIDList)
moduleIDListPosition = 1
for moduleID in moduleIDList:
print '*'*40
print ' %s (%d/%d):'%(moduleID, moduleIDListPosition, moduleIDListLength)
print '*'*40
converter.convertModuleData(moduleID=moduleID, testOptions=testOptions)
moduleIDListPosition += 1