blob: 6765891d1986f9ef4bec8bb753791ba5ce148767 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
3import os
4import sys
5import glob
6import shutil
7import errno
8
9def mkdir(d):
10 try:
11 os.makedirs(d)
12 except OSError as e:
13 if e.errno != errno.EEXIST:
14 raise e
15
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050016if len(sys.argv) < 5:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017 print("Incorrect number of arguments specified")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060018 print("syntax: gen-lockedsig-cache <locked-sigs.inc> <input-cachedir> <output-cachedir> <nativelsbstring> [filterfile]")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019 sys.exit(1)
20
Patrick Williamsc0f7c042017-02-23 20:41:17 -060021filterlist = []
22if len(sys.argv) > 5:
23 print('Reading filter file %s' % sys.argv[5])
24 with open(sys.argv[5]) as f:
25 for l in f.readlines():
26 if ":" in l:
27 filterlist.append(l.rstrip())
28
Patrick Williamsf1e5d692016-03-30 15:21:19 -050029print('Reading %s' % sys.argv[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030sigs = []
31with open(sys.argv[1]) as f:
32 for l in f.readlines():
33 if ":" in l:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060034 task, sig = l.split()[0].rsplit(':', 1)
35 if filterlist and not task in filterlist:
36 print('Filtering out %s' % task)
37 else:
38 sigs.append(sig)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039
Patrick Williamsf1e5d692016-03-30 15:21:19 -050040print('Gathering file list')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041files = set()
42for s in sigs:
43 p = sys.argv[2] + "/" + s[:2] + "/*" + s + "*"
44 files |= set(glob.glob(p))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045 p = sys.argv[2] + "/%s/" % sys.argv[4] + s[:2] + "/*" + s + "*"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 files |= set(glob.glob(p))
47
Patrick Williamsf1e5d692016-03-30 15:21:19 -050048print('Processing files')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049for f in files:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050050 sys.stdout.write('Processing %s... ' % f)
51 _, ext = os.path.splitext(f)
52 if not ext in ['.tgz', '.siginfo', '.sig']:
53 # Most likely a temp file, skip it
54 print('skipping')
55 continue
56 dst = os.path.join(sys.argv[3], os.path.relpath(f, sys.argv[2]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 destdir = os.path.dirname(dst)
58 mkdir(destdir)
59
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060 src = os.path.realpath(f)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 if os.path.exists(dst):
62 os.remove(dst)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060063 if (os.stat(src).st_dev == os.stat(destdir).st_dev):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050064 print('linking')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050065 try:
66 os.link(src, dst)
67 except OSError as e:
68 print('hard linking failed, copying')
69 shutil.copyfile(src, dst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 else:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050071 print('copying')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072 shutil.copyfile(src, dst)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050073
74print('Done!')