コード例 #1
0
ファイル: utils.py プロジェクト: walllam/polemarch
class Executor(CmdExecutor):
    __slots__ = 'history', 'counter', 'exchanger'

    def __init__(self, history):
        super(Executor, self).__init__()
        self.history = history
        self.counter = 0
        self.exchanger = KVExchanger(self.CANCEL_PREFIX + str(self.history.id))

    @property
    def output(self):
        return self.history.raw_stdout

    @output.setter
    def output(self, value):
        pass  # nocv

    def working_handler(self, proc):
        if proc.poll() is None and self.exchanger.get() is not None:
            self.write_output("\n[ERROR]: User interrupted execution")
            self.exchanger.delete()
            proc.kill()
            proc.wait()
        super(Executor, self).working_handler(proc)

    def write_output(self, line):
        self.counter += 1
        self.history.write_line(line, self.counter, '\n')

    def execute(self, cmd, cwd):
        pm_ansible_path = ' '.join(self.pm_ansible())
        self.history.raw_args = " ".join(cmd).replace(pm_ansible_path,
                                                      '').lstrip()
        return super(Executor, self).execute(cmd, cwd)
コード例 #2
0
class Executor(CmdExecutor):
    __slots__ = 'history', 'counter', 'exchanger'

    def __init__(self, history: History):
        super(Executor, self).__init__()
        self.history = history
        self.counter = 0
        self.exchanger = KVExchanger(self.CANCEL_PREFIX + str(self.history.id))
        env_vars = {}
        if self.history.project is not None:
            env_vars = self.history.project.env_vars
        self.env = env_vars

    @property
    def output(self) -> Text:
        # Optimize for better performance.
        return ''

    @output.setter
    def output(self, value) -> NoReturn:
        pass  # nocv

    def working_handler(self, proc: Popen):
        if proc.poll() is None and self.exchanger.get() is not None:  # nocv
            self.write_output("\n[ERROR]: User interrupted execution")
            self.exchanger.delete()
            for _ in range(5):
                try:
                    os.kill(-proc.pid, signal.SIGTERM)
                except Exception:  # nocv
                    break
                proc.send_signal(signal.SIGINT)
                time.sleep(5)
            proc.terminate()
            proc.kill()
            proc.wait()
        super(Executor, self).working_handler(proc)

    def write_output(self, line: Text):
        self.counter += 1
        self.history.write_line(line, self.counter, '\n')

    def execute(self, cmd: Iterable[Text], cwd: Text):
        pm_ansible_path = ' '.join(self.pm_ansible())
        new_cmd = list()
        for one_cmd in cmd:
            if isinstance(one_cmd, str):
                with raise_context():
                    one_cmd = one_cmd.decode('utf-8')
            new_cmd.append(one_cmd)
        self.history.raw_args = " ".join(new_cmd).replace(pm_ansible_path,
                                                          '').lstrip()
        return super(Executor, self).execute(new_cmd, cwd)
コード例 #3
0
class Executor(CmdExecutor):
    __slots__ = 'history', 'counter', 'exchanger'

    def __init__(self, history):
        super(Executor, self).__init__()
        self.history = history
        self.counter = 0
        self.exchanger = KVExchanger(self.CANCEL_PREFIX + str(self.history.id))
        env_vars = {}
        if self.history.project is not None:
            env_vars = self.history.project.env_vars
        self.env = env_vars

    @property
    def output(self):
        return self.history.raw_stdout

    @output.setter
    def output(self, value):
        pass  # nocv

    def working_handler(self, proc):
        if proc.poll() is None and self.exchanger.get() is not None:  # nocv
            self.write_output("\n[ERROR]: User interrupted execution")
            self.exchanger.delete()
            proc.kill()
            proc.wait()
        super(Executor, self).working_handler(proc)

    def write_output(self, line):
        self.counter += 1
        self.history.write_line(line, self.counter, '\n')

    def execute(self, cmd, cwd):
        pm_ansible_path = ' '.join(self.pm_ansible())
        new_cmd = list()
        for one_cmd in cmd:
            if isinstance(one_cmd, six.string_types):
                with raise_context():
                    one_cmd = one_cmd.decode('utf-8')
            new_cmd.append(one_cmd)
        self.history.raw_args = " ".join(new_cmd).replace(pm_ansible_path,
                                                          '').lstrip()
        return super(Executor, self).execute(new_cmd, cwd)