On Fri, Nov 14, 2014 at 04:22:29PM -0500, Anne Mulhern wrote:
----- Original Message -----
From: "Brian C. Lane" <bcl(a)redhat.com>
To: anaconda-patches(a)lists.fedorahosted.org
Sent: Friday, November 14, 2014 3:31:48 PM
Subject: [rhel7/master/f21] Create missing parent directories for user's
home directory (#1163775)
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])
The os.path.join() call is the identity here, since you're giving it a single list
as an argument; it can be eliminated entirely.
Maybe you meant os.path.join(* <the subexpression>), but in that case the
"/".join()
call would have to be altered to fit the result in some way.
oops, that snuck in via cut-n-paste, os.path.join shouldn't be in there.
I would like name parent_dir, instead of parent_dirs (parent_dirs makes me expect a list
of
parent directories). parent_path would be even better, actually.
yeah, that looks better.
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")]
What should "home/bcl" result in?
"home"
I've added a test for it. I also added a check to add a leading "/"
since libuser will blow up without it.
See new patches on the list with these changes and a new patch to fix
setting the gid.
--
Brian C. Lane | Anaconda Team | IRC: bcl #anaconda | Port Orchard, WA (PST8PDT)