[master 1/4] Remove iutil.mkdirChain

dashea installerbot-noreply at redhat.com
Fri Jul 17 19:18:42 UTC 2015


From: David Shea <dshea at redhat.com>

os.makedirs added a parameter that takes care of this.
---
 anaconda                             |  2 +-
 pyanaconda/iutil.py                  | 21 +------------------
 pyanaconda/network.py                |  3 +--
 pyanaconda/packaging/__init__.py     |  4 ++--
 pyanaconda/users.py                  |  2 +-
 tests/pyanaconda_tests/iutil_test.py | 40 ------------------------------------
 6 files changed, 6 insertions(+), 66 deletions(-)

diff --git a/anaconda b/anaconda
index 203281c..2f904eb 100755
--- a/anaconda
+++ b/anaconda
@@ -922,7 +922,7 @@ if __name__ == "__main__":
     setupEnvironment()
 
     # make sure we have /var/log soon, some programs fail to start without it
-    iutil.mkdirChain("/var/log")
+    os.makedirs("/var/log", exist_ok=True)
 
     # Share these with the exit handler, installed later
     pidfile_path = '/var/run/anaconda.pid'
diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 7a261a1..e946074 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -24,7 +24,6 @@
 import os
 import stat
 import os.path
-import errno
 import subprocess
 import unicodedata
 # Used for ascii_lowercase, ascii_uppercase constants
@@ -662,24 +661,6 @@ def getSubdirSize(directory):
         return dsize
     return getSubdirSize(directory) // 1024
 
-## Create a directory path.  Don't fail if the directory already exists.
-def mkdirChain(directory):
-    """
-    :param dir: The directory path to create
-
-    """
-
-    try:
-        os.makedirs(directory, 0o755)
-    except OSError as e:
-        try:
-            if e.errno == errno.EEXIST and stat.S_ISDIR(os.stat(directory).st_mode):
-                return
-        except OSError:
-            pass
-
-        log.error("could not create directory %s: %s", dir, e.strerror)
-
 def get_active_console(dev="console"):
     '''Find the active console device.
 
@@ -794,7 +775,7 @@ def dracut_eject(device):
 
     try:
         if not os.path.exists(DRACUT_SHUTDOWN_EJECT):
-            mkdirChain(os.path.dirname(DRACUT_SHUTDOWN_EJECT))
+            os.makedirs(os.path.dirname(DRACUT_SHUTDOWN_EJECT), exist_ok=True)
             f = open_with_perm(DRACUT_SHUTDOWN_EJECT, "w", 0o755)
             f.write("#!/bin/sh\n")
             f.write("# Created by Anaconda\n")
diff --git a/pyanaconda/network.py b/pyanaconda/network.py
index 001a15c..fac1bc2 100644
--- a/pyanaconda/network.py
+++ b/pyanaconda/network.py
@@ -936,8 +936,7 @@ def copyFileToPath(fileName, destPath='', overwrite=False):
     destfile = os.path.join(destPath, fileName.lstrip('/'))
     if (os.path.isfile(destfile) and not overwrite):
         return False
-    if not os.path.isdir(os.path.dirname(destfile)):
-        iutil.mkdirChain(os.path.dirname(destfile))
+    os.makedirs(os.path.dirname(destfile), exist_ok=True)
     shutil.copy(fileName, destfile)
     return True
 
diff --git a/pyanaconda/packaging/__init__.py b/pyanaconda/packaging/__init__.py
index 870d289..0805d37 100644
--- a/pyanaconda/packaging/__init__.py
+++ b/pyanaconda/packaging/__init__.py
@@ -474,7 +474,7 @@ def _setupNFS(mountpoint, server, path, options):
     ###
     def preInstall(self, packages=None, groups=None):
         """ Perform pre-installation tasks. """
-        iutil.mkdirChain(iutil.getSysroot() + "/root")
+        os.makedirs(iutil.getSysroot() + "/root", exist_ok=True)
 
         self._writeModuleBlacklist()
 
@@ -490,7 +490,7 @@ def _writeModuleBlacklist(self):
         if "modprobe.blacklist" not in flags.cmdline:
             return
 
-        iutil.mkdirChain(iutil.getSysroot() + "/etc/modprobe.d")
+        os.makedirs(iutil.getSysroot() + "/etc/modprobe.d", exist_ok=True)
         with open(iutil.getSysroot() + "/etc/modprobe.d/anaconda-blacklist.conf", "w") as f:
             f.write("# Module blacklists written by anaconda\n")
             for module in flags.cmdline["modprobe.blacklist"].split():
diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 5eee4fc..2cd876a 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -315,7 +315,7 @@ def createUser(self, user_name, *args, **kwargs):
             # libuser expects the parent directory tree to exist.
             parent_dir = iutil.parent_dir(homedir)
             if parent_dir:
-                iutil.mkdirChain(parent_dir)
+                os.makedirs(parent_dir, exist_ok=True)
             userEnt.set(libuser.HOMEDIRECTORY, homedir)
 
             if kwargs.get("shell", False):
diff --git a/tests/pyanaconda_tests/iutil_test.py b/tests/pyanaconda_tests/iutil_test.py
index 9f19455..f7706ac 100644
--- a/tests/pyanaconda_tests/iutil_test.py
+++ b/tests/pyanaconda_tests/iutil_test.py
@@ -460,46 +460,6 @@ def get_dir_size_test(self):
         # TODO: mock some dirs and check if their size is
         # computed correctly
 
-    def mkdir_chain_test(self):
-        """Test mkdirChain."""
-
-        # don't fail if directory path already exists
-        iutil.mkdirChain('/dev/null')
-        iutil.mkdirChain('/')
-        iutil.mkdirChain('/tmp')
-
-        # create a path and test it exists
-        test_folder = "test_mkdir_chain"
-        test_paths = [
-            "foo",
-            "foo/bar/baz",
-            u"foo/bar/baz",
-            "",
-            "čřščščřščř",
-            u"čřščščřščř",
-            "asdasd asdasd",
-            "! spam"
-        ]
-
-        # join with the toplevel test folder and the folder for this
-        # test
-        test_paths = [os.path.join(ANACONDA_TEST_DIR, test_folder, p)
-                      for p in test_paths]
-
-        def create_return(path):
-            iutil.mkdirChain(path)
-            return path
-
-        # create the folders and check that they exist
-        for p in test_paths:
-            self.assertTrue(os.path.exists(create_return(p)))
-
-        # try to create them again - all the paths should already exist
-        # and the mkdirChain function needs to handle that
-        # without a traceback
-        for p in test_paths:
-            iutil.mkdirChain(p)
-
     def get_active_console_test(self):
         """Test get_active_console."""
 


-- 
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/e9d27a5c9572f5e98b31552db706113161a81e1b


More information about the anaconda-patches mailing list