Bug 14670

Summary: Promoting a Function to a Task: example is wrong
Product: [Documentation] BitBake User Manual Reporter: Robert Berger <pokylinux>
Component: bitbake-manualAssignee: Antonin Godard <antonin.godard>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: Medium+ CC: randy.macleod, tim.orling
Version: 4.1   
Target Milestone: 5.2 M4   
Hardware: x86   
OS: Multiple   
Whiteboard:
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: Yes (doc changes required)

Description Robert Berger 2021-12-21 09:57:04 UTC
https://docs.yoctoproject.org/bitbake/bitbake-user-manual/bitbake-user-manual-metadata.html#promoting-a-function-to-a-task

Bitbake does not like this python task - we should at least try to show valid examples ;)

python do_printdate () {
    import time
    print time.strftime('%Y%m%d', time.gmtime())
}
addtask printdate after do_fetch before do_build

Not sure what exactly we'll try to show here but maybe it is more like this:

    python do_printdate () {
-       import time
-       print time.strftime('%Y%m%d', time.gmtime())
+       import datetime
+       bb.plain('Date: %s' % (datetime.date.today()))
    }
    addtask printdate after do_fetch before do_build

With bb.plain it shows up in the cooker shell and not just in some log file like with print. This would of course need additional explanations.

In case we would like to show even more maybe something like this, or a subset of it:

# this is a python task:
python do_printdate () {
    import datetime
    # goes to cooker shell:
    bb.warn('py: --> S: %s <--' % (d.getVar("S", True)))
    bb.plain('py: Date: %s' % (datetime.date.today()))
    # goes to log:
    print(datetime.datetime.now())
    print(datetime.date.today())
    # goes to cooker shell:
    bb.plain("py: Check the logs!")
    bb.plain('py: this log file: %s/log.do_printdate' % (d.getVar("T", True)))
}
addtask do_printdate after do_compile before do_install

Please advise how the example should look like.
Comment 1 Randy MacLeod 2024-10-24 15:03:56 UTC
Bulk move of 5.1 M4 to 5.2 M2 as approved by AlexB.
Comment 2 Antonin Godard 2025-04-11 08:39:23 UTC
Fixed with commit https://git.openembedded.org/bitbake/commit/?id=874c607f475f3d60677d2720b80a28d0c2e963c6.

Moving to Resolved Fixed.