diff options
author | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2019-03-25 08:15:20 +0000 |
---|---|---|
committer | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2019-03-27 21:26:07 +0000 |
commit | f4e4b76f0141e6d82ea7f6a1122ddb143dbaf6a3 (patch) | |
tree | e1095b97014c77ecc636d7ab9b9400e239ba999f | |
parent | 3816dcf8901b06f2b9c0153e5b5fe394acf104a5 (diff) | |
download | buildstream-f4e4b76f0141e6d82ea7f6a1122ddb143dbaf6a3.tar.gz |
_cachekey.py _artifactelement.py: Create new is_key() and use it
To more cleanly check if a given string could be a cache key, create
a new _cachekey.is_key() function and use that in _artifactelement's
verify_artifact_ref() function.
Signed-off-by: Daniel Silverstone <daniel.silverstone@codethink.co.uk>
-rw-r--r-- | buildstream/_artifactelement.py | 4 | ||||
-rw-r--r-- | buildstream/_cachekey.py | 25 |
2 files changed, 27 insertions, 2 deletions
diff --git a/buildstream/_artifactelement.py b/buildstream/_artifactelement.py index 8210b4d97..d65d46173 100644 --- a/buildstream/_artifactelement.py +++ b/buildstream/_artifactelement.py @@ -83,8 +83,8 @@ class ArtifactElement(Element): def verify_artifact_ref(ref): try: project, element, key = ref.split('/', 2) # This will raise a Value error if unable to split - # Explicitly raise a ValueError if the key lenght is not as expected - if len(key) != len(_cachekey.generate_key(_yaml.new_empty_node())): + # Explicitly raise a ValueError if the key length is not as expected + if not _cachekey.is_key(key): raise ValueError except ValueError: raise ArtifactElementError("Artifact: {} is not of the expected format".format(ref)) diff --git a/buildstream/_cachekey.py b/buildstream/_cachekey.py index 953263aa9..e56b582fa 100644 --- a/buildstream/_cachekey.py +++ b/buildstream/_cachekey.py @@ -24,6 +24,31 @@ import ujson from . import _yaml +# Internal record of the size of a cache key +_CACHEKEY_SIZE = len(hashlib.sha256().hexdigest()) + + +# Hex digits +_HEX_DIGITS = "0123456789abcdef" + + +# is_key() +# +# Check if the passed in string *could be* a cache key. This basically checks +# that the length matches a sha256 hex digest, and that the string does not +# contain any non-hex characters and is fully lower case. +# +# Args: +# key (str): The string to check +# +# Returns: +# (bool): Whether or not `key` could be a cache key +# +def is_key(key): + if len(key) != _CACHEKEY_SIZE: + return False + return not any(ch not in _HEX_DIGITS for ch in key) + # generate_key() # |