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 |
| 8 | import glob |
| 9 | import shutil |
| 10 | import errno |
| 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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 19 | if len(sys.argv) < 5: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | print("Incorrect number of arguments specified") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 21 | 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] | 22 | sys.exit(1) |
| 23 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 24 | filterlist = [] |
| 25 | if len(sys.argv) > 5: |
| 26 | print('Reading filter file %s' % sys.argv[5]) |
| 27 | with open(sys.argv[5]) as f: |
| 28 | for l in f.readlines(): |
| 29 | if ":" in l: |
| 30 | filterlist.append(l.rstrip()) |
| 31 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 32 | print('Reading %s' % sys.argv[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | sigs = [] |
| 34 | with open(sys.argv[1]) as f: |
| 35 | for l in f.readlines(): |
| 36 | if ":" in l: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 37 | task, sig = l.split()[0].rsplit(':', 1) |
| 38 | if filterlist and not task in filterlist: |
| 39 | print('Filtering out %s' % task) |
| 40 | else: |
| 41 | sigs.append(sig) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 43 | print('Gathering file list') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | files = set() |
| 45 | for s in sigs: |
| 46 | p = sys.argv[2] + "/" + s[:2] + "/*" + s + "*" |
| 47 | files |= set(glob.glob(p)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 48 | p = sys.argv[2] + "/%s/" % sys.argv[4] + s[:2] + "/*" + s + "*" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | files |= set(glob.glob(p)) |
| 50 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 51 | print('Processing files') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | for f in files: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 53 | sys.stdout.write('Processing %s... ' % f) |
| 54 | _, ext = os.path.splitext(f) |
| 55 | if not ext in ['.tgz', '.siginfo', '.sig']: |
| 56 | # Most likely a temp file, skip it |
| 57 | print('skipping') |
| 58 | continue |
| 59 | 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] | 60 | destdir = os.path.dirname(dst) |
| 61 | mkdir(destdir) |
| 62 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 | src = os.path.realpath(f) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | if os.path.exists(dst): |
| 65 | os.remove(dst) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 66 | if (os.stat(src).st_dev == os.stat(destdir).st_dev): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 67 | print('linking') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 68 | try: |
| 69 | os.link(src, dst) |
| 70 | except OSError as e: |
| 71 | print('hard linking failed, copying') |
| 72 | shutil.copyfile(src, dst) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | else: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 74 | print('copying') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 75 | shutil.copyfile(src, dst) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 76 | |
| 77 | print('Done!') |