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 347761381F3 for ; Sat, 22 Jun 2013 15:24:36 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 72FA6E0ABB; Sat, 22 Jun 2013 15:24:25 +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 901DAE0AB2 for ; Sat, 22 Jun 2013 15:24:19 +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 68B2933E674 for ; Sat, 22 Jun 2013 15:24:18 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 25845E5466 for ; Sat, 22 Jun 2013 15:24:16 +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: <1371770861.ddc3781f3fe2df940306c14a0ee2779a07454cb4.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/digest.py X-VCS-Directories: roverlay/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: ddc3781f3fe2df940306c14a0ee2779a07454cb4 X-VCS-Branch: master Date: Sat, 22 Jun 2013 15:24:16 +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: 24c4212d-7ebf-47f9-82cb-f5bf57850b17 X-Archives-Hash: 129eb0e35b2d1719ecc346bd14eb5daf commit: ddc3781f3fe2df940306c14a0ee2779a07454cb4 Author: André Erdmann mailerd de> AuthorDate: Thu Jun 20 23:27:41 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Thu Jun 20 23:27:41 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=ddc3781f roverlay/digest: fixup --- roverlay/digest.py | 86 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/roverlay/digest.py b/roverlay/digest.py index 983ca1a..a031889 100644 --- a/roverlay/digest.py +++ b/roverlay/digest.py @@ -6,13 +6,19 @@ """provides digest related utility functions (e.g. md5sum_file())""" -__all__ = [ 'digest_compare', 'digest_supported', - 'dodigest_file', 'md5sum_file' +__all__ = [ + 'digest_compare', 'digest_comparator', + 'digest_supported', 'dodigest_file', + 'multihash', 'multihash_file', + 'md5sum_file', 'sha1_file', 'sha256_file', 'sha512_file', + 'whirlpool_file', ] import hashlib import portage.util.whirlpool +DEFAULT_BLOCKSIZE=16384 + _HASH_CREATE_MAP = { 'md5' : hashlib.md5, 'sha1' : hashlib.sha1, @@ -21,7 +27,10 @@ _HASH_CREATE_MAP = { 'whirlpool' : portage.util.whirlpool.new, } -def _generic_obj_hash ( hashobj, fh, binary_digest=False, blocksize=16384 ): + +def _generic_obj_hash ( + hashobj, fh, binary_digest=False, blocksize=DEFAULT_BLOCKSIZE +): block = fh.read ( blocksize ) while block: hashobj.update ( block ) @@ -30,7 +39,17 @@ def _generic_obj_hash ( hashobj, fh, binary_digest=False, blocksize=16384 ): return hashobj.digest() if binary_digest else hashobj.hexdigest() # --- end of _hashsum_generic (...) --- -def multihash ( fh, hashlist, binary_digest=False, blocksize=16384 ): +def _generic_file_obj_hash ( + hashobj, filepath, binary_digest=False, blocksize=DEFAULT_BLOCKSIZE +): + with open ( filepath, 'rb' ) as fh: + ret = _generic_obj_hash ( hashobj, fh, binary_digest, blocksize ) + return ret +# --- end of _generic_file_obj_hash (...) --- + +def multihash ( + fh, hashlist, binary_digest=False, blocksize=DEFAULT_BLOCKSIZE +): """Calculates multiple digests for an already openened file and returns the resulting hashes as dict. @@ -73,50 +92,53 @@ def multihash_file ( filepath, digest_types, **kwargs ): return dict() # --- end of multihash_file (...) --- -def md5sum_file ( fh, binary_digest=False ): - """Returns the md5 sum for an already opened file.""" - return _generic_obj_hash ( hashlib.md5(), fh, binary_digest ) +def md5sum_file ( filepath, **kw ): + """Returns the md5 sum for a file.""" + return _generic_file_obj_hash ( hashlib.md5(), filepath, **kw ) # --- end of md5sum_file (...) --- -def sha1_file ( fh, binary_digest=False ): - return _generic_obj_hash ( hashlib.sha1(), fh, binary_digest ) +def sha1_file ( filepath, **kw ): + return _generic_obj_hash ( hashlib.sha1(), filepath, **kw ) # --- end of sha1_file (...) --- -def sha256_file ( fh, binary_digest=False ): - return _generic_obj_hash ( hashlib.sha256(), fh, binary_digest ) +def sha256_file ( filepath, **kw ): + return _generic_obj_hash ( hashlib.sha256(), filepath, **kw ) # --- end of sha256_file (...) --- -def sha512_file ( fh, binary_digest=False ): - return _generic_obj_hash ( hashlib.sha512(), fh, binary_digest ) +def sha512_file ( filepath, **kw ): + return _generic_obj_hash ( hashlib.sha512(), filepath, **kw ) # --- end of sha512_file (...) --- -def whirlpool_file ( fh, binary_digest=False ): +def whirlpool_file ( filepath, **kw ): return _generic_obj_hash ( - portage.util.whirlpool.new(), fh, binary_digest + portage.util.whirlpool.new(), filepath, **kw ) # --- end of whirlpool_file (...) --- -# TODO: remove -_DIGEST_MAP = dict ( - md5 = md5sum_file, - sha1 = sha1_file, - sha256 = sha256_file, - sha512 = sha512_file, - whirlpool = whirlpool_file, -) - def digest_supported ( digest_type ): """Returns True if the given digest type is supported, else False.""" - return digest_type in _DIGEST_MAP + return digest_type in _HASH_CREATE_MAP # --- digest_supported (...) --- -def dodigest_file ( _file, digest_type, binary_digest=False ): - ret = None - with open ( _file, mode='rb' ) as fh: - ret = _DIGEST_MAP [digest_type] ( fh, binary_digest=binary_digest ) - return ret +def dodigest_file ( _file, digest_type, **kwargs ): + return _generic_file_obj_hash ( + hashobj = _HASH_CREATE_MAP [digest_type](), + filepath = _file, + **kwargs + ) # --- end of dodigest_file (...) --- -def digest_compare ( _file, digest, digest_type, binary_digest=False ): - return digest == dodigest_file ( _file, digest_type, binary_digest ) +def digest_compare ( digest, digest_type, filepath, **kwargs ): + return digest == dodigest_file ( filepath, digest_type, **kwargs ) # --- end of digest_compare (...) --- + +# digest_comparator :: digest_type -> digest -> ( filepath, ... ) -> bool +digest_comparator = ( + lambda digest_type : ( + lambda digest : ( + lambda filepath, *args, **kwargs : digest_compare ( + digest, digest_type, *args, **kwargs + ) + ) + ) +) 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 D6D4B138202 for ; Thu, 20 Jun 2013 23:40:46 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id E8E43E09B5; Thu, 20 Jun 2013 23:40:44 +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 55366E086E for ; Thu, 20 Jun 2013 23:40:44 +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 27FDB33E61B for ; Thu, 20 Jun 2013 23:40:43 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id C40D4E5462 for ; Thu, 20 Jun 2013 23:40:41 +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: <1371770861.ddc3781f3fe2df940306c14a0ee2779a07454cb4.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:gsoc13/next commit in: roverlay/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/digest.py X-VCS-Directories: roverlay/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: ddc3781f3fe2df940306c14a0ee2779a07454cb4 X-VCS-Branch: gsoc13/next Date: Thu, 20 Jun 2013 23:40:41 +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: 96ea24ad-84fb-4bee-b6eb-b4407f0ef25f X-Archives-Hash: 13ffc6c072ac45f29645709cb87b19e8 Message-ID: <20130620234041.mpPGDu8HZv8Wd-LoVxzRgVTXo8copwCiK3iu3qY7q9A@z> commit: ddc3781f3fe2df940306c14a0ee2779a07454cb4 Author: André Erdmann mailerd de> AuthorDate: Thu Jun 20 23:27:41 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Thu Jun 20 23:27:41 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=ddc3781f roverlay/digest: fixup --- roverlay/digest.py | 86 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/roverlay/digest.py b/roverlay/digest.py index 983ca1a..a031889 100644 --- a/roverlay/digest.py +++ b/roverlay/digest.py @@ -6,13 +6,19 @@ """provides digest related utility functions (e.g. md5sum_file())""" -__all__ = [ 'digest_compare', 'digest_supported', - 'dodigest_file', 'md5sum_file' +__all__ = [ + 'digest_compare', 'digest_comparator', + 'digest_supported', 'dodigest_file', + 'multihash', 'multihash_file', + 'md5sum_file', 'sha1_file', 'sha256_file', 'sha512_file', + 'whirlpool_file', ] import hashlib import portage.util.whirlpool +DEFAULT_BLOCKSIZE=16384 + _HASH_CREATE_MAP = { 'md5' : hashlib.md5, 'sha1' : hashlib.sha1, @@ -21,7 +27,10 @@ _HASH_CREATE_MAP = { 'whirlpool' : portage.util.whirlpool.new, } -def _generic_obj_hash ( hashobj, fh, binary_digest=False, blocksize=16384 ): + +def _generic_obj_hash ( + hashobj, fh, binary_digest=False, blocksize=DEFAULT_BLOCKSIZE +): block = fh.read ( blocksize ) while block: hashobj.update ( block ) @@ -30,7 +39,17 @@ def _generic_obj_hash ( hashobj, fh, binary_digest=False, blocksize=16384 ): return hashobj.digest() if binary_digest else hashobj.hexdigest() # --- end of _hashsum_generic (...) --- -def multihash ( fh, hashlist, binary_digest=False, blocksize=16384 ): +def _generic_file_obj_hash ( + hashobj, filepath, binary_digest=False, blocksize=DEFAULT_BLOCKSIZE +): + with open ( filepath, 'rb' ) as fh: + ret = _generic_obj_hash ( hashobj, fh, binary_digest, blocksize ) + return ret +# --- end of _generic_file_obj_hash (...) --- + +def multihash ( + fh, hashlist, binary_digest=False, blocksize=DEFAULT_BLOCKSIZE +): """Calculates multiple digests for an already openened file and returns the resulting hashes as dict. @@ -73,50 +92,53 @@ def multihash_file ( filepath, digest_types, **kwargs ): return dict() # --- end of multihash_file (...) --- -def md5sum_file ( fh, binary_digest=False ): - """Returns the md5 sum for an already opened file.""" - return _generic_obj_hash ( hashlib.md5(), fh, binary_digest ) +def md5sum_file ( filepath, **kw ): + """Returns the md5 sum for a file.""" + return _generic_file_obj_hash ( hashlib.md5(), filepath, **kw ) # --- end of md5sum_file (...) --- -def sha1_file ( fh, binary_digest=False ): - return _generic_obj_hash ( hashlib.sha1(), fh, binary_digest ) +def sha1_file ( filepath, **kw ): + return _generic_obj_hash ( hashlib.sha1(), filepath, **kw ) # --- end of sha1_file (...) --- -def sha256_file ( fh, binary_digest=False ): - return _generic_obj_hash ( hashlib.sha256(), fh, binary_digest ) +def sha256_file ( filepath, **kw ): + return _generic_obj_hash ( hashlib.sha256(), filepath, **kw ) # --- end of sha256_file (...) --- -def sha512_file ( fh, binary_digest=False ): - return _generic_obj_hash ( hashlib.sha512(), fh, binary_digest ) +def sha512_file ( filepath, **kw ): + return _generic_obj_hash ( hashlib.sha512(), filepath, **kw ) # --- end of sha512_file (...) --- -def whirlpool_file ( fh, binary_digest=False ): +def whirlpool_file ( filepath, **kw ): return _generic_obj_hash ( - portage.util.whirlpool.new(), fh, binary_digest + portage.util.whirlpool.new(), filepath, **kw ) # --- end of whirlpool_file (...) --- -# TODO: remove -_DIGEST_MAP = dict ( - md5 = md5sum_file, - sha1 = sha1_file, - sha256 = sha256_file, - sha512 = sha512_file, - whirlpool = whirlpool_file, -) - def digest_supported ( digest_type ): """Returns True if the given digest type is supported, else False.""" - return digest_type in _DIGEST_MAP + return digest_type in _HASH_CREATE_MAP # --- digest_supported (...) --- -def dodigest_file ( _file, digest_type, binary_digest=False ): - ret = None - with open ( _file, mode='rb' ) as fh: - ret = _DIGEST_MAP [digest_type] ( fh, binary_digest=binary_digest ) - return ret +def dodigest_file ( _file, digest_type, **kwargs ): + return _generic_file_obj_hash ( + hashobj = _HASH_CREATE_MAP [digest_type](), + filepath = _file, + **kwargs + ) # --- end of dodigest_file (...) --- -def digest_compare ( _file, digest, digest_type, binary_digest=False ): - return digest == dodigest_file ( _file, digest_type, binary_digest ) +def digest_compare ( digest, digest_type, filepath, **kwargs ): + return digest == dodigest_file ( filepath, digest_type, **kwargs ) # --- end of digest_compare (...) --- + +# digest_comparator :: digest_type -> digest -> ( filepath, ... ) -> bool +digest_comparator = ( + lambda digest_type : ( + lambda digest : ( + lambda filepath, *args, **kwargs : digest_compare ( + digest, digest_type, *args, **kwargs + ) + ) + ) +)