2014-12-19 11:41:55 +00:00
#!/usr/bin/env python
# Development tool - utility functions for plugins
#
# Copyright (C) 2014 Intel Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2015-05-11 13:17:01 +00:00
""" Devtool plugins module """
2014-12-19 11:41:55 +00:00
import os
import sys
import subprocess
import logging
logger = logging . getLogger ( ' devtool ' )
2015-05-27 14:59:09 +00:00
class DevtoolError ( Exception ) :
""" Exception for handling devtool errors """
pass
2014-12-19 11:41:55 +00:00
def exec_build_env_command ( init_path , builddir , cmd , watch = False , * * options ) :
2015-05-11 13:17:01 +00:00
""" Run a program in bitbake build context """
2014-12-19 11:41:55 +00:00
import bb
if not ' cwd ' in options :
options [ " cwd " ] = builddir
if init_path :
2015-04-20 16:47:05 +00:00
# As the OE init script makes use of BASH_SOURCE to determine OEROOT,
# and can't determine it when running under dash, we need to set
# the executable to bash to correctly set things up
if not ' executable ' in options :
options [ ' executable ' ] = ' bash '
2014-12-19 11:41:55 +00:00
logger . debug ( ' Executing command: " %s " using init path %s ' % ( cmd , init_path ) )
init_prefix = ' . %s %s > /dev/null && ' % ( init_path , builddir )
else :
logger . debug ( ' Executing command " %s " ' % cmd )
init_prefix = ' '
if watch :
if sys . stdout . isatty ( ) :
# Fool bitbake into thinking it's outputting to a terminal (because it is, indirectly)
2015-05-14 09:18:18 +00:00
cmd = ' script -e -q -c " %s " /dev/null ' % cmd
2014-12-19 11:41:55 +00:00
return exec_watch ( ' %s %s ' % ( init_prefix , cmd ) , * * options )
else :
return bb . process . run ( ' %s %s ' % ( init_prefix , cmd ) , * * options )
def exec_watch ( cmd , * * options ) :
2015-05-11 13:17:01 +00:00
""" Run program with stdout shown on sys.stdout """
2015-05-14 09:18:18 +00:00
import bb
2014-12-19 11:41:55 +00:00
if isinstance ( cmd , basestring ) and not " shell " in options :
options [ " shell " ] = True
process = subprocess . Popen (
cmd , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , * * options
)
buf = ' '
while True :
out = process . stdout . read ( 1 )
if out :
sys . stdout . write ( out )
sys . stdout . flush ( )
buf + = out
elif out == ' ' and process . poll ( ) != None :
break
2015-05-14 09:18:18 +00:00
if process . returncode != 0 :
raise bb . process . ExecutionError ( cmd , process . returncode , buf , None )
return buf , None
2014-12-19 11:41:55 +00:00
2015-06-16 16:16:51 +00:00
def exec_fakeroot ( d , cmd , * * kwargs ) :
""" Run a command under fakeroot (pseudo, in fact) so that it picks up the appropriate file permissions """
# Grab the command and check it actually exists
fakerootcmd = d . getVar ( ' FAKEROOTCMD ' , True )
if not os . path . exists ( fakerootcmd ) :
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 ' )
return 2
# Set up the appropriate environment
newenv = dict ( os . environ )
fakerootenv = d . getVar ( ' FAKEROOTENV ' , True )
for varvalue in fakerootenv . split ( ) :
if ' = ' in varvalue :
splitval = varvalue . split ( ' = ' , 1 )
newenv [ splitval [ 0 ] ] = splitval [ 1 ]
return subprocess . call ( " %s %s " % ( fakerootcmd , cmd ) , env = newenv , * * kwargs )
devtool: also load plugins from BBPATH
This makes it easier to extend, as a layer can add its own sub-commands.
Argument parsing is also separated into two steps, the same way it's done in
recipetool, as we need access to the global command-line arguments early,
before plugins are loaded, both for debugging arguments and for the bitbake
path (we need to load the bitbake module to get tinfoil, which is now needed
to load the plugins).
Rather than constructing tinfoil once and passing it through into sub-commands
for their use, we have to construct it for configuration metadata, use it, and
then shut it down, as some sub-commands call out to recipetool, which needs
its own tinfoil instance, and therefore needs to acquire the bitbake lock. If
we're still holding the lock at that point, that's clearly a problem.
[YOCTO #7625]
(From OE-Core rev: f9bc3b27244a141ec7273445d3ea139a047e0ddf)
Signed-off-by: Christopher Larson <chris_larson@mentor.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2015-07-13 18:43:40 +00:00
def setup_tinfoil ( config_only = False ) :
2015-05-11 13:17:01 +00:00
""" Initialize tinfoil api from bitbake """
2014-12-19 11:41:55 +00:00
import scriptpath
bitbakepath = scriptpath . add_bitbake_lib_path ( )
if not bitbakepath :
logger . error ( " Unable to find bitbake by searching parent directory of this script or PATH " )
sys . exit ( 1 )
import bb . tinfoil
tinfoil = bb . tinfoil . Tinfoil ( )
devtool: also load plugins from BBPATH
This makes it easier to extend, as a layer can add its own sub-commands.
Argument parsing is also separated into two steps, the same way it's done in
recipetool, as we need access to the global command-line arguments early,
before plugins are loaded, both for debugging arguments and for the bitbake
path (we need to load the bitbake module to get tinfoil, which is now needed
to load the plugins).
Rather than constructing tinfoil once and passing it through into sub-commands
for their use, we have to construct it for configuration metadata, use it, and
then shut it down, as some sub-commands call out to recipetool, which needs
its own tinfoil instance, and therefore needs to acquire the bitbake lock. If
we're still holding the lock at that point, that's clearly a problem.
[YOCTO #7625]
(From OE-Core rev: f9bc3b27244a141ec7273445d3ea139a047e0ddf)
Signed-off-by: Christopher Larson <chris_larson@mentor.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2015-07-13 18:43:40 +00:00
tinfoil . prepare ( config_only )
2015-05-18 15:15:04 +00:00
tinfoil . logger . setLevel ( logger . getEffectiveLevel ( ) )
2014-12-19 11:41:55 +00:00
return tinfoil
2015-08-30 07:49:10 +00:00
def get_recipe_file ( cooker , pn ) :
""" Find recipe file corresponding a package name """
import oe . recipeutils
recipefile = oe . recipeutils . pn_to_recipe ( cooker , pn )
if not recipefile :
skipreasons = oe . recipeutils . get_unavailable_reasons ( cooker , pn )
if skipreasons :
logger . error ( ' \n ' . join ( skipreasons ) )
else :
logger . error ( " Unable to find any recipe file matching %s " % pn )
return recipefile
def parse_recipe ( config , tinfoil , pn , appends ) :
""" Parse recipe of a package """
import oe . recipeutils
recipefile = get_recipe_file ( tinfoil . cooker , pn )
if not recipefile :
# Error already logged
return None
if appends :
append_files = tinfoil . cooker . collection . get_file_appends ( recipefile )
# Filter out appends from the workspace
append_files = [ path for path in append_files if
not path . startswith ( config . workspace_path ) ]
2015-09-22 16:21:26 +00:00
else :
append_files = None
2015-08-30 07:49:10 +00:00
return oe . recipeutils . parse_recipe ( recipefile , append_files ,
tinfoil . config_data )
2015-09-22 16:21:24 +00:00
def check_workspace_recipe ( workspace , pn , checksrc = True ) :
"""
Check that a recipe is in the workspace and ( optionally ) that source
is present .
"""
if not pn in workspace :
raise DevtoolError ( " No recipe named ' %s ' in your workspace " % pn )
if checksrc :
srctree = workspace [ pn ] [ ' srctree ' ]
if not os . path . exists ( srctree ) :
raise DevtoolError ( " Source tree %s for recipe %s does not exist " % ( srctree , pn ) )
if not os . listdir ( srctree ) :
raise DevtoolError ( " Source tree %s for recipe %s is empty " % ( srctree , pn ) )
2015-09-22 16:21:27 +00:00
def use_external_build ( same_dir , no_same_dir , d ) :
"""
Determine if we should use B != S ( separate build and source directories ) or not
"""
b_is_s = True
if no_same_dir :
logger . info ( ' Using separate build directory since --no-same-dir specified ' )
b_is_s = False
elif same_dir :
logger . info ( ' Using source tree as build directory since --same-dir specified ' )
elif bb . data . inherits_class ( ' autotools-brokensep ' , d ) :
logger . info ( ' Using source tree as build directory since recipe inherits autotools-brokensep ' )
elif d . getVar ( ' B ' , True ) == os . path . abspath ( d . getVar ( ' S ' , True ) ) :
logger . info ( ' Using source tree as build directory since that would be the default for this recipe ' )
else :
b_is_s = False
return b_is_s