Skip to contentSkip to conversation

cooker: fix -w with a file name that has no directory

PATCH In review Babanpreet Singh 2 participants 3 messages Nov 14 – Nov 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cooker: fix -w with a file name that has no directory

"bitbake -w events.json" fails with:

FileNotFoundError: [Errno 2] No such file or directory: ''

There is no directory to create for a bare file name, so don't try.

Fixes ee174b231897 ("cooker: Add support for BB_DEFAULT_EVENTLOG")

---

Notes:
Found while looking at YOCTO #16492. Applies with or without Mathieu's patch for that bug.

 lib/bb/cooker.py       |  5 +++--
 lib/bb/tests/cooker.py | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)
lib/bb/cooker.py +3 -2
index 8a422e506..f79cd5e6c 100644
@@ -457,8 +457,9 @@ You can also remove the BB_HASHSERVE_UPSTREAM setting, but this may result in si
         if not self.eventlog or self.eventlog[0] != eventlog:
             # we log all events to a file if so directed
             # register the log file writer as UI Handler
-            if not os.path.exists(os.path.dirname(eventlog)):
-                bb.utils.mkdirhier(os.path.dirname(eventlog))
+            dirname = os.path.dirname(eventlog)
+            if dirname and not os.path.exists(dirname):
+                bb.utils.mkdirhier(dirname)
             writer = EventWriter(self, eventlog)
             EventLogWriteHandler = namedtuple('EventLogWriteHandler', ['event'])
             self.eventlog = (eventlog, bb.event.register_UIHhandler(EventLogWriteHandler(writer)), writer)
lib/bb/tests/cooker.py +18 -0
index db4104d40..c6a0cac2a 100644
@@ -409,3 +409,21 @@ addtask marker
             with open(os.path.join(builddir, "marker.log")) as f:
                 self.assertEqual(f.read(), "bbappend-applied",
                                  "bitbake -b did not apply the recipe's bbappend")
+
+
+class EventLogTest(_BitbakeSubprocessTestCase):
+    """Tests for the event log ("bitbake -w")."""
+
+    _parsetests = os.path.realpath(os.path.join(os.path.dirname(__file__), "parse-tests"))
+
+    def test_write_log_bare_filename(self):
+        """bitbake -w must accept a file name without a directory."""
+        with self._build_dir(prefix="eventlogtest") as builddir:
+            env = os.environ.copy()
+            env["BBPATH"] = self._parsetests
+            env["BB_ENV_PASSTHROUGH_ADDITIONS"] = "TOPDIR"
+            env["TOPDIR"] = builddir
+
+            self._run_subprocess(["bitbake", "-w", "events.json", "-e"], env, builddir)
+
+            self.assertTrue(os.path.exists(os.path.join(builddir, "events.json")))
Signature · 1 line
2.43.0
#1 lore raw
Quoted 24 lines · Baban via bitbake-devel

"bitbake -w events.json" fails with:

  FileNotFoundError: [Errno 2] No such file or directory: ''

There is no directory to create for a bare file name, so don't try.

Fixes ee174b231897 ("cooker: Add support for BB_DEFAULT_EVENTLOG")

---

Notes:
    Found while looking at YOCTO #16492. Applies with or without Mathieu's patch for that bug.

 lib/bb/cooker.py       |  5 +++--
 lib/bb/tests/cooker.py | 18 ++++++++++++++++++

 2 files changed, 21 insertions(+), 2 deletions(-)
lib/bb/cooker.py +0 -0
index 8a422e506..f79cd5e6c 100644
@@ -457,8 +457,9 @@ You can also remove the BB_HASHSERVE_UPSTREAM setting, but this may result in si

         if not self.eventlog or self.eventlog[0] != eventlog:

Quoted 10 lines
             # we log all events to a file if so directed
             # register the log file writer as UI Handler
-            if not os.path.exists(os.path.dirname(eventlog)):
-                bb.utils.mkdirhier(os.path.dirname(eventlog))
+            dirname = os.path.dirname(eventlog)
+            if dirname and not os.path.exists(dirname):
+                bb.utils.mkdirhier(dirname)
             writer = EventWriter(self, eventlog)
             EventLogWriteHandler = namedtuple('EventLogWriteHandler', ['event'])
             self.eventlog = (eventlog, bb.event.register_UIHhandler(EventLogWriteHandler(writer)), writer)

Thanks for the patch. When reviewing this I realised that the current working directory in the server code might not be the same as the commandline.

Would you be able to tweak the code in main.py where it sets "options.writeeventlog = eventlog" to resolve the path there to a full path?

It can also be set via BB_DEFAULT_EVENTLOG and I think we should just show an error if that path isn't absolute?

I think that would resolve my concerns.

Thanks,

Richard

#2 lore raw

PATCH v2 1/1 cooker: make the event log path absolute

The server's current directory may not be the one bitbake was run from, so a relative -w path is not safe to hand over. A bare file name fails outright:

FileNotFoundError: [Errno 2] No such file or directory: ''

Resolve the -w path in main.py, and show an error if BB_DEFAULT_EVENTLOG is relative.

Fixes ee174b231897 ("cooker: Add support for BB_DEFAULT_EVENTLOG")

AI-Generated: Uses Claude (claude-opus-5-5)

---

 lib/bb/cooker.py       |  2 ++
 lib/bb/main.py         |  4 ++++
 lib/bb/tests/cooker.py | 45 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)
lib/bb/cooker.py +2 -0
index a6bc2827d..54f87278f 100644
@@ -314,6 +314,8 @@ class BBCooker:
 
         eventlog = self.data.getVar("BB_DEFAULT_EVENTLOG")
         if not self.configuration.writeeventlog and eventlog:
+            if not os.path.isabs(eventlog):
+                bb.fatal("BB_DEFAULT_EVENTLOG must be an absolute path, not \"%s\"" % eventlog)
             self.setupEventLog(eventlog)
 
         if consolelog:
lib/bb/main.py +4 -0
index 5cce8eb27..5a49f73b3 100755
@@ -338,6 +338,10 @@ class BitBakeConfigParameters(cookerdata.ConfigParameters):
             eventlog = "bitbake_eventlog_%s.json" % datetime.now().strftime("%Y%m%d%H%M%S")
             options.writeeventlog = eventlog
 
+        # the server may not share our current directory
+        if options.writeeventlog:
+            options.writeeventlog = os.path.abspath(options.writeeventlog)
+
         if options.bind:
             try:
                 #Checking that the port is a number and is a ':' delimited value
lib/bb/tests/cooker.py +45 -0

48 lines not on this page · raw message

Signature · 1 line
2.43.0