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



Title: GitLab

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

Commits:

6 changed files:

Changes:

  • 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.
    

  • buildstream/_project.py
    ... ... @@ -40,6 +40,7 @@ from .element import Element
    40 40
     from ._message import Message, MessageType
    
    41 41
     from ._includes import Includes
    
    42 42
     from ._platform import Platform
    
    43
    +from ._workspaces import WorkspaceLocal
    
    43 44
     
    
    44 45
     
    
    45 46
     # Project Configuration file
    
    ... ... @@ -95,7 +96,7 @@ class Project():
    95 96
             self.name = None
    
    96 97
     
    
    97 98
             # The project directory
    
    98
    -        self.directory = self._ensure_project_dir(directory)
    
    99
    +        self.directory = self._find_project_dir(directory)
    
    99 100
     
    
    100 101
             # Absolute path to where elements are loaded from within the project
    
    101 102
             self.element_path = None
    
    ... ... @@ -647,7 +648,7 @@ class Project():
    647 648
             # Source url aliases
    
    648 649
             output._aliases = _yaml.node_get(config, Mapping, 'aliases', default_value={})
    
    649 650
     
    
    650
    -    # _ensure_project_dir()
    
    651
    +    # _find_project_dir()
    
    651 652
         #
    
    652 653
         # Returns path of the project directory, if a configuration file is found
    
    653 654
         # in given directory or any of its parent directories.
    
    ... ... @@ -658,18 +659,19 @@ class Project():
    658 659
         # Raises:
    
    659 660
         #    LoadError if project.conf is not found
    
    660 661
         #
    
    661
    -    def _ensure_project_dir(self, directory):
    
    662
    -        directory = os.path.abspath(directory)
    
    663
    -        while not os.path.isfile(os.path.join(directory, _PROJECT_CONF_FILE)):
    
    664
    -            parent_dir = os.path.dirname(directory)
    
    665
    -            if directory == parent_dir:
    
    662
    +    def _find_project_dir(self, directory):
    
    663
    +        project_directory = utils._search_upward_for_file(directory, _PROJECT_CONF_FILE)
    
    664
    +        if not project_directory:
    
    665
    +            workspace_local = WorkspaceLocal.load(directory)
    
    666
    +            if workspace_local:
    
    667
    +                project_directory = workspace_local.get_default_path()
    
    668
    +            else:
    
    666 669
                     raise LoadError(
    
    667 670
                         LoadErrorReason.MISSING_PROJECT_CONF,
    
    668 671
                         '{} not found in current directory or any of its parent directories'
    
    669 672
                         .format(_PROJECT_CONF_FILE))
    
    670
    -            directory = parent_dir
    
    671 673
     
    
    672
    -        return directory
    
    674
    +        return project_directory
    
    673 675
     
    
    674 676
         def _load_plugin_factories(self, config, output):
    
    675 677
             plugin_source_origins = []   # Origins of custom sources
    

  • buildstream/_stream.py
    ... ... @@ -32,6 +32,7 @@ from ._exceptions import StreamError, ImplError, BstError, set_last_task_error
    32 32
     from ._message import Message, MessageType
    
    33 33
     from ._scheduler import Scheduler, SchedStatus, TrackQueue, FetchQueue, BuildQueue, PullQueue, PushQueue
    
    34 34
     from ._pipeline import Pipeline, PipelineSelection
    
    35
    +from ._workspaces import WorkspaceLocal
    
    35 36
     from . import utils, _yaml, _site
    
    36 37
     from . import Scope, Consistency
    
    37 38
     
    
    ... ... @@ -516,6 +517,10 @@ class Stream():
    516 517
                 with target.timed_activity("Staging sources to {}".format(directory)):
    
    517 518
                     target._open_workspace()
    
    518 519
     
    
    520
    +        project = self._context.get_toplevel_project()
    
    521
    +        workspace_local = WorkspaceLocal.create(directory, project.directory)
    
    522
    +        workspace_local.write()
    
    523
    +
    
    519 524
             workspaces.save_config()
    
    520 525
             self._message(MessageType.INFO, "Saved workspace configuration")
    
    521 526
     
    
    ... ... @@ -540,6 +545,11 @@ class Stream():
    540 545
                     except OSError as e:
    
    541 546
                         raise StreamError("Could not remove  '{}': {}"
    
    542 547
                                           .format(workspace.get_absolute_path(), e)) from e
    
    548
    +        else:
    
    549
    +            # TODO: At some point, closing a workspace only deletes the file if no projects are using it.
    
    550
    +            workspace_local = WorkspaceLocal.load(workspace.get_absolute_path())
    
    551
    +            if workspace_local:
    
    552
    +                workspace_local.delete()
    
    543 553
     
    
    544 554
             # Delete the workspace and save the configuration
    
    545 555
             workspaces.delete_workspace(element_name)
    

  • buildstream/_workspaces.py
    ... ... @@ -25,6 +25,127 @@ 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_paths (list of str): The project paths used to refer back
    
    40
    +#                                 to buildstream projects.
    
    41
    +class WorkspaceLocal():
    
    42
    +    def __init__(self, directory, project_paths):
    
    43
    +        self._project_paths = project_paths
    
    44
    +        self._directory = directory
    
    45
    +
    
    46
    +    # get_default_path()
    
    47
    +    #
    
    48
    +    # Retrieves the default path to a project.
    
    49
    +    #
    
    50
    +    # Returns:
    
    51
    +    #    (str): The path to a project
    
    52
    +    def get_default_path(self):
    
    53
    +        return self._project_paths[0]
    
    54
    +
    
    55
    +    # to_dict()
    
    56
    +    #
    
    57
    +    # Turn the members data into a dict for serialization purposes
    
    58
    +    #
    
    59
    +    # Returns:
    
    60
    +    #    (dict): A dict representation of the WorkspaceLocal
    
    61
    +    #
    
    62
    +    def to_dict(self):
    
    63
    +        ret = {
    
    64
    +            'projects': self._project_paths,
    
    65
    +            'format-version': BST_WORKSPACE_LOCAL_FORMAT_VERSION,
    
    66
    +        }
    
    67
    +        return ret
    
    68
    +
    
    69
    +    # from_dict()
    
    70
    +    #
    
    71
    +    # Loads a new WorkspaceLocal from a simple dictionary
    
    72
    +    #
    
    73
    +    # Args:
    
    74
    +    #    directory (str): The directory that the workspace exists in
    
    75
    +    #    dictionary (dict): The dict to generate a WorkspaceLocal from
    
    76
    +    #
    
    77
    +    # Returns:
    
    78
    +    #   (WorkspaceLocal): A newly instantiated WorkspaceLocal
    
    79
    +    @classmethod
    
    80
    +    def from_dict(cls, directory, dictionary):
    
    81
    +        # Only know how to handle one format-version at the moment.
    
    82
    +        format_version = int(dictionary['format-version'])
    
    83
    +        assert format_version == BST_WORKSPACE_LOCAL_FORMAT_VERSION, \
    
    84
    +            "Format version {} not found in {}".format(BST_WORKSPACE_LOCAL_FORMAT_VERSION, dictionary)
    
    85
    +
    
    86
    +        return cls(directory, dictionary['projects'])
    
    87
    +
    
    88
    +    # create()
    
    89
    +    #
    
    90
    +    # Creates a new WorkspaceLocal
    
    91
    +    #
    
    92
    +    # Args:
    
    93
    +    #    directory (str): The directory that the workspace exists in
    
    94
    +    #    project_path (str): The path to the project to store
    
    95
    +    #
    
    96
    +    # Returns:
    
    97
    +    #    (WorkspaceLocal): The created WorkspaceLocal
    
    98
    +    @classmethod
    
    99
    +    def create(cls, directory, project_path):
    
    100
    +        # TODO: Load WorkspaceLocal if it exists, and maybe add project_path to it
    
    101
    +        return cls(directory, [project_path])
    
    102
    +
    
    103
    +    # load()
    
    104
    +    #
    
    105
    +    # Loads the WorkspaceLocal for a given directory. This directory may be a
    
    106
    +    # subdirectory of the workspace's directory.
    
    107
    +    #
    
    108
    +    # Args:
    
    109
    +    #    directory (str): The directory
    
    110
    +    # Returns:
    
    111
    +    #    (WorkspaceLocal): The created WorkspaceLocal, if in a workspace, or
    
    112
    +    #    (NoneType): None, if the directory is not inside a workspace.
    
    113
    +    @classmethod
    
    114
    +    def load(cls, directory):
    
    115
    +        local_dir = cls.search_for_dir(directory)
    
    116
    +        if local_dir:
    
    117
    +            workspace_file = os.path.join(local_dir, WORKSPACE_LOCAL_FILE)
    
    118
    +            data_dict = _yaml.load(workspace_file)
    
    119
    +            return cls.from_dict(local_dir, data_dict)
    
    120
    +        else:
    
    121
    +            return None
    
    122
    +
    
    123
    +    # write()
    
    124
    +    #
    
    125
    +    # Writes the WorkspaceLocal to disk
    
    126
    +    def write(self):
    
    127
    +        os.makedirs(self._directory, exist_ok=True)
    
    128
    +        _yaml.dump(self.to_dict(), self._get_filename())
    
    129
    +
    
    130
    +    # delete()
    
    131
    +    #
    
    132
    +    # Deletes the WorkspaceLocal from disk, if it exists.
    
    133
    +    def delete(self):
    
    134
    +        try:
    
    135
    +            os.unlink(self._get_filename())
    
    136
    +        except FileNotFoundError:
    
    137
    +            pass
    
    138
    +
    
    139
    +    # search_for_dir()
    
    140
    +    #
    
    141
    +    # Returns the directory that contains the workspace local file,
    
    142
    +    # searching upwards from search_dir.
    
    143
    +    @staticmethod
    
    144
    +    def search_for_dir(search_dir):
    
    145
    +        return utils._search_upward_for_file(search_dir, WORKSPACE_LOCAL_FILE)
    
    146
    +
    
    147
    +    def _get_filename(self):
    
    148
    +        return os.path.join(self._directory, WORKSPACE_LOCAL_FILE)
    
    28 149
     
    
    29 150
     
    
    30 151
     # Workspace()
    

  • buildstream/utils.py
    ... ... @@ -1198,3 +1198,17 @@ def _deduplicate(iterable, key=None):
    1198 1198
     def _get_link_mtime(path):
    
    1199 1199
         path_stat = os.lstat(path)
    
    1200 1200
         return path_stat.st_mtime
    
    1201
    +
    
    1202
    +
    
    1203
    +# Returns the first directory to contain filename, or an empty string if
    
    1204
    +# none found
    
    1205
    +#
    
    1206
    +def _search_upward_for_file(directory, filename):
    
    1207
    +    directory = os.path.abspath(directory)
    
    1208
    +    while not os.path.isfile(os.path.join(directory, filename)):
    
    1209
    +        parent_dir = os.path.dirname(directory)
    
    1210
    +        if directory == parent_dir:
    
    1211
    +            return ""
    
    1212
    +        directory = parent_dir
    
    1213
    +
    
    1214
    +    return directory

  • tests/frontend/workspace.py
    ... ... @@ -93,6 +93,13 @@ def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir
    93 93
     
    
    94 94
         result.assert_success()
    
    95 95
     
    
    96
    +    # Assert that a .bstproject.yaml file has been created
    
    97
    +    # and contains the path to the project
    
    98
    +    bstproject_path = os.path.join(workspace_dir, '.bstproject.yaml')
    
    99
    +    assert os.path.exists(bstproject_path)
    
    100
    +    with open(bstproject_path) as f:
    
    101
    +        assert project_path in f.read()
    
    102
    +
    
    96 103
         # Assert that we are now buildable because the source is
    
    97 104
         # now cached.
    
    98 105
         assert cli.get_element_state(project_path, element_name) == 'buildable'
    
    ... ... @@ -148,6 +155,10 @@ def test_open_force(cli, tmpdir, datafiles, kind):
    148 155
         # Assert the workspace dir still exists
    
    149 156
         assert os.path.exists(workspace)
    
    150 157
     
    
    158
    +    # Assert the bstproject doesn't exist
    
    159
    +    bstproject_path = os.path.join(workspace, '.bstproject.yaml')
    
    160
    +    assert not os.path.exists(bstproject_path)
    
    161
    +
    
    151 162
         # Now open the workspace again with --force, this should happily succeed
    
    152 163
         result = cli.run(project=project, args=[
    
    153 164
             'workspace', 'open', '--force', element_name, workspace
    
    ... ... @@ -436,7 +447,9 @@ def test_list(cli, tmpdir, datafiles):
    436 447
     @pytest.mark.datafiles(DATA_DIR)
    
    437 448
     @pytest.mark.parametrize("kind", repo_kinds)
    
    438 449
     @pytest.mark.parametrize("strict", [("strict"), ("non-strict")])
    
    439
    -def test_build(cli, tmpdir, datafiles, kind, strict):
    
    450
    +@pytest.mark.parametrize("call_from", [("project"), ("workspace")])
    
    451
    +def test_build(cli, tmpdir_factory, datafiles, kind, strict, call_from):
    
    452
    +    tmpdir = tmpdir_factory.mktemp('')
    
    440 453
         element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False)
    
    441 454
         checkout = os.path.join(str(tmpdir), 'checkout')
    
    442 455
     
    
    ... ... @@ -461,7 +474,8 @@ def test_build(cli, tmpdir, datafiles, kind, strict):
    461 474
         # Build modified workspace
    
    462 475
         assert cli.get_element_state(project, element_name) == 'buildable'
    
    463 476
         assert cli.get_element_key(project, element_name) == "{:?<64}".format('')
    
    464
    -    result = cli.run(project=project, args=['build', element_name])
    
    477
    +    result = cli.run(project=project if call_from == "project" else workspace,
    
    478
    +                     args=['build', element_name])
    
    465 479
         result.assert_success()
    
    466 480
         assert cli.get_element_state(project, element_name) == 'cached'
    
    467 481
         assert cli.get_element_key(project, element_name) != "{:?<64}".format('')
    



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