Ejemplo n.º 1
0
    def run(self,
            ptr_from: ReferencePointer,
            data_service_head: DataService,
            data_service_base: Optional[DataService] = None
            ) -> Iterable[FileFix]:
        """
        Run `generate_file_fixes` for all files in ptr_from revision.

        :param ptr_from: Git repository state pointer to the base revision.
        :param data_service_head: Connection to the Lookout data retrieval service to get \
                                the new files.
        :param data_service_base: Connection to the Lookout data retrieval service to get \
                                  the initial files. If it is None, we assume the empty contents.
        :return: Generator of fixes for each file.
        """
        files_head = list(
            request_files(data_service_head.get_data(),
                          ptr_from,
                          contents=True,
                          uast=True,
                          unicode=True))

        if data_service_base is not None:
            files_base = list(
                request_files(data_service_base.get_data(),
                              ptr_from,
                              contents=True,
                              uast=True,
                              unicode=True))
        else:
            files_base = [File(path=f.path) for f in files_head]
        return self.generate_file_fixes(
            data_service_head,
            [self.Changes(f1, f2) for f1, f2 in zip(files_base, files_head)])
Ejemplo n.º 2
0
    def run(self, ptr: ReferencePointer,
            data_service: DataService) -> Iterable[TypoFix]:
        """
        Run `generate_typos_fixes` for all lines and all files in `ptr_from` revision.

        :param ptr: Git repository state pointer to the revision that should be analyzed.
        :param data_service: Connection to the Lookout data retrieval service to get the files.
        :return: Generator of fixes for each file.
        """
        for file in request_files(data_service.get_data(),
                                  ptr,
                                  contents=True,
                                  uast=True,
                                  unicode=False):
            if file.path == self.config["analyze"]["filepath"]:
                break
        else:
            raise ValueError("No such file %s in %s" %
                             (self.config["analyze"]["filepath"], ptr))
        line = self.config["analyze"]["line"] + 1
        try:
            self._find_new_lines_return_value = [line]
            typos_fixes = list(
                self.generate_typos_fixes([Change(head=file, base=file)]))
            line_identifiers = self._get_identifiers(file.uast, [line])
            line_identifiers = [n.token for n in line_identifiers]
            identifiers_number = len(set(line_identifiers))
            if not identifiers_number:
                raise ValueError("No identifiers for %s:%d in %s" %
                                 (self.config["analyze"]["filepath"],
                                  self.config["analyze"]["line"] + 1, ptr))
            assert self.config["analyze"]["wrong_id"] in line_identifiers, \
                "Identifier %s was not found in the %s:%d.\nLine identifiers are %s" % (
                    self.config["analyze"]["wrong_id"], self.config["analyze"]["filepath"], line,
                    line_identifiers)
            if typos_fixes:
                return typos_fixes
            return [
                TypoFix(content=file.content.decode("utf-8", "replace"),
                        path=file.path,
                        line_number=0,
                        identifier="",
                        candidates=[],
                        identifiers_number=identifiers_number)
            ]
        finally:
            self._find_new_lines_return_value = None
Ejemplo n.º 3
0
    def run(self, ptr: ReferencePointer,
            data_service: DataService) -> Iterable[TypoFix]:
        """
        Run `generate_typos_fixes` for all lines and all files in `ptr_from` revision.

        :param ptr: Git repository state pointer to the revision that should be analyzed.
        :param data_service: Connection to the Lookout data retrieval service to get the files.
        :return: Generator of fixes for each file.
        """
        for file in request_files(data_service.get_data(),
                                  ptr,
                                  contents=True,
                                  uast=True,
                                  unicode=False):
            if file.path == self.config["filepath_to_analyze"]:
                break
        else:
            raise ValueError("No such file %s in %s" %
                             (self.config["filepath_to_analyze"], ptr))

        typos_fixes = list(
            self.generate_typos_fixes([
                UnicodeChange(head=file,
                              base=File(path=file.path,
                                        language=file.language))
            ]))
        if typos_fixes:
            return typos_fixes
        identifiers_number = len(self._get_identifiers(file.uast, []))
        if not identifiers_number:
            raise ValueError("No identifiers for file %s in %s" %
                             (self.config["filepath_to_analyze"], ptr))
        return [
            TypoFix(content=file.content.decode("utf-8", "replace"),
                    path=file.path,
                    line_number=0,
                    identifier="",
                    candidates=[],
                    identifiers_number=identifiers_number)
        ]