| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # | 
| Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 3 | # | 
|  | 4 | # Copyright OpenEmbedded Contributors | 
|  | 5 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only | 
|  | 7 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 |  | 
|  | 9 | import os | 
|  | 10 | import sys | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | import shutil | 
|  | 12 | import errno | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 13 | import time | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 14 |  | 
|  | 15 | def mkdir(d): | 
|  | 16 | try: | 
|  | 17 | os.makedirs(d) | 
|  | 18 | except OSError as e: | 
|  | 19 | if e.errno != errno.EEXIST: | 
|  | 20 | raise e | 
|  | 21 |  | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 22 | # extract the hash from past the last colon to last underscore | 
|  | 23 | def 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 | 
|  | 28 | def map_sha_to_files(dir_, prefix, sha_map): | 
|  | 29 | sstate_prefix_path = dir_ + '/' + prefix + '/' | 
| Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 30 | if not os.path.exists(sstate_prefix_path): | 
|  | 31 | return | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 32 | 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 | 
|  | 43 | def 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 54 | if len(sys.argv) < 5: | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | print("Incorrect number of arguments specified") | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 56 | print("syntax: gen-lockedsig-cache <locked-sigs.inc> <input-cachedir> <output-cachedir> <nativelsbstring> [filterfile]") | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | sys.exit(1) | 
|  | 58 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 59 | filterlist = [] | 
|  | 60 | if 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 67 | print('Reading %s' % sys.argv[1]) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | sigs = [] | 
|  | 69 | with open(sys.argv[1]) as f: | 
|  | 70 | for l in f.readlines(): | 
|  | 71 | if ":" in l: | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 72 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 |  | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 78 | print('Gathering file list') | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 79 | start_time = time.perf_counter() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | files = set() | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 81 | sstate_content_cache = {} | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 82 | for s in sigs: | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 83 | prefix = s[:2] | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 84 | prefix2 = s[2:4] | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 85 | if prefix not in sstate_content_cache: | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 86 | 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 Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 89 |  | 
| Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 90 | if s in sstate_content_cache[prefix][prefix2]: | 
|  | 91 | for f in sstate_content_cache[prefix][prefix2][s]: | 
|  | 92 | files.add(f) | 
| Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 93 |  | 
|  | 94 | elapsed = time.perf_counter() - start_time | 
|  | 95 | print("Gathering file list took %.1fs" % elapsed) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 |  | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 97 | print('Processing files') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | for f in files: | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 99 | sys.stdout.write('Processing %s... ' % f) | 
| Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 100 | if not f.endswith(('.tar.zst', '.siginfo', '.sig')): | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 101 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | destdir = os.path.dirname(dst) | 
|  | 106 | mkdir(destdir) | 
|  | 107 |  | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | src = os.path.realpath(f) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | if os.path.exists(dst): | 
|  | 110 | os.remove(dst) | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | if (os.stat(src).st_dev == os.stat(destdir).st_dev): | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 112 | print('linking') | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 113 | try: | 
|  | 114 | os.link(src, dst) | 
|  | 115 | except OSError as e: | 
|  | 116 | print('hard linking failed, copying') | 
|  | 117 | shutil.copyfile(src, dst) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 118 | else: | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 119 | print('copying') | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 120 | shutil.copyfile(src, dst) | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 121 |  | 
|  | 122 | print('Done!') |