Пример #1
0
    def test_write_convert_read(self):
        out_stream = OutputStream()

        out_stream.write_line('xyz')
        out_stream.write('1')

        inp_stream = out_stream.to_input_stream()
        self.assertEqual(inp_stream.get_input(), 'xyz{}1'.format(os.linesep))
    def run(self, input_stream, env):
        if len(self._args_lst) != 1:
            output = OutputStream()
            output.write('exit got wrong number of arguments: expected 0, '\
                         'got {}.'.format(len(self._args_lst) - 1))
            return_code = CommandExit.BAD_NUMBER_OF_ARGS
            return RunnableCommandResult(output, env, return_code)

        raise ExitException()
    def run(self, input_stream, env):
        output = OutputStream()

        if len(self._args_lst) != 1:
            output.write('pwd got wrong number of arguments: expected 0, '\
                         'got {}.'.format(len(self._args_lst) - 1))
            return_code = CommandPwd.BAD_NUMBER_OF_ARGS
            return RunnableCommandResult(output, env, return_code)

        cur_dir = env.get_cwd()
        output.write(cur_dir)

        return RunnableCommandResult(output, env, 0)
    def run(self, input_stream, env):
        output = OutputStream()
        return_code = 0

        if len(self._args_lst) != 2:
            output.write('cd got wrong number of arguments: expected 1, '\
                         'got {}.'.format(len(self._args_lst) - 1))
            return_code = CommandCd.BAD_NUMBER_OF_ARGS
            return RunnableCommandResult(output, env, return_code)

        cur_dir = env.get_cwd()
        new_dir = os.path.join(cur_dir, self._args_lst[1])
        logging.debug('cd: trying to change dir to {}.'.format(new_dir))

        if not os.path.isdir(new_dir):
            output.write('{} is not a directory.'.format(new_dir))
            return_code = CommandCd.NEW_DIR_INVALID
        else:
            env.set_cwd(new_dir)

        return RunnableCommandResult(output, env, return_code)
    def run(self, input_stream, env):
        return_code = 0
        output = OutputStream()
        wc_result = None

        num_args = len(self._args_lst)
        if num_args == 2:
            fl_name = self._args_lst[1]
            full_fl_name = os.path.join(env.get_cwd(), fl_name)

            if not os.path.isfile(full_fl_name):
                output.write('wc: file {} not found.'.format(full_fl_name))
                return_code = CommandWc.FILE_NOT_FOUND
            else:
                with open(full_fl_name, 'r') as opened_file:
                    wc_result = CommandWc._wc_routine(opened_file.read())
        elif num_args == 1:
            wc_result = CommandWc._wc_routine(input_stream.get_input())
        else:
            output.write('wc got wrong number of arguments: expected 0 or 1, '\
                         'got {}.'.format(num_args - 1))
            return_code = CommandWc.BAD_NUMBER_OF_ARGS

        if return_code == 0:
            n_lines, n_words, n_bytes = wc_result
            output.write('{} {} {}'.format(n_lines, n_words, n_bytes))

        return RunnableCommandResult(output, env, return_code)
    def run(self, input_stream, env):
        output = OutputStream()

        cmd_name = self._args_lst[0]
        cur_dir = env.get_cwd()
        cmd_name_full = os.path.join(cur_dir, cmd_name)

        modified_args = self._args_lst[:]
        modified_args[0] = cmd_name_full

        return_code = 0
        try:
            completed_process = subprocess.run(modified_args,
                                               input=input_stream.get_input(),
                                               stdout=subprocess.PIPE,
                                               encoding=sys.stdout.encoding)
            return_code = completed_process.returncode
            output.write(completed_process.stdout)
        except FileNotFoundError:
            output.write('Command {} not found.'.format(cmd_name_full))
            return_code = CommandExternal.COMMAND_NOT_FOUND

        return RunnableCommandResult(output, env, return_code)
    def run(self, input_stream, env):
        return_code = 0
        output = OutputStream()

        num_args = len(self._args_lst)
        if num_args == 2:
            fl_name = self._args_lst[1]
            full_fl_name = os.path.join(env.get_cwd(), fl_name)

            if not os.path.isfile(full_fl_name):
                output.write('cat: file {} not found.'.format(full_fl_name))
                return_code = CommandCat.FILE_NOT_FOUND
            else:
                with open(full_fl_name, 'r') as opened_file:
                    output.write(opened_file.read())
        elif num_args == 1:
            output.write(input_stream.get_input())
        else:
            output.write('cat got wrong number of arguments: expected 0 or 1, '\
                         'got {}.'.format(num_args - 1))
            return_code = CommandCat.BAD_NUMBER_OF_ARGS

        return RunnableCommandResult(output, env, return_code)