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 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 | |
| 6 | import os |
| 7 | import sys |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | import shutil |
| 9 | import errno |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 10 | import time |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | |
| 12 | def mkdir(d): |
| 13 | try: |
| 14 | os.makedirs(d) |
| 15 | except OSError as e: |
| 16 | if e.errno != errno.EEXIST: |
| 17 | raise e |
| 18 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 19 | # extract the hash from past the last colon to last underscore |
| 20 | def 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 |
| 25 | def map_sha_to_files(dir_, prefix, sha_map): |
| 26 | sstate_prefix_path = dir_ + '/' + prefix + '/' |
Brad Bishop | f3f93bb | 2019-10-16 14:33:32 -0400 | [diff] [blame] | 27 | if not os.path.exists(sstate_prefix_path): |
| 28 | return |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 29 | sstate_files = os.listdir(sstate_prefix_path) |
| 30 | for f in sstate_files: |
| 31 | try: |
| 32 | sha = extract_sha(f) |
| 33 | if sha not in sha_map: |
| 34 | sha_map[sha] = [] |
| 35 | sha_map[sha].append(sstate_prefix_path + f) |
| 36 | except IndexError: |
| 37 | continue |
| 38 | |
| 39 | # given a prefix build a map of hash to list of files |
| 40 | def build_sha_cache(prefix): |
| 41 | sha_map = {} |
| 42 | |
| 43 | sstate_dir = sys.argv[2] |
| 44 | map_sha_to_files(sstate_dir, prefix, sha_map) |
| 45 | |
| 46 | native_sstate_dir = sys.argv[2] + '/' + sys.argv[4] |
| 47 | map_sha_to_files(native_sstate_dir, prefix, sha_map) |
| 48 | |
| 49 | return sha_map |
| 50 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 51 | if len(sys.argv) < 5: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | print("Incorrect number of arguments specified") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | 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] | 54 | sys.exit(1) |
| 55 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 56 | filterlist = [] |
| 57 | if len(sys.argv) > 5: |
| 58 | print('Reading filter file %s' % sys.argv[5]) |
| 59 | with open(sys.argv[5]) as f: |
| 60 | for l in f.readlines(): |
| 61 | if ":" in l: |
| 62 | filterlist.append(l.rstrip()) |
| 63 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 64 | print('Reading %s' % sys.argv[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | sigs = [] |
| 66 | with open(sys.argv[1]) as f: |
| 67 | for l in f.readlines(): |
| 68 | if ":" in l: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 69 | task, sig = l.split()[0].rsplit(':', 1) |
| 70 | if filterlist and not task in filterlist: |
| 71 | print('Filtering out %s' % task) |
| 72 | else: |
| 73 | sigs.append(sig) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 75 | print('Gathering file list') |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 76 | start_time = time.perf_counter() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | files = set() |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 78 | sstate_content_cache = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | for s in sigs: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 80 | prefix = s[:2] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 81 | prefix2 = s[2:4] |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 82 | if prefix not in sstate_content_cache: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 83 | sstate_content_cache[prefix] = {} |
| 84 | if prefix2 not in sstate_content_cache[prefix]: |
| 85 | sstate_content_cache[prefix][prefix2] = build_sha_cache(prefix + "/" + prefix2) |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 86 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 87 | if s in sstate_content_cache[prefix][prefix2]: |
| 88 | for f in sstate_content_cache[prefix][prefix2][s]: |
| 89 | files.add(f) |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 90 | |
| 91 | elapsed = time.perf_counter() - start_time |
| 92 | print("Gathering file list took %.1fs" % elapsed) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 94 | print('Processing files') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | for f in files: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 96 | sys.stdout.write('Processing %s... ' % f) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame^] | 97 | if not f.endswith(('.tar.zst', '.siginfo', '.sig')): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 98 | # Most likely a temp file, skip it |
| 99 | print('skipping') |
| 100 | continue |
| 101 | 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] | 102 | destdir = os.path.dirname(dst) |
| 103 | mkdir(destdir) |
| 104 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 105 | src = os.path.realpath(f) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | if os.path.exists(dst): |
| 107 | os.remove(dst) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | if (os.stat(src).st_dev == os.stat(destdir).st_dev): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 109 | print('linking') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | try: |
| 111 | os.link(src, dst) |
| 112 | except OSError as e: |
| 113 | print('hard linking failed, copying') |
| 114 | shutil.copyfile(src, dst) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | else: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 116 | print('copying') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 117 | shutil.copyfile(src, dst) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 118 | |
| 119 | print('Done!') |