Ejemplo n.º 1
0
        def register_project(self, project_name, project_path, external=False,
                             fast=True, from_path=None):
            """Register a project with given path, return it afterward (might take a while async ?)."""
            if self.exist(project_name):
                if not external:
                    logging.info(f"{project_name} already registered!")
                else:
                    logging.info(f"{project_name} is already referenced as an external project.")
                    self.update_project(project_name, from_path)
                return self.get_project(project_name)
            # TODO -> venv support!

            project = Project(name=project_name, path=str(project_path), external=external,
                              config=Config(python_home="/usr/",
                                            fast=fast))

            self.session.add(project)
            self.session.commit()

            project.index(from_path)
            self.session.commit()

            project.build(from_path)
            self.session.commit()

            return project
Ejemplo n.º 2
0
        def lsp_add_workspace(self, workspacePath):
            """Called on LS initialization to either register current workspace if it's not already referenced
            or update it. Returns Project associated to workspacePath, None in case of failure."""

            # Let's use pathlib to assure cross-OS compatibility :
            workspacePath = Path(workspacePath).absolute()
            if not workspacePath.is_dir() or not workspacePath.exists():
                logging.error("Project root doesn't exist!")
                return None

            # Workspace exists, let's check if it's already referenced.
            project = self.is_workspace_registered(workspacePath)

            if project:
                logging.info("Workspace already registered, updating...")

                self.session.add(project)
                project.update()
                self.session.commit()
                project.build()
                self.session.commit()

                return project
            else:
                logging.info("New workspace, registering...")
                project = self.register_project(workspacePath.name, workspacePath)
                self.session.add(project)

                return project
Ejemplo n.º 3
0
    def complete(self, to_complete, module_path):
        """Return a list of LSP::CompletionItem that corresponds to possible completion for TO_COMPLETE (CompletionParams) in file MODULE_PATH."""

        logging.info(f"Tring to complete {to_complete} in module {module_path}")

        for project_module in self.module:
            if project_module.path == module_path:
                return project_module.complete(to_complete)

        return []
Ejemplo n.º 4
0
    def build(self, from_path=None):
        """Build the relationships between the project and its modules. Bind imports and external projects.
        Should be called once on project registration. Doesn't commit change to database.
        If FROM_PATH is not None, index only subproject beginning at FROM_PATH"""

        if from_path:
            logging.info(f"building project {self.name} from {from_path}")
        else:
            logging.info(f"building project {self.name}")

        if not self.external:
            self.bind_external_project()
            self.bind_imports()
Ejemplo n.º 5
0
        def update_project(self, project_name, from_path):
            """Index new files in project, update bindings and
            external dependencies."""
            session = self.session

            # check if project exists.
            query_result = session.query(Project).filter_by(
                name=project_name).first()
            if not query_result:
                logging.info(f"{project_name} not found, skipping.")
                return

            if query_result.fully_indexed and query_result.external:
                logging.debug(
                    f"{project_name} already fully indexed, skipping.")
                return

            if query_result.external and from_path:
                module_name = Path(from_path).name
                if query_result.is_module_in_project(module_name):
                    logging.info(f"{project_name} is already enough indexed, skipping.")
                    return

            logging.info(f"Indexing {project_name} from {from_path}.")
            query_result.index(from_path)
            logging.info(f"Building {project_name}.")
            query_result.build()

            session.commit()
Ejemplo n.º 6
0
    async def bind_external_project_async(self, for_modules=None):
        """Bind external projects to this project. Call this AFTER all modules have been indexed.
        Fast make that only necessary files are indexed, not everything. if FOR_MODULE is not None, search only in this
        list."""
        if not for_modules:
            imports_name = self.get_project_unbound_imports_name()
        else:
            imports_name = []
            for module in for_modules:
                imports_name += module.get_unbound_imports_name()

        for import_name in imports_name:
            if not self.is_module_in_project(import_name):
                module_path = self.get_module_path(import_name)
                if module_path:
                    project_root = self.get_project_root(module_path)
                    project_name = project_root.stem
                    from_path = None

                    if self.config.fast:
                        from_path = module_path
                    logging.info(f"Binding {import_name} to {self.name}.")
                    ProjectManager().register_project(
                        project_name, str(project_root), True, True, from_path)