[rhel7/master/f21] Create missing parent directories for user's home directory (#1163775)
Brian C. Lane
bcl at redhat.com
Fri Nov 14 20:31:48 UTC 2014
libuser doesn't create parent directories. This simplifies the homedir
creation slightly and adds a utility function, with a test, that returns
the home directories parents, suitable for passing to mkdirChain.
Resolves: rhbz#1163775
---
pyanaconda/iutil.py | 4 ++++
pyanaconda/users.py | 11 ++++++-----
tests/pyanaconda_tests/iutil_test.py | 9 +++++++++
3 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index ad09850..2de3e68 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -883,3 +883,7 @@ def save_screenshots():
except OSError:
log.exception("saving screenshots to installed system failed")
+
+def parent_dirs(directory):
+ """Return the parent directories"""
+ return "/".join(os.path.join(os.path.normpath(directory).split("/"))[:-1])
diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 0eb7435..849329c 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -287,11 +287,12 @@ class Users:
userEnt.set(libuser.GIDNUMBER, [groupEnt.get(libuser.GIDNUMBER)[0]] +
map(lambda grp: grp.get(libuser.GIDNUMBER)[0], grpLst))
- if kwargs.get("homedir", False):
- userEnt.set(libuser.HOMEDIRECTORY, kwargs["homedir"])
- else:
- iutil.mkdirChain('/home')
- userEnt.set(libuser.HOMEDIRECTORY, "/home/" + user_name)
+ homedir = kwargs.get("homedir", "/home/" + user_name)
+ # libuser expects the parent directory tree to exist.
+ parent_dirs = iutil.parent_dirs(homedir)
+ if parent_dirs:
+ iutil.mkdirChain(parent_dirs)
+ userEnt.set(libuser.HOMEDIRECTORY, homedir)
if kwargs.get("shell", False):
userEnt.set(libuser.LOGINSHELL, kwargs["shell"])
diff --git a/tests/pyanaconda_tests/iutil_test.py b/tests/pyanaconda_tests/iutil_test.py
index bd7aced..6bd28d9 100644
--- a/tests/pyanaconda_tests/iutil_test.py
+++ b/tests/pyanaconda_tests/iutil_test.py
@@ -363,3 +363,12 @@ class RunProgramTests(unittest.TestCase):
self.assertFalse(iutil.cmp_obj_attrs(a, b, ["b", "c"]))
self.assertFalse(iutil.cmp_obj_attrs(b, a, ["b", "c"]))
self.assertFalse(iutil.cmp_obj_attrs(b, a, ["c", "b"]))
+
+ def parent_dirs_test(self):
+ """Test the parent_dirs function"""
+ dirs = [("", ""), ("/", ""), ("/home/", ""), ("/home/bcl", "/home"),
+ ("/home/bcl/", "/home"), ("/home/extra/bcl", "/home/extra"),
+ ("/home/extra/bcl/", "/home/extra"), ("/home/extra/../bcl/", "/home")]
+
+ for d, r in dirs:
+ self.assertEquals(iutil.parent_dirs(d), r)
--
1.9.3
More information about the anaconda-patches
mailing list