diff --git a/buildstream/_artifactcache/artifactcache.py b/buildstream/_artifactcache/artifactcache.py index 7080f2151fbacc89cf4c61f102dafd7d36effc8d..bfa705127bd31a97763b21427417143a3de03c16 100644 --- a/buildstream/_artifactcache/artifactcache.py +++ b/buildstream/_artifactcache/artifactcache.py @@ -835,6 +835,20 @@ class ArtifactCache(): self.cas.link_ref(oldref, newref) + # checkout_artifact_subdir() + # + # Checkout given artifact subdir into provided directory + # + # Args: + # element (Element): The Element + # key (str): The cache key to use + # subdir (str): The subdir to checkout + # tmpdir (str): The dir to place the subdir content + # + def checkout_artifact_subdir(self, element, key, subdir, tmpdir): + ref = self.get_artifact_fullname(element, key) + return self.cas.checkout_artifact_subdir(ref, subdir, tmpdir) + ################################################ # Local Private Methods # ################################################ diff --git a/buildstream/_artifactcache/cascache.py b/buildstream/_artifactcache/cascache.py index f07bd24a11701e91cf8d80bdaad548c00589c668..2962c9fa4945db3446ad6cced22440b234d88389 100644 --- a/buildstream/_artifactcache/cascache.py +++ b/buildstream/_artifactcache/cascache.py @@ -409,6 +409,21 @@ class CASCache(): return True + # checkout_artifact_subdir(): + # + # Checkout given artifact subdir into provided directory + # + # Args: + # ref (str): The ref to check + # subdir (str): The subdir to checkout + # tmpdir (str): The dir to place the subdir content + # + def checkout_artifact_subdir(self, ref, subdir, tmpdir): + tree = self.resolve_ref(ref) + # This assumes that the subdir digest is present in the element tree + subdirdigest = self._get_subdir(tree, subdir) + self._checkout(tmpdir, subdirdigest) + # objpath(): # # Return the path of an object based on its digest. diff --git a/buildstream/_context.py b/buildstream/_context.py index d4c338f0768b84675fd4bb6ae5ffbafa7539775c..33744af63f5eb1cc650d93587da533464a05e832 100644 --- a/buildstream/_context.py +++ b/buildstream/_context.py @@ -107,6 +107,9 @@ class Context(): # Whether or not to attempt to pull build trees globally self.pull_build_trees = False + # Whether to not include artifact buildtrees in workspaces if available + self.workspace_build_trees = True + # Whether elements must be rebuilt when their dependencies have changed self._strict_build_plan = None @@ -163,7 +166,7 @@ class Context(): _yaml.node_validate(defaults, [ 'sourcedir', 'builddir', 'artifactdir', 'logdir', 'scheduler', 'artifacts', 'logging', 'projects', - 'cache', 'pullbuildtrees' + 'cache', 'pullbuildtrees', 'workspacebuildtrees' ]) for directory in ['sourcedir', 'builddir', 'artifactdir', 'logdir']: @@ -191,6 +194,9 @@ class Context(): # Load pull build trees configuration self.pull_build_trees = _yaml.node_get(defaults, bool, 'pullbuildtrees', default_value='False') + # Load workspace buildtrees configuration + self.workspace_build_trees = _yaml.node_get(defaults, bool, 'workspacebuildtrees', default_value='True') + # Load logging config logging = _yaml.node_get(defaults, Mapping, 'logging') _yaml.node_validate(logging, [ diff --git a/buildstream/_frontend/cli.py b/buildstream/_frontend/cli.py index f019058283d9b71c9acfbb9aeb4043df362f5f78..b62cb5b9ea76ce774b55ff4b1e8874dd7e6a1e62 100644 --- a/buildstream/_frontend/cli.py +++ b/buildstream/_frontend/cli.py @@ -678,17 +678,21 @@ def workspace(): ################################################################## @workspace.command(name='open', short_help="Open a new workspace") @click.option('--no-checkout', default=False, is_flag=True, - help="Do not checkout the source, only link to the given directory") + help="Do not checkout the source or cached buildtree, only link to the given directory") @click.option('--force', '-f', default=False, is_flag=True, help="Overwrite files existing in checkout directory") @click.option('--track', 'track_', default=False, is_flag=True, help="Track and fetch new source references before checking out the workspace") +@click.option('--no-cache', default=False, is_flag=True, + help="Do not checkout the cached buildtree") @click.argument('element', type=click.Path(readable=False)) @click.argument('directory', type=click.Path(file_okay=False)) @click.pass_obj -def workspace_open(app, no_checkout, force, track_, element, directory): - """Open a workspace for manual source modification""" +def workspace_open(app, no_checkout, force, track_, no_cache, element, directory): + """Open a workspace for manual source modification, the elements buildtree + will be provided if available in the local artifact cache. + """ if os.path.exists(directory): @@ -700,11 +704,15 @@ def workspace_open(app, no_checkout, force, track_, element, directory): click.echo("Checkout directory is not empty: {}".format(directory), err=True) sys.exit(-1) + if not no_cache and not no_checkout: + click.echo("WARNING: Workspace will be opened without the cached buildtree if not cached locally") + with app.initialized(): app.stream.workspace_open(element, directory, no_checkout=no_checkout, track_first=track_, - force=force) + force=force, + no_cache=no_cache) ################################################################## diff --git a/buildstream/_stream.py b/buildstream/_stream.py index 6e2e8b25b24c4a67670a36f401d0267ea3210373..f921718998875037de45027ce12ba6e858b6fe70 100644 --- a/buildstream/_stream.py +++ b/buildstream/_stream.py @@ -453,11 +453,17 @@ class Stream(): # no_checkout (bool): Whether to skip checking out the source # track_first (bool): Whether to track and fetch first # force (bool): Whether to ignore contents in an existing directory + # no_cache (bool): Whether to not include the cached buildtree # def workspace_open(self, target, directory, *, no_checkout, track_first, - force): + force, + no_cache): + + # Override no_cache if the global user conf workspacebuildtrees is false + if not self._context.workspace_build_trees: + no_cache = True if track_first: track_targets = (target,) @@ -470,6 +476,20 @@ class Stream(): target = elements[0] directory = os.path.abspath(directory) + # Check if given target has a buildtree artifact cached locally + buildtree = None + if target._cached(): + buildtree = self._artifacts.contains_subdir_artifact(target, target._get_cache_key(), 'buildtree') + + # If we're running in the default state, make the user aware of buildtree usage + if not no_cache and not no_checkout: + if buildtree: + self._message(MessageType.INFO, "{} buildtree artifact is available," + " workspace will be opened with it".format(target.name)) + else: + self._message(MessageType.WARN, "{} buildtree artifact not available," + " workspace will be opened with source checkout".format(target.name)) + if not list(target.sources()): build_depends = [x.name for x in target.dependencies(Scope.BUILD, recurse=False)] if not build_depends: @@ -501,6 +521,7 @@ class Stream(): "fetch the latest version of the " + "source.") + # Presume workspace to be forced if previous StreamError not raised if workspace: workspaces.delete_workspace(target._get_full_name()) workspaces.save_config() @@ -510,11 +531,16 @@ class Stream(): except OSError as e: raise StreamError("Failed to create workspace directory: {}".format(e)) from e - workspaces.create_workspace(target._get_full_name(), directory) - - if not no_checkout: - with target.timed_activity("Staging sources to {}".format(directory)): - target._open_workspace() + # Handle opening workspace with buildtree included + if (buildtree and not no_cache) and not no_checkout: + workspaces.create_workspace(target._get_full_name(), directory, cached_build=buildtree) + with target.timed_activity("Staging buildtree to {}".format(directory)): + target._open_workspace(buildtree=buildtree) + else: + workspaces.create_workspace(target._get_full_name(), directory) + if (not buildtree or no_cache) and not no_checkout: + with target.timed_activity("Staging sources to {}".format(directory)): + target._open_workspace() workspaces.save_config() self._message(MessageType.INFO, "Saved workspace configuration") @@ -598,10 +624,24 @@ class Stream(): .format(workspace_path, e)) from e workspaces.delete_workspace(element._get_full_name()) - workspaces.create_workspace(element._get_full_name(), workspace_path) - with element.timed_activity("Staging sources to {}".format(workspace_path)): - element._open_workspace() + # Create the workspace, ensuring the original optional cached build state is preserved if + # possible. + buildtree = False + if workspace.cached_build and element._cached(): + if self._artifacts.contains_subdir_artifact(element, element._get_cache_key(), 'buildtree'): + buildtree = True + + # Warn the user if the workspace cannot be opened with the original cached build state + if workspace.cached_build and not buildtree: + self._message(MessageType.WARN, "{} original buildtree artifact not available," + " workspace will be opened with source checkout".format(element.name)) + + workspaces.create_workspace(element._get_full_name(), workspace_path, + cached_build=buildtree) + + with element.timed_activity("Staging to {}".format(workspace_path)): + element._open_workspace(buildtree=buildtree) self._message(MessageType.INFO, "Reset workspace for {} at: {}".format(element.name, diff --git a/buildstream/_workspaces.py b/buildstream/_workspaces.py index 468073f0584826e8aaf8aed47ec7cb90d9667fed..6e246e0a655709ee7ecba139897b63749b42c73e 100644 --- a/buildstream/_workspaces.py +++ b/buildstream/_workspaces.py @@ -24,7 +24,7 @@ from . import _yaml from ._exceptions import LoadError, LoadErrorReason -BST_WORKSPACE_FORMAT_VERSION = 3 +BST_WORKSPACE_FORMAT_VERSION = 4 # Workspace() @@ -43,9 +43,11 @@ BST_WORKSPACE_FORMAT_VERSION = 3 # running_files (dict): A dict mapping dependency elements to files # changed between failed builds. Should be # made obsolete with failed build artifacts. +# cached_build (bool): If the workspace is staging the cached build artifact # class Workspace(): - def __init__(self, toplevel_project, *, last_successful=None, path=None, prepared=False, running_files=None): + def __init__(self, toplevel_project, *, last_successful=None, path=None, prepared=False, + running_files=None, cached_build=False): self.prepared = prepared self.last_successful = last_successful self._path = path @@ -53,6 +55,7 @@ class Workspace(): self._toplevel_project = toplevel_project self._key = None + self.cached_build = cached_build # to_dict() # @@ -65,7 +68,8 @@ class Workspace(): ret = { 'prepared': self.prepared, 'path': self._path, - 'running_files': self.running_files + 'running_files': self.running_files, + 'cached_build': self.cached_build } if self.last_successful is not None: ret["last_successful"] = self.last_successful @@ -224,12 +228,13 @@ class Workspaces(): # Args: # element_name (str) - The element name to create a workspace for # path (str) - The path in which the workspace should be kept + # cached_build (bool) - If the workspace is staging the cached build artifact # - def create_workspace(self, element_name, path): + def create_workspace(self, element_name, path, cached_build=False): if path.startswith(self._toplevel_project.directory): path = os.path.relpath(path, self._toplevel_project.directory) - self._workspaces[element_name] = Workspace(self._toplevel_project, path=path) + self._workspaces[element_name] = Workspace(self._toplevel_project, path=path, cached_build=cached_build) return self._workspaces[element_name] @@ -396,6 +401,7 @@ class Workspaces(): 'path': _yaml.node_get(node, str, 'path'), 'last_successful': _yaml.node_get(node, str, 'last_successful', default_value=None), 'running_files': _yaml.node_get(node, dict, 'running_files', default_value=None), + 'cached_build': _yaml.node_get(node, bool, 'cached_build', default_value=False) } return Workspace.from_dict(self._toplevel_project, dictionary) diff --git a/buildstream/element.py b/buildstream/element.py index 792be3fdb4275c4bd8ed7464bbef4caf95278399..d572d7c7c0d725a4cf55b72d9507b7baf32ec86c 100644 --- a/buildstream/element.py +++ b/buildstream/element.py @@ -1896,7 +1896,10 @@ class Element(Plugin): # This requires that a workspace already be created in # the workspaces metadata first. # - def _open_workspace(self): + # Args: + # buildtree (bool): Whether to open workspace with artifact buildtree + # + def _open_workspace(self, buildtree=False): context = self._get_context() workspace = self._get_workspace() assert workspace is not None @@ -1909,12 +1912,22 @@ class Element(Plugin): # files in the target directory actually works without any # additional support from Source implementations. # + os.makedirs(context.builddir, exist_ok=True) - with utils._tempdir(dir=context.builddir, prefix='workspace-{}' - .format(self.normal_name)) as temp: + with utils._tempdir(dir=context.builddir, prefix='workspace-source-{}' + .format(self.normal_name)) as temp,\ + utils._tempdir(dir=context.builddir, prefix='workspace-buildtree-{}' + .format(self.normal_name)) as buildtreetemp: + for source in self.sources(): source._init_workspace(temp) + # Overwrite the source checkout with the cached buildtree + if buildtree: + self.__artifacts.checkout_artifact_subdir(self, self._get_cache_key(), 'buildtree', buildtreetemp) + if utils._call([utils.get_host_tool('cp'), '-pfr', "".join((buildtreetemp, '/.')), temp])[0] != 0: + raise ElementError("Failed to copy buildtree into workspace checkout at {}".format(buildtreetemp)) + # Now hardlink the files into the workspace target. utils.link_files(temp, workspace.get_absolute_path()) diff --git a/doc/source/developing/workspaces.rst b/doc/source/developing/workspaces.rst index b5ed64b2c62c6f958c6c47a13d2cd62c381464e4..4b68b24d566343acb0576f76122d5873f03a3a4d 100644 --- a/doc/source/developing/workspaces.rst +++ b/doc/source/developing/workspaces.rst @@ -24,9 +24,32 @@ Suppose we now want to alter the functionality of the *hello* command. We can make changes to the source code of Buildstream elements by making use of BuildStream's workspace command. +Utilising cached buildtrees +--------------------------- + When a BuildStream build element artifact is created and cached, a snapshot of + the build directory after the build commands have completed is included in the + artifact. This `build tree` can be considered an intermediary state of element, + where the source is present along with any output created during the build + execution. + + By default when opening a workspace, bst will attempt to stage the build tree + into the workspace if it's available in the local cache. If the respective + build tree is not present in the cache (element not cached, partially cached or + is a non build element) then the source will be staged as is. The default + behaviour to attempt to use the build tree can be overriden with specific bst + workspace open option of `--no-cache`, or via setting user configuration option + `workspacebuildtrees: False` + Opening a workspace ------------------- +.. note:: + + This example presumes you built the hello.bst during + :ref:`running commands <tutorial_running_commands>` + if not, please start by building it. + + First we need to open a workspace, we can do this by running .. raw:: html @@ -88,6 +111,15 @@ Alternatively, if we wish to discard the changes we can use This resets the workspace to its original state. +.. note:: + + bst reset will attempt to open the workspace in + the condition in which it was originally staged, + i.e with or without consuming the element build tree. + If it was originally staged with a cached build tree + and there's no longer one available, the source will + be staged as is. + To discard the workspace completely we can do: .. raw:: html diff --git a/tests/frontend/workspace.py b/tests/frontend/workspace.py index 51b7d608894dd5fac55af329b55e4f67a87cf8ac..26535e4ab9020e80df6da98982668033ee4cda6e 100644 --- a/tests/frontend/workspace.py +++ b/tests/frontend/workspace.py @@ -44,7 +44,7 @@ DATA_DIR = os.path.join( def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir=None, - project_path=None, element_attrs=None): + project_path=None, element_attrs=None, no_cache=False): if not workspace_dir: workspace_dir = os.path.join(str(tmpdir), 'workspace{}'.format(suffix)) if not project_path: @@ -88,6 +88,8 @@ def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir args = ['workspace', 'open'] if track: args.append('--track') + if no_cache: + args.append('--no-cache') args.extend([element_name, workspace_dir]) result = cli.run(project=project_path, args=args) @@ -101,7 +103,7 @@ def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir filename = os.path.join(workspace_dir, 'usr', 'bin', 'hello') assert os.path.exists(filename) - return (element_name, project_path, workspace_dir) + return (element_name, project_path, workspace_dir, result) @pytest.mark.datafiles(DATA_DIR) @@ -112,7 +114,7 @@ def test_open(cli, tmpdir, datafiles, kind): @pytest.mark.datafiles(DATA_DIR) def test_open_bzr_customize(cli, tmpdir, datafiles): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, "bzr", False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, "bzr", False) # Check that the .bzr dir exists bzrdir = os.path.join(workspace, ".bzr") @@ -137,7 +139,7 @@ def test_open_track(cli, tmpdir, datafiles, kind): @pytest.mark.datafiles(DATA_DIR) @pytest.mark.parametrize("kind", repo_kinds) def test_open_force(cli, tmpdir, datafiles, kind): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, kind, False) # Close the workspace result = cli.run(project=project, args=[ @@ -158,7 +160,7 @@ def test_open_force(cli, tmpdir, datafiles, kind): @pytest.mark.datafiles(DATA_DIR) @pytest.mark.parametrize("kind", repo_kinds) def test_open_force_open(cli, tmpdir, datafiles, kind): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, kind, False) # Assert the workspace dir exists assert os.path.exists(workspace) @@ -173,7 +175,7 @@ def test_open_force_open(cli, tmpdir, datafiles, kind): @pytest.mark.datafiles(DATA_DIR) @pytest.mark.parametrize("kind", repo_kinds) def test_open_force_different_workspace(cli, tmpdir, datafiles, kind): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False, "-alpha") + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, kind, False, "-alpha") # Assert the workspace dir exists assert os.path.exists(workspace) @@ -183,7 +185,7 @@ def test_open_force_different_workspace(cli, tmpdir, datafiles, kind): tmpdir = os.path.join(str(tmpdir), "-beta") shutil.move(hello_path, hello1_path) - element_name2, project2, workspace2 = open_workspace(cli, tmpdir, datafiles, kind, False, "-beta") + element_name2, project2, workspace2, _ = open_workspace(cli, tmpdir, datafiles, kind, False, "-beta") # Assert the workspace dir exists assert os.path.exists(workspace2) @@ -210,7 +212,7 @@ def test_open_force_different_workspace(cli, tmpdir, datafiles, kind): @pytest.mark.datafiles(DATA_DIR) @pytest.mark.parametrize("kind", repo_kinds) def test_close(cli, tmpdir, datafiles, kind): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, kind, False) # Close the workspace result = cli.run(project=project, args=[ @@ -226,7 +228,7 @@ def test_close(cli, tmpdir, datafiles, kind): def test_close_external_after_move_project(cli, tmpdir, datafiles): workspace_dir = os.path.join(str(tmpdir), "workspace") project_path = os.path.join(str(tmpdir), 'initial_project') - element_name, _, _ = open_workspace(cli, tmpdir, datafiles, 'git', False, "", workspace_dir, project_path) + element_name, _, _, _ = open_workspace(cli, tmpdir, datafiles, 'git', False, "", workspace_dir, project_path) assert os.path.exists(workspace_dir) moved_dir = os.path.join(str(tmpdir), 'external_project') shutil.move(project_path, moved_dir) @@ -246,8 +248,8 @@ def test_close_external_after_move_project(cli, tmpdir, datafiles): def test_close_internal_after_move_project(cli, tmpdir, datafiles): initial_dir = os.path.join(str(tmpdir), 'initial_project') initial_workspace = os.path.join(initial_dir, 'workspace') - element_name, _, _ = open_workspace(cli, tmpdir, datafiles, 'git', False, - workspace_dir=initial_workspace, project_path=initial_dir) + element_name, _, _, _ = open_workspace(cli, tmpdir, datafiles, 'git', False, + workspace_dir=initial_workspace, project_path=initial_dir) moved_dir = os.path.join(str(tmpdir), 'internal_project') shutil.move(initial_dir, moved_dir) assert os.path.exists(moved_dir) @@ -265,7 +267,7 @@ def test_close_internal_after_move_project(cli, tmpdir, datafiles): @pytest.mark.datafiles(DATA_DIR) def test_close_removed(cli, tmpdir, datafiles): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, 'git', False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, 'git', False) # Remove it first, closing the workspace should work shutil.rmtree(workspace) @@ -282,7 +284,7 @@ def test_close_removed(cli, tmpdir, datafiles): @pytest.mark.datafiles(DATA_DIR) def test_close_nonexistant_element(cli, tmpdir, datafiles): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, 'git', False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, 'git', False) element_path = os.path.join(datafiles.dirname, datafiles.basename, 'elements', element_name) # First brutally remove the element.bst file, ensuring that @@ -304,9 +306,9 @@ def test_close_nonexistant_element(cli, tmpdir, datafiles): def test_close_multiple(cli, tmpdir, datafiles): tmpdir_alpha = os.path.join(str(tmpdir), 'alpha') tmpdir_beta = os.path.join(str(tmpdir), 'beta') - alpha, project, workspace_alpha = open_workspace( + alpha, project, workspace_alpha, _ = open_workspace( cli, tmpdir_alpha, datafiles, 'git', False, suffix='-alpha') - beta, project, workspace_beta = open_workspace( + beta, project, workspace_beta, _ = open_workspace( cli, tmpdir_beta, datafiles, 'git', False, suffix='-beta') # Close the workspaces @@ -324,9 +326,9 @@ def test_close_multiple(cli, tmpdir, datafiles): def test_close_all(cli, tmpdir, datafiles): tmpdir_alpha = os.path.join(str(tmpdir), 'alpha') tmpdir_beta = os.path.join(str(tmpdir), 'beta') - alpha, project, workspace_alpha = open_workspace( + alpha, project, workspace_alpha, _ = open_workspace( cli, tmpdir_alpha, datafiles, 'git', False, suffix='-alpha') - beta, project, workspace_beta = open_workspace( + beta, project, workspace_beta, _ = open_workspace( cli, tmpdir_beta, datafiles, 'git', False, suffix='-beta') # Close the workspaces @@ -343,7 +345,7 @@ def test_close_all(cli, tmpdir, datafiles): @pytest.mark.datafiles(DATA_DIR) def test_reset(cli, tmpdir, datafiles): # Open the workspace - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, 'git', False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, 'git', False) # Modify workspace shutil.rmtree(os.path.join(workspace, 'usr', 'bin')) @@ -366,9 +368,9 @@ def test_reset_multiple(cli, tmpdir, datafiles): # Open the workspaces tmpdir_alpha = os.path.join(str(tmpdir), 'alpha') tmpdir_beta = os.path.join(str(tmpdir), 'beta') - alpha, project, workspace_alpha = open_workspace( + alpha, project, workspace_alpha, _ = open_workspace( cli, tmpdir_alpha, datafiles, 'git', False, suffix='-alpha') - beta, project, workspace_beta = open_workspace( + beta, project, workspace_beta, _ = open_workspace( cli, tmpdir_beta, datafiles, 'git', False, suffix='-beta') # Modify workspaces @@ -392,9 +394,9 @@ def test_reset_all(cli, tmpdir, datafiles): # Open the workspaces tmpdir_alpha = os.path.join(str(tmpdir), 'alpha') tmpdir_beta = os.path.join(str(tmpdir), 'beta') - alpha, project, workspace_alpha = open_workspace( + alpha, project, workspace_alpha, _ = open_workspace( cli, tmpdir_alpha, datafiles, 'git', False, suffix='-alpha') - beta, project, workspace_beta = open_workspace( + beta, project, workspace_beta, _ = open_workspace( cli, tmpdir_beta, datafiles, 'git', False, suffix='-beta') # Modify workspaces @@ -415,7 +417,7 @@ def test_reset_all(cli, tmpdir, datafiles): @pytest.mark.datafiles(DATA_DIR) def test_list(cli, tmpdir, datafiles): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, 'git', False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, 'git', False) # Now list the workspaces result = cli.run(project=project, args=[ @@ -437,7 +439,7 @@ def test_list(cli, tmpdir, datafiles): @pytest.mark.parametrize("kind", repo_kinds) @pytest.mark.parametrize("strict", [("strict"), ("non-strict")]) def test_build(cli, tmpdir, datafiles, kind, strict): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, kind, False) checkout = os.path.join(str(tmpdir), 'checkout') # Modify workspace @@ -516,7 +518,7 @@ def test_buildable_no_ref(cli, tmpdir, datafiles): @pytest.mark.parametrize("modification", [("addfile"), ("removefile"), ("modifyfile")]) @pytest.mark.parametrize("strict", [("strict"), ("non-strict")]) def test_detect_modifications(cli, tmpdir, datafiles, modification, strict): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, 'git', False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, 'git', False) checkout = os.path.join(str(tmpdir), 'checkout') # Configure strict mode @@ -637,7 +639,8 @@ def test_list_unsupported_workspace(cli, tmpdir, datafiles, workspace_cfg): "alpha.bst": { "prepared": False, "path": "/workspaces/bravo", - "running_files": {} + "running_files": {}, + "cached_build": False } } }), @@ -652,7 +655,8 @@ def test_list_unsupported_workspace(cli, tmpdir, datafiles, workspace_cfg): "alpha.bst": { "prepared": False, "path": "/workspaces/bravo", - "running_files": {} + "running_files": {}, + "cached_build": False } } }), @@ -670,7 +674,8 @@ def test_list_unsupported_workspace(cli, tmpdir, datafiles, workspace_cfg): "alpha.bst": { "prepared": False, "path": "/workspaces/bravo", - "running_files": {} + "running_files": {}, + "cached_build": False } } }), @@ -695,7 +700,8 @@ def test_list_unsupported_workspace(cli, tmpdir, datafiles, workspace_cfg): "last_successful": "some_key", "running_files": { "beta.bst": ["some_file"] - } + }, + "cached_build": False } } }), @@ -715,7 +721,30 @@ def test_list_unsupported_workspace(cli, tmpdir, datafiles, workspace_cfg): "alpha.bst": { "prepared": True, "path": "/workspaces/bravo", - "running_files": {} + "running_files": {}, + "cached_build": False + } + } + }), + # Test loading version 4 + ({ + "format-version": 4, + "workspaces": { + "alpha.bst": { + "prepared": False, + "path": "/workspaces/bravo", + "running_files": {}, + "cached_build": True + } + } + }, { + "format-version": BST_WORKSPACE_FORMAT_VERSION, + "workspaces": { + "alpha.bst": { + "prepared": False, + "path": "/workspaces/bravo", + "running_files": {}, + "cached_build": True } } }) @@ -779,7 +808,7 @@ def test_list_supported_workspace(cli, tmpdir, datafiles, workspace_cfg, expecte @pytest.mark.datafiles(DATA_DIR) @pytest.mark.parametrize("kind", repo_kinds) def test_inconsitent_pipeline_message(cli, tmpdir, datafiles, kind): - element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False) + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, kind, False) shutil.rmtree(workspace) @@ -793,8 +822,8 @@ def test_inconsitent_pipeline_message(cli, tmpdir, datafiles, kind): @pytest.mark.parametrize("strict", [("strict"), ("non-strict")]) def test_cache_key_workspace_in_dependencies(cli, tmpdir, datafiles, strict): checkout = os.path.join(str(tmpdir), 'checkout') - element_name, project, workspace = open_workspace(cli, os.path.join(str(tmpdir), 'repo-a'), - datafiles, 'git', False) + element_name, project, workspace, _ = open_workspace(cli, os.path.join(str(tmpdir), 'repo-a'), + datafiles, 'git', False) element_path = os.path.join(project, 'elements') back_dep_element_name = 'workspace-test-back-dep.bst' @@ -869,10 +898,75 @@ def test_multiple_failed_builds(cli, tmpdir, datafiles): ] } } - element_name, project, _ = open_workspace(cli, tmpdir, datafiles, - "git", False, element_attrs=element_config) + element_name, project, _, _ = open_workspace(cli, tmpdir, datafiles, + "git", False, element_attrs=element_config) for _ in range(2): result = cli.run(project=project, args=["build", element_name]) assert "BUG" not in result.stderr assert cli.get_element_state(project, element_name) != "cached" + + +@pytest.mark.datafiles(DATA_DIR) +def test_nocache_open_messages(cli, tmpdir, datafiles): + # cli default WARN for source dropback possibility when no-cache flag is not passed + element_name, project, workspace, result = open_workspace(cli, tmpdir, datafiles, 'git', False, suffix='1') + assert "WARNING: Workspace will be opened without the cached buildtree if not cached locally" in result.output + + # cli WARN for source dropback happening when no-cache flag not given, but buildtree not available + assert "workspace will be opened with source checkout" in result.stderr + + # cli default WARN for source dropback possibilty not given when no-cache flag is passed + tmpdir = os.path.join(str(tmpdir), "2") + element_name, project, workspace, result = open_workspace(cli, tmpdir, datafiles, 'git', False, suffix='2', + no_cache=True) + assert "WARNING: Workspace will be opened without the cached buildtree if not cached locally" not in result.output + + +@pytest.mark.datafiles(DATA_DIR) +def test_nocache_reset_messages(cli, tmpdir, datafiles): + element_name, project, workspace, _ = open_workspace(cli, tmpdir, datafiles, 'git', False, suffix='1') + + # Modify workspace, without building so the artifact is not cached + shutil.rmtree(os.path.join(workspace, 'usr', 'bin')) + os.makedirs(os.path.join(workspace, 'etc')) + with open(os.path.join(workspace, 'etc', 'pony.conf'), 'w') as f: + f.write("PONY='pink'") + + # Now reset the open workspace, this should have the + # effect of reverting our changes to the original source, as it + # was not originally opened with a cached buildtree and as such + # should not notify the user + result = cli.run(project=project, args=[ + 'workspace', 'reset', element_name + ]) + result.assert_success() + assert "original buildtree artifact not available" not in result.output + assert os.path.exists(os.path.join(workspace, 'usr', 'bin', 'hello')) + assert not os.path.exists(os.path.join(workspace, 'etc', 'pony.conf')) + + # Close the workspace + result = cli.run(project=project, args=[ + 'workspace', 'close', '--remove-dir', element_name + ]) + result.assert_success() + + # Build the workspace so we have a cached buildtree artifact for the element + assert cli.get_element_state(project, element_name) == 'buildable' + result = cli.run(project=project, args=['build', element_name]) + result.assert_success() + + # Opening the workspace after a build should lead to the cached buildtree being + # staged by default + result = cli.run(project=project, args=[ + 'workspace', 'open', element_name, workspace + ]) + result.assert_success() + + # Now reset the workspace and ensure that a warning is not given about the artifact + # buildtree not being available + result = cli.run(project=project, args=[ + 'workspace', 'reset', element_name + ]) + result.assert_success() + assert "original buildtree artifact not available" not in result.output