#! /usr/bin/env python3 from pathlib import Path, PurePath import re import hashlib import tarfile import os import shutil cwd = Path.cwd() subproject_dir = cwd / '3rd' wrap_patches_dir = subproject_dir / 'wrap_patches' cache_dir = subproject_dir / 'packagecache' def process_wrap_patches(): for p in wrap_patches_dir.iterdir(): if not p.is_dir(): continue tf = tarfile.open(str(subproject_dir / (p.name + '_patch.tar')), 'w') tf.add(str(p), arcname=p.name) tf.close() if not os.path.exists( str(cache_dir) ): os.mkdir( str(cache_dir) ) shutil.copyfile( str(subproject_dir / (p.name + '_patch.tar')), str(cache_dir / (p.name + '_patch.tar')) ) def process_wrap_templates(): for tf in subproject_dir.glob('*.wrap.tmpl'): wf = subproject_dir / PurePath(tf).stem with wf.open(mode='w') as w: with tf.open(mode='r') as t: 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() with (subproject_dir / pf).open(mode='rb') as f: 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) # 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)) process_wrap_patches() process_wrap_templates()