blob: e3076e11a535dd4bb469257f64e06f6358df54b6 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Brad Bishopc342db32019-05-15 21:57:59 -04002#
3# SPDX-License-Identifier: GPL-2.0-only
4#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005
6import os
7import sys
8import glob
9import shutil
10import errno
11
12def mkdir(d):
13 try:
14 os.makedirs(d)
15 except OSError as e:
16 if e.errno != errno.EEXIST:
17 raise e
18
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050019if len(sys.argv) < 5:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020 print("Incorrect number of arguments specified")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060021 print("syntax: gen-lockedsig-cache <locked-sigs.inc> <input-cachedir> <output-cachedir> <nativelsbstring> [filterfile]")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022 sys.exit(1)
23
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024filterlist = []
25if len(sys.argv) > 5:
26 print('Reading filter file %s' % sys.argv[5])
27 with open(sys.argv[5]) as f:
28 for l in f.readlines():
29 if ":" in l:
30 filterlist.append(l.rstrip())
31
Patrick Williamsf1e5d692016-03-30 15:21:19 -050032print('Reading %s' % sys.argv[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033sigs = []
34with open(sys.argv[1]) as f:
35 for l in f.readlines():
36 if ":" in l:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060037 task, sig = l.split()[0].rsplit(':', 1)
38 if filterlist and not task in filterlist:
39 print('Filtering out %s' % task)
40 else:
41 sigs.append(sig)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
Patrick Williamsf1e5d692016-03-30 15:21:19 -050043print('Gathering file list')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044files = set()
45for s in sigs:
46 p = sys.argv[2] + "/" + s[:2] + "/*" + s + "*"
47 files |= set(glob.glob(p))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050048 p = sys.argv[2] + "/%s/" % sys.argv[4] + s[:2] + "/*" + s + "*"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 files |= set(glob.glob(p))
50
Patrick Williamsf1e5d692016-03-30 15:21:19 -050051print('Processing files')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052for f in files:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050053 sys.stdout.write('Processing %s... ' % f)
54 _, ext = os.path.splitext(f)
55 if not ext in ['.tgz', '.siginfo', '.sig']:
56 # Most likely a temp file, skip it
57 print('skipping')
58 continue
59 dst = os.path.join(sys.argv[3], os.path.relpath(f, sys.argv[2]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050060 destdir = os.path.dirname(dst)
61 mkdir(destdir)
62
Patrick Williamsc0f7c042017-02-23 20:41:17 -060063 src = os.path.realpath(f)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 if os.path.exists(dst):
65 os.remove(dst)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060066 if (os.stat(src).st_dev == os.stat(destdir).st_dev):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050067 print('linking')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 try:
69 os.link(src, dst)
70 except OSError as e:
71 print('hard linking failed, copying')
72 shutil.copyfile(src, dst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 else:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050074 print('copying')
Patrick Williamsc0f7c042017-02-23 20:41:17 -060075 shutil.copyfile(src, dst)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050076
77print('Done!')