[PATCH 1/3] Add a function to recursively change owner of a directory

Vratislav Podzimek vpodzime at redhat.com
Thu May 30 14:58:29 UTC 2013


On Thu, 2013-05-30 at 16:53 +0200, Vratislav Podzimek wrote:
> 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
I see that the version I sent misses two ifs around the function
invocation that would make the 'files' and 'dirs' parameters work.
Already fixed locally.

-- 
Vratislav Podzimek

Anaconda Rider | Red Hat, Inc. | Brno - Czech Republic



More information about the anaconda-patches mailing list