Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
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 | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 18 | print("syntax: gen-lockedsig-cache <locked-sigs.inc> <input-cachedir> <output-cachedir> <nativelsbstring>") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | sys.exit(1) |
| 20 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 21 | print('Reading %s' % sys.argv[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | sigs = [] |
| 23 | with open(sys.argv[1]) as f: |
| 24 | for l in f.readlines(): |
| 25 | if ":" in l: |
| 26 | sigs.append(l.split(":")[2].split()[0]) |
| 27 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 28 | print('Gathering file list') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | files = set() |
| 30 | for s in sigs: |
| 31 | p = sys.argv[2] + "/" + s[:2] + "/*" + s + "*" |
| 32 | files |= set(glob.glob(p)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 33 | p = sys.argv[2] + "/%s/" % sys.argv[4] + s[:2] + "/*" + s + "*" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | files |= set(glob.glob(p)) |
| 35 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 36 | print('Processing files') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | for f in files: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 38 | sys.stdout.write('Processing %s... ' % f) |
| 39 | _, ext = os.path.splitext(f) |
| 40 | if not ext in ['.tgz', '.siginfo', '.sig']: |
| 41 | # Most likely a temp file, skip it |
| 42 | print('skipping') |
| 43 | continue |
| 44 | 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] | 45 | destdir = os.path.dirname(dst) |
| 46 | mkdir(destdir) |
| 47 | |
| 48 | if os.path.exists(dst): |
| 49 | os.remove(dst) |
| 50 | if (os.stat(f).st_dev == os.stat(destdir).st_dev): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 51 | print('linking') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | os.link(f, dst) |
| 53 | else: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 54 | print('copying') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | shutil.copyfile(f, dst) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 56 | |
| 57 | print('Done!') |