Esempio n. 1
0
 def test_segment_raises_value_error_if_offset_greater_than_total_length(
         self):
     complete_output_path = self.create_temp_plaintext_file(
         _COMPLETE_OUTPUT)
     console_output = ConsoleOutput.from_plaintext(complete_output_path)
     with self.assertRaises(BadRequestError):
         console_output.segment(max_lines=5, offset_line=155)
    def get_console_output(
        cls,
        build_id: int,
        subjob_id: int,
        atom_id: int,
        result_root: str,
        max_lines: int = 50,
        offset_line: Optional[int] = None,
    ) -> Optional[ConsoleOutputSegment]:
        """
        Return the console output if it exists in the specified result_root. Return None if it does not exist.
        :param build_id: build id
        :param subjob_id: subjob id
        :param atom_id: atom id
        :param result_root: the sys path to either the results or artifacts directory where results are stored.
        :param max_lines: The maximum total number of lines to return. If this max_lines + offset_line lines do not
            exist in the output file, just return what there is.
        :param offset_line: The line number (0-indexed) to start reading content for. If none is specified, we will
            return the console output starting from the end of the file.
        :return: The console output if it exists in the specified result_root, None if it does not exist
        """
        console_output = None

        artifact_dir = cls.atom_artifact_directory(build_id,
                                                   subjob_id,
                                                   atom_id,
                                                   result_root=result_root)
        output_file_path = os.path.join(artifact_dir, cls.OUTPUT_FILE)
        if os.path.isfile(output_file_path):
            # Read directly from output file if it exists (while build is in progress).
            console_output = ConsoleOutput.from_plaintext(output_file_path)
        else:
            # Read from build artifact archive if it exists (after build is finished).
            build_dir = cls.build_artifact_directory(build_id,
                                                     result_root=result_root)
            archive_file_path = os.path.join(build_dir,
                                             cls.ARTIFACT_ZIPFILE_NAME)
            if os.path.isfile(archive_file_path):
                path_in_archive = os.path.join(
                    os.path.relpath(artifact_dir, build_dir), cls.OUTPUT_FILE)
                console_output = ConsoleOutput.from_zipfile(
                    archive_file_path, path_in_archive)

        if console_output:
            return console_output.segment(max_lines, offset_line)
        return None
Esempio n. 3
0
    def test_segment_for_incomplete_console_output(
            self,
            input_max_lines: int,
            input_offset_line: Optional[int],
            expected_num_lines: int,
            expected_offset_line: int,
            expected_total_num_lines: int,
            expected_content: str,
    ):
        incomplete_output_path = self.create_temp_plaintext_file(_INCOMPLETE_OUTPUT)

        console_output = ConsoleOutput.from_plaintext(incomplete_output_path)
        segment = console_output.segment(max_lines=input_max_lines, offset_line=input_offset_line)

        self.assertEquals(segment.num_lines, expected_num_lines)
        self.assertEquals(segment.offset_line, expected_offset_line)
        self.assertEquals(segment.total_num_lines, expected_total_num_lines)
        self.assertEquals(segment.content, expected_content)
Esempio n. 4
0
    def test_segment_for_incomplete_console_output(
        self,
        input_max_lines: int,
        input_offset_line: Optional[int],
        expected_num_lines: int,
        expected_offset_line: int,
        expected_total_num_lines: int,
        expected_content: str,
    ):
        incomplete_output_path = self.create_temp_plaintext_file(
            _INCOMPLETE_OUTPUT)

        console_output = ConsoleOutput.from_plaintext(incomplete_output_path)
        segment = console_output.segment(max_lines=input_max_lines,
                                         offset_line=input_offset_line)

        self.assertEquals(segment.num_lines, expected_num_lines)
        self.assertEquals(segment.offset_line, expected_offset_line)
        self.assertEquals(segment.total_num_lines, expected_total_num_lines)
        self.assertEquals(segment.content, expected_content)
Esempio n. 5
0
    def get_console_output(
            cls,
            build_id: int,
            subjob_id: int,
            atom_id: int,
            result_root: str,
            max_lines: int=50,
            offset_line: Optional[int]=None,
    ) -> Optional[ConsoleOutputSegment]:
        """
        Return the console output if it exists in the specified result_root. Return None if it does not exist.
        :param build_id: build id
        :param subjob_id: subjob id
        :param atom_id: atom id
        :param result_root: the sys path to either the results or artifacts directory where results are stored.
        :param max_lines: The maximum total number of lines to return. If this max_lines + offset_line lines do not
            exist in the output file, just return what there is.
        :param offset_line: The line number (0-indexed) to start reading content for. If none is specified, we will
            return the console output starting from the end of the file.
        :return: The console output if it exists in the specified result_root, None if it does not exist
        """
        console_output = None

        artifact_dir = cls.atom_artifact_directory(build_id, subjob_id, atom_id, result_root=result_root)
        output_file_path = os.path.join(artifact_dir, cls.OUTPUT_FILE)
        if os.path.isfile(output_file_path):
            # Read directly from output file if it exists (while build is in progress).
            console_output = ConsoleOutput.from_plaintext(output_file_path)
        else:
            # Read from build artifact archive if it exists (after build is finished).
            build_dir = cls.build_artifact_directory(build_id, result_root=result_root)
            archive_file_path = os.path.join(build_dir, cls.ARTIFACT_ZIPFILE_NAME)
            if os.path.isfile(archive_file_path):
                path_in_archive = os.path.join(os.path.relpath(artifact_dir, build_dir), cls.OUTPUT_FILE)
                console_output = ConsoleOutput.from_zipfile(archive_file_path, path_in_archive)

        if console_output:
            return console_output.segment(max_lines, offset_line)
        return None
Esempio n. 6
0
 def test_segment_raises_value_error_if_offset_greater_than_total_length(self):
     complete_output_path = self.create_temp_plaintext_file(_COMPLETE_OUTPUT)
     console_output = ConsoleOutput.from_plaintext(complete_output_path)
     with self.assertRaises(BadRequestError):
         console_output.segment(max_lines=5, offset_line=155)