1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# Copyright (C) 2012 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import hashlib
import morphlib
class CacheKeyComputer():
def __init__(self, build_env):
self._arch = morphlib.util.arch()
self._env = self._filterenv(build_env.env)
self._calculated = {}
def _filterenv(self, env):
return dict([(k, env[k]) for k in ("USER", "USERNAME", "LOGNAME",
"TOOLCHAIN_TARGET", "PREFIX",
"BOOTSTRAP", "CFLAGS")])
def get_cache_key(self, source):
return self._hash_id(self.get_cache_id(source))
def _hash_id(self, cache_id):
sha = hashlib.sha256()
self._hash_dict(sha, cache_id)
return sha.hexdigest()
def _hash_thing(self, sha, thing):
if type(thing) == dict:
self._hash_dict(sha, thing)
elif type(thing) == list:
self._hash_list(sha, thing)
elif type(thing) == tuple:
self._hash_tuple(sha, thing)
else:
sha.update(str(thing))
def _hash_dict(self, sha, d):
for tup in sorted(d.iteritems()):
self._hash_thing(sha, tup)
def _hash_list(self, sha, l):
for item in l:
self._hash_thing(sha, item)
def _hash_tuple(self, sha, tup):
for item in tup:
self._hash_thing(sha, item)
def get_cache_id(self, source):
try:
return self._calculated[source]
except KeyError:
cacheid = self._calculate(source)
self._calculated[source] = cacheid
return cacheid
def _calculate(self, source):
return {
'arch': self._arch,
'env': self._env,
'ref': source.sha1,
'filename': source.filename,
'kids': [self.get_cache_id(dependency)
for dependency in source.dependencies],
}
|