fighting-konoha/tools/build_precache.py

17 lines
1.5 KiB
Python

# Rebuilds precache.js and files.json. Run from anywhere: python3 tools/build_precache.py
import os,json,hashlib,subprocess
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)),'..'))
# every top-level page, style and script (new files like feel.js/meta.js are picked up automatically)
root=sorted(f for f in os.listdir('.') if f.endswith(('.html','.css','.js')) and f not in ('sw.js','precache.js'))
tracked=subprocess.run(['git','ls-files','assets'],capture_output=True,text=True).stdout.split('\n')
others=subprocess.run(['git','ls-files','--others','--exclude-standard','assets'],capture_output=True,text=True).stdout.split('\n')
assets=sorted({f for f in tracked+others if f and os.path.isfile(f)})
files=[f for f in root+assets if os.path.isfile(f)]
entries=[]
for f in files:
b=open(f,'rb').read(); entries.append([f,len(b),hashlib.sha1(b).hexdigest()[:12]])
total=sum(e[1] for e in entries); ver=hashlib.sha1(''.join(e[0]+e[2] for e in entries).encode()).hexdigest()[:12]
open('precache.js','w').write('/* Generated by tools/build_dist.mjs (regenerated by hand for Konoha Fighting): runtime files [path, bytes, sha1] for preload.js and sw.js. Do not edit. */\nself.AETHER_PRECACHE = '+json.dumps({'version':ver,'total':total,'files':entries},separators=(',',':'))+';\n')
open('files.json','w').write(json.dumps({'files':len(files),'bytes':total,'list':[{'file':f,'bytes':b} for f,b,_ in entries]},indent=2)+'\n')
print(len(files),'files',round(total/1e6,1),'MB; ver',ver)