]> code.ossystems Code Review - openembedded-core.git/commitdiff
use bb.utils.rename to rename file or directory
authorDevendra Tewari <devendra.tewari@gmail.com>
Wed, 21 Apr 2021 20:49:48 +0000 (17:49 -0300)
committerRichard Purdie <richard.purdie@linuxfoundation.org>
Sat, 24 Apr 2021 16:53:08 +0000 (17:53 +0100)
bb.utils.rename handles cross-device link error with os.rename and
retries with shutil.move.

[YOCTO #14301]

Signed-off-by: Devendra Tewari <devendra.tewari@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
14 files changed:
meta/classes/buildhistory.bbclass
meta/classes/package.bbclass
meta/classes/populate_sdk_ext.bbclass
meta/classes/reproducible_build.bbclass
meta/classes/sstate.bbclass
meta/classes/update-alternatives.bbclass
meta/lib/oe/package_manager/deb/__init__.py
meta/lib/oe/package_manager/ipk/__init__.py
meta/lib/oe/rootfs.py
meta/lib/oeqa/selftest/cases/wic.py
scripts/combo-layer
scripts/lib/devtool/standard.py
scripts/lib/devtool/upgrade.py
scripts/lib/wic/plugins/imager/direct.py

index 554c8810ac9bfd881760036010f5666c4861c0e4..059de36a5d4946de4df08b7b4b9d3173c470d5bb 100644 (file)
@@ -875,11 +875,7 @@ python buildhistory_eventhandler() {
                 entries = [ x for x in os.listdir(rootdir) if not x.startswith('.') ]
                 bb.utils.mkdirhier(olddir)
                 for entry in entries:
-                    try:
-                        os.rename(os.path.join(rootdir, entry),
-                              os.path.join(olddir, entry))
-                    except OSError:
-                        shutil.move(os.path.join(rootdir, entry),
+                    bb.utils.rename(os.path.join(rootdir, entry),
                               os.path.join(olddir, entry))
         elif isinstance(e, bb.event.BuildCompleted):
             if reset:
index 6707c2e9db5bb6aea64a6cde60edb7014caf675e..cf30f33f3d1da7deb7a84555b67980a02cbb225a 100644 (file)
@@ -835,7 +835,6 @@ perform_packagecopy[dirs] = "${PKGD}"
 # the fs-perms.txt files
 python fixup_perms () {
     import pwd, grp
-    import shutil
 
     # init using a string with the same format as a line as documented in
     # the fs-perms.txt file
@@ -1050,10 +1049,7 @@ python fixup_perms () {
         # Create path to move directory to, move it, and then setup the symlink
         bb.utils.mkdirhier(os.path.dirname(target))
         #bb.note("Fixup Perms: Rename %s -> %s" % (dir, ptarget))
-        try:
-            os.rename(origin, target)
-        except OSError:
-            shutil.move(origin, target)
+        bb.utils.rename(origin, target)
         #bb.note("Fixup Perms: Link %s -> %s" % (dir, link))
         os.symlink(link, origin)
 
@@ -1780,7 +1776,6 @@ python package_do_shlibs() {
     import itertools
     import re, pipes
     import subprocess
-    import shutil
 
     exclude_shlibs = d.getVar('EXCLUDE_FROM_SHLIBS', False)
     if exclude_shlibs:
@@ -1972,10 +1967,7 @@ python package_do_shlibs() {
 
         for (old, new) in renames:
             bb.note("Renaming %s to %s" % (old, new))
-            try:
-                os.rename(old, new)
-            except OSError:
-                shutil.move(old, new)
+            bb.utils.rename(old, new)
             pkgfiles[pkg].remove(old)
 
         shlibs_file = os.path.join(shlibswork_dir, pkg + ".list")
index 12e758c2ad68cb230b2f51ae8a78b163b14873ad..fe840d9cfb7b1724280f418e524653e6b0f274e0 100644 (file)
@@ -165,10 +165,7 @@ def create_filtered_tasklist(d, sdkbasepath, tasklistfile, conf_initpath):
             shutil.rmtree(temp_sdkbasepath)
         except FileNotFoundError:
             pass
-        try:
-            os.rename(sdkbasepath, temp_sdkbasepath)
-        except OSError:
-            shutil.move(sdkbasepath, temp_sdkbasepath)
+        bb.utils.rename(sdkbasepath, temp_sdkbasepath)
         cmdprefix = '. %s .; ' % conf_initpath
         logfile = d.getVar('WORKDIR') + '/tasklist_bb_log.txt'
         try:
@@ -178,10 +175,7 @@ def create_filtered_tasklist(d, sdkbasepath, tasklistfile, conf_initpath):
             if 'attempted to execute unexpectedly and should have been setscened' in e.stdout:
                 msg += '\n----------\n\nNOTE: "attempted to execute unexpectedly and should have been setscened" errors indicate this may be caused by missing sstate artifacts that were likely produced in earlier builds, but have been subsequently deleted for some reason.\n'
             bb.fatal(msg)
-        try:
-            os.rename(temp_sdkbasepath, sdkbasepath)
-        except OSError:
-            shutil.move(temp_sdkbasepath, sdkbasepath)
+        bb.utils.rename(temp_sdkbasepath, sdkbasepath)
         # Clean out residue of running bitbake, which check_sstate_task_list()
         # will effectively do
         clean_esdk_builddir(d, sdkbasepath)
index 480d58f86d40c815a6acea4430b582c212691655..f2d93244405d36db4f95400f921931e2f92bda96 100644 (file)
@@ -57,17 +57,13 @@ do_deploy_source_date_epoch () {
 }
 
 python do_deploy_source_date_epoch_setscene () {
-    import shutil
     sstate_setscene(d)
     bb.utils.mkdirhier(d.getVar('SDE_DIR'))
     sde_file = os.path.join(d.getVar('SDE_DEPLOYDIR'), '__source_date_epoch.txt')
     if os.path.exists(sde_file):
         target = d.getVar('SDE_FILE')
         bb.debug(1, "Moving setscene SDE file %s -> %s" % (sde_file, target))
-        try:
-            os.rename(sde_file, target)
-        except OSError:
-            shutil.move(sde_file, target)
+        bb.utils.rename(sde_file, target)
     else:
         bb.debug(1, "%s not found!" % sde_file)
 }
index 60f7a942857fa93c5ca3dab93cd0ba270e5d2862..b1c608dcb1418eb50479fcf392b98429deb0d90a 100644 (file)
@@ -384,7 +384,6 @@ def sstate_installpkg(ss, d):
 def sstate_installpkgdir(ss, d):
     import oe.path
     import subprocess
-    import shutil
 
     sstateinst = d.getVar("SSTATE_INSTDIR")
     d.setVar('SSTATE_FIXMEDIR', ss['fixmedir'])
@@ -402,10 +401,7 @@ def sstate_installpkgdir(ss, d):
 
     for state in ss['dirs']:
         prepdir(state[1])
-        try:
-            os.rename(sstateinst + state[0], state[1])
-        except OSError:
-            shutil.move(sstateinst + state[0], state[1])
+        bb.utils.rename(sstateinst + state[0], state[1])
     sstate_install(ss, d)
 
     for plain in ss['plaindirs']:
@@ -417,10 +413,7 @@ def sstate_installpkgdir(ss, d):
         dest = plain
         bb.utils.mkdirhier(src)
         prepdir(dest)
-        try:
-            os.rename(src, dest)
-        except OSError:
-            shutil.move(src, dest)
+        bb.utils.rename(src, dest)
 
     return True
 
@@ -645,7 +638,6 @@ python sstate_hardcode_path () {
 
 def sstate_package(ss, d):
     import oe.path
-    import shutil
 
     tmpdir = d.getVar('TMPDIR')
 
@@ -672,10 +664,7 @@ def sstate_package(ss, d):
                     continue
                 bb.error("sstate found an absolute path symlink %s pointing at %s. Please replace this with a relative link." % (srcpath, link))
         bb.debug(2, "Preparing tree %s for packaging at %s" % (state[1], sstatebuild + state[0]))
-        try:
-            os.rename(state[1], sstatebuild + state[0])
-        except OSError:
-            shutil.move(state[1], sstatebuild + state[0])
+        bb.utils.rename(state[1], sstatebuild + state[0])
 
     workdir = d.getVar('WORKDIR')
     sharedworkdir = os.path.join(d.getVar('TMPDIR'), "work-shared")
@@ -685,10 +674,7 @@ def sstate_package(ss, d):
             pdir = plain.replace(sharedworkdir, sstatebuild)
         bb.utils.mkdirhier(plain)
         bb.utils.mkdirhier(pdir)
-        try:
-            os.rename(plain, pdir)
-        except OSError:
-            shutil.move(plain, pdir)
+        bb.utils.rename(plain, pdir)
 
     d.setVar('SSTATE_BUILDDIR', sstatebuild)
     d.setVar('SSTATE_INSTDIR', sstatebuild)
index 7e213472e3934952a211ebb793e135990aa1b2b2..000e4d56641e18845787fa2ab8d5942f1501bf02 100644 (file)
@@ -139,7 +139,6 @@ python apply_update_alternative_renames () {
        return
 
     import re
-    import shutil
 
     def update_files(alt_target, alt_target_rename, pkg, d):
         f = d.getVar('FILES_' + pkg)
@@ -185,10 +184,7 @@ python apply_update_alternative_renames () {
                         link_rename.append((alt_target, alt_target_rename))
                     else:
                         bb.note('%s: Rename %s -> %s' % (pn, alt_target, alt_target_rename))
-                        try:
-                            os.rename(src, dest)
-                        except OSError:
-                            shutil.move(src, dest)
+                        bb.utils.rename(src, dest)
                         update_files(alt_target, alt_target_rename, pkg, d)
                 else:
                     bb.warn("%s: alternative target (%s or %s) does not exist, skipping..." % (pn, alt_target, alt_target_rename))
@@ -205,10 +201,7 @@ python apply_update_alternative_renames () {
             if os.path.lexists(link_target):
                 # Ok, the link_target exists, we can rename
                 bb.note('%s: Rename (link) %s -> %s' % (pn, alt_target, alt_target_rename))
-                try:
-                    os.rename(src, dest)
-                except OSError:
-                    shutil.move(src, dest)
+                bb.utils.rename(src, dest)
             else:
                 # Try to resolve the broken link to link.${BPN}
                 link_maybe = '%s.%s' % (os.readlink(src), pn)
index 8edc653dff4dec923a0fe82face4d730aae5a6a9..a4b6b6f64722949b514b280a5f8a5da11281e77b 100644 (file)
@@ -5,7 +5,6 @@
 import re
 import subprocess
 from oe.package_manager import *
-import shutil
 
 class DpkgIndexer(Indexer):
     def _create_configs(self):
@@ -215,10 +214,7 @@ class DpkgPM(OpkgDpkgPM):
 
                     tmp_sf.write(status)
 
-        try:
-            os.rename(status_file + ".tmp", status_file)
-        except OSError:
-            shutil.move(status_file + ".tmp", status_file)
+        bb.utils.rename(status_file + ".tmp", status_file)
 
     def run_pre_post_installs(self, package_name=None):
         """
@@ -303,21 +299,13 @@ class DpkgPM(OpkgDpkgPM):
             for dir in dirs:
                 new_dir = re.sub(r"\.dpkg-new", "", dir)
                 if dir != new_dir:
-                    try:
-                        os.rename(os.path.join(root, dir),
-                              os.path.join(root, new_dir))
-                    except OSError:
-                        shutil.move(os.path.join(root, dir),
+                    bb.utils.rename(os.path.join(root, dir),
                               os.path.join(root, new_dir))
 
             for file in files:
                 new_file = re.sub(r"\.dpkg-new", "", file)
                 if file != new_file:
-                    try:
-                        os.rename(os.path.join(root, file),
-                              os.path.join(root, new_file))
-                    except OSError:
-                        shutil.move(os.path.join(root, file),
+                    bb.utils.rename(os.path.join(root, file),
                               os.path.join(root, new_file))
 
 
index 2aa21949f3df647752755e66bb9b759523436ac6..4cd3963111c4347e00d6ee868b0168f343f8e895 100644 (file)
@@ -213,10 +213,7 @@ class OpkgPM(OpkgDpkgPM):
 
                     tmp_sf.write(status)
 
-        try:
-            os.rename(status_file + ".tmp", status_file)
-        except OSError:
-            shutil.move(status_file + ".tmp", status_file)
+        bb.utils.rename(status_file + ".tmp", status_file)
 
     def _create_custom_config(self):
         bb.note("Building from feeds activated!")
index ef0001004733a18cbb669ba5ab578a078c6fdb30..a7cc4e084a668063c5179dfc6ef2fa789c0f2564 100644 (file)
@@ -114,10 +114,7 @@ class Rootfs(object, metaclass=ABCMeta):
             shutil.rmtree(self.image_rootfs + '-orig')
         except:
             pass
-        try:
-            os.rename(self.image_rootfs, self.image_rootfs + '-orig')
-        except OSError:
-            shutil.move(self.image_rootfs, self.image_rootfs + '-orig')
+        bb.utils.rename(self.image_rootfs, self.image_rootfs + '-orig')
 
         bb.note("  Creating debug rootfs...")
         bb.utils.mkdirhier(self.image_rootfs)
@@ -168,16 +165,10 @@ class Rootfs(object, metaclass=ABCMeta):
             shutil.rmtree(self.image_rootfs + '-dbg')
         except:
             pass
-        try:
-            os.rename(self.image_rootfs, self.image_rootfs + '-dbg')
-        except OSError:
-            shutil.move(self.image_rootfs, self.image_rootfs + '-dbg')
+        bb.utils.rename(self.image_rootfs, self.image_rootfs + '-dbg')
 
         bb.note("  Restoreing original rootfs...")
-        try:
-            os.rename(self.image_rootfs + '-orig', self.image_rootfs)
-        except OSError:
-            shutil.move(self.image_rootfs + '-orig', self.image_rootfs)
+        bb.utils.rename(self.image_rootfs + '-orig', self.image_rootfs)
 
     def _exec_shell_cmd(self, cmd):
         fakerootcmd = self.d.getVar('FAKEROOT')
index fa93973087fe971e06dd603f55c5b39bc83de016..a11e2d07810ba36fc000bb7e43dcef0478e8e0df 100644 (file)
@@ -1269,7 +1269,6 @@ class Wic2(WicTestCase):
 
     def test_expand_mbr_image(self):
         """Test wic write --expand command for mbr image"""
-        import shutil
         # build an image
         config = 'IMAGE_FSTYPES = "wic"\nWKS_FILE = "directdisk.wks"\n'
         self.append_config(config)
@@ -1307,14 +1306,8 @@ class Wic2(WicTestCase):
             result = runCmd("%s/usr/sbin/sfdisk -F %s" % (sysroot, new_image_path))
             self.assertTrue("0 B, 0 bytes, 0 sectors" in result.output)
 
-            try:
-                os.rename(image_path, image_path + '.bak')
-            except OSError:
-                shutil.move(image_path, image_path + '.bak')
-            try:
-                os.rename(new_image_path, image_path)
-            except OSError:
-                shutil.move(new_image_path, image_path)
+            bb.utils.rename(image_path, image_path + '.bak')
+            bb.utils.rename(new_image_path, image_path)
 
             # Check if it boots in qemu
             with runqemu('core-image-minimal', ssh=False) as qemu:
@@ -1325,10 +1318,7 @@ class Wic2(WicTestCase):
             if os.path.exists(new_image_path):
                 os.unlink(new_image_path)
             if os.path.exists(image_path + '.bak'):
-                try:
-                    os.rename(image_path + '.bak', image_path)
-                except OSError:
-                    shutil.move(image_path + '.bak', image_path)
+                bb.utils.rename(image_path + '.bak', image_path)
 
     def test_wic_ls_ext(self):
         """Test listing content of the ext partition using 'wic ls'"""
index 1b88340b6e4bd8288b23e6eea0a6ca0fb083dc71..045de65642bcb915f392cad6fb452d3afad5b11f 100755 (executable)
@@ -508,10 +508,7 @@ def check_patch(patchfile):
     f.close()
     if of:
         of.close()
-        try:
-            os.rename(patchfile + '.tmp', patchfile)
-        except OSError:
-            shutil.move(patchfile + '.tmp', patchfile)
+        bb.utils.rename(patchfile + '.tmp', patchfile)
 
 def drop_to_shell(workdir=None):
     if not sys.stdin.isatty():
index 3854dfb3e8d1099a1df50611f6edcea8b299aa85..5eba2191d91ea574f591cd5a7aa3bee930ba3bef 100644 (file)
@@ -746,10 +746,7 @@ def _check_preserve(config, recipename):
                         os.remove(removefile)
                 else:
                     tf.write(line)
-    try:
-        os.rename(newfile, origfile)
-    except OSError:
-        shutil.move(newfile, origfile)
+    bb.utils.rename(newfile, origfile)
 
 def get_staging_kver(srcdir):
     # Kernel version from work-shared
@@ -1097,16 +1094,10 @@ def rename(args, config, basepath, workspace):
 
     # Rename bbappend
     logger.info('Renaming %s to %s' % (append, newappend))
-    try:
-        os.rename(append, newappend)
-    except OSError:
-        shutil.move(append, newappend)
+    bb.utils.rename(append, newappend)
     # Rename recipe file
     logger.info('Renaming %s to %s' % (recipefile, newfile))
-    try:
-        os.rename(recipefile, newfile)
-    except OSError:
-        shutil.move(recipefile, newfile)
+    bb.utils.rename(recipefile, newfile)
 
     # Rename source tree if it's the default path
     appendmd5 = None
@@ -1342,11 +1333,7 @@ def _export_patches(srctree, rd, start_rev, destdir, changed_revs=None):
         if match_name:
             # Rename patch files
             if new_patch != match_name:
-                try:
-                    os.rename(os.path.join(destdir, new_patch),
-                          os.path.join(destdir, match_name))
-                except OSError:
-                    shutil.move(os.path.join(destdir, new_patch),
+                bb.utils.rename(os.path.join(destdir, new_patch),
                           os.path.join(destdir, match_name))
             # Need to pop it off the list now before checking changed_revs
             oldpath = existing_patches.pop(old_patch)
index 2bff69f5a2cf5bb649b4b4d08810d0e3bc5a8146..24e3700ece25cc31479793bd4349dd7c24a18fd4 100644 (file)
@@ -71,12 +71,8 @@ def _rename_recipe_dirs(oldpv, newpv, path):
                 if oldfile.find(oldpv) != -1:
                     newfile = oldfile.replace(oldpv, newpv)
                     if oldfile != newfile:
-                        try:
-                            os.rename(os.path.join(path, oldfile),
-                                  os.path.join(path, newfile))
-                        except OSError:
-                            shutil.move(os.path.join(path, oldfile),
-                                  os.path.join(path, newfile))
+                        bb.utils.rename(os.path.join(path, oldfile),
+                              os.path.join(path, newfile))
 
 def _rename_recipe_file(oldrecipe, bpn, oldpv, newpv, path):
     oldrecipe = os.path.basename(oldrecipe)
index fb5e52b2a65113262d1689eaa971ff785796c2b4..96168aadb4c297c7a51eac4c0bdfbee311c2db7c 100644 (file)
@@ -616,8 +616,5 @@ class PartitionedImage():
                              part.start + part.size_sec - 1, part.size_sec)
 
                 partimage = self.path + '.p%d' % part.num
-                try:
-                    os.rename(source, partimage)
-                except OSError:
-                    shutil.move(source, partimage)
+                bb.utils.rename(source, partimage)
                 self.partimages.append(partimage)