diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2018-01-04 12:28:58 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2018-01-04 12:30:12 +0000 |
commit | 3628cbbe0ec04e4f6e202d473a04a8fb3cf60840 (patch) | |
tree | 80cde7c589f48caf663ba43da8a70af5a625c0d6 | |
parent | bb8f807bc6f5bcc6259bc06d6995b7f0bc2c6076 (diff) | |
download | buildstream-3628cbbe0ec04e4f6e202d473a04a8fb3cf60840.tar.gz |
Move utils._generate_key() into a new 'cachekey' module
This avoids a circular dependency between the 'utils' and '_yaml'
modules.
-rw-r--r-- | buildstream/_cachekey.py | 43 | ||||
-rw-r--r-- | buildstream/_context.py | 4 | ||||
-rw-r--r-- | buildstream/_project.py | 3 | ||||
-rw-r--r-- | buildstream/element.py | 3 | ||||
-rw-r--r-- | buildstream/utils.py | 20 |
5 files changed, 50 insertions, 23 deletions
diff --git a/buildstream/_cachekey.py b/buildstream/_cachekey.py new file mode 100644 index 000000000..3d0c19b44 --- /dev/null +++ b/buildstream/_cachekey.py @@ -0,0 +1,43 @@ +#!/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() diff --git a/buildstream/_context.py b/buildstream/_context.py index 08ce83adf..6d0a19bde 100644 --- a/buildstream/_context.py +++ b/buildstream/_context.py @@ -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 diff --git a/buildstream/_project.py b/buildstream/_project.py index fc8fa06f9..50f5d9c57 100644 --- a/buildstream/_project.py +++ b/buildstream/_project.py @@ -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 diff --git a/buildstream/element.py b/buildstream/element.py index ef5e9ea76..8c7fad71d 100644 --- a/buildstream/element.py +++ b/buildstream/element.py @@ -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(), diff --git a/buildstream/utils.py b/buildstream/utils.py index 9c65d8e74..2b6c4517c 100644 --- a/buildstream/utils.py +++ b/buildstream/utils.py @@ -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' |