[Notes] [Git][BuildStream/buildstream][jonathan/workspace-fragment-create] 2 commits: TIDYME: Neaten workspace opening logic



Title: GitLab

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

Commits:

3 changed files:

Changes:

  • buildstream/_context.py
    ... ... @@ -293,7 +293,7 @@ class Context():
    293 293
         #
    
    294 294
         def add_project(self, project):
    
    295 295
             if not self._projects:
    
    296
    -            self._workspaces = Workspaces(project)
    
    296
    +            self._workspaces = Workspaces(project, self._workspace_project_cache)
    
    297 297
             self._projects.append(project)
    
    298 298
     
    
    299 299
         # get_projects():
    

  • buildstream/_stream.py
    ... ... @@ -563,8 +563,6 @@ class Stream():
    563 563
             # So far this function has tried to catch as many issues as possible with out making any changes
    
    564 564
             # Now it dose the bits that can not be made atomic.
    
    565 565
             targetGenerator = zip(elements, expanded_directories)
    
    566
    -        workspace_project_cache = self._context.get_workspace_project_cache()
    
    567
    -        project = self._context.get_toplevel_project()
    
    568 566
             for target, directory in targetGenerator:
    
    569 567
                 self._message(MessageType.INFO, "Creating workspace for element {}"
    
    570 568
                               .format(target.name))
    
    ... ... @@ -583,19 +581,7 @@ class Stream():
    583 581
                         todo_elements = "\nDid not try to create workspaces for " + todo_elements
    
    584 582
                     raise StreamError("Failed to create workspace directory: {}".format(e) + todo_elements) from e
    
    585 583
     
    
    586
    -            workspaces.create_workspace(target._get_full_name(), directory)
    
    587
    -
    
    588
    -            if not no_checkout:
    
    589
    -                with target.timed_activity("Staging sources to {}".format(directory)):
    
    590
    -                    target._open_workspace()
    
    591
    -
    
    592
    -            workspace_project = workspace_project_cache.add(directory, project.directory,
    
    593
    -                                                            target._get_full_name())
    
    594
    -            workspace_project.write()
    
    595
    -
    
    596
    -            # Saving the workspace once it is set up means that if the next workspace fails to be created before
    
    597
    -            # the configuration gets saved. The successfully created workspace still gets saved.
    
    598
    -            workspaces.save_config()
    
    584
    +            workspaces.create_workspace(target, directory, not no_checkout)
    
    599 585
                 self._message(MessageType.INFO, "Created a workspace for element: {}"
    
    600 586
                               .format(target._get_full_name()))
    
    601 587
     
    
    ... ... @@ -620,16 +606,6 @@ class Stream():
    620 606
                     except OSError as e:
    
    621 607
                         raise StreamError("Could not remove  '{}': {}"
    
    622 608
                                           .format(workspace.get_absolute_path(), e)) from e
    
    623
    -        else:
    
    624
    -            workspace_project_cache = self._context.get_workspace_project_cache()
    
    625
    -            try:
    
    626
    -                workspace_project_cache.remove(workspace.get_absolute_path())
    
    627
    -            except LoadError as e:
    
    628
    -                # We might be closing a workspace with a deleted directory
    
    629
    -                if e.reason == LoadErrorReason.MISSING_FILE:
    
    630
    -                    pass
    
    631
    -                else:
    
    632
    -                    raise
    
    633 609
     
    
    634 610
             # Delete the workspace and save the configuration
    
    635 611
             workspaces.delete_workspace(element_name)
    
    ... ... @@ -673,8 +649,6 @@ class Stream():
    673 649
             for element in elements:
    
    674 650
                 workspace = workspaces.get_workspace(element._get_full_name())
    
    675 651
                 workspace_path = workspace.get_absolute_path()
    
    676
    -            workspace_project_cache = self._context.get_workspace_project_cache()
    
    677
    -            workspace_project = workspace_project_cache.get(workspace_path)
    
    678 652
                 if soft:
    
    679 653
                     workspace.prepared = False
    
    680 654
                     self._message(MessageType.INFO, "Reset workspace state for {} at: {}"
    
    ... ... @@ -690,12 +664,7 @@ class Stream():
    690 664
                                           .format(workspace_path, e)) from e
    
    691 665
     
    
    692 666
                 workspaces.delete_workspace(element._get_full_name())
    
    693
    -            workspaces.create_workspace(element._get_full_name(), workspace_path)
    
    694
    -
    
    695
    -            with element.timed_activity("Staging sources to {}".format(workspace_path)):
    
    696
    -                element._open_workspace()
    
    697
    -
    
    698
    -            workspace_project.write()
    
    667
    +            workspaces.create_workspace(element, workspace_path, True)
    
    699 668
     
    
    700 669
                 self._message(MessageType.INFO,
    
    701 670
                               "Reset workspace for {} at: {}".format(element.name,
    

  • buildstream/_workspaces.py
    ... ... @@ -417,12 +417,14 @@ class Workspace():
    417 417
     #
    
    418 418
     # Args:
    
    419 419
     #    toplevel_project (Project): Top project used to resolve paths.
    
    420
    +#    workspace_project_cache (WorkspaceProjectCache): The cache of WorkspaceProjects
    
    420 421
     #
    
    421 422
     class Workspaces():
    
    422
    -    def __init__(self, toplevel_project):
    
    423
    +    def __init__(self, toplevel_project, workspace_project_cache):
    
    423 424
             self._toplevel_project = toplevel_project
    
    424 425
             self._bst_directory = os.path.join(toplevel_project.directory, ".bst")
    
    425 426
             self._workspaces = self._load_config()
    
    427
    +        self._workspace_project_cache = workspace_project_cache
    
    426 428
     
    
    427 429
         # list()
    
    428 430
         #
    
    ... ... @@ -437,19 +439,36 @@ class Workspaces():
    437 439
     
    
    438 440
         # create_workspace()
    
    439 441
         #
    
    440
    -    # Create a workspace in the given path for the given element.
    
    442
    +    # Create a workspace in the given path for the given element, and potentially
    
    443
    +    # checks-out the target into it.
    
    441 444
         #
    
    442 445
         # Args:
    
    443
    -    #    element_name (str) - The element name to create a workspace for
    
    446
    +    #    target (Element) - The element to create a workspace for
    
    444 447
         #    path (str) - The path in which the workspace should be kept
    
    448
    +    #    checkout (bool): Whether to check-out the element's sources into the directory
    
    445 449
         #
    
    446
    -    def create_workspace(self, element_name, path):
    
    447
    -        if path.startswith(self._toplevel_project.directory):
    
    448
    -            path = os.path.relpath(path, self._toplevel_project.directory)
    
    450
    +    def create_workspace(self, target, path, checkout):
    
    451
    +        element_name = target._get_full_name()
    
    452
    +        project_dir = self._toplevel_project.directory
    
    453
    +        if path.startswith(project_dir):
    
    454
    +            workspace_path = os.path.relpath(path, project_dir)
    
    455
    +        else:
    
    456
    +            workspace_path = path
    
    449 457
     
    
    450
    -        self._workspaces[element_name] = Workspace(self._toplevel_project, path=path)
    
    458
    +        self._workspaces[element_name] = Workspace(self._toplevel_project, path=workspace_path)
    
    451 459
     
    
    452
    -        return self._workspaces[element_name]
    
    460
    +        if checkout:
    
    461
    +            with target.timed_activity("Staging sources to {}".format(path)):
    
    462
    +                target._open_workspace()
    
    463
    +
    
    464
    +        workspace_project = self._workspace_project_cache.add(path, project_dir, element_name)
    
    465
    +        project_file_path = workspace_project.get_filename()
    
    466
    +
    
    467
    +        if os.path.exists(project_file_path):
    
    468
    +            target.warn("{} was staged from this element's sources".format(WORKSPACE_PROJECT_FILE))
    
    469
    +        workspace_project.write()
    
    470
    +
    
    471
    +        self.save_config()
    
    453 472
     
    
    454 473
         # get_workspace()
    
    455 474
         #
    
    ... ... @@ -498,8 +517,19 @@ class Workspaces():
    498 517
         #    element_name (str) - The element name whose workspace to delete
    
    499 518
         #
    
    500 519
         def delete_workspace(self, element_name):
    
    520
    +        workspace = self.get_workspace(element_name)
    
    501 521
             del self._workspaces[element_name]
    
    502 522
     
    
    523
    +        # Remove from the cache if it exists
    
    524
    +        try:
    
    525
    +            self._workspace_project_cache.remove(workspace.get_absolute_path())
    
    526
    +        except LoadError as e:
    
    527
    +            # We might be closing a workspace with a deleted directory
    
    528
    +            if e.reason == LoadErrorReason.MISSING_FILE:
    
    529
    +                pass
    
    530
    +            else:
    
    531
    +                raise
    
    532
    +
    
    503 533
         # save_config()
    
    504 534
         #
    
    505 535
         # Dump the current workspace element to the project configuration
    



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