From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 65B821381F3 for ; Thu, 12 Sep 2013 16:36:44 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id A97EEE0D54; Thu, 12 Sep 2013 16:36:43 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 2CDC2E0D54 for ; Thu, 12 Sep 2013 16:36:43 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 39A4533DA56 for ; Thu, 12 Sep 2013 16:36:42 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id B64BFE5466 for ; Thu, 12 Sep 2013 16:36:39 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1379003355.f67be36918f6105b692360e1eb186617620eb26d.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/fsutil.py X-VCS-Directories: roverlay/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: f67be36918f6105b692360e1eb186617620eb26d X-VCS-Branch: master Date: Thu, 12 Sep 2013 16:36:39 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 8301dd93-3735-4c34-babf-0bee8701de7a X-Archives-Hash: b0fc60e2abb539b638895b02bcc2248b commit: f67be36918f6105b692360e1eb186617620eb26d Author: André Erdmann mailerd de> AuthorDate: Thu Sep 12 16:29:15 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Thu Sep 12 16:29:15 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=f67be369 roverlay/fsutil: get fs dict from os.walk() --- roverlay/fsutil.py | 53 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/roverlay/fsutil.py b/roverlay/fsutil.py index 5efac20..515f214 100644 --- a/roverlay/fsutil.py +++ b/roverlay/fsutil.py @@ -5,6 +5,7 @@ # either version 2 of the License, or (at your option) any later version. import errno +import functools import os import pwd import stat @@ -17,17 +18,47 @@ _OS_CHOWN = getattr ( os, 'lchown', os.chown ) _OS_CHMOD = getattr ( os, 'lchmod', os.chmod ) -def readlink_f ( fspath ): - try: - f = os.readlink ( fspath ) - except OSError as oserr: - if oserr.errno in { errno.ENOENT, errno.EINVAL }: - f = fspath - else: - raise - - return os.path.abspath ( f ) -# --- end of readlink_f (...) --- +def get_fs_dict ( + initial_root, create_item=None, dict_cls=dict, + dirname_filter=None, filename_filter=None, + include_root=False, prune_empty=False, +): + # http://code.activestate.com/recipes/577879-create-a-nested-dictionary-from-oswalk/ + fsdict = dict_cls() + my_root = os.path.abspath ( initial_root ) + + dictpath_begin = ( + 1 + ( my_root.rfind ( os.sep ) if include_root else len ( my_root ) ) + ) + + for root, dirnames, filenames in os.walk ( initial_root ): + if dirname_filter: + dirnames[:] = [ d for d in dirnames if dirname_filter ( d ) ] + + if filename_filter: + filenames[:] = [ f for f in filenames if filename_filter ( f ) ] + + if not prune_empty or filenames or dirnames: + dict_relpath = root[dictpath_begin:] + + if dict_relpath: + dictpath = dict_relpath.split ( os.sep ) + parent = functools.reduce ( dict_cls.get, dictpath[:-1], fsdict ) + + if create_item is None: + parent [dictpath[-1]] = dict_cls.fromkeys ( filenames ) + else: + parent [dictpath[-1]] = dict_cls ( + ( + fname, + create_item ( ( root + os.sep + fname ), fname, root ) + ) + for fname in filenames + ) + # -- end for + + return fsdict +# --- end of get_fs_dict (...) --- def create_subdir_check ( parent, fs_sep=os.sep ): PARENT_PATH = parent.rstrip ( fs_sep ).split ( fs_sep )