#!/usr/bin/python import os import re import sys import shutil if len(sys.argv) != 2 and len(sys.argv) != 4 : print("Usage %s [path to insert_ppm_values_types.sql]" % sys.argv[0]) sys.exit dirPath = os.path.abspath(sys.argv[1]) if not os.path.isdir(dirPath) : print("directory \"%s\" doesn't exists. Abort ..." % dirPath) sys.exit fdSql = open("guez_flm_symbols.sql", "wt") maxId = -1 nameDict = {} idDict = {} symbolCount = 0 print ("\nParsing XML symbol definitions:") for fileN in os.listdir(dirPath) : symbolId = -1 if fileN.endswith(".xml") : fd = open(dirPath + "/"+ fileN,"rt") xmlContent = fd.read() fd.close() # print(xmlContent) idBegin = xmlContent.find("id=\"") idEnd = xmlContent.find("\"",idBegin+4) if idBegin > 0 and idEnd > 0 : idBegin = idBegin + 4 symbolId = int(xmlContent[idBegin:idEnd],0) if symbolId >= 0 : if symbolId > maxId : maxId = symbolId nameBegin = xmlContent.find("") nameEnd = xmlContent.find("") name = "" if nameBegin > 0 and nameEnd > 0 : nameBegin = nameBegin + 6 name = xmlContent[nameBegin:nameEnd] else : name = "noname" nameCount = nameDict.get(name,0) + 1 nameDict[name] = nameCount if nameCount > 1 : name = name +"(" + str(nameCount)+")" symbolCount = symbolCount+1 print(" [%3d] : Insert symbol %s with id=%d to sql database" % (symbolCount,name,symbolId) ) fdSql.write("INSERT INTO flm.flm_file_content (id, name, folder_id, file_category_id, file_type_id, file_status_id, operator_id, is_used_by, used_by, properties )") fdSql.write(" VALUES (" + str(symbolId) + ",'" + name + "', 30, 1, 8, 0, 99, true, 1, '{\"imageFormat\": \"xml\"}');\n") symbolPath = "@CMAKE_SOURCE_DIR@/Lts/Tests/DatabaseInitialisation/GuezSymbols/" + fileN fdSql.write("UPDATE flm.flm_file_content SET content = ( SELECT bytea_import('" + symbolPath + "')) WHERE id = " + str(symbolId) + "; \n") maxId = maxId + 1 fdSql.write("ALTER SEQUENCE flm.flm_file_content_id_seq RESTART WITH " + str(maxId) + ";") fdSql.close() print("\n"+str(symbolCount)+" symbols added to sql script") if len(sys.argv) == 4 : typeDump = os.path.abspath(sys.argv[2]) if not os.path.exists(typeDump) : print("types dump file \"%s\" doesn't exists. Abort ..." % typeDump) sys.exit print("\nParsing db type dump \"%s\" ..." % typeDump) fdTypen = open(typeDump,"rt") while True: line = fdTypen.readline() if (line == "") : break typeDataList = line.split("|") if len(typeDataList) > 4 : name = typeDataList[2] symbolId = int(typeDataList[3]) if symbolId > 0 : nameId = re.sub(r"\s+", "", name, flags=re.UNICODE) idDict[nameId] = symbolId print(nameId + " ---> " + str(idDict[nameId])) fdTypen.close() # ------------------------------------------------------------------------------------------- print("\nModify insert_ppm_values_types.sql ...") ppm_script_template = os.path.abspath(sys.argv[3]) + "/insert_ppm_values_types.sql" ppm_script = os.path.abspath(sys.argv[3]) + "/insert_ppm_values_types_with_symbols.sql" flm_script = os.path.abspath(sys.argv[3]) + "/update_flm_used_by.sql" fdRead = open( ppm_script_template,"rt") fdWrite = open( ppm_script,"wt") fdFlmWrite = open(flm_script, "wt") flm_update_lines = set() ppmValueCnt = 0 changeLine = False while True: line = fdRead.readline() if (line == "") : break flmLine = "" if line.startswith("INSERT INTO ppm.ppm_type (") : fdWrite.write(line) changeLine = True continue if line.startswith(" VALUES (") and changeLine == True : nameBegin = line.find("'") + 1 nameEnd = line.find("'",nameBegin) sidc = "" if nameBegin > 0 and nameEnd > 0 : name = line[nameBegin:nameEnd] name = re.sub(r"\s+", "", name, flags=re.UNICODE) keyName = name for key in idDict : if key != "" : if name.startswith(key): keyName = key break if key.startswith("BPz3B") and name.startswith("BPz3B"): keyName = key break if idDict.get(keyName,0) > 0 : symbolBegin = line.find("'",nameEnd+1)+1 symbolEnd = line.find("'",symbolBegin) if symbolBegin == symbolEnd : sidc = "NONE|CVLX|" + str(idDict.get(keyName,0)) tmpLine = line[:symbolBegin]+sidc+line[symbolEnd:] line = tmpLine ppmValueCnt += 1 else : sidc = line[symbolBegin:symbolEnd] # only for logging flmLine = "UPDATE flm.flm_file_content SET used_by = ( SELECT COUNT(*) FROM ppm.ppm_type WHERE symbol = '"+ sidc + "') WHERE id = "+str(idDict.get(keyName,0))+"; \n" if flmLine not in flm_update_lines: fdFlmWrite.write(flmLine) flm_update_lines.add(flmLine) if sidc == "" : print("\"" + name + "\" not found in db type dump." ) print(name + " : " + sidc) fdWrite.write(line) changeLine = False fdWrite.close() fdRead.close() fdFlmWrite.write("UPDATE flm.flm_file_content SET is_used_by = true WHERE used_by > 0; \n") fdFlmWrite.close() print("\n%d symbols added to ppm types\n" % ppmValueCnt)