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 + '/' |
| 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 |
| 38 | def 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 49 | if len(sys.argv) < 5: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | print("Incorrect number of arguments specified") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 51 | 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] | 52 | sys.exit(1) |
| 53 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 54 | filterlist = [] |
| 55 | if 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 62 | print('Reading %s' % sys.argv[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | sigs = [] |
| 64 | with open(sys.argv[1]) as f: |
| 65 | for l in f.readlines(): |
| 66 | if ":" in l: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 67 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 73 | print('Gathering file list') |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 74 | start_time = time.perf_counter() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | files = set() |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 76 | sstate_content_cache = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | for s in sigs: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 78 | 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 | |
| 85 | elapsed = time.perf_counter() - start_time |
| 86 | print("Gathering file list took %.1fs" % elapsed) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 87 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 88 | print('Processing files') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 | for f in files: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 90 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | destdir = os.path.dirname(dst) |
| 98 | mkdir(destdir) |
| 99 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 100 | src = os.path.realpath(f) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | if os.path.exists(dst): |
| 102 | os.remove(dst) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 103 | if (os.stat(src).st_dev == os.stat(destdir).st_dev): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 104 | print('linking') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 105 | try: |
| 106 | os.link(src, dst) |
| 107 | except OSError as e: |
| 108 | print('hard linking failed, copying') |
| 109 | shutil.copyfile(src, dst) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 | else: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 111 | print('copying') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 112 | shutil.copyfile(src, dst) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 113 | |
| 114 | print('Done!') |