Bug 9068

Summary: Provide means to expand multiple variables in a string, in python functions,
Product: [Build System, Metadata & Runtime] BitBake Reporter: Igor Stoppa <igor.stoppa>
Component: bitbakeAssignee: Scott Rifenbark <srifenbark>
Status: RESOLVED DUPLICATE QA Contact:
Severity: normal    
Priority: Medium CC: bluelightning, joshuagloe, poky.bs.watcher, poky.watcher, richard.purdie, srifenbark
Version: unspecified   
Target Milestone: 2.1 M4   
Hardware: x86   
OS: Multiple   
Whiteboard: 23 Feb 2016: RESOLVED/DUPLICATE
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: No (bug/feature does not impact docs)

Description Igor Stoppa 2016-02-05 11:48:23 UTC
With d.getVar('variable', expand=True) it is possible to get the full expansion of a variable.

However it's a bit inconvenient to do that, when using multiple variables.

Having something like:

expand("something1 ${Variable1} something2  ${Variable2} something3") to return

"something1 somethingelse1 something2  somethingelse2 something3"

would be much nicer and would avoid different implementations to pop up in recipes.

Ex. of possible implementation, still in python:

  def expand(item):
      item = str(item)
      
      while re.search('.*\$\{(.*)\}.*', item):
          match = re.search('.*\$\{(.*)\}.*', item)
          sub = d.getVar(match.group(1), expand=True)
          item = item.replace('${' + match.group(1) + '}', sub)
      return item

Taking this even further: provide a wrapper for executing shell commands that might use BB variables. Ex:

def run_command(cmd):
  check_call(expand(cmd).split())

These 2 enhancements would provide much more compact and readable python tasks.
Comment 1 Igor Stoppa 2016-02-05 13:08:08 UTC
This feature would also open the door to the python equivalent of run_command:

def eval_command(cmd):
    eval(expand(cmd))

This doesn't look like a big improvement, but most likely some additional quoting/escaping is needed and it could be hidden here.

While eval is not particularly liked in many circles, it might make for some more concise/readable code, at least in some cases.
Comment 2 Richard Purdie 2016-02-11 15:40:58 UTC
We already have d.expand() which does what you describe, expand variables in a string. I do notice the manual fails to mention this however it is in common use in the codebase. We should note this in the manual, then resolve this.

Assigning to Scott to fix the manual.
Comment 3 Scott Rifenbark 2016-02-23 18:09:33 UTC
Hi, 

Paul filed a bug 9147 to deal with adding d.expand to the manual.  I have added that and Paul has reviewed and approved the change.  So the status of 9147 is RESOLVED.  

Since this bug appears to be documentation only... and it is assigned to me.  I am going to mark it off as RESOLVED/DUPLICATE.

I set the doc flag to "no" for this one.

If you care to see the change in the manual - see http://www.yoctoproject.org/docs/2.1/bitbake-user-manual/bitbake-user-manual.html#accessing-datastore-variables-using-python.

Scott

*** This bug has been marked as a duplicate of bug 9147 ***