예제 #1
0
 def _run_command(self, command, raw=False, **kwargs):
     # type: (Iterable[Text], bool, Any) -> Union[Dict, Text]
     """
     Run a conda command, returning JSON output.
     The command is prepended with 'conda' and run with JSON output flags.
     :param command: command to run
     :param raw: return text output and don't change command
     :param kwargs: kwargs for Argv.get_output()
     :return: JSON output or text output
     """
     command = Argv(*command)  # type: Executable
     if not raw:
         command = (self.conda, ) + command + ("--quiet", "--json")
     try:
         print('Executing Conda: {}'.format(command.serialize()))
         result = command.get_output(stdin=DEVNULL, **kwargs)
     except Exception as e:
         if raw:
             raise
         result = e.output if hasattr(e, 'output') else ''
     if raw:
         return result
     result = json.loads(result) if result else {}
     if result.get('success', False):
         print('Pass')
     elif result.get('error'):
         print('Conda error: {}'.format(result.get('error')))
     return result
예제 #2
0
    def _run_command(self, command, raw=False, **kwargs):
        # type: (Iterable[Text], bool, Any) -> Union[Dict, Text]
        """
        Run a conda command, returning JSON output.
        The command is prepended with 'conda' and run with JSON output flags.
        :param command: command to run
        :param raw: return text output and don't change command
        :param kwargs: kwargs for Argv.get_output()
        :return: JSON output or text output
        """
        def escape_ansi(line):
            ansi_escape = re.compile(
                r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]')
            return ansi_escape.sub('', line)

        command = Argv(*command)  # type: Executable
        if not raw:
            command = (self.conda, ) + command + ("--quiet", "--json")
        try:
            print('Executing Conda: {}'.format(command.serialize()))
            result = command.get_output(stdin=DEVNULL, **kwargs)
            if self.session.debug_mode:
                print(result)
        except Exception as e:
            result = e.output if hasattr(e, 'output') else ''
            if self.session.debug_mode:
                print(result)
            if raw:
                raise
        if raw:
            return result

        result = json.loads(escape_ansi(result)) if result else {}
        if result.get('success', False):
            print('Pass')
        elif result.get('error'):
            print('Conda error: {}'.format(result.get('error')))
        return result