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 scriptutils
22import scriptpath
23scriptpath.add_bitbake_lib_path()
24import argparse_oe
25
26
27def translate_virtualfns(tasks):
28    import bb.tinfoil
29    tinfoil = bb.tinfoil.Tinfoil()
30    try:
31        tinfoil.prepare(False)
32
33        recipecaches = tinfoil.cooker.recipecaches
34        outtasks = []
35        for task in tasks:
36            (mc, fn, taskname) = bb.runqueue.split_tid(task)
37            if taskname.endswith('_setscene'):
38                taskname = taskname[:-9]
39            outtasks.append('%s:%s' % (recipecaches[mc].pkg_fn[fn], taskname))
40    finally:
41        tinfoil.shutdown()
42    return outtasks
43
44
45def check(args):
46    tmpdir = tempfile.mkdtemp(prefix='oe-check-sstate-')
47    try:
48        env = os.environ.copy()
49        if not args.same_tmpdir:
50            env['BB_ENV_EXTRAWHITE'] = env.get('BB_ENV_EXTRAWHITE', '') + ' TMPDIR_forcevariable'
51            env['TMPDIR_forcevariable'] = tmpdir
52
53        try:
54            output = subprocess.check_output(
55                    'bitbake -n %s' % ' '.join(args.target),
56                    stderr=subprocess.STDOUT,
57                    env=env,
58                    shell=True)
59
60            task_re = re.compile('NOTE: Running setscene task [0-9]+ of [0-9]+ \(([^)]+)\)')
61            tasks = []
62            for line in output.decode('utf-8').splitlines():
63                res = task_re.match(line)
64                if res:
65                    tasks.append(res.group(1))
66            outtasks = translate_virtualfns(tasks)
67        except subprocess.CalledProcessError as e:
68            print('ERROR: bitbake failed:\n%s' % e.output.decode('utf-8'))
69            return e.returncode
70    finally:
71        shutil.rmtree(tmpdir)
72
73    if args.log:
74        with open(args.log, 'wb') as f:
75            f.write(output)
76
77    if args.outfile:
78        with open(args.outfile, 'w') as f:
79            for task in outtasks:
80                f.write('%s\n' % task)
81    else:
82        for task in outtasks:
83            print(task)
84
85    return 0
86
87
88def main():
89    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.')
90
91    parser.add_argument('target', nargs='+', help='Target to check')
92    parser.add_argument('-o', '--outfile', help='Write list to a file instead of stdout')
93    parser.add_argument('-l', '--log', help='Write full log to a file')
94    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)')
95
96    parser.set_defaults(func=check)
97
98    args = parser.parse_args()
99
100    ret = args.func(args)
101    return ret
102
103
104if __name__ == "__main__":
105    try:
106        ret = main()
107    except Exception:
108        ret = 1
109        import traceback
110        traceback.print_exc()
111    sys.exit(ret)
112