blob: 023015ec41c13af47e10946d60020600835b1f82 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Brad Bishopc342db32019-05-15 21:57:59 -04002#
Patrick Williams92b42cb2022-09-03 06:53:57 -05003#
4# Copyright OpenEmbedded Contributors
5#
Brad Bishopc342db32019-05-15 21:57:59 -04006# SPDX-License-Identifier: GPL-2.0-only
7#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
9import os
10import sys
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011import shutil
12import errno
Brad Bishop00e122a2019-10-05 11:10:57 -040013import time
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014
15def mkdir(d):
16 try:
17 os.makedirs(d)
18 except OSError as e:
19 if e.errno != errno.EEXIST:
20 raise e
21
Brad Bishop00e122a2019-10-05 11:10:57 -040022# extract the hash from past the last colon to last underscore
23def extract_sha(filename):
24 return filename.split(':')[7].split('_')[0]
25
26# get all files in a directory, extract hash and make
27# a map from hash to list of file with that hash
28def map_sha_to_files(dir_, prefix, sha_map):
29 sstate_prefix_path = dir_ + '/' + prefix + '/'
Brad Bishopf3f93bb2019-10-16 14:33:32 -040030 if not os.path.exists(sstate_prefix_path):
31 return
Brad Bishop00e122a2019-10-05 11:10:57 -040032 sstate_files = os.listdir(sstate_prefix_path)
33 for f in sstate_files:
34 try:
35 sha = extract_sha(f)
36 if sha not in sha_map:
37 sha_map[sha] = []
38 sha_map[sha].append(sstate_prefix_path + f)
39 except IndexError:
40 continue
41
42# given a prefix build a map of hash to list of files
43def build_sha_cache(prefix):
44 sha_map = {}
45
46 sstate_dir = sys.argv[2]
47 map_sha_to_files(sstate_dir, prefix, sha_map)
48
49 native_sstate_dir = sys.argv[2] + '/' + sys.argv[4]
50 map_sha_to_files(native_sstate_dir, prefix, sha_map)
51
52 return sha_map
53
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050054if len(sys.argv) < 5:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 print("Incorrect number of arguments specified")
Patrick Williamsc0f7c042017-02-23 20:41:17 -060056 print("syntax: gen-lockedsig-cache <locked-sigs.inc> <input-cachedir> <output-cachedir> <nativelsbstring> [filterfile]")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 sys.exit(1)
58
Patrick Williamsc0f7c042017-02-23 20:41:17 -060059filterlist = []
60if len(sys.argv) > 5:
61 print('Reading filter file %s' % sys.argv[5])
62 with open(sys.argv[5]) as f:
63 for l in f.readlines():
64 if ":" in l:
65 filterlist.append(l.rstrip())
66
Patrick Williamsf1e5d692016-03-30 15:21:19 -050067print('Reading %s' % sys.argv[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068sigs = []
69with open(sys.argv[1]) as f:
70 for l in f.readlines():
71 if ":" in l:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060072 task, sig = l.split()[0].rsplit(':', 1)
73 if filterlist and not task in filterlist:
74 print('Filtering out %s' % task)
75 else:
76 sigs.append(sig)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077
Patrick Williamsf1e5d692016-03-30 15:21:19 -050078print('Gathering file list')
Brad Bishop00e122a2019-10-05 11:10:57 -040079start_time = time.perf_counter()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080files = set()
Brad Bishop00e122a2019-10-05 11:10:57 -040081sstate_content_cache = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082for s in sigs:
Brad Bishop00e122a2019-10-05 11:10:57 -040083 prefix = s[:2]
Andrew Geissler82c905d2020-04-13 13:39:40 -050084 prefix2 = s[2:4]
Brad Bishop00e122a2019-10-05 11:10:57 -040085 if prefix not in sstate_content_cache:
Andrew Geissler82c905d2020-04-13 13:39:40 -050086 sstate_content_cache[prefix] = {}
87 if prefix2 not in sstate_content_cache[prefix]:
88 sstate_content_cache[prefix][prefix2] = build_sha_cache(prefix + "/" + prefix2)
Brad Bishop00e122a2019-10-05 11:10:57 -040089
Andrew Geissler82c905d2020-04-13 13:39:40 -050090 if s in sstate_content_cache[prefix][prefix2]:
91 for f in sstate_content_cache[prefix][prefix2][s]:
92 files.add(f)
Brad Bishop00e122a2019-10-05 11:10:57 -040093
94elapsed = time.perf_counter() - start_time
95print("Gathering file list took %.1fs" % elapsed)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
Patrick Williamsf1e5d692016-03-30 15:21:19 -050097print('Processing files')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098for f in files:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050099 sys.stdout.write('Processing %s... ' % f)
Andrew Geisslereff27472021-10-29 15:35:00 -0500100 if not f.endswith(('.tar.zst', '.siginfo', '.sig')):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500101 # Most likely a temp file, skip it
102 print('skipping')
103 continue
104 dst = os.path.join(sys.argv[3], os.path.relpath(f, sys.argv[2]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 destdir = os.path.dirname(dst)
106 mkdir(destdir)
107
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600108 src = os.path.realpath(f)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500109 if os.path.exists(dst):
110 os.remove(dst)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600111 if (os.stat(src).st_dev == os.stat(destdir).st_dev):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500112 print('linking')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 try:
114 os.link(src, dst)
115 except OSError as e:
116 print('hard linking failed, copying')
117 shutil.copyfile(src, dst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 else:
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500119 print('copying')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600120 shutil.copyfile(src, dst)
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500121
122print('Done!')