blob: 48cb67112fd0583bace4036aff83ef84734ab046 [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
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008import shutil
9import errno
Brad Bishop00e122a2019-10-05 11:10:57 -040010import time
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011
12def mkdir(d):
13 try:
14 os.makedirs(d)
15 except OSError as e:
16 if e.errno != errno.EEXIST:
17 raise e
18
Brad Bishop00e122a2019-10-05 11:10:57 -040019# extract the hash from past the last colon to last underscore
20def extract_sha(filename):
21 return filename.split(':')[7].split('_')[0]
22
23# get all files in a directory, extract hash and make
24# a map from hash to list of file with that hash
25def map_sha_to_files(dir_, prefix, sha_map):
26 sstate_prefix_path = dir_ + '/' + prefix + '/'
27 sstate_files = os.listdir(sstate_prefix_path)
28 for f in sstate_files:
29 try:
30 sha = extract_sha(f)
31 if sha not in sha_map:
32 sha_map[sha] = []
33 sha_map[sha].append(sstate_prefix_path + f)
34 except IndexError:
35 continue
36
37# given a prefix build a map of hash to list of files
38def build_sha_cache(prefix):
39 sha_map = {}
40
41 sstate_dir = sys.argv[2]
42 map_sha_to_files(sstate_dir, prefix, sha_map)
43
44 native_sstate_dir = sys.argv[2] + '/' + sys.argv[4]
45 map_sha_to_files(native_sstate_dir, prefix, sha_map)
46
47 return sha_map
48
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050049if len(sys.argv) < 5:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 print("Incorrect number of arguments specified")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060051 print("syntax: gen-lockedsig-cache <locked-sigs.inc> <input-cachedir> <output-cachedir> <nativelsbstring> [filterfile]")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052 sys.exit(1)
53
Patrick Williamsc0f7c042017-02-23 20:41:17 -060054filterlist = []
55if len(sys.argv) > 5:
56 print('Reading filter file %s' % sys.argv[5])
57 with open(sys.argv[5]) as f:
58 for l in f.readlines():
59 if ":" in l:
60 filterlist.append(l.rstrip())
61
Patrick Williamsf1e5d692016-03-30 15:21:19 -050062print('Reading %s' % sys.argv[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063sigs = []
64with open(sys.argv[1]) as f:
65 for l in f.readlines():
66 if ":" in l:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060067 task, sig = l.split()[0].rsplit(':', 1)
68 if filterlist and not task in filterlist:
69 print('Filtering out %s' % task)
70 else:
71 sigs.append(sig)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072
Patrick Williamsf1e5d692016-03-30 15:21:19 -050073print('Gathering file list')
Brad Bishop00e122a2019-10-05 11:10:57 -040074start_time = time.perf_counter()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075files = set()
Brad Bishop00e122a2019-10-05 11:10:57 -040076sstate_content_cache = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077for s in sigs:
Brad Bishop00e122a2019-10-05 11:10:57 -040078 prefix = s[:2]
79 if prefix not in sstate_content_cache:
80 sstate_content_cache[prefix] = build_sha_cache(prefix)
81
82 for f in sstate_content_cache[prefix][s]:
83 files.add(f)
84
85elapsed = time.perf_counter() - start_time
86print("Gathering file list took %.1fs" % elapsed)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
Patrick Williamsf1e5d692016-03-30 15:21:19 -050088print('Processing files')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089for f in files:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050090 sys.stdout.write('Processing %s... ' % f)
91 _, ext = os.path.splitext(f)
92 if not ext in ['.tgz', '.siginfo', '.sig']:
93 # Most likely a temp file, skip it
94 print('skipping')
95 continue
96 dst = os.path.join(sys.argv[3], os.path.relpath(f, sys.argv[2]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 destdir = os.path.dirname(dst)
98 mkdir(destdir)
99
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600100 src = os.path.realpath(f)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 if os.path.exists(dst):
102 os.remove(dst)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600103 if (os.stat(src).st_dev == os.stat(destdir).st_dev):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500104 print('linking')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 try:
106 os.link(src, dst)
107 except OSError as e:
108 print('hard linking failed, copying')
109 shutil.copyfile(src, dst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 else:
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500111 print('copying')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600112 shutil.copyfile(src, dst)
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500113
114print('Done!')