Esempio n. 1
0
 def _run_prog(self, text, xargs, selection):
     if xargs:
         text = text.replace('\n', ' ')
     self.ishell.run_cmd(['xclip', '-selection', selection],
                         stdin=text,
                         must_work=True,
                         fetch_output=False)
     logging.verbose_print('Copied.')
Esempio n. 2
0
    def run(self):
        info = False
        cmd_res = None

        try:
            # Welcoming gesture if entering interactive mode without command
            if not self.args.command:
                print(
                    'Hey, welcome to gofritools interactive mode! see help above'
                )
                self.parser = argparse.make_parser(
                    interactive=True, virt=False)  # adjust parser for help
                self.parser.print_help()
                self.args = None

            while True:
                # Handle next cmd
                try:
                    if self.args:
                        logging.set_verbosity(self.__verbosity)
                        if self.args.command:
                            logging.verbose_print(
                                f'Handling next command: {self.args.command}')
                            if self.args.command == 'quit':
                                print('Goodbye!')
                                break
                            cmd_res = self.stack.put(self.args)
                except Stack.OpMessage as e:
                    logging.print_warning(e)
                    info = True
                    failed = False  # stack ops failure
                except (Exception, SystemExit) as e:
                    info = True
                    logging.print_warning(e)
                    if logging.global_verbosity > 1:
                        logging.log_ex()  # TODO this is ugly

                if cmd_res:
                    if not (self.__verbosity or info):
                        ui_tools.clear_screen()
                    print(f'Result:\n===\n{cmd_res.humanize()}\n===\n')

                # Read next cmd
                print(ui_tools.colored('>>> ', 'green'), end='')
                parser = argparse.make_parser(interactive=True, virt=True)

                try:
                    cmdline = shlex.split(input())
                    self.args = argparse.parse_args(parser, cmdline)
                except (Exception, SystemExit):
                    # Remove current command and retry naturally
                    info = True
                    self.args = None

        except KeyboardInterrupt:
            ### handle keyboard interrupt ###
            logging.verbose_print('Exiting due to user interrupt')
            return 0
Esempio n. 3
0
    def run_cmd(self,
                cmd,
                expect_output=None,
                stdin='',
                must_work=True,
                stdout_enough=False,
                fetch_output=True,
                **kwargs):
        ''' TODO: fetch_output=False is probably broken '''
        ''' TODO: expect_output|must_work|stdout_enough -- put into enum or something'''
        cmd = [x for x in cmd if x.strip()]
        logging.verbose_print_cmd(cmd, 'running cmd:')

        output_pipe = subprocess.PIPE if fetch_output else None
        p = subprocess.Popen(cmd,
                             stdout=output_pipe,
                             stderr=output_pipe,
                             stdin=subprocess.PIPE,
                             encoding='utf-8',
                             **kwargs)
        stdout, stderr = p.communicate(stdin)

        res = IShell.build_res(p.returncode, stdout, stderr)

        if res['rc'] != self.POSIX_SUCCESS:
            msg = 'command execution failed:\n{}'.format(res)
            if stdout_enough and res['stdout']:
                logging.verbose_print(f'Command partially failed: {stderr}')
                return res
            if must_work:
                raise IShell.CmdFailureException(res, cmd,
                                                 kwargs.get('cwd', None))
            else:
                logging.verbose_print(msg)
        elif expect_output:
            assert expect_output == stdout, 'Expected output:\n{}\n\nGot:\n{}'.format(
                expect_output, stdout)

        return res
Esempio n. 4
0
 def add_record(self, *args, **kwargs):
     r = Record(*args, **kwargs)
     if (path := utils.should_ignore_record(r)):
         logging.verbose_print(f'Ignoring record with path: {path}')
         return
Esempio n. 5
0
 def _run_prog(self, pairs):
     pairs = FileLine.as_safe_list(pairs)
     for p in pairs:
         logging.verbose_print(f'Deleting: {p}')
         self.ishell.run_cmd(['sed', '-i'] + [f'{p.line}d'] +
                             ['--', p.file])
Esempio n. 6
0
 def run_on_shell(self, cmd_str, must_work=True):
     logging.verbose_print(f'running shell cmd: {cmd_str}')
     res = subprocess.call(cmd_str, encoding='utf8', shell=True)
     if must_work and res != self.POSIX_SUCCESS:
         raise IShell.CmdFailureException(res, cmd_str)
Esempio n. 7
0
 def _run_prog(self, search_res):
     search_res.trim()
     logging.verbose_print('Trimed raw text (colored text does not trim).')
     return search_res