diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-11-06 20:24:29 +0900 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-11-06 20:31:31 +0900 |
commit | a97c0fd86a17db72edb26f91bab9364da533fd21 (patch) | |
tree | 0c8efc0154e9533429a06d9ffd61b4b64a9fa640 | |
parent | 79f9f065d7f22039ec189d59f557bbe00123eacd (diff) | |
download | buildstream-a97c0fd86a17db72edb26f91bab9364da533fd21.tar.gz |
refactoring: Made Project option private
This required adding two new APIs to make up for it on the Source
o get_project_directory()
Added here because elements should not be accessing external
resources, Sources needed for local files and GPG keys and such
o translate_url()
Used by sources to mish-mash the project aliases and create
real urls.
-rw-r--r-- | buildstream/__init__.py | 1 | ||||
-rw-r--r-- | buildstream/_artifactcache/ostreecache.py | 4 | ||||
-rw-r--r-- | buildstream/_artifactcache/tarcache.py | 8 | ||||
-rw-r--r-- | buildstream/_frontend/main.py | 3 | ||||
-rw-r--r-- | buildstream/_project.py (renamed from buildstream/project.py) | 75 | ||||
-rw-r--r-- | buildstream/_scheduler/trackqueue.py | 2 | ||||
-rw-r--r-- | buildstream/element.py | 20 | ||||
-rw-r--r-- | buildstream/plugin.py | 15 | ||||
-rw-r--r-- | buildstream/plugins/sources/_downloadablefilesource.py | 4 | ||||
-rw-r--r-- | buildstream/plugins/sources/bzr.py | 2 | ||||
-rw-r--r-- | buildstream/plugins/sources/git.py | 3 | ||||
-rw-r--r-- | buildstream/plugins/sources/local.py | 4 | ||||
-rw-r--r-- | buildstream/plugins/sources/ostree.py | 5 | ||||
-rw-r--r-- | buildstream/plugins/sources/patch.py | 4 | ||||
-rw-r--r-- | buildstream/source.py | 31 | ||||
-rw-r--r-- | tests/pipeline/load.py | 3 | ||||
-rw-r--r-- | tests/plugins/pipeline.py | 3 | ||||
-rw-r--r-- | tests/project/plugins.py | 3 | ||||
-rw-r--r-- | tests/project/project.py | 3 | ||||
-rw-r--r-- | tests/variables/variables.py | 3 |
20 files changed, 105 insertions, 91 deletions
diff --git a/buildstream/__init__.py b/buildstream/__init__.py index bb7a338a4..b3751a6e3 100644 --- a/buildstream/__init__.py +++ b/buildstream/__init__.py @@ -20,7 +20,6 @@ # Core components from .context import Context -from .project import Project from .sandbox import Sandbox, SandboxFlags # Plugin auther facing APIs diff --git a/buildstream/_artifactcache/ostreecache.py b/buildstream/_artifactcache/ostreecache.py index 431dd565a..e2e623e78 100644 --- a/buildstream/_artifactcache/ostreecache.py +++ b/buildstream/_artifactcache/ostreecache.py @@ -35,7 +35,7 @@ from .pushreceive import PushException def buildref(element, key): - project = element.get_project() + project = element._get_project() # Normalize ostree ref unsupported chars valid_chars = string.digits + string.ascii_letters + '-._' @@ -180,7 +180,7 @@ class OSTreeCache(ArtifactCache): if not rev: raise ArtifactError("Artifact missing for {}".format(ref)) - dest = os.path.join(self.extractdir, element.get_project().name, element.normal_name, rev) + dest = os.path.join(self.extractdir, element._get_project().name, element.normal_name, rev) if os.path.isdir(dest): # artifact has already been extracted return dest diff --git a/buildstream/_artifactcache/tarcache.py b/buildstream/_artifactcache/tarcache.py index 3d4c925f0..90821874e 100644 --- a/buildstream/_artifactcache/tarcache.py +++ b/buildstream/_artifactcache/tarcache.py @@ -32,7 +32,7 @@ from . import ArtifactCache def buildref(element, key): - project = element.get_project() + project = element._get_project() # Normalize ostree ref unsupported chars element_name = element.normal_name.replace('+', 'X') @@ -42,7 +42,7 @@ def buildref(element, key): def tarpath(element, key): - project = element.get_project() + project = element._get_project() return os.path.join(project.name, element.normal_name, key + '.tar.bz2') @@ -283,7 +283,7 @@ class TarCache(ArtifactCache): ref = tarpath(element, element._get_cache_key_for_build()) weak_ref = tarpath(element, element._get_cache_key(strength=_KeyStrength.WEAK)) - os.makedirs(os.path.join(self.tardir, element.get_project().name, element.normal_name), exist_ok=True) + os.makedirs(os.path.join(self.tardir, element._get_project().name, element.normal_name), exist_ok=True) with utils._tempdir() as temp: refdir = os.path.join(temp, element._get_cache_key_for_build()) @@ -330,7 +330,7 @@ class TarCache(ArtifactCache): with utils._tempdir(dir=self.extractdir) as tmpdir: Tar.extract(os.path.join(self.tardir, path), tmpdir) - os.makedirs(os.path.join(self.extractdir, element.get_project().name, element.normal_name), + os.makedirs(os.path.join(self.extractdir, element._get_project().name, element.normal_name), exist_ok=True) try: os.rename(os.path.join(tmpdir, key), dest) diff --git a/buildstream/_frontend/main.py b/buildstream/_frontend/main.py index 33e900d5c..3ac6b5222 100644 --- a/buildstream/_frontend/main.py +++ b/buildstream/_frontend/main.py @@ -25,9 +25,10 @@ from contextlib import contextmanager from blessings import Terminal # Import buildstream public symbols -from .. import Context, Project, Scope, Consistency +from .. import Context, Scope, Consistency # Import various buildstream internals +from .._project import Project from .._exceptions import BstError, LoadError from .._message import MessageType, unconditional_messages from .._pipeline import Pipeline, PipelineError diff --git a/buildstream/project.py b/buildstream/_project.py index d83159711..46f271f5b 100644 --- a/buildstream/project.py +++ b/buildstream/_project.py @@ -18,15 +18,6 @@ # Authors: # Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> -""" -Project -======= - -The :class:`.Project` object holds all of the project settings from -the project configuration file including the project directory it -was loaded from. -""" - import os import multiprocessing # for cpu_count() from collections import Mapping @@ -37,42 +28,43 @@ from ._profile import Topics, profile_start, profile_end from ._exceptions import LoadError, LoadErrorReason from ._options import OptionPool -BST_FORMAT_VERSION = 0 -"""The base BuildStream format version -This version is bumped whenever enhancements are made -to the ``project.conf`` format or the format in general. -""" +# The base BuildStream format version +# +# This version is bumped whenever enhancements are made +# to the ``project.conf`` format or the format in general. +# +BST_FORMAT_VERSION = 0 +# The base BuildStream artifact version +# +# The artifact version changes whenever the cache key +# calculation algorithm changes in an incompatible way +# or if buildstream was changed in a way which can cause +# the same cache key to produce something that is no longer +# the same. BST_ARTIFACT_VERSION = 0 -"""The base BuildStream artifact version - -The artifact version changes whenever the cache key -calculation algorithm changes in an incompatible way -or if buildstream was changed in a way which can cause -the same cache key to produce something that is no longer -the same. -""" # The separator we use for user specified aliases _ALIAS_SEPARATOR = ':' +# Project() +# +# The Project Configuration +# class Project(): - """Project() - The Project Configuration - """ def __init__(self, directory, context): + # The project name self.name = None - """str: The project name""" + # The project directory self.directory = os.path.abspath(directory) - """str: The project directory""" + # Absolute path to where elements are loaded from within the project self.element_path = None - """str: Absolute path to where elements are loaded from within the project""" self._context = context # The invocation Context self._variables = {} # The default variables overridden with project wide overrides @@ -91,20 +83,21 @@ class Project(): self._load() profile_end(Topics.LOAD_PROJECT, self.directory.replace(os.sep, '-')) + # translate_url(): + # + # Translates the given url which may be specified with an alias + # into a fully qualified url. + # + # Args: + # url (str): A url, which may be using an alias + # + # Returns: + # str: The fully qualified url, with aliases resolved + # + # This method is provided for :class:`.Source` objects to resolve + # fully qualified urls based on the shorthand which is allowed + # to be specified in the YAML def translate_url(self, url): - """Translates the given url which may be specified with an alias - into a fully qualified url. - - Args: - url (str): A url, which may be using an alias - - Returns: - str: The fully qualified url, with aliases resolved - - This method is provided for :class:`.Source` objects to resolve - fully qualified urls based on the shorthand which is allowed - to be specified in the YAML - """ if url and _ALIAS_SEPARATOR in url: url_alias, url_body = url.split(_ALIAS_SEPARATOR, 1) alias_url = self._aliases.get(url_alias) diff --git a/buildstream/_scheduler/trackqueue.py b/buildstream/_scheduler/trackqueue.py index e45fb2db2..3159a3adc 100644 --- a/buildstream/_scheduler/trackqueue.py +++ b/buildstream/_scheduler/trackqueue.py @@ -62,7 +62,7 @@ class TrackQueue(Queue): if source._set_ref(new_ref, source._Source__origin_node): changed = True - project = source.get_project() + project = source._get_project() toplevel = source._Source__origin_toplevel filename = source._Source__origin_filename fullname = os.path.join(project.element_path, filename) diff --git a/buildstream/element.py b/buildstream/element.py index 6c55026ee..86dd2b8dc 100644 --- a/buildstream/element.py +++ b/buildstream/element.py @@ -39,7 +39,7 @@ from . import _yaml from ._variables import Variables from ._exceptions import BstError, LoadError, LoadErrorReason, ImplError from . import Plugin, Consistency -from .project import BST_ARTIFACT_VERSION as BST_CORE_ARTIFACT_VERSION +from ._project import BST_ARTIFACT_VERSION as BST_CORE_ARTIFACT_VERSION from . import SandboxFlags from . import utils from . import _signals @@ -856,7 +856,7 @@ class Element(Plugin): } context = self.get_context() - project = self.get_project() + project = self._get_project() return utils._generate_key({ 'artifact-version': "{}.{}".format(BST_CORE_ARTIFACT_VERSION, self.BST_ARTIFACT_VERSION), @@ -1202,7 +1202,7 @@ class Element(Plugin): # This matches the order in which things are stored in the artifact cache # def _logfile(self, action_name, pid=None): - project = self.get_project() + project = self._get_project() context = self.get_context() key = self._get_display_key() if pid is None: @@ -1411,7 +1411,7 @@ class Element(Plugin): # (bool): Whether the build plan is strict for this element # def _get_strict(self): - project = self.get_project() + project = self._get_project() context = self.get_context() return context._get_strict(project.name) @@ -1421,7 +1421,7 @@ class Element(Plugin): @contextmanager def __sandbox(self, directory, stdout=None, stderr=None): context = self.get_context() - project = self.get_project() + project = self._get_project() platform = Platform.get_platform() if directory is not None and os.path.exists(directory): @@ -1443,7 +1443,7 @@ class Element(Plugin): utils._force_rmtree(rootdir) def __compose_default_splits(self, defaults): - project = self.get_project() + project = self._get_project() project_splits = _yaml.node_chain_copy(project._splits) element_public = _yaml.node_get(defaults, Mapping, 'public', default_value={}) @@ -1476,7 +1476,7 @@ class Element(Plugin): # Override the element's defaults with element specific # overrides from the project.conf - project = self.get_project() + project = self._get_project() elements = project._elements overrides = elements.get(self.get_kind()) if overrides: @@ -1490,7 +1490,7 @@ class Element(Plugin): # creating sandboxes for this element # def __extract_environment(self, meta): - project = self.get_project() + project = self._get_project() default_env = _yaml.node_get(self.__defaults, Mapping, 'environment', default_value={}) environment = _yaml.node_chain_copy(project._environment) @@ -1506,7 +1506,7 @@ class Element(Plugin): return final_env def __extract_env_nocache(self, meta): - project = self.get_project() + project = self._get_project() project_nocache = project._env_nocache default_nocache = _yaml.node_get(self.__defaults, list, 'environment-nocache', default_value=[]) element_nocache = meta.env_nocache @@ -1522,7 +1522,7 @@ class Element(Plugin): # substituting command strings to be run in the sandbox # def __extract_variables(self, meta): - project = self.get_project() + project = self._get_project() default_vars = _yaml.node_get(self.__defaults, Mapping, 'variables', default_value={}) variables = _yaml.node_chain_copy(project._variables) diff --git a/buildstream/plugin.py b/buildstream/plugin.py index abbed7dca..56bee90c1 100644 --- a/buildstream/plugin.py +++ b/buildstream/plugin.py @@ -184,14 +184,6 @@ class Plugin(): """ return self.__context - def get_project(self): - """Fetches the project - - Returns: - (object): The :class:`.Project` - """ - return self.__project - def node_items(self, node): """Iterate over a dictionary loaded from YAML @@ -570,6 +562,13 @@ class Plugin(): # Private Methods used in BuildStream # ############################################################# + # _get_project() + # + # Fetches the project object associated with this plugin + # + def _get_project(self): + return self.__project + # _get_unique_id(): # # Fetch the plugin's unique identifier diff --git a/buildstream/plugins/sources/_downloadablefilesource.py b/buildstream/plugins/sources/_downloadablefilesource.py index 90d3709c2..a2ed366ad 100644 --- a/buildstream/plugins/sources/_downloadablefilesource.py +++ b/buildstream/plugins/sources/_downloadablefilesource.py @@ -13,11 +13,9 @@ class DownloadableFileSource(Source): COMMON_CONFIG_KEYS = Source.COMMON_CONFIG_KEYS + ['url', 'ref'] def configure(self, node): - project = self.get_project() - self.original_url = self.node_get_member(node, str, 'url') self.ref = self.node_get_member(node, str, 'ref', '') or None - self.url = project.translate_url(self.original_url) + self.url = self.translate_url(self.original_url) def preflight(self): return diff --git a/buildstream/plugins/sources/bzr.py b/buildstream/plugins/sources/bzr.py index 82ac711e7..679a3436c 100644 --- a/buildstream/plugins/sources/bzr.py +++ b/buildstream/plugins/sources/bzr.py @@ -63,7 +63,7 @@ class BzrSource(Source): self.original_url = self.node_get_member(node, str, 'url') self.tracking = self.node_get_member(node, str, 'track') self.ref = self.node_get_member(node, str, 'ref', '') or None - self.url = self.get_project().translate_url(self.original_url) + self.url = self.translate_url(self.original_url) def preflight(self): # Check if bzr is installed, get the binary at the same time. diff --git a/buildstream/plugins/sources/git.py b/buildstream/plugins/sources/git.py index b3e22494a..afbca252c 100644 --- a/buildstream/plugins/sources/git.py +++ b/buildstream/plugins/sources/git.py @@ -78,10 +78,9 @@ class GitMirror(): def __init__(self, source, path, url, ref): - project = source.get_project() self.source = source self.path = path - self.url = project.translate_url(url) + self.url = source.translate_url(url) self.ref = ref self.mirror = os.path.join(source.get_mirror_directory(), utils.url_directory_name(self.url)) diff --git a/buildstream/plugins/sources/local.py b/buildstream/plugins/sources/local.py index 9f02b0ed4..02486dffb 100644 --- a/buildstream/plugins/sources/local.py +++ b/buildstream/plugins/sources/local.py @@ -42,12 +42,10 @@ from buildstream import utils class LocalSource(Source): def configure(self, node): - project = self.get_project() - self.node_validate(node, ['path'] + Source.COMMON_CONFIG_KEYS) self.path = self.node_get_member(node, str, 'path') - self.fullpath = os.path.join(project.directory, self.path) + self.fullpath = os.path.join(self.get_project_directory(), self.path) def preflight(self): # Check if the configured file or directory really exists diff --git a/buildstream/plugins/sources/ostree.py b/buildstream/plugins/sources/ostree.py index 1875384e6..6f2562fc0 100644 --- a/buildstream/plugins/sources/ostree.py +++ b/buildstream/plugins/sources/ostree.py @@ -61,12 +61,11 @@ from buildstream._ostree import OSTreeError class OSTreeSource(Source): def configure(self, node): - project = self.get_project() self.node_validate(node, ['url', 'ref', 'track', 'gpg-key'] + Source.COMMON_CONFIG_KEYS) self.original_url = self.node_get_member(node, str, 'url') - self.url = project.translate_url(self.original_url) + self.url = self.translate_url(self.original_url) self.ref = self.node_get_member(node, str, 'ref', '') or None self.tracking = self.node_get_member(node, str, 'track', '') or None self.mirror = os.path.join(self.get_mirror_directory(), @@ -76,7 +75,7 @@ class OSTreeSource(Source): self.gpg_key = self.node_get_member(node, str, 'gpg-key', '') or None self.gpg_key_path = None if self.gpg_key is not None: - self.gpg_key_path = os.path.join(project.directory, self.gpg_key) + self.gpg_key_path = os.path.join(self.get_project_directory(), self.gpg_key) # Our OSTree repo handle self.repo = None diff --git a/buildstream/plugins/sources/patch.py b/buildstream/plugins/sources/patch.py index ab527ec69..9da6ade40 100644 --- a/buildstream/plugins/sources/patch.py +++ b/buildstream/plugins/sources/patch.py @@ -46,11 +46,9 @@ from buildstream import utils class PatchSource(Source): def configure(self, node): - project = self.get_project() - self.path = self.node_get_member(node, str, "path") self.strip_level = self.node_get_member(node, int, "strip-level", 1) - self.fullpath = os.path.join(project.directory, self.path) + self.fullpath = os.path.join(self.get_project_directory(), self.path) def preflight(self): # Check if the configured file really exists diff --git a/buildstream/source.py b/buildstream/source.py index 629d41a6b..ed9ff3aaa 100644 --- a/buildstream/source.py +++ b/buildstream/source.py @@ -104,6 +104,31 @@ class Source(Plugin): os.makedirs(directory, exist_ok=True) return directory + def translate_url(self, url): + """Translates the given url which may be specified with an alias + into a fully qualified url. + + Args: + url (str): A url, which may be using an alias + + Returns: + str: The fully qualified url, with aliases resolved + """ + project = self._get_project() + return project.translate_url(url) + + def get_project_directory(self): + """Fetch the project base directory + + This is useful for sources which need to load resources + stored somewhere inside the project. + + Returns: + str: The project base directory + """ + project = self._get_project() + return project.directory + @contextmanager def tempdir(self): """Context manager for working in a temporary directory @@ -220,7 +245,7 @@ class Source(Plugin): # A workspace is considered inconsistent in the case # that it's directory went missing # - fullpath = os.path.join(self.get_project().directory, self.__workspace) + fullpath = os.path.join(self.get_project_directory(), self.__workspace) if not os.path.exists(fullpath): self.__consistency = Consistency.INCONSISTENT @@ -331,7 +356,7 @@ class Source(Plugin): # Stage the workspace # def _stage_workspace(self, directory): - fullpath = os.path.join(self.get_project().directory, self.__workspace) + fullpath = os.path.join(self.get_project_directory(), self.__workspace) with self.timed_activity("Staging local files at {}".format(self.__workspace)): if os.path.isdir(fullpath): @@ -342,7 +367,7 @@ class Source(Plugin): # Get a unique key for the workspace def _get_workspace_key(self): - fullpath = os.path.join(self.get_project().directory, self.__workspace) + fullpath = os.path.join(self.get_project_directory(), self.__workspace) # Get a list of tuples of the the project relative paths and fullpaths if os.path.isdir(fullpath): diff --git a/tests/pipeline/load.py b/tests/pipeline/load.py index 1d5319c45..a0b6d7ae3 100644 --- a/tests/pipeline/load.py +++ b/tests/pipeline/load.py @@ -1,7 +1,8 @@ import os import pytest -from buildstream import Context, Project, Scope +from buildstream import Context, Scope +from buildstream._project import Project from buildstream._pipeline import Pipeline from buildstream._platform import Platform diff --git a/tests/plugins/pipeline.py b/tests/plugins/pipeline.py index f5c9a0c8f..6fac82b89 100644 --- a/tests/plugins/pipeline.py +++ b/tests/plugins/pipeline.py @@ -1,7 +1,8 @@ import os import pytest -from buildstream import Context, Project, Scope +from buildstream import Context, Scope +from buildstream._project import Project from buildstream._exceptions import PluginError from buildstream._pipeline import Pipeline from buildstream._platform import Platform diff --git a/tests/project/plugins.py b/tests/project/plugins.py index 785b42418..424548082 100644 --- a/tests/project/plugins.py +++ b/tests/project/plugins.py @@ -1,7 +1,8 @@ import os import pytest -from buildstream import Context, Project, Scope +from buildstream import Context, Scope +from buildstream._project import Project from buildstream._pipeline import Pipeline from buildstream._platform import Platform diff --git a/tests/project/project.py b/tests/project/project.py index 430e15b11..32a264c6d 100644 --- a/tests/project/project.py +++ b/tests/project/project.py @@ -1,7 +1,8 @@ import os import pytest -from buildstream import Project, Context +from buildstream import Context +from buildstream._project import Project from buildstream._exceptions import LoadError, LoadErrorReason DATA_DIR = os.path.join( diff --git a/tests/variables/variables.py b/tests/variables/variables.py index 8f9fd2750..51af7e27b 100644 --- a/tests/variables/variables.py +++ b/tests/variables/variables.py @@ -1,7 +1,8 @@ import os import pytest -from buildstream import Context, Project, BuildElement +from buildstream import Context, BuildElement +from buildstream._project import Project from buildstream._pipeline import Pipeline from buildstream._platform import Platform |