xref: /openbmc/openbmc/poky/scripts/gen-lockedsig-cache (revision c342db356d4f451821781eb24eb9f3d39d6c0c5e)
1eb8dc403SDave Cobbley#!/usr/bin/env python3
2*c342db35SBrad Bishop#
3*c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
4*c342db35SBrad Bishop#
5eb8dc403SDave Cobbley
6eb8dc403SDave Cobbleyimport os
7eb8dc403SDave Cobbleyimport sys
8eb8dc403SDave Cobbleyimport glob
9eb8dc403SDave Cobbleyimport shutil
10eb8dc403SDave Cobbleyimport errno
11eb8dc403SDave Cobbley
12eb8dc403SDave Cobbleydef mkdir(d):
13eb8dc403SDave Cobbley    try:
14eb8dc403SDave Cobbley        os.makedirs(d)
15eb8dc403SDave Cobbley    except OSError as e:
16eb8dc403SDave Cobbley        if e.errno != errno.EEXIST:
17eb8dc403SDave Cobbley            raise e
18eb8dc403SDave Cobbley
19eb8dc403SDave Cobbleyif len(sys.argv) < 5:
20eb8dc403SDave Cobbley    print("Incorrect number of arguments specified")
21eb8dc403SDave Cobbley    print("syntax: gen-lockedsig-cache <locked-sigs.inc> <input-cachedir> <output-cachedir> <nativelsbstring> [filterfile]")
22eb8dc403SDave Cobbley    sys.exit(1)
23eb8dc403SDave Cobbley
24eb8dc403SDave Cobbleyfilterlist = []
25eb8dc403SDave Cobbleyif len(sys.argv) > 5:
26eb8dc403SDave Cobbley    print('Reading filter file %s' % sys.argv[5])
27eb8dc403SDave Cobbley    with open(sys.argv[5]) as f:
28eb8dc403SDave Cobbley        for l in f.readlines():
29eb8dc403SDave Cobbley            if ":" in l:
30eb8dc403SDave Cobbley                filterlist.append(l.rstrip())
31eb8dc403SDave Cobbley
32eb8dc403SDave Cobbleyprint('Reading %s' % sys.argv[1])
33eb8dc403SDave Cobbleysigs = []
34eb8dc403SDave Cobbleywith open(sys.argv[1]) as f:
35eb8dc403SDave Cobbley    for l in f.readlines():
36eb8dc403SDave Cobbley        if ":" in l:
37eb8dc403SDave Cobbley            task, sig = l.split()[0].rsplit(':', 1)
38eb8dc403SDave Cobbley            if filterlist and not task in filterlist:
39eb8dc403SDave Cobbley                print('Filtering out %s' % task)
40eb8dc403SDave Cobbley            else:
41eb8dc403SDave Cobbley                sigs.append(sig)
42eb8dc403SDave Cobbley
43eb8dc403SDave Cobbleyprint('Gathering file list')
44eb8dc403SDave Cobbleyfiles = set()
45eb8dc403SDave Cobbleyfor s in sigs:
46eb8dc403SDave Cobbley    p = sys.argv[2] + "/" + s[:2] + "/*" + s + "*"
47eb8dc403SDave Cobbley    files |= set(glob.glob(p))
48eb8dc403SDave Cobbley    p = sys.argv[2] + "/%s/" % sys.argv[4] + s[:2] + "/*" + s + "*"
49eb8dc403SDave Cobbley    files |= set(glob.glob(p))
50eb8dc403SDave Cobbley
51eb8dc403SDave Cobbleyprint('Processing files')
52eb8dc403SDave Cobbleyfor f in files:
53eb8dc403SDave Cobbley    sys.stdout.write('Processing %s... ' % f)
54eb8dc403SDave Cobbley    _, ext = os.path.splitext(f)
55eb8dc403SDave Cobbley    if not ext in ['.tgz', '.siginfo', '.sig']:
56eb8dc403SDave Cobbley        # Most likely a temp file, skip it
57eb8dc403SDave Cobbley        print('skipping')
58eb8dc403SDave Cobbley        continue
59eb8dc403SDave Cobbley    dst = os.path.join(sys.argv[3], os.path.relpath(f, sys.argv[2]))
60eb8dc403SDave Cobbley    destdir = os.path.dirname(dst)
61eb8dc403SDave Cobbley    mkdir(destdir)
62eb8dc403SDave Cobbley
63eb8dc403SDave Cobbley    src = os.path.realpath(f)
64eb8dc403SDave Cobbley    if os.path.exists(dst):
65eb8dc403SDave Cobbley        os.remove(dst)
66eb8dc403SDave Cobbley    if (os.stat(src).st_dev == os.stat(destdir).st_dev):
67eb8dc403SDave Cobbley        print('linking')
68eb8dc403SDave Cobbley        try:
69eb8dc403SDave Cobbley            os.link(src, dst)
70eb8dc403SDave Cobbley        except OSError as e:
71eb8dc403SDave Cobbley            print('hard linking failed, copying')
72eb8dc403SDave Cobbley            shutil.copyfile(src, dst)
73eb8dc403SDave Cobbley    else:
74eb8dc403SDave Cobbley        print('copying')
75eb8dc403SDave Cobbley        shutil.copyfile(src, dst)
76eb8dc403SDave Cobbley
77eb8dc403SDave Cobbleyprint('Done!')
78