[OE-core] [oe-core][PATCH 2/2] cleanup-workdir: wait for bitbake instances to finish

Lucas Dutra Nunes ldnunes at ossystems.com.br
Mon May 18 20:08:18 UTC 2015


bitbake uses a lock file on the build dir, "bitbake.lock", to prevent it
from running before an instance has exited. And sometimes the
cleanup-workdir script can call bibake too many times, too fast, before
the lock has been released.

By simply waiting that the lock has been released solves this problem.

Signed-off-by: Lucas Dutra Nunes <ldnunes at ossystems.com.br>
---
 scripts/cleanup-workdir | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/scripts/cleanup-workdir b/scripts/cleanup-workdir
index bf37f90..3e1df1f 100755
--- a/scripts/cleanup-workdir
+++ b/scripts/cleanup-workdir
@@ -21,6 +21,8 @@ import optparse
 import re
 import subprocess
 import shutil
+import fcntl
+from contextlib import contextmanager
 
 pkg_cur_dirs = {}
 obsolete_dirs = []
@@ -51,7 +53,7 @@ def get_cur_arch_dirs(workdir, arch_dirs):
     pattern = workdir + '/(.*?)/'
 
     cmd = "bitbake -e | grep ^SDK_ARCH="
-    output = run_command(cmd)
+    output = run_bitbake_command(cmd)
     sdk_arch = output.split('"')[1]
 
     # select thest 5 packages to get the dirs of current arch
@@ -59,7 +61,7 @@ def get_cur_arch_dirs(workdir, arch_dirs):
 
     for pkg in pkgs:
         cmd = "bitbake -e " + pkg + " | grep ^IMAGE_ROOTFS="
-        output = run_command(cmd)
+        output = run_bitbake_command(cmd)
         output = output.split('"')[1]
         m = re.match(pattern, output)
         arch_dirs.append(m.group(1))
@@ -67,6 +69,27 @@ def get_cur_arch_dirs(workdir, arch_dirs):
 def get_build_dir():
     return run_command('echo $BUILDDIR').strip()
 
+ at contextmanager
+def wait_for_bitbake():
+    builddir = get_build_dir()
+    bitbake_lock_file = os.path.join(builddir, 'bitbake.lock')
+
+    with open(bitbake_lock_file, 'w+') as f:
+        fd = f.fileno()
+        try:
+            # Lock and unlock the lock file, to be sure that there are no other
+            # instances of bitbake running at the moment:
+            fcntl.flock(fd, fcntl.LOCK_EX)
+            fcntl.flock(fd, fcntl.LOCK_UN)
+            yield
+        finally:
+            # And unlock again, just to be safe:
+            fcntl.flock(fd, fcntl.LOCK_UN)
+
+def run_bitbake_command(cmd):
+    with wait_for_bitbake():
+        return run_command(cmd)
+
 def main():
     global parser
     parser = optparse.OptionParser(
@@ -89,7 +112,7 @@ will be deleted. Be CAUTIOUS.""")
 
     print 'Updating bitbake caches...'
     cmd = "bitbake -s"
-    output = run_command(cmd)
+    output = run_bitbake_command(cmd)
 
     output = output.split('\n')
     index = 0
@@ -111,7 +134,7 @@ will be deleted. Be CAUTIOUS.""")
         pkg_cur_dirs[elems[0]] = version
 
     cmd = "bitbake -e"
-    output = run_command(cmd)
+    output = run_bitbake_command(cmd)
 
     tmpdir = None
     image_rootfs = None
-- 
2.1.4




More information about the Openembedded-core mailing list