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# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License version 2 as
10# published by the Free Software Foundation.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21import sys
22import os
23import subprocess
24import tempfile
25import shutil
26import re
27
28scripts_path = os.path.dirname(os.path.realpath(__file__))
29lib_path = scripts_path + '/lib'
30sys.path = sys.path + [lib_path]
31import scriptutils
32import scriptpath
33scriptpath.add_bitbake_lib_path()
34import argparse_oe
35
36
37def translate_virtualfns(tasks):
38    import bb.tinfoil
39    tinfoil = bb.tinfoil.Tinfoil()
40    try:
41        tinfoil.prepare(False)
42
43        recipecaches = tinfoil.cooker.recipecaches
44        outtasks = []
45        for task in tasks:
46            (mc, fn, taskname) = bb.runqueue.split_tid(task)
47            if taskname.endswith('_setscene'):
48                taskname = taskname[:-9]
49            outtasks.append('%s:%s' % (recipecaches[mc].pkg_fn[fn], taskname))
50    finally:
51        tinfoil.shutdown()
52    return outtasks
53
54
55def check(args):
56    tmpdir = tempfile.mkdtemp(prefix='oe-check-sstate-')
57    try:
58        env = os.environ.copy()
59        if not args.same_tmpdir:
60            env['BB_ENV_EXTRAWHITE'] = env.get('BB_ENV_EXTRAWHITE', '') + ' TMPDIR_forcevariable'
61            env['TMPDIR_forcevariable'] = tmpdir
62
63        try:
64            output = subprocess.check_output(
65                    'bitbake -n %s' % ' '.join(args.target),
66                    stderr=subprocess.STDOUT,
67                    env=env,
68                    shell=True)
69
70            task_re = re.compile('NOTE: Running setscene task [0-9]+ of [0-9]+ \(([^)]+)\)')
71            tasks = []
72            for line in output.decode('utf-8').splitlines():
73                res = task_re.match(line)
74                if res:
75                    tasks.append(res.group(1))
76            outtasks = translate_virtualfns(tasks)
77        except subprocess.CalledProcessError as e:
78            print('ERROR: bitbake failed:\n%s' % e.output.decode('utf-8'))
79            return e.returncode
80    finally:
81        shutil.rmtree(tmpdir)
82
83    if args.log:
84        with open(args.log, 'wb') as f:
85            f.write(output)
86
87    if args.outfile:
88        with open(args.outfile, 'w') as f:
89            for task in outtasks:
90                f.write('%s\n' % task)
91    else:
92        for task in outtasks:
93            print(task)
94
95    return 0
96
97
98def main():
99    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.')
100
101    parser.add_argument('target', nargs='+', help='Target to check')
102    parser.add_argument('-o', '--outfile', help='Write list to a file instead of stdout')
103    parser.add_argument('-l', '--log', help='Write full log to a file')
104    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)')
105
106    parser.set_defaults(func=check)
107
108    args = parser.parse_args()
109
110    ret = args.func(args)
111    return ret
112
113
114if __name__ == "__main__":
115    try:
116        ret = main()
117    except Exception:
118        ret = 1
119        import traceback
120        traceback.print_exc()
121    sys.exit(ret)
122