Gentoo Archives: gentoo-portage-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-portage-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-portage-dev] [PATCH] portage.checksum: Store supported hash types in a frozenset
Date: Mon, 13 Mar 2017 15:12:13
Message-Id: 20170313144529.4509-1-mgorny@gentoo.org
In Reply to: Re: [gentoo-portage-dev] [PATCH 01/14] Use public API: hashfunc_map -> get_valid_checksum_keys() by "Michał Górny"
1 Copy the list of supported hash types (hashfunc_map dict) into
2 a frozenset to support efficient access in the public API.
3 ---
4 pym/portage/checksum.py | 25 ++++++++++++++-----------
5 1 file changed, 14 insertions(+), 11 deletions(-)
6
7 diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py
8 index df896533f..ff132751b 100644
9 --- a/pym/portage/checksum.py
10 +++ b/pym/portage/checksum.py
11 @@ -278,6 +278,9 @@ class SizeHash(object):
12
13 hashfunc_map["size"] = SizeHash()
14
15 +# cache all supported hash methods in a frozenset
16 +hashfunc_keys = frozenset(hashfunc_map)
17 +
18 # end actual hash functions
19
20
21 @@ -312,15 +315,15 @@ def _perform_md5_merge(x, **kwargs):
22
23 def perform_all(x, calc_prelink=0):
24 mydict = {}
25 - for k in hashfunc_map:
26 + for k in hashfunc_keys:
27 mydict[k] = perform_checksum(x, k, calc_prelink)[0]
28 return mydict
29
30 def get_valid_checksum_keys():
31 - return list(hashfunc_map)
32 + return hashfunc_keys
33
34 def get_hash_origin(hashtype):
35 - if hashtype not in hashfunc_map:
36 + if hashtype not in hashfunc_keys:
37 raise KeyError(hashtype)
38 return hashorigin_map.get(hashtype, "unknown")
39
40 @@ -334,7 +337,7 @@ def _filter_unaccelarated_hashes(digests):
41 due to minimization of dependencies.
42 """
43 if _whirlpool_unaccelerated and "WHIRLPOOL" in digests:
44 - verifiable_hash_types = set(digests).intersection(hashfunc_map)
45 + verifiable_hash_types = set(digests).intersection(hashfunc_keys)
46 verifiable_hash_types.discard("size")
47 if len(verifiable_hash_types) > 1:
48 digests = dict(digests)
49 @@ -383,7 +386,7 @@ def _apply_hash_filter(digests, hash_filter):
50 @type hash_filter: callable
51 """
52
53 - verifiable_hash_types = set(digests).intersection(hashfunc_map)
54 + verifiable_hash_types = set(digests).intersection(hashfunc_keys)
55 verifiable_hash_types.discard("size")
56 modified = False
57 if len(verifiable_hash_types) > 1:
58 @@ -430,10 +433,10 @@ def verify_all(filename, mydict, calc_prelink=0, strict=0):
59 raise portage.exception.FileNotFound(filename)
60 return False, (str(e), None, None)
61
62 - verifiable_hash_types = set(mydict).intersection(hashfunc_map)
63 + verifiable_hash_types = set(mydict).intersection(hashfunc_keys)
64 verifiable_hash_types.discard("size")
65 if not verifiable_hash_types:
66 - expected = set(hashfunc_map)
67 + expected = set(hashfunc_keys)
68 expected.discard("size")
69 expected = list(expected)
70 expected.sort()
71 @@ -448,7 +451,7 @@ def verify_all(filename, mydict, calc_prelink=0, strict=0):
72 for x in sorted(mydict):
73 if x == "size":
74 continue
75 - elif x in hashfunc_map:
76 + elif x in hashfunc_keys:
77 myhash = perform_checksum(filename, x, calc_prelink=calc_prelink)[0]
78 if mydict[x] != myhash:
79 if strict:
80 @@ -504,7 +507,7 @@ def perform_checksum(filename, hashname="MD5", calc_prelink=0):
81 # This happens during uninstallation of prelink.
82 prelink_capable = False
83 try:
84 - if hashname not in hashfunc_map:
85 + if hashname not in hashfunc_keys:
86 raise portage.exception.DigestException(hashname + \
87 " hash function not available (needs dev-python/pycrypto)")
88 myhash, mysize = hashfunc_map[hashname].checksum_file(myfilename)
89 @@ -541,7 +544,7 @@ def perform_multiple_checksums(filename, hashes=["MD5"], calc_prelink=0):
90 """
91 rVal = {}
92 for x in hashes:
93 - if x not in hashfunc_map:
94 + if x not in hashfunc_keys:
95 raise portage.exception.DigestException(x+" hash function not available (needs dev-python/pycrypto or >=dev-lang/python-2.5)")
96 rVal[x] = perform_checksum(filename, x, calc_prelink)[0]
97 return rVal
98 @@ -558,7 +561,7 @@ def checksum_str(data, hashname="MD5"):
99 @rtype: String
100 @return: The hash (hex-digest) of the data
101 """
102 - if hashname not in hashfunc_map:
103 + if hashname not in hashfunc_keys:
104 raise portage.exception.DigestException(hashname + \
105 " hash function not available (needs dev-python/pycrypto)")
106 return hashfunc_map[hashname].checksum_str(data)
107 --
108 2.12.0

Replies