blob: 404d3e66456adbbfbdc5cb658af949b470959a86 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/usr/bin/env python
2
3# Development tool - utility functions for plugins
4#
5# Copyright (C) 2014 Intel Corporation
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License version 2 as
9# published by the Free Software Foundation.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with this program; if not, write to the Free Software Foundation, Inc.,
18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19"""Devtool plugins module"""
20
21import os
22import sys
23import subprocess
24import logging
25
26logger = logging.getLogger('devtool')
27
28
29class DevtoolError(Exception):
30 """Exception for handling devtool errors"""
31 pass
32
33
34def exec_build_env_command(init_path, builddir, cmd, watch=False, **options):
35 """Run a program in bitbake build context"""
36 import bb
37 if not 'cwd' in options:
38 options["cwd"] = builddir
39 if init_path:
40 # As the OE init script makes use of BASH_SOURCE to determine OEROOT,
41 # and can't determine it when running under dash, we need to set
42 # the executable to bash to correctly set things up
43 if not 'executable' in options:
44 options['executable'] = 'bash'
45 logger.debug('Executing command: "%s" using init path %s' % (cmd, init_path))
46 init_prefix = '. %s %s > /dev/null && ' % (init_path, builddir)
47 else:
48 logger.debug('Executing command "%s"' % cmd)
49 init_prefix = ''
50 if watch:
51 if sys.stdout.isatty():
52 # Fool bitbake into thinking it's outputting to a terminal (because it is, indirectly)
53 cmd = 'script -e -q -c "%s" /dev/null' % cmd
54 return exec_watch('%s%s' % (init_prefix, cmd), **options)
55 else:
56 return bb.process.run('%s%s' % (init_prefix, cmd), **options)
57
58def exec_watch(cmd, **options):
59 """Run program with stdout shown on sys.stdout"""
60 import bb
61 if isinstance(cmd, basestring) and not "shell" in options:
62 options["shell"] = True
63
64 process = subprocess.Popen(
65 cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **options
66 )
67
68 buf = ''
69 while True:
70 out = process.stdout.read(1)
71 if out:
72 sys.stdout.write(out)
73 sys.stdout.flush()
74 buf += out
75 elif out == '' and process.poll() != None:
76 break
77
78 if process.returncode != 0:
79 raise bb.process.ExecutionError(cmd, process.returncode, buf, None)
80
81 return buf, None
82
83def exec_fakeroot(d, cmd, **kwargs):
84 """Run a command under fakeroot (pseudo, in fact) so that it picks up the appropriate file permissions"""
85 # Grab the command and check it actually exists
86 fakerootcmd = d.getVar('FAKEROOTCMD', True)
87 if not os.path.exists(fakerootcmd):
88 logger.error('pseudo executable %s could not be found - have you run a build yet? pseudo-native should install this and if you have run any build then that should have been built')
89 return 2
90 # Set up the appropriate environment
91 newenv = dict(os.environ)
92 fakerootenv = d.getVar('FAKEROOTENV', True)
93 for varvalue in fakerootenv.split():
94 if '=' in varvalue:
95 splitval = varvalue.split('=', 1)
96 newenv[splitval[0]] = splitval[1]
97 return subprocess.call("%s %s" % (fakerootcmd, cmd), env=newenv, **kwargs)
98
99def setup_tinfoil(config_only=False):
100 """Initialize tinfoil api from bitbake"""
101 import scriptpath
102 bitbakepath = scriptpath.add_bitbake_lib_path()
103 if not bitbakepath:
104 logger.error("Unable to find bitbake by searching parent directory of this script or PATH")
105 sys.exit(1)
106
107 import bb.tinfoil
108 tinfoil = bb.tinfoil.Tinfoil()
109 tinfoil.prepare(config_only)
110 tinfoil.logger.setLevel(logger.getEffectiveLevel())
111 return tinfoil
112
113def get_recipe_file(cooker, pn):
114 """Find recipe file corresponding a package name"""
115 import oe.recipeutils
116 recipefile = oe.recipeutils.pn_to_recipe(cooker, pn)
117 if not recipefile:
118 skipreasons = oe.recipeutils.get_unavailable_reasons(cooker, pn)
119 if skipreasons:
120 logger.error('\n'.join(skipreasons))
121 else:
122 logger.error("Unable to find any recipe file matching %s" % pn)
123 return recipefile
124
125def parse_recipe(config, tinfoil, pn, appends):
126 """Parse recipe of a package"""
127 import oe.recipeutils
128 recipefile = get_recipe_file(tinfoil.cooker, pn)
129 if not recipefile:
130 # Error already logged
131 return None
132 if appends:
133 append_files = tinfoil.cooker.collection.get_file_appends(recipefile)
134 # Filter out appends from the workspace
135 append_files = [path for path in append_files if
136 not path.startswith(config.workspace_path)]
137 return oe.recipeutils.parse_recipe(recipefile, append_files,
138 tinfoil.config_data)