Skip to contentSkip to conversation

Use python in variable assigment of a recipe

DISCUSSION Steffen Sledz 3 participants 5 messages Feb 28, 2012

lore
Share & tools
lore
#0 lore raw

According to the user manual it is possible to use python code in variable assignments by using ${@...}.

I like to use a function (subprocess.check_output) here which requires an import statement. Where do i have to place this import?

And another question. Is it possible to determine the path to the recipe itself from with it?

Thx,
Steffen

Signature · 7 lines
DResearch Fahrzeugelektronik GmbH
Otto-Schmirgal-Str. 3, 10319 Berlin, Germany
Tel: +49 30 515932-237 mailto:[email protected]
Fax: +49 30 515932-299
Geschäftsführer: Dr. Michael Weber, Werner Mögle;
Amtsgericht Berlin Charlottenburg; HRB 130120 B;
Ust.-IDNr. DE273952058
#1 lore raw
Quoted 5 lines · On Tuesday 28 February 2012 10:26:16 Steffen Sledz wrote:

According to the user manual it is possible to use python code in variable assignments by using ${@...}.

I like to use a function (subprocess.check_output) here which requires an import statement. Where do i have to place this import?

If you've got more than one line of Python then you should probably define a normal Python function (just use "def" as you would in a normal python script
- there are plenty of examples of this) and then call this using ${@...}.

Quoted 2 lines

And another question. Is it possible to determine the path to the recipe itself from with it?

I think you just need to get the value of the FILE variable e.g

d.getVar('FILE', True)

Cheers,
Paul

Signature · 2 lines
Paul Eggleton
Intel Open Source Technology Centre
#2 lore raw
Quoted 10 lines · Paul Eggleton
Quoted 5 lines · On Tuesday 28 February 2012 10:26:16 Steffen Sledz wrote:

According to the user manual it is possible to use python code in variable assignments by using ${@...}.

I like to use a function (subprocess.check_output) here which requires an import statement. Where do i have to place this import?

If you've got more than one line of Python then you should probably define a normal Python function (just use "def" as you would in a normal python script
- there are plenty of examples of this) and then call this using ${@...}.

Works. Thx.

Quoted 6 lines
Quoted 2 lines

And another question. Is it possible to determine the path to the recipe itself from with it?

I think you just need to get the value of the FILE variable e.g

d.getVar('FILE', True)

Because i'm working with oe-classic i tried this (found in some other examples):

  MYVAR = "${@my_func()}"

def my_func():

      bbpath = os.path.dirname(bb.data.getVar('FILE',d,1))
      ...

But this leads to an error:

NameError: global name 'd' is not defined

:(

Signature · 7 lines
DResearch Fahrzeugelektronik GmbH
Otto-Schmirgal-Str. 3, 10319 Berlin, Germany
Tel: +49 30 515932-237 mailto:[email protected]
Fax: +49 30 515932-299
Geschäftsführer: Dr. Michael Weber, Werner Mögle;
Amtsgericht Berlin Charlottenburg; HRB 130120 B;
Ust.-IDNr. DE273952058
#3 lore raw
Quoted 5 lines · On Tuesday 28 February 2012 13:11:08 Steffen Sledz wrote:

Because i'm working with oe-classic i tried this (found in some other examples):

  MYVAR = "${@my_func()}"
Quoted 7 lines

def my_func():

      bbpath = os.path.dirname(bb.data.getVar('FILE',d,1))
      ...

But this leads to an error:

NameError: global name 'd' is not defined

If you pass in d it should work, i.e.:

   MYVAR = "${@my_func(d)}"

def my_func(d):

       bbpath = os.path.dirname(bb.data.getVar('FILE',d,1))

BTW, d.getVar() should work fine in OE-Classic (or to be fully correct, it should work fine with older versions of bitbake).

Cheers,
Paul

Signature · 2 lines
Paul Eggleton
Intel Open Source Technology Centre
#4 lore raw
In reply to Steffen Sledz
Quoted 5 lines · Steffen Sledz

Because i'm working with oe-classic i tried this (found in some other examples):

  MYVAR = "${@my_func()}"

def my_func():

Quoted 8 lines
      bbpath = os.path.dirname(bb.data.getVar('FILE',d,1))
      ...

But this leads to an error:

NameError: global name 'd' is not defined

:(

MYVAR = "${@my_func(d)}"

def my_func(d):
...

p.