Exemplo n.º 1
0
 def temp_file(self, prefix, contents, suffix=".txt"):
     # type: (Union[Text, Iterable[Text]], Iterable[Text], Text) -> Text
     """
     Write contents to a temporary file, yielding its path. Finally, delete it.
     :param prefix: file name prefix
     :param contents: text lines to write
     :param suffix: file name suffix
     """
     f, temp_path = mkstemp(suffix=suffix, prefix=prefix)
     with f:
         f.write(contents if isinstance(contents, six.text_type
                                        ) else join_lines(contents))
     try:
         yield temp_path
     finally:
         if not self.session.debug_mode:
             safe_remove_file(temp_path)
Exemplo n.º 2
0
    def replace(self, requirements):  # type: (Text) -> Text
        def safe_parse(req_str):
            try:
                return next(parse(req_str))
            except Exception as ex:
                return Requirement(req_str)

        parsed_requirements = tuple(
            map(MarkerRequirement, [
                safe_parse(line) for line in (requirements.splitlines(
                ) if isinstance(requirements, six.text_type) else requirements)
            ]))
        if not parsed_requirements:
            # return the original requirements just in case
            return requirements

        def replace_one(i, req):
            # type: (int, MarkerRequirement) -> Optional[Text]
            try:
                return self._replace_one(req)
            except FatalSpecsResolutionError:
                warning(
                    'could not resolve python wheel replacement for {}'.format(
                        req))
                raise
            except Exception:
                warning(
                    'could not resolve python wheel replacement for \"{}\", '
                    'using original requirements line: {}'.format(req, i))
                return None

        new_requirements = tuple(
            replace_one(i, req) for i, req in enumerate(parsed_requirements))
        conda = is_conda(self.config)
        result = map(
            lambda x, y: (x if x is not None else y.tostr(markers=not conda)),
            new_requirements, parsed_requirements)
        if not conda:
            result = map(self.translator.translate, result)
        return join_lines(result)