[PATCH 1/3] Add a function to recursively change owner of a directory
Vratislav Podzimek
vpodzime at redhat.com
Thu May 30 14:53:42 UTC 2013
Implemented by using a universal function for mapping a function on the
directory tree.
Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
pyanaconda/iutil.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 7020da5..459a890 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -628,3 +628,54 @@ def cmp_obj_attrs(obj1, obj2, attr_list):
else:
return False
return True
+
+def dir_tree_map(root, func, files=True, dirs=True):
+ """
+ Apply the given function to all files and directories in the directory tree
+ under the given root directory.
+
+ :param root: root of the directory tree the function should be mapped to
+ :type root: str
+ :param func: a function taking the directory/file path
+ :type func: path -> None
+ :param files: whether to apply the function to the files in the dir. tree
+ :type files: bool
+ :param dirs: whether to apply the function to the directories in the dir. tree
+ :type dirs: bool
+
+ TODO: allow using globs and thus more trees?
+
+ """
+
+ for (dir_ent, dirs, files) in os.walk(root):
+ # try to call the function on the directory entry
+ try:
+ func(dir_ent)
+ except:
+ pass
+
+ # try to call the function on the files in the directory entry
+ for file_ent in (os.path.join(dir_ent, f) for f in files):
+ try:
+ func(file_ent)
+ except:
+ pass
+
+ # directories under the directory entry will appear as directory entries
+ # in the loop
+
+def chown_dir_tree(root, uid, gid):
+ """
+ Change owner (uid and gid) of the files and directories under the given
+ directory tree (recursively).
+
+ :param root: root of the directory tree that should be chown'ed
+ :type root: str
+ :param uid: UID that should be set as the owner
+ :type uid: int
+ :param gid: GID that should be set as the owner
+ :type gid: int
+
+ """
+
+ dir_tree_map(root, lambda path: os.chown(path, uid, gid))
--
1.7.11.7
More information about the anaconda-patches
mailing list