Merge branch 'wellenvogel:master' into feature/env2
This commit is contained in:
commit
71d20fa14d
257
extra_script.py
257
extra_script.py
|
@ -10,10 +10,18 @@ Import("env")
|
||||||
GEN_DIR='generated'
|
GEN_DIR='generated'
|
||||||
CFG_FILE='web/config.json'
|
CFG_FILE='web/config.json'
|
||||||
XDR_FILE='web/xdrconfig.json'
|
XDR_FILE='web/xdrconfig.json'
|
||||||
FILES=['web/index.html',CFG_FILE,XDR_FILE,'web/index.js','web/index.css']
|
|
||||||
CFG_INCLUDE='GwConfigDefinitions.h'
|
CFG_INCLUDE='GwConfigDefinitions.h'
|
||||||
XDR_INCLUDE='GwXdrTypeMappings.h'
|
XDR_INCLUDE='GwXdrTypeMappings.h'
|
||||||
|
EMBEDDED_INCLUDE="GwEmbeddedFiles.h"
|
||||||
|
|
||||||
|
def getEmbeddedFiles(env):
|
||||||
|
rt=[]
|
||||||
|
efiles=env.GetProjectOption("board_build.embed_files")
|
||||||
|
for f in efiles.split("\n"):
|
||||||
|
if f == '':
|
||||||
|
continue
|
||||||
|
rt.append(f)
|
||||||
|
return rt
|
||||||
|
|
||||||
def basePath():
|
def basePath():
|
||||||
#see: https://stackoverflow.com/questions/16771894/python-nameerror-global-name-file-is-not-defined
|
#see: https://stackoverflow.com/questions/16771894/python-nameerror-global-name-file-is-not-defined
|
||||||
|
@ -38,126 +46,163 @@ def isCurrent(infile,outfile):
|
||||||
print("%s is newer then %s, no need to recreate"%(outfile,infile))
|
print("%s is newer then %s, no need to recreate"%(outfile,infile))
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
def compressFile(inFile):
|
def compressFile(inFile,outfile):
|
||||||
outfile=os.path.basename(inFile)+".gz"
|
|
||||||
inFile=os.path.join(basePath(),inFile)
|
|
||||||
outfile=os.path.join(outPath(),outfile)
|
|
||||||
if isCurrent(inFile,outfile):
|
if isCurrent(inFile,outfile):
|
||||||
return
|
return
|
||||||
with open(inFile, 'rb') as f_in:
|
with open(inFile, 'rb') as f_in:
|
||||||
with gzip.open(outfile, 'wb') as f_out:
|
with gzip.open(outfile, 'wb') as f_out:
|
||||||
shutil.copyfileobj(f_in, f_out)
|
shutil.copyfileobj(f_in, f_out)
|
||||||
|
|
||||||
|
def generateFile(infile,outfile,callback,inMode='rb',outMode='w'):
|
||||||
def generateCfg():
|
|
||||||
outfile=os.path.join(outPath(),CFG_INCLUDE)
|
|
||||||
infile=os.path.join(basePath(),CFG_FILE)
|
|
||||||
if isCurrent(infile,outfile):
|
if isCurrent(infile,outfile):
|
||||||
return
|
return
|
||||||
print("creating %s"%CFG_INCLUDE)
|
print("creating %s"%outfile)
|
||||||
oh=None
|
oh=None
|
||||||
with open(CFG_FILE,'rb') as ch:
|
with open(infile,inMode) as ch:
|
||||||
config=json.load(ch)
|
with open(outfile,'w') as oh:
|
||||||
try:
|
try:
|
||||||
with open(outfile,'w') as oh:
|
callback(ch,oh,inFile=infile)
|
||||||
oh.write("//generated from %s\n"%CFG_FILE)
|
oh.close()
|
||||||
oh.write('#include "GwConfigItem.h"\n')
|
except Exception as e:
|
||||||
l=len(config)
|
|
||||||
oh.write('class GwConfigDefinitions{\n')
|
|
||||||
oh.write(' public:\n')
|
|
||||||
oh.write(' int getNumConfig() const{return %d;}\n'%(l))
|
|
||||||
for item in config:
|
|
||||||
n=item.get('name')
|
|
||||||
if n is None:
|
|
||||||
continue
|
|
||||||
if len(n) > 15:
|
|
||||||
raise Exception("%s: config names must be max 15 caracters"%n)
|
|
||||||
oh.write(' const String %s=F("%s");\n'%(n,n))
|
|
||||||
oh.write(' protected:\n')
|
|
||||||
oh.write(' GwConfigItem *configs[%d]={\n'%(l))
|
|
||||||
first=True
|
|
||||||
for item in config:
|
|
||||||
if not first:
|
|
||||||
oh.write(',\n')
|
|
||||||
first=False
|
|
||||||
oh.write(" new GwConfigItem(%s,\"%s\")"%(item.get('name'),item.get('default')))
|
|
||||||
oh.write('};\n')
|
|
||||||
oh.write('};\n')
|
|
||||||
oh.close()
|
|
||||||
except Exception as e:
|
|
||||||
if oh is not None:
|
|
||||||
try:
|
try:
|
||||||
oh.close()
|
oh.close()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
os.unlink(outfile)
|
os.unlink(outfile)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def generateXdrMappings():
|
def writeFileIfChanged(fileName,data):
|
||||||
outfile=os.path.join(outPath(),XDR_INCLUDE)
|
if os.path.exists(fileName):
|
||||||
infile=os.path.join(basePath(),XDR_FILE)
|
with open(fileName,"r") as ih:
|
||||||
if isCurrent(infile,outfile):
|
old=ih.read()
|
||||||
return
|
ih.close()
|
||||||
print("creating %s"%XDR_INCLUDE)
|
if old == data:
|
||||||
oh=None
|
return
|
||||||
|
print("#generating %s"%fileName)
|
||||||
|
with open(fileName,"w") as oh:
|
||||||
|
oh.write(data)
|
||||||
|
|
||||||
with open(infile,"rb") as fp:
|
def generateCfg(ch,oh,inFile=''):
|
||||||
jdoc=json.load(fp)
|
config=json.load(ch)
|
||||||
try:
|
oh.write("//generated from %s\n"%inFile)
|
||||||
with open(outfile,"w") as oh:
|
oh.write('#include "GwConfigItem.h"\n')
|
||||||
oh.write("static GwXDRTypeMapping* typeMappings[]={\n")
|
l=len(config)
|
||||||
first=True
|
oh.write('class GwConfigDefinitions{\n')
|
||||||
for cat in jdoc:
|
oh.write(' public:\n')
|
||||||
item=jdoc[cat]
|
oh.write(' int getNumConfig() const{return %d;}\n'%(l))
|
||||||
cid=item.get('id')
|
for item in config:
|
||||||
if cid is None:
|
n=item.get('name')
|
||||||
continue
|
if n is None:
|
||||||
tc=item.get('type')
|
continue
|
||||||
if tc is not None:
|
if len(n) > 15:
|
||||||
if first:
|
raise Exception("%s: config names must be max 15 caracters"%n)
|
||||||
first=False
|
oh.write(' const String %s=F("%s");\n'%(n,n))
|
||||||
else:
|
oh.write(' protected:\n')
|
||||||
oh.write(",\n")
|
oh.write(' GwConfigItem *configs[%d]={\n'%(l))
|
||||||
oh.write(" new GwXDRTypeMapping(%d,%d,0) /*%s*/"%(cid,tc,cat))
|
first=True
|
||||||
fields=item.get('fields')
|
for item in config:
|
||||||
if fields is None:
|
if not first:
|
||||||
continue
|
oh.write(',\n')
|
||||||
idx=0
|
first=False
|
||||||
for fe in fields:
|
oh.write(" new GwConfigItem(%s,\"%s\")"%(item.get('name'),item.get('default')))
|
||||||
if not isinstance(fe,dict):
|
oh.write('};\n')
|
||||||
continue
|
oh.write('};\n')
|
||||||
tc=fe.get('t')
|
|
||||||
id=fe.get('v')
|
|
||||||
if id is None:
|
|
||||||
id=idx
|
|
||||||
idx+=1
|
|
||||||
l=fe.get('l') or ''
|
|
||||||
if tc is None or id is None:
|
|
||||||
continue
|
|
||||||
if first:
|
|
||||||
first=False
|
|
||||||
else:
|
|
||||||
oh.write(",\n")
|
|
||||||
oh.write(" new GwXDRTypeMapping(%d,%d,%d) /*%s:%s*/"%(cid,tc,id,cat,l))
|
|
||||||
oh.write("\n")
|
|
||||||
oh.write("};\n")
|
|
||||||
except Exception as e:
|
|
||||||
if oh:
|
|
||||||
try:
|
|
||||||
oh.close()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
os.unlink(outfile)
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
def generateXdrMappings(fp,oh,inFile=''):
|
||||||
|
jdoc=json.load(fp)
|
||||||
|
oh.write("static GwXDRTypeMapping* typeMappings[]={\n")
|
||||||
|
first=True
|
||||||
|
for cat in jdoc:
|
||||||
|
item=jdoc[cat]
|
||||||
|
cid=item.get('id')
|
||||||
|
if cid is None:
|
||||||
|
continue
|
||||||
|
tc=item.get('type')
|
||||||
|
if tc is not None:
|
||||||
|
if first:
|
||||||
|
first=False
|
||||||
|
else:
|
||||||
|
oh.write(",\n")
|
||||||
|
oh.write(" new GwXDRTypeMapping(%d,%d,0) /*%s*/"%(cid,tc,cat))
|
||||||
|
fields=item.get('fields')
|
||||||
|
if fields is None:
|
||||||
|
continue
|
||||||
|
idx=0
|
||||||
|
for fe in fields:
|
||||||
|
if not isinstance(fe,dict):
|
||||||
|
continue
|
||||||
|
tc=fe.get('t')
|
||||||
|
id=fe.get('v')
|
||||||
|
if id is None:
|
||||||
|
id=idx
|
||||||
|
idx+=1
|
||||||
|
l=fe.get('l') or ''
|
||||||
|
if tc is None or id is None:
|
||||||
|
continue
|
||||||
|
if first:
|
||||||
|
first=False
|
||||||
|
else:
|
||||||
|
oh.write(",\n")
|
||||||
|
oh.write(" new GwXDRTypeMapping(%d,%d,%d) /*%s:%s*/"%(cid,tc,id,cat,l))
|
||||||
|
oh.write("\n")
|
||||||
|
oh.write("};\n")
|
||||||
|
|
||||||
|
def generateEmbedded(elist,outFile):
|
||||||
|
content=""
|
||||||
|
for entry in elist:
|
||||||
|
content+="EMBED_GZ_FILE(\"%s\",%s,\"%s\");\n"%entry
|
||||||
|
writeFileIfChanged(outFile,content)
|
||||||
|
|
||||||
if not checkDir():
|
def getContentType(fn):
|
||||||
sys.exit(1)
|
if (fn.endswith('.gz')):
|
||||||
for f in FILES:
|
fn=fn[0:-3]
|
||||||
print("compressing %s"%f)
|
if (fn.endswith('html')):
|
||||||
compressFile(f)
|
return "text/html"
|
||||||
generateCfg()
|
if (fn.endswith('json')):
|
||||||
generateXdrMappings()
|
return "application/json"
|
||||||
version="dev"+datetime.now().strftime("%Y%m%d")
|
if (fn.endswith('js')):
|
||||||
env.Append(CPPDEFINES=[('GWDEVVERSION',version)])
|
return "text/javascript"
|
||||||
|
return "application/octet-stream"
|
||||||
|
|
||||||
|
def prebuild(env):
|
||||||
|
print("#prebuild running")
|
||||||
|
if not checkDir():
|
||||||
|
sys.exit(1)
|
||||||
|
embedded=getEmbeddedFiles(env)
|
||||||
|
filedefs=[]
|
||||||
|
for ef in embedded:
|
||||||
|
print("#checking embedded file %s"%ef)
|
||||||
|
(dn,fn)=os.path.split(ef)
|
||||||
|
pureName=fn
|
||||||
|
if pureName.endswith('.gz'):
|
||||||
|
pureName=pureName[0:-3]
|
||||||
|
ct=getContentType(pureName)
|
||||||
|
usname=ef.replace('/','_').replace('.','_')
|
||||||
|
filedefs.append((pureName,usname,ct))
|
||||||
|
inFile=os.path.join(basePath(),"web",pureName)
|
||||||
|
if os.path.exists(inFile):
|
||||||
|
print("compressing %s"%inFile)
|
||||||
|
compressFile(inFile,ef)
|
||||||
|
else:
|
||||||
|
print("#WARNING: infile %s for %s not found"%(inFile,ef))
|
||||||
|
generateEmbedded(filedefs,os.path.join(outPath(),EMBEDDED_INCLUDE))
|
||||||
|
generateFile(os.path.join(basePath(),CFG_FILE),os.path.join(outPath(),CFG_INCLUDE),generateCfg)
|
||||||
|
generateFile(os.path.join(basePath(),XDR_FILE),os.path.join(outPath(),XDR_INCLUDE),generateXdrMappings)
|
||||||
|
version="dev"+datetime.now().strftime("%Y%m%d")
|
||||||
|
env.Append(CPPDEFINES=[('GWDEVVERSION',version)])
|
||||||
|
|
||||||
|
def cleangenerated(source, target, env):
|
||||||
|
od=outPath()
|
||||||
|
if os.path.isdir(od):
|
||||||
|
print("#cleaning up %s"%od)
|
||||||
|
for f in os.listdir(od):
|
||||||
|
if f == "." or f == "..":
|
||||||
|
continue
|
||||||
|
fn=os.path.join(od,f)
|
||||||
|
os.unlink(f)
|
||||||
|
|
||||||
|
print("#prescript...")
|
||||||
|
prebuild(env)
|
||||||
|
#script does not run on clean yet - maybe in the future
|
||||||
|
env.AddPostAction("clean",cleangenerated)
|
||||||
|
|
|
@ -16,15 +16,12 @@ class EmbeddedFile {
|
||||||
embeddedFiles[name]=this;
|
embeddedFiles[name]=this;
|
||||||
}
|
}
|
||||||
} ;
|
} ;
|
||||||
#define EMBED_GZ_FILE(fileName, fileExt, contentType) \
|
#define EMBED_GZ_FILE(fileName, binName, contentType) \
|
||||||
extern const uint8_t fileName##_##fileExt##_File[] asm("_binary_generated_" #fileName "_" #fileExt "_gz_start"); \
|
extern const uint8_t binName##_File[] asm("_binary_" #binName "_start"); \
|
||||||
extern const uint8_t fileName##_##fileExt##_FileLen[] asm("_binary_generated_" #fileName "_" #fileExt "_gz_size"); \
|
extern const uint8_t binName##_FileLen[] asm("_binary_" #binName "_size"); \
|
||||||
const EmbeddedFile fileName##_##fileExt##_Config(#fileName "." #fileExt,contentType,(const uint8_t*)fileName##_##fileExt##_File,(int)fileName##_##fileExt##_FileLen);
|
const EmbeddedFile binName##_Config(fileName,contentType,(const uint8_t*)binName##_File,(int)binName##_FileLen);
|
||||||
|
|
||||||
EMBED_GZ_FILE(index,html,"text/html")
|
#include "GwEmbeddedFiles.h"
|
||||||
EMBED_GZ_FILE(config,json,"application/json")
|
|
||||||
EMBED_GZ_FILE(index,js,"text/javascript")
|
|
||||||
EMBED_GZ_FILE(index,css,"text/css")
|
|
||||||
|
|
||||||
void sendEmbeddedFile(String name,String contentType,AsyncWebServerRequest *request){
|
void sendEmbeddedFile(String name,String contentType,AsyncWebServerRequest *request){
|
||||||
std::map<String,EmbeddedFile*>::iterator it=embeddedFiles.find(name);
|
std::map<String,EmbeddedFile*>::iterator it=embeddedFiles.find(name);
|
||||||
|
|
|
@ -23,6 +23,7 @@ board_build.embed_files =
|
||||||
generated/index.js.gz
|
generated/index.js.gz
|
||||||
generated/index.css.gz
|
generated/index.css.gz
|
||||||
generated/config.json.gz
|
generated/config.json.gz
|
||||||
|
generated/xdrconfig.json.gz
|
||||||
board_build.partitions = partitions_custom.csv
|
board_build.partitions = partitions_custom.csv
|
||||||
extra_scripts =
|
extra_scripts =
|
||||||
pre:extra_script.py
|
pre:extra_script.py
|
||||||
|
|
Loading…
Reference in New Issue