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 EBDBB13888C for ; Tue, 5 Feb 2013 17:48:27 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 6CCE921C00E; Tue, 5 Feb 2013 17:48:27 +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 B6D7E21C00E for ; Tue, 5 Feb 2013 17:48:26 +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 8810C33DF20 for ; Tue, 5 Feb 2013 17:48:25 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 278D2E4090 for ; Tue, 5 Feb 2013 17:48:24 +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: <1360085191.1b78546536ddad0c80eaa6c98d4e073f0c7c3357.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/util.py X-VCS-Directories: roverlay/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: 1b78546536ddad0c80eaa6c98d4e073f0c7c3357 X-VCS-Branch: master Date: Tue, 5 Feb 2013 17:48:24 +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: 57e7a4b1-007e-4ade-9992-d523dda48e26 X-Archives-Hash: 7c6c5f64ce8e991363086637d8af910b commit: 1b78546536ddad0c80eaa6c98d4e073f0c7c3357 Author: André Erdmann mailerd de> AuthorDate: Tue Feb 5 17:26:31 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Tue Feb 5 17:26:31 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=1b785465 roverlay/util: for_all_files(), get_dict_hash() for_all_files(): accepts a list of "files or dirs" and calls a function for each file (dirs will be recursively expanded) get_dict_hash(): a (slow) function that creates a hash for a dict by hashing a frozenset of (key,value)-tuples --- roverlay/util.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 55 insertions(+), 1 deletions(-) diff --git a/roverlay/util.py b/roverlay/util.py index 8f737c1..4712b63 100644 --- a/roverlay/util.py +++ b/roverlay/util.py @@ -6,13 +6,56 @@ """provides utility functions commonly used""" -__all__= [ 'dodir', 'keepenv', 'sysnop', 'get_dict_hash', 'priosort', ] +__all__= [ + 'dodir', 'keepenv', 'sysnop', 'get_dict_hash', 'priosort', + 'for_all_files' +] import os import logging LOGGER = logging.getLogger ( 'util' ) +def for_all_files ( + files_or_dirs, func, + args=(), kwargs={}, file_filter=None, ignore_missing=False +): + """ + Runs "func ( , *args, **kwargs )" for each in files_or_dirs. + Dirs will be recursively "expanded" (= for all files/dirs in dir...). + + arguments: + * files_or_dirs -- an iterable with files or dirs + * func -- function that will be called for each file + * args -- args that will be passed to each func call + Defaults to () (empty tuple) + * kwargs -- keyword args that will be passed to each func call + Defaults to {} (empty dict) + * file_filter -- if not None: func will only be called if this function + returns True for + Defaults to None + * ignore_missing -- if True: do not raise an exception if a file/dir is + missing + Defaults to False + """ + # alternative: os.walk() + def recursive_do ( fpath ): + if os.path.isfile ( fpath ): + if file_filter is None or file_filter ( fpath ): + func ( fpath, *args, **kwargs ) + elif os.path.isdir ( fpath ): + for fname in os.listdir ( fpath ): + recursive_do ( fpath + os.sep + fname ) + elif os.access ( fpath, os.F_OK ): + raise Exception ( "{}: neither a file nor a dir.".format ( fpath ) ) + elif not ignore_missing: + raise Exception ( "{!r} does not exist!".format ( fpath ) ) + # --- end of recursive_do (...) --- + + for f in files_or_dirs: + recursive_do ( f ) +# --- end of for_all_files (...) --- + def priosort ( iterable ): """Sorts the items of an iterable by priority (lower value means higher priority). @@ -32,6 +75,17 @@ def priosort ( iterable ): return sorted ( iterable, key=priokey ) # --- end of priosort (...) --- +def get_dict_hash ( kwargs ): + # dict is not hashable, instead hash a frozenset of (key,value) tuples + # !!! this operations costs (time) + return hash ( + frozenset ( + ( k, v ) for k, v in kwargs.items() + ) + ) +# --- end of get_dict_hash (...) --- + + def keepenv ( *to_keep ): """Selectively imports os.environ.