diff options
-rw-r--r-- | morphlib/cachekeycomputer.py | 17 | ||||
-rw-r--r-- | morphlib/cachekeycomputer_tests.py | 18 |
2 files changed, 33 insertions, 2 deletions
diff --git a/morphlib/cachekeycomputer.py b/morphlib/cachekeycomputer.py index 033c0318..a1c2ebd6 100644 --- a/morphlib/cachekeycomputer.py +++ b/morphlib/cachekeycomputer.py @@ -78,10 +78,23 @@ class CacheKeyComputer(object): return cacheid def _calculate(self, artifact): - return { + keys = { 'arch': self._build_env.arch, 'env': self._filterenv(self._build_env.env), - 'ref': artifact.source.sha1, 'filename': artifact.source.filename, 'kids': [self.compute_key(x) for x in artifact.dependencies] } + + kind = artifact.source.morphology['kind'] + if kind == 'chunk': + keys['ref'] = artifact.source.sha1 + elif kind in ('system', 'stratum'): + checksum = hashlib.sha1() + morphology = artifact.source.morphology + getkey = lambda s: [ord(c) for c in s] + for key in sorted(morphology.keys(), key=getkey): + checksum.update('%s=%s' % (key, morphology[key])) + keys['morphology-sha1'] = checksum.hexdigest() + + return keys + diff --git a/morphlib/cachekeycomputer_tests.py b/morphlib/cachekeycomputer_tests.py index 51616a12..7fc47370 100644 --- a/morphlib/cachekeycomputer_tests.py +++ b/morphlib/cachekeycomputer_tests.py @@ -148,3 +148,21 @@ class CacheKeyComputerTests(unittest.TestCase): ckc = morphlib.cachekeycomputer.CacheKeyComputer(build_env) self.assertNotEqual(oldsha, ckc.compute_key(artifact)) + + def test_same_morphology_text_but_changed_sha1_gives_same_cache_key(self): + old_artifact = self._find_artifact('system') + morphology = old_artifact.source.morphology + new_source = morphlib.source.Source('repo', 'original/ref', 'newsha', + morphology, + old_artifact.source.filename) + self.source_pool.add(new_source) + artifacts = self.artifact_resolver.resolve_artifacts(self.source_pool) + for new_artifact in artifacts: + if new_artifact.source == new_source: + break + else: + self.assertTrue(False) + + old_sha = self.ckc.compute_key(old_artifact) + new_sha = self.ckc.compute_key(new_artifact) + self.assertEqual(old_sha, new_sha) |