def run(self, params: List[str], check: bool = True) -> str: """ Args: params (list): list of parameters given to the tezos-codec, check (bool): raises an exception if codec call fails """ cmd = [self._codec] + params print(format_command(cmd)) stdout = "" stderr = "" completed_process = subprocess.run(cmd, capture_output=True, check=check, text=True) stdout = completed_process.stdout stderr = completed_process.stderr if stdout: print(stdout) if stderr: print(stderr, file=sys.stderr) if check: completed_process.check_returncode() return stdout.rstrip()
def __init__( self, endorser: str, rpc_port: int, base_dir: str, params: List[str] = None, log_file: str = None, ): """Create a new Popen instance for the endorser process. Args: endorser (str): path to the endorser executable file rpc_port (int): rpc port of the node base_dir (str): client directory params (list): additional parameters to be added to the command log_file (str): log file name (optional) Returns: A Popen instance """ assert os.path.isfile(endorser), f'{endorser} not a file' assert os.path.isdir(base_dir), f'{base_dir} not a dir' if params is None: params = [] endpoint = f'http://127.0.0.1:{rpc_port}' cmd = [endorser, '-base-dir', base_dir, '-endpoint', endpoint] cmd.extend(params) cmd_string = process_utils.format_command(cmd) print(cmd_string) stdout, stderr = process_utils.prepare_log(cmd, log_file) subprocess.Popen.__init__(self, cmd, stdout=stdout, stderr=stderr) # type: ignore
def run_generic( self, params: List[str], admin: bool = False, check: bool = True, trace: bool = False, stdin: str = "", env_change: dict = None, ) -> Tuple[str, str, int]: """Run an arbitrary command Args: params (list): list of parameters given to the tezos-client, admin (bool): False to call tezos-client, True to call tezos-admin-client check (bool): raises an exception if client call fails trace (bool): use '-l' option to trace RPCs stdin (string): string that will be passed as standard input to the process env_change (dict): overrides to environment variables Returns: (stdout of command, stderr of command, return code) The actual command will be displayed according to 'format_command'. Client output (stdout, stderr) will be displayed unprocessed. Fails with `CalledProcessError` if command fails """ client = self._admin_client if admin else self._client trace_opt = ['-l'] if trace else [] cmd = client + trace_opt + params print(format_command(cmd)) new_env = os.environ.copy() if env_change is not None: new_env.update(env_change) if self._disable_disclaimer: new_env["TEZOS_CLIENT_UNSAFE_DISABLE_DISCLAIMER"] = "Y" process = subprocess.Popen( cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=new_env, ) outstream, errstream = process.communicate(input=stdin.encode()) stdout = outstream.decode('utf-8') stderr = errstream.decode('utf-8') if stdout: print(stdout) if stderr: print(stderr, file=sys.stderr) if check: if process.returncode != 0: raise subprocess.CalledProcessError(process.returncode, cmd, stdout, stderr) # `+ ""` makes pylint happy. It can't infer stdout/stderr can't # be `None` thanks to the `capture_output=True` option. return (stdout + "", stderr + "", process.returncode)
def run_cmd(cmd: List[str]) -> str: """Pretty print a command. Execute it, print and return its standard output.""" print(process_utils.format_command(cmd)) process_ret = subprocess.run(cmd, check=True, capture_output=True, text=True) print(process_ret.stdout) return process_ret.stdout.strip()
def _run_and_print(cmd): cmd_str = process_utils.format_command(cmd) print(cmd_str) completed_process = subprocess.run( cmd, capture_output=True, text=True, check=False ) stdout = completed_process.stdout stderr = completed_process.stderr if stdout: print(stdout) if stderr: print(stderr, file=sys.stderr) completed_process.check_returncode()
def run(self): node_run_str = process_utils.format_command(self._node_run) print(node_run_str) print(self._new_env) # overwrite old log on on first invocation only overwrite_log = not self._run_called_before stdout, stderr = process_utils.prepare_log( self._node_run, self.log_file, overwrite_log ) self._process = subprocess.Popen( self._node_run, stdout=stdout, stderr=stderr, env=self._new_env ) self._run_called_before = True
def __init__( self, baker: str, rpc_port: int, base_dir: str, node_dir: str, accounts: List[str], params: List[str] = None, log_levels: Dict[str, str] = None, log_file: str = None, run_params: List[str] = None, ): """Create a new Popen instance for the baker process. Args: baker (str): path to the baker executable file rpc_port (int): rpc port of the node base_dir (str): client directory node_dir (str): node directory accounts (List[str]): delegates accounts params (list): additional parameters to be added to the command log_levels (dict): log levels. e.g. {"p2p.connection-pool":"debug"} log_file (str): log file name (optional) Returns: A Popen instance """ assert os.path.isfile(baker), f'{baker} not a file' assert os.path.isdir(node_dir), f'{node_dir} not a dir' assert os.path.isdir(base_dir), f'{base_dir} not a dir' if params is None: params = [] if run_params is None: run_params = [] endpoint = f'http://127.0.0.1:{rpc_port}' cmd = [baker, '-base-dir', base_dir, '-endpoint', endpoint] cmd.extend(params) cmd.extend(['run', 'with', 'local', 'node', node_dir] + accounts) cmd.extend(run_params) env = os.environ.copy() if log_levels is not None: lwt_log = ";".join(f'{key} -> {values}' for key, values in log_levels.items()) env['TEZOS_LOG'] = lwt_log cmd_string = process_utils.format_command(cmd) print(cmd_string) stdout, stderr = process_utils.prepare_log(cmd, log_file) subprocess.Popen.__init__(self, cmd, stdout=stdout, env=env, stderr=stderr) # type: ignore