uBlock/tools/build_meta.py

86 lines
2.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os
import re
import json
from time import strftime
from datetime import datetime
from shutil import rmtree as rmt, copy
from collections import OrderedDict
from xml.sax.saxutils import escape
osp = os.path
pj = osp.join
os.chdir('..')
def rmtree(path):
if osp.exists(path):
rmt(path)
def mkdirs(path):
try:
os.makedirs(path)
finally:
return osp.exists(path)
src_dir = pj('src')
2014-11-07 12:59:01 +01:00
meta_dir = pj('meta')
with open(pj(meta_dir, 'config.json'), encoding='utf-8') as f:
config = json.load(f)
vendors = config['vendors']
del config['vendors']
tmp = datetime.now() - datetime(year=datetime.today().year, month=1, day=1)
config['build_number'] = strftime('%y' + str(int(tmp.total_seconds() * 65535 / 31536000)).zfill(5))
2014-11-07 12:59:01 +01:00
descriptions = OrderedDict({})
with open(pj(src_dir, 'js', 'vapi-appinfo.js'), 'r+t', encoding='utf-8', newline='\n') as f:
tmp = f.read()
f.seek(0)
f.write(re.sub(
r'/\*\*/([^:]+:).+',
lambda m: '/**/' + m.group(1) + " '" + config[m.group(1)[:-1]] + "',",
tmp
))
2014-11-07 12:59:01 +01:00
with open(pj(src_dir, vendors['crx']['manifest']), 'wt', encoding='utf-8', newline='\n') as f:
with open(pj(meta_dir, 'crx', vendors['crx']['manifest']), 'r') as cf:
cf_content = cf.read()
2014-11-07 12:59:01 +01:00
f.write(
re.sub(r"\{(?=\W)|(?<=\W)\}", r'\g<0>\g<0>', cf_content).format(**config)
)
with open(pj(src_dir, 'locales.json'), 'wt', encoding='utf-8', newline='\n') as f:
tmp = {
'_': config['def_lang']
}
for alpha2 in descriptions:
tmp[alpha2] = 1
json.dump(tmp, f, sort_keys=True, ensure_ascii=False)
with open(pj(src_dir, vendors['safariextz']['manifest']['Info']), 'wt', encoding='utf-8', newline='\n') as f:
2014-10-25 09:42:07 +02:00
config['app_id'] = vendors['safariextz']['app_id']
config['description'] = descriptions[config['def_lang']]
with open(pj(meta_dir, 'safariextz', vendors['safariextz']['manifest']['Info']), 'r') as cf:
2014-10-25 09:42:07 +02:00
cf_content = cf.read()
f.write(cf_content.format(**config))
copy(pj(meta_dir, 'safariextz', vendors['safariextz']['manifest']['Settings']), pj(src_dir, vendors['safariextz']['manifest']['Settings']))