pyren/pyren3/convert_db.py
2022-08-14 10:59:31 +03:00

101 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python3
import glob
import os
import pickle
import sys
import zipfile
from io import StringIO
import mod_db_manager
from mod_optfile import *
if __name__ == "__main__":
zipoutput = StringIO ()
if len (sys.argv) < 2:
print("Usage : convert_db.py [path/to/GenAppli]")
exit ()
if not os.path.exists('./cache'):
os.makedirs('./cache')
mod_db_manager.find_DBs()
inputpath = sys.argv[1]
ecudir = os.path.join (inputpath, "EcuRenault")
vehicledir = os.path.join (inputpath, "Vehicles")
locationdir = os.path.join (inputpath, "Location")
ecufiles = glob.glob (os.path.join (ecudir, "*.xml"))
fbsessionfiles = glob.glob (os.path.join (ecudir, "Sessions", "FB*.xml"))
fbsessionfiles += glob.glob (os.path.join (ecudir, "Sessions", "FG*.xml"))
fgsessionfiles = glob.glob (os.path.join (ecudir, "Sessions", "SG*.xml"))
vehiclesfiles = glob.glob (os.path.join (vehicledir, "*.xml"))
dfgfiles = glob.glob (os.path.join (vehicledir, "DFG", "*.xml"))
locationsfiles = glob.glob (os.path.join (locationdir, "*.bqm"))
scnerariosfiles = glob.glob (os.path.join (ecudir, "Scenarios", "*.xml"))
with zipfile.ZipFile (zipoutput, mode='w', compression=zipfile.ZIP_DEFLATED, allowZip64=True) as zf:
for vf in scnerariosfiles:
print("Processing file ", vf)
f = open (vf, "r")
data = f.read ()
zf.writestr (os.path.join ("EcuRenault", "Scenarios", os.path.basename (vf)), str (data))
for vf in vehiclesfiles:
print("Processing file ", vf)
f = open (vf, "r")
data = f.read ()
zf.writestr (os.path.join ("Vehicles", os.path.basename (vf)), str (data))
for vf in dfgfiles:
print("Processing file ", vf)
f = open(vf, "r")
data = f.read()
zf.writestr(os.path.join("Vehicles", "DFG", os.path.basename(vf)), str(data))
for vf in ecufiles:
vf = vf[:-4] + vf[-4:].lower()
print("Processing file ", vf)
f = open (vf, "r")
data = f.read ()
zf.writestr (os.path.join ("EcuRenault", os.path.basename (vf)), str (data))
for vf in fbsessionfiles:
vf = vf[:-4] + vf[-4:].lower()
print("Processing file ", vf)
f = open (vf, "r")
data = f.read ()
zf.writestr (os.path.join ("EcuRenault", "Sessions", os.path.basename (vf)), str (data))
for vf in locationsfiles:
print("Processing file ", vf)
try:
while len(vf) and vf[0] in ['.', '/', '\\']:
vf = vf[1:]
optf = optfile (vf)
except:
print("Skipping file ", vf)
continue
data = pickle.dumps (optf.dict)
zf.writestr (os.path.join ("Location",
os.path.basename (vf).replace (".bqm", ".p")), str (data))
for vf in fgsessionfiles:
vf = vf[:-4] + vf[-4:].lower()
print("Processing file ", vf)
try:
while len(vf) and vf[0] in ['.', '/', '\\']:
vf = vf[1:]
optf = optfile (vf)
except:
print("Skipping file ", vf)
continue
data = pickle.dumps (optf.dict)
zf.writestr (os.path.join ("EcuRenault", "Sessions",
os.path.basename (vf).replace ("FG", "UG").replace (".xml", ".p")), str (data))
with open ("pyrendata.zip", "wb") as zf:
zf.write (zipoutput.getvalue ())