catmeta.py
17.4 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import sys
import os
import StringIO
import traceback
import time
import struct
import re
import shutil
import difflib
import datetime
import csv
import getopt
####################################################
# CAT Resource Population Object
####################################################
class CATResource:
def __init__(self, resourceId, resourceIdString, appId, contentPath, descirption, usage):
self.resourceId = resourceId
self.resourceIdString = resourceIdString
self.descirption = descirption
self.usage = usage
self.appId = appId
def toCATString(self):
result = str(self.resourceId) + '=' + '\"' + self.resourceIdString + '\",'+ '\"' + self.appId + '\",'+ '\"' + self.descirption + '\",\"' + self.usage + '\"'
return result
####################################################
# Image Resource Population Object
####################################################
class ImageCATResource(CATResource):
def __init__(self, resourceId, resourceIdString, appId, contentPath, typeId, descirption, usage, mbaSupport):
self.resourceId = resourceId
self.resourceIdString = resourceIdString
self.appId = appId
self.contentPath = contentPath.upper()
self.descirption = descirption
self.usage = usage
self.typeId = typeId
self.mbaSupport = mbaSupport
def toCATString(self):
# since the original description is the path of the res now, we use ID name string as the CAT resoruce description
result = str(self.resourceId) + '=' + '\"' + self.resourceIdString + '\",'+ '\"' + self.appId + '\",'+ '\"' + self.contentPath + '\",' + str(self.typeId) + ',\"' + self.resourceIdString + '\",\"' + self.usage + '\"'
return result
####################################################
# Audio Resource Population Object
####################################################
class AudioCATResource(CATResource):
def __init__(self, resourceId, resourceIdString, appId, contentPath, typeId, descirption, usage, mbaSupport):
self.resourceId = resourceId
self.resourceIdString = resourceIdString
self.appId = appId
self.contentPath = contentPath.upper()
self.descirption = descirption
self.usage = usage
self.typeId=typeId
self.mbaSupport = mbaSupport
def toCATString(self):
# since the original description is the path of the res now, we use ID name string as the CAT resoruce description
result = str(self.resourceId) + '=' + '\"' + self.resourceIdString + '\",'+ '\"' + self.appId + '\",'+ '\"' + self.contentPath + '\",' + str(self.typeId) + ',\"' + self.resourceIdString + '\",\"' + self.usage + '\"'
return result
####################################################
# String Resource Population Object
####################################################
class StringCATResource(CATResource):
def __init__(self, resourceId, resourceIdString, appId, descirption, usage):
self.resourceId = resourceId
self.resourceIdString = resourceIdString
self.descirption = descirption
self.usage = usage
self.appId = appId
# def toCATString(self):
# result = str(self.resourceId) + '=\"' + self.descirption + '\",\"' + self.usage + '\"'
####################################################
# CAT Resource MODEL Object
####################################################
class CATResourceModel:
def __init__(self,name):
self.name = name
self.items = []
self.itemsMap = {}
self.allowDuplicated = False
def add(self, catResource):
if (self.allowDuplicated == False) and (catResource.resourceIdString in self.itemsMap):
print('[WARN] Duplicated ID is ignored: <' + catResource.resourceIdString + ', ' + str(catResource.resourceId) + '>' )
else:
self.itemsMap[catResource.resourceIdString] = catResource
self.items.append(catResource)
def show(self):
for item in self.items:
print(item.toCATString())
####################################################
# String Resource DAO Object
####################################################
class StringCATResourceDAO:
def getCATResources(self, fileName, model):
print('Load string resource from :' + os.path.abspath(fileName))
inputStream = open(fileName,'r')
for line in inputStream.readlines():
params = line.split('\t')
id = int(params[0])
idString = params[1]
appId = params[6]
desc = params[2]
usage = 'COMMON'
model.add(StringCATResource(id,idString,appId,desc,usage))
return model
####################################################
# Image Resource DAO Object
####################################################
class ImageCATResourceDAO:
def normalizePath(self, path):
#the path should be plutommi
upperPath = path.upper()
index = upperPath.find('CUSTOMER\\\\IMAGES\\\\')
result = upperPath
if(index>=0):
result = result[index:]
result = result.replace('\\\\','\\')
result = 'plutommi\\' + result
return result
def getCATResources(self, codebasePath, repoFileName, model):
fileName = os.path.abspath(codebasePath + '\\' + repoFileName)
print('Load image resource from :' + os.path.abspath(fileName))
inputStream = open(fileName,'r')
for line in inputStream.readlines():
params = line.split('\t')
id = int(params[0])
idString = params[1]
appId = params[10]
desc = params[3]
contentPath = self.normalizePath(params[7])
typeId = int(params[6])
mbaSupport = int(params[5])
usage = 'COMMON'
tmpObj = ImageCATResource(id,idString,appId,contentPath,typeId,desc,usage,mbaSupport)
model.add(tmpObj)
return model
####################################################
# Audio Resource DAO Object
####################################################
class AudioCATResourceDAO:
def normalizePath(self, path):
#the path should be plutommi
upperPath = path.upper()
index = upperPath.find('CUSTOMER\\\\AUDIO\\\\')
result = upperPath
if(index>=0):
result = result[index:]
result = result.replace('\\\\','\\')
result = 'plutommi\\' + result
return result
def getCATResources(self, codebasePath, repoFileName, model):
fileName = os.path.abspath(codebasePath + '\\' + repoFileName)
print('Load audio resource from :' + fileName)
inputStream = open(fileName,'r')
for line in inputStream.readlines():
params = line.split('\t')
id = int(params[0])
idString = params[1]
appId = params[8]
desc = params[3]
contentPath = self.normalizePath(params[7])
typeId = int(params[6])
mbaSupport = int(params[5])
usage = 'COMMON'
tmpObj = AudioCATResource(id,idString, appId, contentPath,typeId,desc,usage,mbaSupport)
model.add(tmpObj)
return model
####################################################
# Language Mapping Object
####################################################
class LanguageMapping:
def __init__(self):
self.item = []
def addLanguage(self, langStr):
if(langStr != None):
self.item.append(langStr)
def toString(self):
result = "LANG_MAPPING="
sizeLimit = len(self.item)
index = 0
while(index < sizeLimit):
item = '\"'+ str(index) + ":" + self.item[index] + '\"'
if(index == 0):
result = result + item
else:
result = result + ',' + item
index = index + 1
return result
####################################################
# Language Mapping Data Access Object
####################################################
class LanguageMappingDAO:
def getLanguageMapping(self, fileName):
inputStream = open(fileName)
rawBytes = inputStream.read()
decoded = rawBytes.decode('utf16').encode('ascii','ignore')
# print(decoded.encode('ascii','ignore'))
lineNum = -1
langMappingObj = LanguageMapping()
for line in decoded.splitlines():
# Skip the first line
if(lineNum != -1):
parsedStr = line.split('\t')
if(len(parsedStr)>2):
langStr = parsedStr[2]
langMappingObj.addLanguage(langStr)
# print(str(lineNum) + ':' + langStr)
lineNum = lineNum + 1
return langMappingObj
####################################################
# CAT String Setting Helper
####################################################
class CATStringSetting:
def __init__(self, isCompress=False, compressAlgo = None):
self.isCompress = isCompress
self.compressAlgo = compressAlgo
def writeToFile(self, fileName):
outputStream = open(fileName, 'w')
outputStream.write('# CAT Settings\n# MMIResource String\n')
outputStream.write(self.toString())
outputStream.close()
def loadSettingItem(self, itemName, itemValue):
if itemName.strip() == 'IsCompression':
if int(itemValue.strip()) == 1:
self.isCompress = True
else:
self.isCompress = False
elif itemName.strip() == 'CompressAlgo':
self.compressAlgo = itemValue.strip()
def toString(self):
result = ''
#if self.isCompress==True:
# result = result + 'IsCompression=' + '1'+ '\n'
#else:
# result = result + 'IsCompression=' + '0'+ '\n'
if(not self.compressAlgo is None):
result = result + 'CompressAlgo=' + self.compressAlgo + '\n'
return result
def loadFromFile(self, fileName):
if( os.path.exists(fileName)):
intputStream = open(fileName,'r')
for line in intputStream.readlines():
if(not line.startswith('#')):
items = line.split('=')
if(len(items) >= 2):
self.loadSettingItem(items[0], items[1])
if(self.isCompress is True):
print('Load CAT String Setting: Compress = ' + str(self.isCompress) + ' ,Algo = ' + self.compressAlgo)
else:
print('Load CAT String Setting: Compress = ' + str(self.isCompress))
intputStream.close()
####################################################
# CAT Meta Data Model
####################################################
class CATMetaModel:
def __init__(self, name):
self.name = name
self.audioCATResource = CATResourceModel('Audio')
self.imageCATResource = CATResourceModel('Image')
self.stringCATResource = CATResourceModel('String')
self.catStringSetting = CATStringSetting(False, None)
self.cacheFolder = 'tools\\CAT\\MMIResource\\cache'
self.languageMappingObj = None
self.imageRepoXML = 'plutommi\\Customer\\ResGenerator\\offline_xml_image_repo.txt'
self.imageRepoPreXML = 'plutommi\\Customer\\ResGenerator\\offline_pre_xml_legacy_image_repo.txt'
self.imageRepoPostXML = 'plutommi\\Customer\\ResGenerator\\offline_post_xml_legacy_image_repo.txt'
self.stringRepoXML = 'plutommi\\Customer\\ResGenerator\\offline_xml_string_repo.txt'
self.stringRepoPreXML = 'plutommi\\Customer\\ResGenerator\\offline_pre_xml_legacy_string_repo.txt'
self.stringRepoPostXML = 'plutommi\\Customer\\ResGenerator\\offline_post_xml_legacy_string_repo.txt'
self.audioRepoXML = 'plutommi\\Customer\\ResGenerator\\offline_xml_audio_repo.txt'
self.audioRepoPreXML = 'plutommi\\Customer\\ResGenerator\\offline_pre_xml_legacy_audio_repo.txt'
self.audioRepoPostXML = 'plutommi\\Customer\\ResGenerator\\offline_post_xml_legacy_audio_repo.txt'
self.stringSettingFile = 'plutommi\\Customer\\ResGenerator\\debug\\cat_string_setting.txt'
self.languageUsageFile = 'plutommi\\Customer\\ResGenerator\\debug\\language_usage.txt'
self.cachedLanguageUsageFile = self.cacheFolder + '\\language_usage.txt'
self.cachedStringSettingFile = self.cacheFolder + '\\cat_string_setting.txt'
def updateCacheFile(self, cacheFile, hasCachedFile):
if( os.path.exists(cacheFile)):
if(os.path.exists(hasCachedFile)):
os.remove(hasCachedFile)
shutil.copyfile(cacheFile, hasCachedFile)
def prepareCache(self, rootPath):
cacheDir = rootPath + '\\' + self.cacheFolder
cacheFileLang = rootPath + '\\' + self.languageUsageFile
cacheFileString = rootPath + '\\' + self.stringSettingFile
hasCachedFileLang = rootPath + '\\' + self.cachedLanguageUsageFile
hasCachedFileString = rootPath + '\\' + self.cachedStringSettingFile
# Create cache folder if it doesn't exist
if not os.path.exists(cacheDir):
os.makedirs(cacheDir)
# Prepare (check and update) String Language Mapping file cache
self.updateCacheFile(cacheFileLang, hasCachedFileLang)
self.updateCacheFile(cacheFileString, hasCachedFileString)
def loadCATResource(self, rootPath):
audioCATResourceDAO = AudioCATResourceDAO()
stringCATResourceDAO = StringCATResourceDAO()
imageCATResourceDAO = ImageCATResourceDAO()
languageMappingDAO=LanguageMappingDAO()
self.prepareCache(rootPath)
self.languageMappingObj = languageMappingDAO.getLanguageMapping(rootPath + '\\' + self.cachedLanguageUsageFile)
audioCATResourceDAO.getCATResources(rootPath , self.audioRepoXML,self.audioCATResource)
audioCATResourceDAO.getCATResources(rootPath ,self.audioRepoPreXML,self.audioCATResource)
audioCATResourceDAO.getCATResources(rootPath ,self.audioRepoPostXML,self.audioCATResource)
imageCATResourceDAO.getCATResources(rootPath , self.imageRepoXML,self.imageCATResource)
imageCATResourceDAO.getCATResources(rootPath ,self.imageRepoPreXML,self.imageCATResource)
imageCATResourceDAO.getCATResources(rootPath ,self.imageRepoPostXML,self.imageCATResource)
self.catStringSetting.loadFromFile(rootPath + '\\' + self.cachedStringSettingFile )
stringCATResourceDAO.getCATResources(rootPath + '\\' + self.stringRepoXML,self.stringCATResource)
stringCATResourceDAO.getCATResources(rootPath + '\\' + self.stringRepoPreXML,self.stringCATResource)
stringCATResourceDAO.getCATResources(rootPath + '\\' + self.stringRepoPostXML,self.stringCATResource)
def writeStringCATResourceSection(self, outputStream, lineMappingObj, modelObj, catStringSetting):
outputStream.write('[' + modelObj.name + ']\n')
outputStream.write(self.catStringSetting.toString())
outputStream.write(lineMappingObj.toString() + '\n')
for item in modelObj.items:
outputStream.write(item.toCATString() + '\n')
def writeCATResourceSection(self, rootPath, outputStream, modelObj, mbaResourceOnly):
# Write Section Name
outputStream.write('[' + modelObj.name + ']\n')
if(mbaResourceOnly == 1):
for item in modelObj.items:
if(item.mbaSupport == 1):
if ( os.path.exists(rootPath + '\\'+ item.contentPath) and (not item.contentPath.upper().endswith('PBM'))):
outputStream.write(item.toCATString() + '\n')
else:
print('[WARN] Content file '+ rootPath + '\\'+ item.contentPath + ' not Found, Skip Resource:< ' + item.toCATString() + '>')
else:
for item in modelObj.items:
if( os.path.exists(rootPath + '\\'+ item.contentPath) and (not item.contentPath.upper().endswith('PBM'))):
outputStream.write(item.toCATString() + '\n')
else:
print('[WARN] Content file '+ rootPath + '\\'+ item.contentPath + ' not Found, Skip Resource:< ' + item.toCATString() + '>')
def toIniFile(self, root, fileName, mbaResourceOnly):
absFileName = os.path.abspath(fileName)
print('Start to write: ' + absFileName)
outputStream = open(fileName,'w')
outputStream.write('[Setting]\nMtkResGenerator=\"plutommi\\customer\\resgenerator\\mtk_resgenerator.exe\"\n')
outputStream.write('Convert=\"plutommi\\customer\\resgenerator\\convert.exe\"\n');
self.writeCATResourceSection(root, outputStream, self.audioCATResource, mbaResourceOnly)
self.writeCATResourceSection(root, outputStream, self.imageCATResource, mbaResourceOnly)
self.writeStringCATResourceSection(outputStream, self.languageMappingObj, self.stringCATResource, self.catStringSetting)
outputStream.write('[ATTACH_FILE]\n')
outputStream.write('imageRepoXML='+self.imageRepoXML.upper() + '\n')
outputStream.write('imageRepoPreXML='+self.imageRepoPreXML.upper() +'\n')
outputStream.write('imageRepoPostXML='+self.imageRepoPostXML.upper() +'\n')
outputStream.write('stringRepoXML='+self.stringRepoXML.upper() + '\n')
outputStream.write('stringRepoPreXML='+self.stringRepoPreXML.upper() +'\n')
outputStream.write('stringRepoPostXML='+self.stringRepoPostXML.upper() +'\n')
outputStream.write('audioRepoXML='+self.audioRepoXML.upper() + '\n')
outputStream.write('audioRepoPreXML='+self.audioRepoPreXML.upper() +'\n')
outputStream.write('audioRepoPostXML='+self.audioRepoPostXML.upper() +'\n')
print('Finish writting: ' + absFileName)
####################################################
# Controller of the parser
####################################################
def main(rootPath, targetIniFile):
CATModelObj = CATMetaModel(targetIniFile)
print '------------------Load CAT resource data from Resgen DB------------------'
CATModelObj.loadCATResource(rootPath)
print '-------------------------------------------------------------------------'
print '---------------------Output CAT meta file--------------------------------'
CATModelObj.toIniFile(rootPath,targetIniFile, 1)
print '-------------------------------------------------------------------------'
####################################################
# Usgae print
####################################################
def usage():
print('Usage: catmeta [meta file name] [-s codebase]')
####################################################
# Module Entry
####################################################
# Check if the file is being run as a top-level program file before
# invoke main method
if __name__ == '__main__':
rootPath = '.'
targetIniFile= 'catresmeta.ini'
result = 0
if(len(sys.argv)<2):
usage()
result = 1
try:
#Parse command line parameter
targetIniFile= sys.argv[1]
opts, args = getopt.getopt(sys.argv[2:], 's:')
for o, a in opts:
if o == '-s':
rootPath = a
rootPath = os.path.normpath(rootPath)
print('Root of SW-Load: ' + os.path.abspath(rootPath))
main(rootPath, targetIniFile)
result = 0
except getopt.GetoptError, err:
usage()
result = 1
except:
result = 1
print '\n', '-'*20, "python exception start", '-'*20
traceback.print_exc(file=sys.stdout)
print '-'*20, "python exception end ", '-'*20
raise
finally:
sys.exit(result)
####################################################