summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2019-01-21 13:58:55 +0100
committerJürg Billeter <j@bitron.ch>2019-01-22 08:08:18 +0100
commitc88962496b5bdf1beb517880ab1378629d1f2480 (patch)
treef84bd61fee3f7eae1672330199c12c000f859b05
parent811ba08bd08dbb9775cb67f08fcfa4c7afb5a103 (diff)
downloadbuildstream-c88962496b5bdf1beb517880ab1378629d1f2480.tar.gz
_project.py: Add get_default_target() and get_default_targets() methods
_frontend/cli.py: Use new methods. Based on patches by Phillip Smyth.
-rw-r--r--buildstream/_context.py16
-rw-r--r--buildstream/_frontend/cli.py90
-rw-r--r--buildstream/_project.py47
3 files changed, 113 insertions, 40 deletions
diff --git a/buildstream/_context.py b/buildstream/_context.py
index 8b9f47bd6..f14f6b746 100644
--- a/buildstream/_context.py
+++ b/buildstream/_context.py
@@ -32,7 +32,7 @@ from ._message import Message, MessageType
from ._profile import Topics, profile_start, profile_end
from ._artifactcache import ArtifactCache
from ._cas import CASCache
-from ._workspaces import Workspaces, WorkspaceProjectCache, WORKSPACE_PROJECT_FILE
+from ._workspaces import Workspaces, WorkspaceProjectCache
from .plugin import _plugin_lookup
from .sandbox import SandboxRemote
@@ -657,20 +657,6 @@ class Context():
self._cascache = CASCache(self.artifactdir)
return self._cascache
- # guess_element()
- #
- # Attempts to interpret which element the user intended to run commands on
- #
- # Returns:
- # (str) The name of the element, or None if no element can be guessed
- def guess_element(self):
- workspace_project_dir, _ = utils._search_upward_for_files(self._directory, [WORKSPACE_PROJECT_FILE])
- if workspace_project_dir:
- workspace_project = self._workspace_project_cache.get(workspace_project_dir)
- return workspace_project.get_default_element()
- else:
- return None
-
# _node_get_option_str()
#
diff --git a/buildstream/_frontend/cli.py b/buildstream/_frontend/cli.py
index 5e8aee51b..6674b14a6 100644
--- a/buildstream/_frontend/cli.py
+++ b/buildstream/_frontend/cli.py
@@ -342,7 +342,15 @@ def init(app, project_name, format_version, element_path, force):
type=click.Path(readable=False))
@click.pass_obj
def build(app, elements, all_, track_, track_save, track_all, track_except, track_cross_junctions):
- """Build elements in a pipeline"""
+ """Build elements in a pipeline
+
+ Specifying no elements will result in building the default targets
+ of the project. If no default targets are configured, all project
+ elements will be built.
+
+ When this command is executed from a workspace directory, the default
+ is to build the workspace element.
+ """
if (track_except or track_cross_junctions) and not (track_ or track_all):
click.echo("ERROR: The --track-except and --track-cross-junctions options "
@@ -354,9 +362,7 @@ def build(app, elements, all_, track_, track_save, track_all, track_except, trac
with app.initialized(session_name="Build"):
if not elements:
- guessed_target = app.context.guess_element()
- if guessed_target:
- elements = (guessed_target,)
+ elements = app.project.get_default_targets()
if track_all:
track_ = elements
@@ -383,6 +389,13 @@ def build(app, elements, all_, track_, track_save, track_all, track_except, trac
def pull(app, elements, deps, remote):
"""Pull a built artifact from the configured remote artifact cache.
+ Specifying no elements will result in pulling the default targets
+ of the project. If no default targets are configured, all project
+ elements will be pulled.
+
+ When this command is executed from a workspace directory, the default
+ is to pull the workspace element.
+
By default the artifact will be pulled one of the configured caches
if possible, following the usual priority order. If the `--remote` flag
is given, only the specified cache will be queried.
@@ -396,9 +409,7 @@ def pull(app, elements, deps, remote):
with app.initialized(session_name="Pull"):
if not elements:
- guessed_target = app.context.guess_element()
- if guessed_target:
- elements = (guessed_target,)
+ elements = app.project.get_default_targets()
app.stream.pull(elements, selection=deps, remote=remote)
@@ -418,6 +429,13 @@ def pull(app, elements, deps, remote):
def push(app, elements, deps, remote):
"""Push a built artifact to a remote artifact cache.
+ Specifying no elements will result in pushing the default targets
+ of the project. If no default targets are configured, all project
+ elements will be pushed.
+
+ When this command is executed from a workspace directory, the default
+ is to push the workspace element.
+
The default destination is the highest priority configured cache. You can
override this by passing a different cache URL with the `--remote` flag.
@@ -433,9 +451,7 @@ def push(app, elements, deps, remote):
"""
with app.initialized(session_name="Push"):
if not elements:
- guessed_target = app.context.guess_element()
- if guessed_target:
- elements = (guessed_target,)
+ elements = app.project.get_default_targets()
app.stream.push(elements, selection=deps, remote=remote)
@@ -462,6 +478,13 @@ def push(app, elements, deps, remote):
def show(app, elements, deps, except_, order, format_):
"""Show elements in the pipeline
+ Specifying no elements will result in showing the default targets
+ of the project. If no default targets are configured, all project
+ elements will be shown.
+
+ When this command is executed from a workspace directory, the default
+ is to show the workspace element.
+
By default this will show all of the dependencies of the
specified target element.
@@ -508,9 +531,7 @@ def show(app, elements, deps, except_, order, format_):
"""
with app.initialized():
if not elements:
- guessed_target = app.context.guess_element()
- if guessed_target:
- elements = (guessed_target,)
+ elements = app.project.get_default_targets()
dependencies = app.stream.load_selection(elements,
selection=deps,
@@ -550,6 +571,9 @@ def show(app, elements, deps, except_, order, format_):
def shell(app, element, sysroot, mount, isolate, build_, cli_buildtree, command):
"""Run a command in the target element's sandbox environment
+ When this command is executed from a workspace directory, the default
+ is to shell into the workspace element.
+
This will stage a temporary sysroot for running the target
element, assuming it has already been built and all required
artifacts are in the local cache.
@@ -583,7 +607,7 @@ def shell(app, element, sysroot, mount, isolate, build_, cli_buildtree, command)
with app.initialized():
if not element:
- element = app.context.guess_element()
+ element = app.project.get_default_target()
if not element:
raise AppError('Missing argument "ELEMENT".')
@@ -648,6 +672,9 @@ def shell(app, element, sysroot, mount, isolate, build_, cli_buildtree, command)
@click.pass_obj
def checkout(app, element, location, force, deps, integrate, hardlinks, tar):
"""Checkout a built artifact to the specified location
+
+ When this command is executed from a workspace directory, the default
+ is to checkout the artifact of the workspace element.
"""
from ..element import Scope
@@ -673,7 +700,7 @@ def checkout(app, element, location, force, deps, integrate, hardlinks, tar):
with app.initialized():
if not element:
- element = app.context.guess_element()
+ element = app.project.get_default_target()
if not element:
raise AppError('Missing argument "ELEMENT".')
@@ -714,6 +741,13 @@ def source():
def source_fetch(app, elements, deps, track_, except_, track_cross_junctions):
"""Fetch sources required to build the pipeline
+ Specifying no elements will result in fetching the default targets
+ of the project. If no default targets are configured, all project
+ elements will be fetched.
+
+ When this command is executed from a workspace directory, the default
+ is to fetch the workspace element.
+
By default this will only try to fetch sources which are
required for the build plan of the specified target element,
omitting sources for any elements which are already built
@@ -739,9 +773,7 @@ def source_fetch(app, elements, deps, track_, except_, track_cross_junctions):
with app.initialized(session_name="Fetch"):
if not elements:
- guessed_target = app.context.guess_element()
- if guessed_target:
- elements = (guessed_target,)
+ elements = app.project.get_default_targets()
app.stream.fetch(elements,
selection=deps,
@@ -769,6 +801,15 @@ def source_track(app, elements, deps, except_, cross_junctions):
"""Consults the specified tracking branches for new versions available
to build and updates the project with any newly available references.
+ Specifying no elements will result in tracking the default targets
+ of the project. If no default targets are configured, all project
+ elements will be tracked.
+
+ When this command is executed from a workspace directory, the default
+ is to track the workspace element.
+
+ If no default is declared, all elements in the project will be tracked
+
By default this will track just the specified element, but you can also
update a whole tree of dependencies in one go.
@@ -780,9 +821,7 @@ def source_track(app, elements, deps, except_, cross_junctions):
"""
with app.initialized(session_name="Track"):
if not elements:
- guessed_target = app.context.guess_element()
- if guessed_target:
- elements = (guessed_target,)
+ elements = app.project.get_default_targets()
# Substitute 'none' for 'redirect' so that element redirections
# will be done
@@ -818,6 +857,9 @@ def source_track(app, elements, deps, except_, cross_junctions):
def source_checkout(app, element, location, force, deps, fetch_, except_,
tar, build_scripts):
"""Checkout sources of an element to the specified location
+
+ When this command is executed from a workspace directory, the default
+ is to checkout the sources of the workspace element.
"""
if not element and not location:
click.echo("ERROR: LOCATION is not specified", err=True)
@@ -830,7 +872,7 @@ def source_checkout(app, element, location, force, deps, fetch_, except_,
with app.initialized():
if not element:
- element = app.context.guess_element()
+ element = app.project.get_default_target()
if not element:
raise AppError('Missing argument "ELEMENT".')
@@ -896,7 +938,7 @@ def workspace_close(app, remove_dir, all_, elements):
if not (all_ or elements):
# NOTE: I may need to revisit this when implementing multiple projects
# opening one workspace.
- element = app.context.guess_element()
+ element = app.project.get_default_target()
if element:
elements = (element,)
else:
@@ -957,7 +999,7 @@ def workspace_reset(app, soft, track_, all_, elements):
with app.initialized():
if not (all_ or elements):
- element = app.context.guess_element()
+ element = app.project.get_default_target()
if element:
elements = (element,)
else:
diff --git a/buildstream/_project.py b/buildstream/_project.py
index 1492fde77..3ec141d58 100644
--- a/buildstream/_project.py
+++ b/buildstream/_project.py
@@ -104,6 +104,9 @@ class Project():
# Absolute path to where elements are loaded from within the project
self.element_path = None
+ # Default target elements
+ self._default_targets = None
+
# ProjectRefs for the main refs and also for junctions
self.refs = ProjectRefs(self.directory, 'project.refs')
self.junction_refs = ProjectRefs(self.directory, 'junction.refs')
@@ -228,7 +231,7 @@ class Project():
'element-path', 'variables',
'environment', 'environment-nocache',
'split-rules', 'elements', 'plugins',
- 'aliases', 'name',
+ 'aliases', 'name', 'defaults',
'artifacts', 'options',
'fail-on-overlap', 'shell', 'fatal-warnings',
'ref-storage', 'sandbox', 'mirrors', 'remote-execution',
@@ -391,6 +394,44 @@ class Project():
# Reset the element loader state
Element._reset_load_state()
+ # get_default_target()
+ #
+ # Attempts to interpret which element the user intended to run a command on.
+ # This is for commands that only accept a single target element and thus,
+ # this only uses the workspace element (if invoked from workspace directory)
+ # and does not use the project default targets.
+ #
+ def get_default_target(self):
+ return self._invoked_from_workspace_element
+
+ # get_default_targets()
+ #
+ # Attempts to interpret which elements the user intended to run a command on.
+ # This is for commands that accept multiple target elements.
+ #
+ def get_default_targets(self):
+
+ # If _invoked_from_workspace_element has a value,
+ # a workspace element was found before a project config
+ # Therefore the workspace does not contain a project
+ if self._invoked_from_workspace_element:
+ return (self._invoked_from_workspace_element,)
+
+ # Default targets from project configuration
+ if self._default_targets:
+ return tuple(self._default_targets)
+
+ # If default targets are not configured, default to all project elements
+ default_targets = []
+ for root, _, files in os.walk(self.element_path):
+ for file in files:
+ if file.endswith(".bst"):
+ rel_dir = os.path.relpath(root, self.element_path)
+ rel_file = os.path.join(rel_dir, file).lstrip("./")
+ default_targets.append(rel_file)
+
+ return tuple(default_targets)
+
# _load():
#
# Loads the project configuration file in the project
@@ -456,6 +497,10 @@ class Project():
self.config.options = OptionPool(self.element_path)
self.first_pass_config.options = OptionPool(self.element_path)
+ defaults = _yaml.node_get(pre_config_node, Mapping, 'defaults')
+ _yaml.node_validate(defaults, ['targets'])
+ self._default_targets = _yaml.node_get(defaults, list, "targets")
+
# Fatal warnings
self._fatal_warnings = _yaml.node_get(pre_config_node, list, 'fatal-warnings', default_value=[])