Example #1
0
    def validate_pre_sds_if_applicable(self,
                                       hds: HomeDs) -> Optional[TextRenderer]:
        if self.path_str == '':
            return text_docs.single_line(_EMPTY_FILE_NAME)

        path = PurePosixPath(self.path_str)
        if path.is_absolute():
            return text_docs.single_line(
                str_constructor.FormatMap(
                    'A {FILE_NAME} must not be absolute: {path}', {
                        'FILE_NAME': syntax.FILE_NAME.name,
                        'path': str(path),
                    }))
        return None
Example #2
0
def _install_into_directory(os_services: OsServices,
                            src_file_path: pathlib.Path,
                            dst_file_name: str,
                            dst_container_path: pathlib.Path):
    target = dst_container_path / dst_file_name
    if target.exists():
        raise HardErrorException(
            failure_details.FailureDetailsRenderer(
                failure_details.FailureDetails.new_message(
                    text_docs.single_line(
                        str_constructor.FormatMap(
                            '{dst} already exists: {target}',
                            {
                                'dst': instruction_arguments.DESTINATION_PATH_ARGUMENT.name,
                                'target': target,
                            }))
                )
            )
        )
    src = str(src_file_path)
    dst = str(target)
    if src_file_path.is_dir():
        os_services.copy_tree__preserve_as_much_as_possible(src, dst)
    else:
        os_services.copy_file__preserve_as_much_as_possible(src, dst)
Example #3
0
def render_failure(properties_with_neg: PropertiesWithNegation,
                   file_path: pathlib.Path) -> TextRenderer:
    return text_docs.single_line(
        str_constructor.Concatenate([
            render_failing_property(properties_with_neg),
            ': ',
            file_path,
        ]))
Example #4
0
 def validate(self) -> Optional[TextRenderer]:
     mb_str_src_err_msg_renderer = self._string_source.validator.validate_post_sds_if_applicable(
         self._tcds)
     return (None if mb_str_src_err_msg_renderer is None else
             rend_comb.ConcatenationR([
                 text_docs.single_line(_STDIN_VALIDATION_ERROR_HEADER),
                 rend_comb.Indented(mb_str_src_err_msg_renderer),
             ]))
Example #5
0
    def validate_post_sds_if_applicable(self, tcds: TestCaseDs) -> Optional[TextRenderer]:
        if self._has_dir_dependencies:
            try:
                x = self._value_computer.value_of_any_dependency(tcds)
                return self._custom_validator(x)
            except NotAnIntegerException as ex:
                return text_docs.single_line(ex.value_string)

        return None
Example #6
0
    def validate_pre_sds_if_applicable(self, hds: HomeDs) -> Optional[TextRenderer]:
        if not self._has_dir_dependencies:
            try:
                x = self._value_computer.value_when_no_dir_dependencies()
                return self._custom_validator(x)
            except NotAnIntegerException as ex:
                return text_docs.single_line(ex.value_string)

        return None
Example #7
0
def _raise_he__single_line(
    format_str: str,
    format_map: Mapping[str, ToStringObject],
    ex: Exception,
):
    raise HardErrorException(
        failure_details.FailureDetailsRenderer(
            FailureDetails.new_message(
                text_docs.single_line(
                    str_constructor.FormatMap(format_str, format_map)), ex)))
Example #8
0
    def _get_exit_code(self) -> int:
        sds = self._sds
        try:
            f = sds.result.exitcode_file.open()
        except IOError:
            rel_path = sds.relative_to_sds_root(sds.result.exitcode_file)
            err_msg = text_docs.single_line(
                str_constructor.FormatMap(
                    'Cannot read {exit_code} from file {file}', {
                        'exit_code': texts.ATTRIBUTE__EXIT_CODE,
                        'file': rel_path,
                    }))
            raise HardErrorException(err_msg)
        try:
            contents = f.read()
        except IOError:
            raise HardErrorException(
                text_docs.single_line(
                    str_constructor.Concatenate([
                        _FAILED_TO_READ_CONTENTS_FROM,
                        sds.result.exitcode_file,
                    ])))
        finally:
            f.close()

        try:
            return int(contents)
        except ValueError:
            msg = text_docs.single_line(
                str_constructor.FormatMap(
                    'The contents of the file for {exit_code} ("{file}") is not an integer: "{contents}"',
                    {
                        'exit_code': texts.ATTRIBUTE__EXIT_CODE,
                        'file': sds.result.exitcode_file,
                        'contents': contents,
                    }))
            raise HardErrorException(msg)
Example #9
0
 def _compile_and_set_pattern(self,
                              regex_pattern: str) -> Optional[TextRenderer]:
     try:
         flags = 0
         if self._is_ignore_case:
             flags = re.IGNORECASE
         self.pattern = re.compile(regex_pattern, flags)
         return None
     except Exception as ex:
         return text_docs.single_line(
             str_constructor.FormatPositional(
                 "Invalid {}: '{}'",
                 syntax_elements.REGEX_SYNTAX_ELEMENT.singular_name,
                 ex,
             ))
Example #10
0
    def validate_pre_sds_if_applicable(self, hds: HomeDs) -> Optional[TextRenderer]:
        path_str = self.path_str
        if path_str == '':
            return text_docs.single_line(_ERR__FILE_NAME__EMPTY)

        for path_separator in _PATH_SEPARATORS:
            if path_separator in path_str:
                return self._err_msg(path_str, _ERR__FILE_NAME__PATH_SEPARATOR_IN_FILE_NAME)

        path = PurePosixPath(path_str)
        if path.is_absolute():
            return self._err_msg(path_str, _ERR__FILE_NAME__ABSOLUTE)
        if '..' in path.parts:
            return self._err_msg(path_str, _ERR__FILE_NAME__RELATIVE_COMPONENTS)

        return None
Example #11
0
def render_failure__wo_file_name(
        properties_with_neg: PropertiesWithNegation) -> TextRenderer:
    return text_docs.single_line(
        str_constructor.Concatenate([
            render_failing_property(properties_with_neg),
        ]))