summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Ennis <james.ennis@codethink.com>2019-01-23 11:24:29 +0000
committerJames Ennis <james.ennis@codethink.com>2019-02-13 09:35:45 +0000
commit55c15f8f7462c4795760e84ae63ac6f5b89dd908 (patch)
tree0fc97c5294bd40a57abb409b669b7a64dfd34bab
parentddf642e1ecfff220651792a55e3949fe15cb72bf (diff)
downloadbuildstream-55c15f8f7462c4795760e84ae63ac6f5b89dd908.tar.gz
_artifactelement.py: New ArtifactElement object (derived from Element)
This object should be used when we want to handle artifact refs directly from the command line. An ArtifactElementError has also been added to _exceptions.py
-rw-r--r--buildstream/_artifactelement.py88
-rw-r--r--buildstream/_exceptions.py9
2 files changed, 97 insertions, 0 deletions
diff --git a/buildstream/_artifactelement.py b/buildstream/_artifactelement.py
new file mode 100644
index 000000000..a88e83aab
--- /dev/null
+++ b/buildstream/_artifactelement.py
@@ -0,0 +1,88 @@
+#
+# Copyright (C) 2019 Bloomberg Finance LP
+#
+# 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:
+# James Ennis <james.ennis@codethink.co.uk>
+from . import Element
+from . import _cachekey
+from ._exceptions import ArtifactElementError
+from ._loader.metaelement import MetaElement
+
+
+# ArtifactElement()
+#
+# Object to be used for directly processing an artifact
+#
+# Args:
+# context (Context): The Context object
+# ref (str): The artifact ref
+#
+class ArtifactElement(Element):
+ def __init__(self, context, ref):
+ _, element, key = verify_artifact_ref(ref)
+
+ self._ref = ref
+ self._key = key
+
+ project = context.get_toplevel_project()
+ meta = MetaElement(project, element) # NOTE element has no .bst suffix
+ plugin_conf = None
+
+ super().__init__(context, project, meta, plugin_conf)
+
+ # Override Element.get_artifact_name()
+ def get_artifact_name(self, key=None):
+ return self._ref
+
+ # Dummy configure method
+ def configure(self, node):
+ pass
+
+ # Dummy preflight method
+ def preflight(self):
+ pass
+
+ # Override Element._calculate_cache_key
+ def _calculate_cache_key(self, dependencies=None):
+ return self._key
+
+
+# verify_artifact_ref()
+#
+# Verify that a ref string matches the format of an artifact
+#
+# Args:
+# ref (str): The artifact ref
+#
+# Returns:
+# project (str): The project's name
+# element (str): The element's name
+# key (str): The cache key
+#
+# Raises:
+# ArtifactElementError if the ref string does not match
+# the expected format
+#
+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({})):
+ raise ValueError
+ except ValueError:
+ raise ArtifactElementError("Artifact: {} is not of the expected format".format(ref))
+
+ return project, element, key
diff --git a/buildstream/_exceptions.py b/buildstream/_exceptions.py
index 6d8ea6d38..0797e7207 100644
--- a/buildstream/_exceptions.py
+++ b/buildstream/_exceptions.py
@@ -344,3 +344,12 @@ class AppError(BstError):
#
class SkipJob(Exception):
pass
+
+
+# ArtifactElementError
+#
+# Raised when errors are encountered by artifact elements
+#
+class ArtifactElementError(BstError):
+ def __init__(self, message, *, detail=None, reason=None):
+ super().__init__(message, detail=detail, domain=ErrorDomain.ELEMENT, reason=reason)