2017-05-07 11:03:58 -04:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
from pathlib import Path, PurePath
|
|
|
|
|
import re
|
|
|
|
|
import hashlib
|
2017-05-10 15:01:19 -07:00
|
|
|
import tarfile
|
2018-02-26 13:14:27 -05:00
|
|
|
import os
|
|
|
|
|
import shutil
|
2017-05-07 11:03:58 -04:00
|
|
|
|
2017-05-10 15:01:19 -07:00
|
|
|
cwd = Path.cwd()
|
|
|
|
|
subproject_dir = cwd / '3rd'
|
|
|
|
|
wrap_patches_dir = subproject_dir / 'wrap_patches'
|
2018-03-06 13:54:39 -05:00
|
|
|
cache_dir = subproject_dir / 'packagecache'
|
2017-05-07 11:03:58 -04:00
|
|
|
|
2017-05-10 15:01:19 -07:00
|
|
|
def process_wrap_patches():
|
|
|
|
|
for p in wrap_patches_dir.iterdir():
|
|
|
|
|
if not p.is_dir(): continue
|
2017-05-10 16:36:43 -07:00
|
|
|
tf = tarfile.open(str(subproject_dir / (p.name + '_patch.tar')), 'w')
|
|
|
|
|
tf.add(str(p), arcname=p.name)
|
2017-05-10 15:01:19 -07:00
|
|
|
tf.close()
|
2018-03-07 15:36:05 -05:00
|
|
|
if not os.path.exists( str(cache_dir) ):
|
|
|
|
|
os.mkdir( str(cache_dir) )
|
2018-03-06 13:54:39 -05:00
|
|
|
shutil.copyfile( str(subproject_dir / (p.name + '_patch.tar')), str(cache_dir / (p.name + '_patch.tar')) )
|
2017-05-07 11:03:58 -04:00
|
|
|
|
2017-05-10 15:01:19 -07:00
|
|
|
def process_wrap_templates():
|
|
|
|
|
for tf in subproject_dir.glob('*.wrap.tmpl'):
|
|
|
|
|
wf = subproject_dir / PurePath(tf).stem
|
2017-05-10 16:36:43 -07:00
|
|
|
with wf.open(mode='w') as w:
|
|
|
|
|
with tf.open(mode='r') as t:
|
2017-05-10 15:01:19 -07:00
|
|
|
d = ''
|
|
|
|
|
while True:
|
|
|
|
|
l = t.readline()
|
|
|
|
|
if l == '': break;
|
|
|
|
|
if re.match('^patch_filename', l):
|
|
|
|
|
h = hashlib.new('sha256')
|
|
|
|
|
bs = h.block_size
|
|
|
|
|
pf = re.split('=', l)[1].strip()
|
2017-05-10 16:36:43 -07:00
|
|
|
with (subproject_dir / pf).open(mode='rb') as f:
|
2017-05-10 15:01:19 -07:00
|
|
|
while True:
|
|
|
|
|
b = f.read(bs)
|
|
|
|
|
if not b: break
|
|
|
|
|
h.update(b)
|
|
|
|
|
d = h.hexdigest()
|
|
|
|
|
l = re.sub('%top%', subproject_dir.as_uri(), l)
|
|
|
|
|
if re.match('^patch_hash', l) and d == '':
|
|
|
|
|
print('%s: patch_hash must follow patch_filename\n' % t.name)
|
|
|
|
|
l = re.sub('%patch_hash%', d, l)
|
|
|
|
|
w.write(l)
|
|
|
|
|
|
2018-02-26 13:14:27 -05:00
|
|
|
# clean up any old patches before re-creating them
|
|
|
|
|
pd = cwd / Path('3rd')
|
|
|
|
|
for p in pd.glob('*_patch.tar'):
|
|
|
|
|
d = str(p)[0:-10]
|
|
|
|
|
if Path(d).is_dir():
|
|
|
|
|
shutil.rmtree(d)
|
|
|
|
|
pc = pd / 'packagecache'
|
|
|
|
|
for c in pc.glob('*_patch.tar'):
|
|
|
|
|
os.remove(str(c))
|
|
|
|
|
|
2017-05-10 15:01:19 -07:00
|
|
|
process_wrap_patches()
|
|
|
|
|
process_wrap_templates()
|
2018-03-06 13:54:39 -05:00
|
|
|
|
|
|
|
|
|