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