movefile: refactor selinux conditional code
authorZac Medico <zmedico@gentoo.org>
Fri, 23 Mar 2012 18:05:36 +0000 (11:05 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 23 Mar 2012 18:20:14 +0000 (11:20 -0700)
pym/portage/util/movefile.py

index 476f8e72adc4fda541c2195ac0c946dc0c4ceecb..252f4a2923f049228c01f805a4a7e58f9f82c8ea 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2010-2011 Gentoo Foundation
+# Copyright 2010-2012 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 __all__ = ['movefile']
@@ -85,15 +85,20 @@ def movefile(src, dest, newmtime=None, sstat=None, mysettings=None,
                mysettings = portage.settings
 
        src_bytes = _unicode_encode(src, encoding=encoding, errors='strict')
+       dest_bytes = _unicode_encode(dest, encoding=encoding, errors='strict')
        xattr_enabled = "xattr" in mysettings.features
        selinux_enabled = mysettings.selinux_enabled()
        if selinux_enabled:
                selinux = _unicode_module_wrapper(_selinux, encoding=encoding)
+               _copyfile = selinux.copyfile
+               _rename = selinux.rename
+       else:
+               _copyfile = _shutil.copyfile
+               _rename = _os.rename
 
        lchown = _unicode_func_wrapper(portage.data.lchown, encoding=encoding)
        os = _unicode_module_wrapper(_os,
                encoding=encoding, overrides=_os_overrides)
-       shutil = _unicode_module_wrapper(_shutil, encoding=encoding)
 
        try:
                if not sstat:
@@ -222,19 +227,12 @@ def movefile(src, dest, newmtime=None, sstat=None, mysettings=None,
                        dest_tmp_bytes = _unicode_encode(dest_tmp, encoding=encoding,
                                errors='strict')
                        try: # For safety copy then move it over.
-                               if selinux_enabled:
-                                       selinux.copyfile(src, dest_tmp)
-                                       if xattr_enabled:
-                                               _copyxattr(src_bytes, dest_tmp_bytes)
-                                       _apply_stat(sstat, dest_tmp_bytes)
-                                       selinux.rename(dest_tmp, dest)
-                               else:
-                                       shutil.copyfile(src, dest_tmp)
-                                       if xattr_enabled:
-                                               _copyxattr(src_bytes, dest_tmp_bytes)
-                                       _apply_stat(sstat, dest_tmp_bytes)
-                                       os.rename(dest_tmp, dest)
-                               os.unlink(src)
+                               _copyfile(src_bytes, dest_tmp_bytes)
+                               if xattr_enabled:
+                                       _copyxattr(src_bytes, dest_tmp_bytes)
+                               _apply_stat(sstat, dest_tmp_bytes)
+                               _rename(dest_tmp_bytes, dest_bytes)
+                               _os.unlink(src_bytes)
                        except SystemExit as e:
                                raise
                        except Exception as e: