forked from GaloisInc/OpenUxAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare
executable file
·63 lines (55 loc) · 2.13 KB
/
prepare
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /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()