1#!/usr/bin/env python3
2
3# Query which tasks will be restored from sstate
4#
5# Copyright 2016 Intel Corporation
6# Authored-by:  Paul Eggleton <paul.eggleton@intel.com>
7#
8# SPDX-License-Identifier: GPL-2.0-only
9#
10
11import sys
12import os
13import subprocess
14import tempfile
15import shutil
16import re
17
18scripts_path = os.path.dirname(os.path.realpath(__file__))
19lib_path = scripts_path + '/lib'
20sys.path = sys.path + [lib_path]
21import scriptpath
22scriptpath.add_bitbake_lib_path()
23import argparse_oe
24
25
26def translate_virtualfns(tasks):
27    import bb.tinfoil
28    tinfoil = bb.tinfoil.Tinfoil()
29    try:
30        tinfoil.prepare(False)
31
32        recipecaches = tinfoil.cooker.recipecaches
33        outtasks = []
34        for task in tasks:
35            (mc, fn, taskname) = bb.runqueue.split_tid(task)
36            if taskname.endswith('_setscene'):
37                taskname = taskname[:-9]
38            outtasks.append('%s:%s' % (recipecaches[mc].pkg_fn[fn], taskname))
39    finally:
40        tinfoil.shutdown()
41    return outtasks
42
43
44def check(args):
45    tmpdir = tempfile.mkdtemp(prefix='oe-check-sstate-')
46    try:
47        env = os.environ.copy()
48        if not args.same_tmpdir:
49            env['BB_ENV_PASSTHROUGH_ADDITIONS'] = env.get('BB_ENV_PASSTHROUGH_ADDITIONS', '') + ' TMPDIR:forcevariable'
50            env['TMPDIR:forcevariable'] = tmpdir
51
52        try:
53            cmd = ['bitbake', '--dry-run', '--runall=build'] + args.target
54            output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, env=env)
55
56            task_re = re.compile(r'NOTE: Running setscene task [0-9]+ of [0-9]+ \(([^)]+)\)')
57            tasks = []
58            for line in output.decode('utf-8').splitlines():
59                res = task_re.match(line)
60                if res:
61                    tasks.append(res.group(1))
62            outtasks = translate_virtualfns(tasks)
63        except subprocess.CalledProcessError as e:
64            print('ERROR: bitbake failed:\n%s' % e.output.decode('utf-8'))
65            return e.returncode
66    finally:
67        shutil.rmtree(tmpdir)
68
69    if args.log:
70        with open(args.log, 'wb') as f:
71            f.write(output)
72
73    if args.outfile:
74        with open(args.outfile, 'w') as f:
75            for task in outtasks:
76                f.write('%s\n' % task)
77    else:
78        for task in outtasks:
79            print(task)
80
81    return 0
82
83
84def main():
85    parser = argparse_oe.ArgumentParser(description='OpenEmbedded sstate check tool. Does a dry-run to check restoring the specified targets from shared state, and lists the tasks that would be restored. Set BB_SETSCENE_ENFORCE=1 in the environment if you wish to ensure real tasks are disallowed.')
86
87    parser.add_argument('target', nargs='+', help='Target to check')
88    parser.add_argument('-o', '--outfile', help='Write list to a file instead of stdout')
89    parser.add_argument('-l', '--log', help='Write full log to a file')
90    parser.add_argument('-s', '--same-tmpdir', action='store_true', help='Use same TMPDIR for check (list will then be dependent on what tasks have executed previously)')
91
92    parser.set_defaults(func=check)
93
94    args = parser.parse_args()
95
96    ret = args.func(args)
97    return ret
98
99
100if __name__ == "__main__":
101    try:
102        ret = main()
103    except Exception:
104        ret = 1
105        import traceback
106        traceback.print_exc()
107    sys.exit(ret)
108