class Session(object): """ Class representing a session object based on a shell command """ def __init__(self, n, cmd, **kwargs): self.id = n self.parent = kwargs.pop('parent') if isinstance(cmd, str): cmd = shlex.split(cmd) self._path = Path(self.parent.console._files.tempdir, "session", str(n), create=True) for i, s in enumerate(["stdin", "stdout", "stderr"]): fifo = str(self._path.joinpath(str(i))) self._named_pipes.append(fifo) os.mkfifo(fifo, 0o777) setattr(self, "_" + s, os.open(fifo, os.O_WRONLY)) def close(self): for s in ["stdin", "stdout", "stderr"]: getattr(self, "_" + s).close() shutil.rmtree(str(self._path)) self._process.wait() del self.parent[self.id] def start(self, **kwargs): kwargs['close_fds'] = True kwargs[ 'preexec_fn'] = os.setsid # NB: see subprocess' doc ; preexec_fn is not thread-safe self._process = Popen(cmd, stdout=self._stdout, stderr=self._stderr, stdin=self._stdin, **kwargs)
def run(self, key, value=None): if key == "files": if value is None: data = [["Path", "Size"]] p = Path(self.config.option("WORKSPACE").value) for f in self.console._files.list: data.append([f, human_readable_size(p.joinpath(f).size)]) print_formatted_text( BorderlessTable(data, "Files from the workspace")) elif self.config.option("TEXT_VIEWER").value: self.console._files.view(value) elif key == "issues": t = Entity.get_issues() if len(t) > 0: print_formatted_text(t) elif key == "modules": h = Module.get_help(value) if h.strip() != "": print_formatted_text(h) else: self.logger.warning("No module loaded") elif key == "options": if value is None: print_formatted_text(ANSI(str(self.config))) else: c = Config() c[self.config.option(value)] = self.config[value] print_formatted_text(ANSI(str(c))) elif key == "projects": if value is None: data = [["Name"]] for p in projects(self): data.append([p]) print_formatted_text(BorderlessTable(data, "Existing projects")) else: print_formatted_text(value) elif key == "sessions": data = [["ID", "Description"]] for i, s in self.console._sessions: data.append([str(i), getattr(s, "description", "<undefined>")]) print_formatted_text(BorderlessTable(data, "Open sessions"))