From fcca03db9cdb0cc598c176a35190ea652d7735ca Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Sat, 19 Jul 2025 22:36:57 +0200 Subject: [PATCH] Add method SerenaAgent.load_project_from_path_or_name --- src/serena/agent.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/serena/agent.py b/src/serena/agent.py index 64f60d3..f5c34cf 100644 --- a/src/serena/agent.py +++ b/src/serena/agent.py @@ -399,27 +399,39 @@ class SerenaAgent: if self._project_activation_callback is not None: self._project_activation_callback() - def activate_project_from_path_or_name(self, project_root_or_name: str) -> Project: + def load_project_from_path_or_name(self, project_root_or_name: str, autogenerate: bool) -> Project | None: """ - Activate a project from a path or a name. - If the project was already registered, it will just be activated. If it was not registered, - the project will be registered and activated. After that, the project can be activated again - by name (not just by path). + Get a project instance from a path or a name. - :return: a tuple of the project instance and a Boolean indicating whether the project was newly - created + :param project_root_or_name: the path to the project root or the name of the project + :param autogenerate: whether to autogenerate the project for the case where first argument is a directory + which does not yet contain a Serena project configuration file + :return: the project instance if it was found/could be created, None otherwise """ project_instance: Project | None = self.serena_config.get_project(project_root_or_name) if project_instance is not None: log.info(f"Found registered project {project_instance.project_name} at path {project_instance.project_root}.") - else: - if not os.path.isdir(project_root_or_name): - raise ProjectNotFoundError( - f"Project '{project_root_or_name}' not found: Not a valid project name or directory. " - f"Existing project names: {self.serena_config.project_names}" - ) + elif autogenerate and os.path.isdir(project_root_or_name): project_instance = self.serena_config.add_project_from_path(project_root_or_name) log.info(f"Added new project {project_instance.project_name} for path {project_instance.project_root}.") + return project_instance + + def activate_project_from_path_or_name(self, project_root_or_name: str) -> Project: + """ + Activate a project from a path or a name. + If the project was already registered, it will just be activated. + If the argument is a path at which no Serena project previously existed, the project will be created beforehand. + Raises ProjectNotFoundError if the project could neither be found nor created. + + :return: a tuple of the project instance and a Boolean indicating whether the project was newly + created + """ + project_instance: Project | None = self.load_project_from_path_or_name(project_root_or_name, autogenerate=True) + if project_instance is None: + raise ProjectNotFoundError( + f"Project '{project_root_or_name}' not found: Not a valid project name or directory. " + f"Existing project names: {self.serena_config.project_names}" + ) self._activate_project(project_instance) return project_instance