Commit 3628cbbe authored by Sam Thursfield's avatar Sam Thursfield

Move utils._generate_key() into a new 'cachekey' module

This avoids a circular dependency between the 'utils' and '_yaml'
modules.
parent bb8f807b
#!/usr/bin/env python3
#
# Copyright (C) 2018 Codethink Limited
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Tristan Van Berkom <tristan.vanberkom@codethink.co.uk>
import hashlib
import pickle
from . import _yaml
# generate_key()
#
# Generate an sha256 hex digest from the given value. The value
# can be a simple value or recursive dictionary with lists etc,
# anything simple enough to serialize.
#
# Args:
# value: A value to get a key for
#
# Returns:
# (str): An sha256 hex digest of the given value
#
def generate_key(value):
ordered = _yaml.node_sanitize(value)
string = pickle.dumps(ordered)
return hashlib.sha256(string).hexdigest()
......@@ -22,10 +22,10 @@ import os
import datetime
from collections import deque, Mapping
from contextlib import contextmanager
from . import _cachekey
from . import _signals
from . import _site
from . import _yaml
from . import utils
from ._exceptions import LoadError, LoadErrorReason, BstError
from ._message import Message, MessageType
from ._profile import Topics, profile_start, profile_end
......@@ -253,7 +253,7 @@ class Context():
if self._cache_key is None:
# Anything that alters the build goes into the unique key
self._cache_key = utils._generate_key({})
self._cache_key = _cachekey.generate_key({})
return self._cache_key
......
......@@ -22,6 +22,7 @@ import os
import multiprocessing # for cpu_count()
from collections import Mapping
from . import utils
from . import _cachekey
from . import _site
from . import _yaml
from ._profile import Topics, profile_start, profile_end
......@@ -402,6 +403,6 @@ class Project():
# Anything that alters the build goes into the unique key
# (currently nothing here)
self._cache_key = utils._generate_key({})
self._cache_key = _cachekey.generate_key({})
return self._cache_key
......@@ -40,6 +40,7 @@ from . import Plugin, Consistency
from ._project import BST_ARTIFACT_VERSION as BST_CORE_ARTIFACT_VERSION
from . import SandboxFlags
from . import utils
from . import _cachekey
from . import _signals
from . import _site
from ._platform import Platform
......@@ -900,7 +901,7 @@ class Element(Plugin):
context = self._get_context()
project = self._get_project()
return utils._generate_key({
return _cachekey.generate_key({
'artifact-version': "{}.{}".format(BST_CORE_ARTIFACT_VERSION,
self.BST_ARTIFACT_VERSION),
'context': context._get_cache_key(),
......
......@@ -40,7 +40,6 @@ import pkg_resources
import psutil
from . import _signals
from . import _yaml
from ._exceptions import BstError, ErrorDomain
......@@ -206,6 +205,7 @@ def sha256sum(filename):
with open(filename, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
h.update(chunk)
except OSError as e:
raise UtilError("Failed to get a checksum of file '{}': {}"
.format(filename, e)) from e
......@@ -734,24 +734,6 @@ def _relative_symlink_target(root, symlink, target):
return target
# _generate_key()
#
# Generate an sha256 hex digest from the given value. The value
# can be a simple value or recursive dictionary with lists etc,
# anything simple enough to serialize.
#
# Args:
# value: A value to get a key for
#
# Returns:
# (str): An sha256 hex digest of the given value
#
def _generate_key(value):
ordered = _yaml.node_sanitize(value)
string = pickle.dumps(ordered)
return hashlib.sha256(string).hexdigest()
# _set_deterministic_user()
#
# Set the uid/gid for every file in a directory tree to the process'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment