[Notes] [Git][BuildStream/buildstream][jonathan/workspace-fragment-create] 12 commits: _workspaces.py: Add a WorkspaceLocal object



Title: GitLab

Jonathan Maw pushed to branch jonathan/workspace-fragment-create at BuildStream / buildstream

Commits:

9 changed files:

Changes:

  • NEWS
    ... ... @@ -63,6 +63,9 @@ buildstream 1.3.1
    63 63
     
    
    64 64
       o Added new `bst source-checkout` command to checkout sources of an element.
    
    65 65
     
    
    66
    +  o Opening a workspace now creates a .bstproject.yaml file that allows buildstream
    
    67
    +    commands to be run from a workspace that is not inside a project.
    
    68
    +
    
    66 69
     
    
    67 70
     =================
    
    68 71
     buildstream 1.1.5
    

  • buildstream/_context.py
    ... ... @@ -31,7 +31,7 @@ from ._exceptions import LoadError, LoadErrorReason, BstError
    31 31
     from ._message import Message, MessageType
    
    32 32
     from ._profile import Topics, profile_start, profile_end
    
    33 33
     from ._artifactcache import ArtifactCache
    
    34
    -from ._workspaces import Workspaces
    
    34
    +from ._workspaces import Workspaces, WorkspaceLocals
    
    35 35
     from .plugin import _plugin_lookup
    
    36 36
     
    
    37 37
     
    
    ... ... @@ -118,6 +118,10 @@ class Context():
    118 118
             # remove a workspace directory.
    
    119 119
             self.prompt_workspace_close_remove_dir = None
    
    120 120
     
    
    121
    +        # Boolean, whether we double-check with the user that they meant to
    
    122
    +        # close the workspace when they're using it to access the project.
    
    123
    +        self.prompt_workspace_close_project_inaccessible = None
    
    124
    +
    
    121 125
             # Boolean, whether we double-check with the user that they meant to do
    
    122 126
             # a hard reset of a workspace, potentially losing changes.
    
    123 127
             self.prompt_workspace_reset_hard = None
    
    ... ... @@ -136,6 +140,7 @@ class Context():
    136 140
             self._projects = []
    
    137 141
             self._project_overrides = {}
    
    138 142
             self._workspaces = None
    
    143
    +        self._workspace_locals = WorkspaceLocals()
    
    139 144
             self._log_handle = None
    
    140 145
             self._log_filename = None
    
    141 146
     
    
    ... ... @@ -245,12 +250,15 @@ class Context():
    245 250
                 defaults, Mapping, 'prompt')
    
    246 251
             _yaml.node_validate(prompt, [
    
    247 252
                 'auto-init', 'really-workspace-close-remove-dir',
    
    253
    +            'really-workspace-close-project-inaccessible',
    
    248 254
                 'really-workspace-reset-hard',
    
    249 255
             ])
    
    250 256
             self.prompt_auto_init = _node_get_option_str(
    
    251 257
                 prompt, 'auto-init', ['ask', 'no']) == 'ask'
    
    252 258
             self.prompt_workspace_close_remove_dir = _node_get_option_str(
    
    253 259
                 prompt, 'really-workspace-close-remove-dir', ['ask', 'yes']) == 'ask'
    
    260
    +        self.prompt_workspace_close_project_inaccessible = _node_get_option_str(
    
    261
    +            prompt, 'really-workspace-close-project-inaccessible', ['ask', 'yes']) == 'ask'
    
    254 262
             self.prompt_workspace_reset_hard = _node_get_option_str(
    
    255 263
                 prompt, 'really-workspace-reset-hard', ['ask', 'yes']) == 'ask'
    
    256 264
     
    
    ... ... @@ -307,6 +315,16 @@ class Context():
    307 315
         def get_workspaces(self):
    
    308 316
             return self._workspaces
    
    309 317
     
    
    318
    +    # get_workspace_locals():
    
    319
    +    #
    
    320
    +    # Return the WorkspaceLocals object used for this BuildStream invocation
    
    321
    +    #
    
    322
    +    # Returns:
    
    323
    +    #    (WorkspaceLocals): The WorkspaceLocals object
    
    324
    +    #
    
    325
    +    def get_workspace_locals(self):
    
    326
    +        return self._workspace_locals
    
    327
    +
    
    310 328
         # get_overrides():
    
    311 329
         #
    
    312 330
         # Fetch the override dictionary for the active project. This returns
    

  • buildstream/_frontend/cli.py
    ... ... @@ -59,18 +59,9 @@ def complete_target(args, incomplete):
    59 59
         :return: all the possible user-specified completions for the param
    
    60 60
         """
    
    61 61
     
    
    62
    +    from .. import utils
    
    62 63
         project_conf = 'project.conf'
    
    63 64
     
    
    64
    -    def ensure_project_dir(directory):
    
    65
    -        directory = os.path.abspath(directory)
    
    66
    -        while not os.path.isfile(os.path.join(directory, project_conf)):
    
    67
    -            parent_dir = os.path.dirname(directory)
    
    68
    -            if directory == parent_dir:
    
    69
    -                break
    
    70
    -            directory = parent_dir
    
    71
    -
    
    72
    -        return directory
    
    73
    -
    
    74 65
         # First resolve the directory, in case there is an
    
    75 66
         # active --directory/-C option
    
    76 67
         #
    
    ... ... @@ -89,7 +80,7 @@ def complete_target(args, incomplete):
    89 80
         else:
    
    90 81
             # Check if this directory or any of its parent directories
    
    91 82
             # contain a project config file
    
    92
    -        base_directory = ensure_project_dir(base_directory)
    
    83
    +        base_directory = utils._search_upward_for_file(base_directory, project_conf)
    
    93 84
     
    
    94 85
         # Now parse the project.conf just to find the element path,
    
    95 86
         # this is unfortunately a bit heavy.
    
    ... ... @@ -764,11 +755,18 @@ def workspace_close(app, remove_dir, all_, elements):
    764 755
     
    
    765 756
             elements = app.stream.redirect_element_names(elements)
    
    766 757
     
    
    767
    -        # Check that the workspaces in question exist
    
    758
    +        # Check that the workspaces in question exist, and that it's safe to
    
    759
    +        # remove them.
    
    768 760
             nonexisting = []
    
    769 761
             for element_name in elements:
    
    770 762
                 if not app.stream.workspace_exists(element_name):
    
    771 763
                     nonexisting.append(element_name)
    
    764
    +            if (app.stream.workspace_is_required(element_name) and app.interactive and
    
    765
    +                    app.context.prompt_workspace_close_project_inaccessible):
    
    766
    +                click.echo("Removing '{}' will prevent you from running buildstream commands".format(element_name))
    
    767
    +                if not click.confirm('Are you sure you want to close this workspace?'):
    
    768
    +                    click.echo('Aborting', err=True)
    
    769
    +                    sys.exit(-1)
    
    772 770
             if nonexisting:
    
    773 771
                 raise AppError("Workspace does not exist", detail="\n".join(nonexisting))
    
    774 772
     
    

  • buildstream/_project.py
    ... ... @@ -94,8 +94,10 @@ class Project():
    94 94
             # The project name
    
    95 95
             self.name = None
    
    96 96
     
    
    97
    -        # The project directory
    
    98
    -        self.directory = self._ensure_project_dir(directory)
    
    97
    +        self._context = context  # The invocation Context, a private member
    
    98
    +
    
    99
    +        # The project directory, and whether the project was found from an external workspace
    
    100
    +        self.directory, self._required_workspace_element = self._find_project_dir(directory)
    
    99 101
     
    
    100 102
             # Absolute path to where elements are loaded from within the project
    
    101 103
             self.element_path = None
    
    ... ... @@ -116,7 +118,6 @@ class Project():
    116 118
             #
    
    117 119
             # Private Members
    
    118 120
             #
    
    119
    -        self._context = context  # The invocation Context
    
    120 121
     
    
    121 122
             self._default_mirror = default_mirror    # The name of the preferred mirror.
    
    122 123
     
    
    ... ... @@ -370,6 +371,14 @@ class Project():
    370 371
     
    
    371 372
             self._load_second_pass()
    
    372 373
     
    
    374
    +    # required_workspace_element()
    
    375
    +    #
    
    376
    +    # Returns the element whose workspace is required to load this project,
    
    377
    +    # if any.
    
    378
    +    #
    
    379
    +    def required_workspace_element(self):
    
    380
    +        return self._required_workspace_element
    
    381
    +
    
    373 382
         # cleanup()
    
    374 383
         #
    
    375 384
         # Cleans up resources used loading elements
    
    ... ... @@ -651,7 +660,7 @@ class Project():
    651 660
             # Source url aliases
    
    652 661
             output._aliases = _yaml.node_get(config, Mapping, 'aliases', default_value={})
    
    653 662
     
    
    654
    -    # _ensure_project_dir()
    
    663
    +    # _find_project_dir()
    
    655 664
         #
    
    656 665
         # Returns path of the project directory, if a configuration file is found
    
    657 666
         # in given directory or any of its parent directories.
    
    ... ... @@ -662,18 +671,26 @@ class Project():
    662 671
         # Raises:
    
    663 672
         #    LoadError if project.conf is not found
    
    664 673
         #
    
    665
    -    def _ensure_project_dir(self, directory):
    
    666
    -        directory = os.path.abspath(directory)
    
    667
    -        while not os.path.isfile(os.path.join(directory, _PROJECT_CONF_FILE)):
    
    668
    -            parent_dir = os.path.dirname(directory)
    
    669
    -            if directory == parent_dir:
    
    674
    +    # Returns:
    
    675
    +    #    (str) - the directory that contains the project, and
    
    676
    +    #    (str) - the name of the element required to find the project, or an empty string
    
    677
    +    #
    
    678
    +    def _find_project_dir(self, directory):
    
    679
    +        workspace_element = ""
    
    680
    +        project_directory = utils._search_upward_for_file(directory, _PROJECT_CONF_FILE)
    
    681
    +        if not project_directory:
    
    682
    +            workspace_locals = self._context.get_workspace_locals()
    
    683
    +            workspace_local = workspace_locals.get(directory)
    
    684
    +            if workspace_local.has_projects():
    
    685
    +                project_directory = workspace_local.get_default_path()
    
    686
    +                workspace_element = workspace_local.get_default_element()
    
    687
    +            else:
    
    670 688
                     raise LoadError(
    
    671 689
                         LoadErrorReason.MISSING_PROJECT_CONF,
    
    672 690
                         '{} not found in current directory or any of its parent directories'
    
    673 691
                         .format(_PROJECT_CONF_FILE))
    
    674
    -            directory = parent_dir
    
    675 692
     
    
    676
    -        return directory
    
    693
    +        return project_directory, workspace_element
    
    677 694
     
    
    678 695
         def _load_plugin_factories(self, config, output):
    
    679 696
             plugin_source_origins = []   # Origins of custom sources
    

  • buildstream/_stream.py
    ... ... @@ -532,6 +532,11 @@ class Stream():
    532 532
                 with target.timed_activity("Staging sources to {}".format(directory)):
    
    533 533
                     target._open_workspace()
    
    534 534
     
    
    535
    +        workspace_locals = self._context.get_workspace_locals()
    
    536
    +        project = self._context.get_toplevel_project()
    
    537
    +        workspace_local = workspace_locals.add(directory, project.directory, target._get_full_name())
    
    538
    +        workspace_local.write()
    
    539
    +
    
    535 540
             workspaces.save_config()
    
    536 541
             self._message(MessageType.INFO, "Saved workspace configuration")
    
    537 542
     
    
    ... ... @@ -556,6 +561,9 @@ class Stream():
    556 561
                     except OSError as e:
    
    557 562
                         raise StreamError("Could not remove  '{}': {}"
    
    558 563
                                           .format(workspace.get_absolute_path(), e)) from e
    
    564
    +        else:
    
    565
    +            workspace_locals = self._context.get_workspace_locals()
    
    566
    +            workspace_locals.remove(workspace.get_absolute_path())
    
    559 567
     
    
    560 568
             # Delete the workspace and save the configuration
    
    561 569
             workspaces.delete_workspace(element_name)
    
    ... ... @@ -599,6 +607,8 @@ class Stream():
    599 607
             for element in elements:
    
    600 608
                 workspace = workspaces.get_workspace(element._get_full_name())
    
    601 609
                 workspace_path = workspace.get_absolute_path()
    
    610
    +            workspace_locals = self._context.get_workspace_locals()
    
    611
    +            workspace_local = workspace_locals.get(workspace_path)
    
    602 612
                 if soft:
    
    603 613
                     workspace.prepared = False
    
    604 614
                     self._message(MessageType.INFO, "Reset workspace state for {} at: {}"
    
    ... ... @@ -619,6 +629,8 @@ class Stream():
    619 629
                 with element.timed_activity("Staging sources to {}".format(workspace_path)):
    
    620 630
                     element._open_workspace()
    
    621 631
     
    
    632
    +            workspace_local.write()
    
    633
    +
    
    622 634
                 self._message(MessageType.INFO,
    
    623 635
                               "Reset workspace for {} at: {}".format(element.name,
    
    624 636
                                                                      workspace_path))
    
    ... ... @@ -649,6 +661,20 @@ class Stream():
    649 661
     
    
    650 662
             return False
    
    651 663
     
    
    664
    +    # workspace_is_required()
    
    665
    +    #
    
    666
    +    # Checks whether the workspace belonging to element_name is required to
    
    667
    +    # load the project
    
    668
    +    #
    
    669
    +    # Args:
    
    670
    +    #    element_name (str): The element whose workspace may be required
    
    671
    +    #
    
    672
    +    # Returns:
    
    673
    +    #    (bool): True if the workspace is required
    
    674
    +    def workspace_is_required(self, element_name):
    
    675
    +        required_elm = self._project.required_workspace_element()
    
    676
    +        return required_elm == element_name
    
    677
    +
    
    652 678
         # workspace_list
    
    653 679
         #
    
    654 680
         # Serializes the workspaces and dumps them in YAML to stdout.
    

  • buildstream/_workspaces.py
    ... ... @@ -25,6 +25,209 @@ from ._exceptions import LoadError, LoadErrorReason
    25 25
     
    
    26 26
     
    
    27 27
     BST_WORKSPACE_FORMAT_VERSION = 3
    
    28
    +BST_WORKSPACE_LOCAL_FORMAT_VERSION = 1
    
    29
    +WORKSPACE_LOCAL_FILE = ".bstproject.yaml"
    
    30
    +
    
    31
    +
    
    32
    +# WorkspaceLocal()
    
    33
    +#
    
    34
    +# An object to contain various helper functions and data required for
    
    35
    +# referring from a workspace back to buildstream.
    
    36
    +#
    
    37
    +# Args:
    
    38
    +#    directory (str): The directory that the workspace exists in
    
    39
    +#    project_path (str): The project path used to refer back
    
    40
    +#                        to buildstream projects.
    
    41
    +#    element_name (str): The name of the element used to create this workspace.
    
    42
    +class WorkspaceLocal():
    
    43
    +    def __init__(self, directory, project_path="", element_name=""):
    
    44
    +        self._projects = []
    
    45
    +        self._directory = directory
    
    46
    +
    
    47
    +        assert (project_path and element_name) or (not project_path and not element_name)
    
    48
    +        if project_path:
    
    49
    +            self._add_project(project_path, element_name)
    
    50
    +
    
    51
    +    # has_projects()
    
    52
    +    #
    
    53
    +    # Whether this object contains any projects
    
    54
    +    #
    
    55
    +    # Returns:
    
    56
    +    #    (bool): True if any projects are stored, else False
    
    57
    +    def has_projects(self):
    
    58
    +        return True if self._projects else False
    
    59
    +
    
    60
    +    # get_default_path()
    
    61
    +    #
    
    62
    +    # Retrieves the default path to a project.
    
    63
    +    #
    
    64
    +    # Returns:
    
    65
    +    #    (str): The path to a project
    
    66
    +    def get_default_path(self):
    
    67
    +        return self._projects[0]['project-path']
    
    68
    +
    
    69
    +    # get_default_element()
    
    70
    +    #
    
    71
    +    # Retrieves the name of the element that owns this workspace.
    
    72
    +    #
    
    73
    +    # Returns:
    
    74
    +    #    (str): The name of an element
    
    75
    +    def get_default_element(self):
    
    76
    +        return self._projects[0]['element-name']
    
    77
    +
    
    78
    +    # to_dict()
    
    79
    +    #
    
    80
    +    # Turn the members data into a dict for serialization purposes
    
    81
    +    #
    
    82
    +    # Returns:
    
    83
    +    #    (dict): A dict representation of the WorkspaceLocal
    
    84
    +    #
    
    85
    +    def to_dict(self):
    
    86
    +        ret = {
    
    87
    +            'projects': self._projects,
    
    88
    +            'format-version': BST_WORKSPACE_LOCAL_FORMAT_VERSION,
    
    89
    +        }
    
    90
    +        return ret
    
    91
    +
    
    92
    +    # from_dict()
    
    93
    +    #
    
    94
    +    # Loads a new WorkspaceLocal from a simple dictionary
    
    95
    +    #
    
    96
    +    # Args:
    
    97
    +    #    directory (str): The directory that the workspace exists in
    
    98
    +    #    dictionary (dict): The dict to generate a WorkspaceLocal from
    
    99
    +    #
    
    100
    +    # Returns:
    
    101
    +    #   (WorkspaceLocal): A newly instantiated WorkspaceLocal
    
    102
    +    @classmethod
    
    103
    +    def from_dict(cls, directory, dictionary):
    
    104
    +        # Only know how to handle one format-version at the moment.
    
    105
    +        format_version = int(dictionary['format-version'])
    
    106
    +        assert format_version == BST_WORKSPACE_LOCAL_FORMAT_VERSION, \
    
    107
    +            "Format version {} not found in {}".format(BST_WORKSPACE_LOCAL_FORMAT_VERSION, dictionary)
    
    108
    +
    
    109
    +        workspace_local = cls(directory)
    
    110
    +        for item in dictionary['projects']:
    
    111
    +            workspace_local._add_project(item['project-path'], item['element-name'])
    
    112
    +
    
    113
    +        return workspace_local
    
    114
    +
    
    115
    +    # load()
    
    116
    +    #
    
    117
    +    # Loads the WorkspaceLocal for a given directory. This directory may be a
    
    118
    +    # subdirectory of the workspace's directory.
    
    119
    +    #
    
    120
    +    # Args:
    
    121
    +    #    directory (str): The directory
    
    122
    +    # Returns:
    
    123
    +    #    (WorkspaceLocal): The created WorkspaceLocal, if in a workspace, or
    
    124
    +    #    (NoneType): None, if the directory is not inside a workspace.
    
    125
    +    @classmethod
    
    126
    +    def load(cls, directory):
    
    127
    +        local_dir = cls.search_for_dir(directory)
    
    128
    +        if local_dir:
    
    129
    +            workspace_file = os.path.join(local_dir, WORKSPACE_LOCAL_FILE)
    
    130
    +            data_dict = _yaml.load(workspace_file)
    
    131
    +            return cls.from_dict(local_dir, data_dict)
    
    132
    +        else:
    
    133
    +            return None
    
    134
    +
    
    135
    +    # write()
    
    136
    +    #
    
    137
    +    # Writes the WorkspaceLocal to disk
    
    138
    +    def write(self):
    
    139
    +        os.makedirs(self._directory, exist_ok=True)
    
    140
    +        _yaml.dump(self.to_dict(), self._get_filename())
    
    141
    +
    
    142
    +    # search_for_dir()
    
    143
    +    #
    
    144
    +    # Returns the directory that contains the workspace local file,
    
    145
    +    # searching upwards from search_dir.
    
    146
    +    @staticmethod
    
    147
    +    def search_for_dir(search_dir):
    
    148
    +        return utils._search_upward_for_file(search_dir, WORKSPACE_LOCAL_FILE)
    
    149
    +
    
    150
    +    def _get_filename(self):
    
    151
    +        return os.path.join(self._directory, WORKSPACE_LOCAL_FILE)
    
    152
    +
    
    153
    +    def _add_project(self, project_path, element_name):
    
    154
    +        assert (project_path and element_name)
    
    155
    +        self._projects.append({'project-path': project_path, 'element-name': element_name})
    
    156
    +
    
    157
    +
    
    158
    +# WorkspaceLocals()
    
    159
    +#
    
    160
    +# A class to manage workspace local data for multiple workspaces.
    
    161
    +#
    
    162
    +class WorkspaceLocals():
    
    163
    +    def __init__(self):
    
    164
    +        self._locals = {}  # Mapping of a workspace directory to its WorkspaceLocal
    
    165
    +
    
    166
    +    # get()
    
    167
    +    #
    
    168
    +    # Returns a WorkspaceLocal for a given directory, loading one from disk if present
    
    169
    +    # and caching it.
    
    170
    +    #
    
    171
    +    # Args:
    
    172
    +    #    directory (str): The directory to search for a WorkspaceLocal.
    
    173
    +    #
    
    174
    +    # Returns:
    
    175
    +    #    (WorkspaceLocal): The WorkspaceLocal that was found for that directory.
    
    176
    +    #
    
    177
    +    def get(self, directory):
    
    178
    +        # NOTE: Later, this will load any WorkspaceLocal found from directory
    
    179
    +        try:
    
    180
    +            local = self._locals[directory]
    
    181
    +        except KeyError:
    
    182
    +            local = WorkspaceLocal.load(directory)
    
    183
    +            if not local:
    
    184
    +                local = WorkspaceLocal(directory)
    
    185
    +            self._locals[directory] = local
    
    186
    +
    
    187
    +        return local
    
    188
    +
    
    189
    +    # add()
    
    190
    +    #
    
    191
    +    # Adds the project path and element name to the WorkspaceLocal that exists
    
    192
    +    # for that directory
    
    193
    +    #
    
    194
    +    # Args:
    
    195
    +    #    directory (str): The directory to search for a WorkspaceLocal.
    
    196
    +    #    project_path (str): The path to the project that refers to this workspace
    
    197
    +    #    element_name (str): The element in the project that was refers to this workspace
    
    198
    +    #
    
    199
    +    # Returns:
    
    200
    +    #    (WorkspaceLocal): The WorkspaceLocal that was found for that directory.
    
    201
    +    #
    
    202
    +    def add(self, directory, project_path='', element_name=''):
    
    203
    +        local = self.get(directory)
    
    204
    +        if project_path:
    
    205
    +            local._add_project(project_path, element_name)
    
    206
    +        return local
    
    207
    +
    
    208
    +    # remove()
    
    209
    +    #
    
    210
    +    # Removes the project path and element name from the WorkspaceLocal that exists
    
    211
    +    # for that directory.
    
    212
    +    #
    
    213
    +    # NOTE: This currently just deletes the file, but with support for multiple
    
    214
    +    # projects opening the same workspace, this will involve decreasing the count
    
    215
    +    # and deleting the file if there are no more projects.
    
    216
    +    #
    
    217
    +    # Args:
    
    218
    +    #    directory (str): The directory to search for a WorkspaceLocal.
    
    219
    +    #    project_path (str): **UNUSED** The path to the project that refers to this workspace
    
    220
    +    #    element_name (str): **UNUSED** The element in the project that was refers to this workspace
    
    221
    +    #
    
    222
    +    def remove(self, directory, project_path='', element_name=''):
    
    223
    +        # NOTE: project_path and element_name will only be used when I implement
    
    224
    +        #       multiple owners of a workspace
    
    225
    +        local = self.get(directory)
    
    226
    +        path = local._get_filename()
    
    227
    +        try:
    
    228
    +            os.unlink(path)
    
    229
    +        except FileNotFoundError:
    
    230
    +            pass
    
    28 231
     
    
    29 232
     
    
    30 233
     # Workspace()
    
    ... ... @@ -174,10 +377,15 @@ class Workspace():
    174 377
             if recalculate or self._key is None:
    
    175 378
                 fullpath = self.get_absolute_path()
    
    176 379
     
    
    380
    +            excluded_files = [WORKSPACE_LOCAL_FILE]
    
    381
    +
    
    177 382
                 # Get a list of tuples of the the project relative paths and fullpaths
    
    178 383
                 if os.path.isdir(fullpath):
    
    179 384
                     filelist = utils.list_relative_paths(fullpath)
    
    180
    -                filelist = [(relpath, os.path.join(fullpath, relpath)) for relpath in filelist]
    
    385
    +                filelist = [
    
    386
    +                    (relpath, os.path.join(fullpath, relpath)) for relpath in filelist
    
    387
    +                    if relpath not in excluded_files
    
    388
    +                ]
    
    181 389
                 else:
    
    182 390
                     filelist = [(self.get_absolute_path(), fullpath)]
    
    183 391
     
    

  • buildstream/data/userconfig.yaml
    ... ... @@ -125,6 +125,14 @@ prompt:
    125 125
       #
    
    126 126
       really-workspace-close-remove-dir: ask
    
    127 127
     
    
    128
    +  # Whether to really proceed with 'bst workspace close' when doing so would
    
    129
    +  # stop them from running bst commands in this workspace.
    
    130
    +  #
    
    131
    +  #  ask - Ask the user if they are sure.
    
    132
    +  #  yes - Always close, without asking.
    
    133
    +  #
    
    134
    +  really-workspace-close-project-inaccessible: ask
    
    135
    +
    
    128 136
       # Whether to really proceed with 'bst workspace reset' doing a hard reset of
    
    129 137
       # a workspace, potentially losing changes.
    
    130 138
       #
    

  • tests/frontend/workspace.py
    ... ... @@ -29,6 +29,7 @@ import shutil
    29 29
     import subprocess
    
    30 30
     from ruamel.yaml.comments import CommentedSet
    
    31 31
     from tests.testutils import cli, create_repo, ALL_REPO_KINDS, wait_for_cache_granularity
    
    32
    +from tests.testutils import create_artifact_share
    
    32 33
     
    
    33 34
     from buildstream import _yaml
    
    34 35
     from buildstream._exceptions import ErrorDomain, LoadError, LoadErrorReason
    
    ... ... @@ -93,6 +94,13 @@ def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir
    93 94
     
    
    94 95
         result.assert_success()
    
    95 96
     
    
    97
    +    # Assert that a .bstproject.yaml file has been created
    
    98
    +    # and contains the path to the project
    
    99
    +    bstproject_path = os.path.join(workspace_dir, '.bstproject.yaml')
    
    100
    +    assert os.path.exists(bstproject_path)
    
    101
    +    with open(bstproject_path) as f:
    
    102
    +        assert project_path in f.read()
    
    103
    +
    
    96 104
         # Assert that we are now buildable because the source is
    
    97 105
         # now cached.
    
    98 106
         assert cli.get_element_state(project_path, element_name) == 'buildable'
    
    ... ... @@ -148,6 +156,10 @@ def test_open_force(cli, tmpdir, datafiles, kind):
    148 156
         # Assert the workspace dir still exists
    
    149 157
         assert os.path.exists(workspace)
    
    150 158
     
    
    159
    +    # Assert the bstproject doesn't exist
    
    160
    +    bstproject_path = os.path.join(workspace, '.bstproject.yaml')
    
    161
    +    assert not os.path.exists(bstproject_path)
    
    162
    +
    
    151 163
         # Now open the workspace again with --force, this should happily succeed
    
    152 164
         result = cli.run(project=project, args=[
    
    153 165
             'workspace', 'open', '--force', element_name, workspace
    
    ... ... @@ -436,9 +448,12 @@ def test_list(cli, tmpdir, datafiles):
    436 448
     @pytest.mark.datafiles(DATA_DIR)
    
    437 449
     @pytest.mark.parametrize("kind", repo_kinds)
    
    438 450
     @pytest.mark.parametrize("strict", [("strict"), ("non-strict")])
    
    439
    -def test_build(cli, tmpdir, datafiles, kind, strict):
    
    451
    +@pytest.mark.parametrize("call_from", [("project"), ("workspace")])
    
    452
    +def test_build(cli, tmpdir_factory, datafiles, kind, strict, call_from):
    
    453
    +    tmpdir = tmpdir_factory.mktemp('')
    
    440 454
         element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False)
    
    441 455
         checkout = os.path.join(str(tmpdir), 'checkout')
    
    456
    +    args_pre = ['-C', workspace] if call_from == "project" else []
    
    442 457
     
    
    443 458
         # Modify workspace
    
    444 459
         shutil.rmtree(os.path.join(workspace, 'usr', 'bin'))
    
    ... ... @@ -461,15 +476,14 @@ def test_build(cli, tmpdir, datafiles, kind, strict):
    461 476
         # Build modified workspace
    
    462 477
         assert cli.get_element_state(project, element_name) == 'buildable'
    
    463 478
         assert cli.get_element_key(project, element_name) == "{:?<64}".format('')
    
    464
    -    result = cli.run(project=project, args=['build', element_name])
    
    479
    +    result = cli.run(project=project, args=args_pre + ['build', element_name])
    
    465 480
         result.assert_success()
    
    466 481
         assert cli.get_element_state(project, element_name) == 'cached'
    
    467 482
         assert cli.get_element_key(project, element_name) != "{:?<64}".format('')
    
    468 483
     
    
    469 484
         # Checkout the result
    
    470
    -    result = cli.run(project=project, args=[
    
    471
    -        'checkout', element_name, checkout
    
    472
    -    ])
    
    485
    +    result = cli.run(project=project,
    
    486
    +                     args=args_pre + ['checkout', element_name, checkout])
    
    473 487
         result.assert_success()
    
    474 488
     
    
    475 489
         # Check that the pony.conf from the modified workspace exists
    
    ... ... @@ -876,3 +890,131 @@ def test_multiple_failed_builds(cli, tmpdir, datafiles):
    876 890
             result = cli.run(project=project, args=["build", element_name])
    
    877 891
             assert "BUG" not in result.stderr
    
    878 892
             assert cli.get_element_state(project, element_name) != "cached"
    
    893
    +
    
    894
    +
    
    895
    +@pytest.mark.datafiles(DATA_DIR)
    
    896
    +def test_external_fetch(cli, datafiles, tmpdir_factory):
    
    897
    +    # Fetching from a workspace outside a project doesn't fail horribly
    
    898
    +    tmpdir = tmpdir_factory.mktemp('')
    
    899
    +    element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", False)
    
    900
    +
    
    901
    +    result = cli.run(project=project, args=['-C', workspace, 'fetch', element_name])
    
    902
    +    result.assert_success()
    
    903
    +
    
    904
    +    # We already fetched it by opening the workspace, but we're also checking
    
    905
    +    # `bst show` works here
    
    906
    +    assert cli.get_element_state(project, element_name) == 'buildable'
    
    907
    +
    
    908
    +
    
    909
    +@pytest.mark.datafiles(DATA_DIR)
    
    910
    +def test_external_push_pull(cli, datafiles, tmpdir_factory):
    
    911
    +    # Pushing and pulling to/from an artifact cache works from an external workspace
    
    912
    +    tmpdir = tmpdir_factory.mktemp('')
    
    913
    +    element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", False)
    
    914
    +
    
    915
    +    with create_artifact_share(os.path.join(str(tmpdir), 'artifactshare')) as share:
    
    916
    +        result = cli.run(project=project, args=['-C', workspace, 'build', element_name])
    
    917
    +        result.assert_success()
    
    918
    +
    
    919
    +        cli.configure({
    
    920
    +            'artifacts': {'url': share.repo, 'push': True}
    
    921
    +        })
    
    922
    +
    
    923
    +        result = cli.run(project=project, args=['-C', workspace, 'push', element_name])
    
    924
    +        result.assert_success()
    
    925
    +
    
    926
    +        result = cli.run(project=project, args=['-C', workspace, 'pull', '--deps', 'all', 'target.bst'])
    
    927
    +        result.assert_success()
    
    928
    +
    
    929
    +
    
    930
    +@pytest.mark.datafiles(DATA_DIR)
    
    931
    +def test_external_track(cli, datafiles, tmpdir_factory):
    
    932
    +    # Tracking does not get horribly confused
    
    933
    +    tmpdir = tmpdir_factory.mktemp('')
    
    934
    +    element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", True)
    
    935
    +
    
    936
    +    # The workspace is necessarily already tracked, so we only care that
    
    937
    +    # there's no weird errors.
    
    938
    +    result = cli.run(project=project, args=['-C', workspace, 'track', element_name])
    
    939
    +    result.assert_success()
    
    940
    +
    
    941
    +
    
    942
    +@pytest.mark.datafiles(DATA_DIR)
    
    943
    +def test_external_open_other(cli, datafiles, tmpdir_factory):
    
    944
    +    # From inside an external workspace, open another workspace
    
    945
    +    tmpdir1 = tmpdir_factory.mktemp('')
    
    946
    +    tmpdir2 = tmpdir_factory.mktemp('')
    
    947
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    948
    +    alpha_element, project, alpha_workspace = open_workspace(cli, tmpdir1, datafiles, "git", False, suffix="-alpha")
    
    949
    +    beta_element, _, beta_workspace = open_workspace(cli, tmpdir2, datafiles, "git", False, suffix="-beta")
    
    950
    +
    
    951
    +    # Closing the other element first, because I'm too lazy to create an
    
    952
    +    # element without opening it
    
    953
    +    result = cli.run(project=project, args=['workspace', 'close', beta_element])
    
    954
    +    result.assert_success()
    
    955
    +
    
    956
    +    result = cli.run(project=project, args=[
    
    957
    +        '-C', alpha_workspace, 'workspace', 'open', '--force', beta_element, beta_workspace
    
    958
    +    ])
    
    959
    +    result.assert_success()
    
    960
    +
    
    961
    +
    
    962
    +@pytest.mark.datafiles(DATA_DIR)
    
    963
    +def test_external_close_other(cli, datafiles, tmpdir_factory):
    
    964
    +    # From inside an external workspace, close the other workspace
    
    965
    +    tmpdir1 = tmpdir_factory.mktemp('')
    
    966
    +    tmpdir2 = tmpdir_factory.mktemp('')
    
    967
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    968
    +    alpha_element, project, alpha_workspace = open_workspace(cli, tmpdir1, datafiles, "git", False, suffix="-alpha")
    
    969
    +    beta_element, _, beta_workspace = open_workspace(cli, tmpdir2, datafiles, "git", False, suffix="-beta")
    
    970
    +
    
    971
    +    result = cli.run(project=project, args=['-C', alpha_workspace, 'workspace', 'close', beta_element])
    
    972
    +    result.assert_success()
    
    973
    +
    
    974
    +
    
    975
    +@pytest.mark.datafiles(DATA_DIR)
    
    976
    +def test_external_close_self(cli, datafiles, tmpdir_factory):
    
    977
    +    # From inside an external workspace, close it
    
    978
    +    tmpdir1 = tmpdir_factory.mktemp('')
    
    979
    +    tmpdir2 = tmpdir_factory.mktemp('')
    
    980
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    981
    +    alpha_element, project, alpha_workspace = open_workspace(cli, tmpdir1, datafiles, "git", False, suffix="-alpha")
    
    982
    +    beta_element, _, beta_workspace = open_workspace(cli, tmpdir2, datafiles, "git", False, suffix="-beta")
    
    983
    +
    
    984
    +    result = cli.run(project=project, args=['-C', alpha_workspace, 'workspace', 'close', alpha_element])
    
    985
    +    result.assert_success()
    
    986
    +
    
    987
    +
    
    988
    +@pytest.mark.datafiles(DATA_DIR)
    
    989
    +def test_external_reset_other(cli, datafiles, tmpdir_factory):
    
    990
    +    tmpdir1 = tmpdir_factory.mktemp('')
    
    991
    +    tmpdir2 = tmpdir_factory.mktemp('')
    
    992
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    993
    +    alpha_element, project, alpha_workspace = open_workspace(cli, tmpdir1, datafiles, "git", False, suffix="-alpha")
    
    994
    +    beta_element, _, beta_workspace = open_workspace(cli, tmpdir2, datafiles, "git", False, suffix="-beta")
    
    995
    +
    
    996
    +    result = cli.run(project=project, args=['-C', alpha_workspace, 'workspace', 'reset', beta_element])
    
    997
    +    result.assert_success()
    
    998
    +
    
    999
    +
    
    1000
    +@pytest.mark.datafiles(DATA_DIR)
    
    1001
    +def test_external_reset_self(cli, datafiles, tmpdir):
    
    1002
    +    element, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", False)
    
    1003
    +
    
    1004
    +    # Command succeeds
    
    1005
    +    result = cli.run(project=project, args=['-C', workspace, 'workspace', 'reset', element])
    
    1006
    +    result.assert_success()
    
    1007
    +
    
    1008
    +    # Successive commands still work (i.e. .bstproject.yaml hasn't been deleted)
    
    1009
    +    result = cli.run(project=project, args=['-C', workspace, 'workspace', 'list'])
    
    1010
    +    result.assert_success()
    
    1011
    +
    
    1012
    +
    
    1013
    +@pytest.mark.datafiles(DATA_DIR)
    
    1014
    +def test_external_list(cli, datafiles, tmpdir_factory):
    
    1015
    +    tmpdir = tmpdir_factory.mktemp('')
    
    1016
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    1017
    +    element, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", False)
    
    1018
    +
    
    1019
    +    result = cli.run(project=project, args=['-C', workspace, 'workspace', 'list'])
    
    1020
    +    result.assert_success()

  • tests/integration/shell.py
    ... ... @@ -353,3 +353,28 @@ def test_integration_devices(cli, tmpdir, datafiles):
    353 353
     
    
    354 354
         result = execute_shell(cli, project, ["true"], element=element_name)
    
    355 355
         assert result.exit_code == 0
    
    356
    +
    
    357
    +
    
    358
    +# Test that a shell can be opened from an external workspace
    
    359
    +@pytest.mark.datafiles(DATA_DIR)
    
    360
    +@pytest.mark.parametrize("build_shell", [("build"), ("nobuild")])
    
    361
    +def test_integration_external_workspace(cli, tmpdir_factory, datafiles, build_shell):
    
    362
    +    tmpdir = tmpdir_factory.mktemp("")
    
    363
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    364
    +    element_name = 'autotools/amhello.bst'
    
    365
    +    workspace_dir = os.path.join(str(tmpdir), 'workspace')
    
    366
    +
    
    367
    +    result = cli.run(project=project, args=[
    
    368
    +        'workspace', 'open', element_name, workspace_dir
    
    369
    +    ])
    
    370
    +    result.assert_success()
    
    371
    +
    
    372
    +    result = cli.run(project=project, args=['-C', workspace_dir, 'build', element_name])
    
    373
    +    result.assert_success()
    
    374
    +
    
    375
    +    command = ['shell']
    
    376
    +    if build_shell == 'build':
    
    377
    +        command.append('--build')
    
    378
    +    command.extend([element_name, '--', 'true'])
    
    379
    +    result = cli.run(project=project, cwd=workspace_dir, args=command)
    
    380
    +    result.assert_success()



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]