Call on the command-line (can be used to run Python script files).
(self,
args,
caller=check_call,
env=None,
print_script=True,
**kwargs)
| 100 | return self._run_internal(script, caller, print_script) |
| 101 | |
| 102 | def run_cmd(self, |
| 103 | args, |
| 104 | caller=check_call, |
| 105 | env=None, |
| 106 | print_script=True, |
| 107 | **kwargs): |
| 108 | """Call <python-exe> <args> on the command-line (can be used to run Python script files).""" |
| 109 | if not os.path.exists(self.workspace): |
| 110 | os.makedirs(self.workspace, exist_ok=True) |
| 111 | |
| 112 | call_args = [self._venv_bin] + args |
| 113 | if env: |
| 114 | new_env = copy.deepcopy(os.environ) |
| 115 | new_env.update(env) |
| 116 | else: |
| 117 | new_env = os.environ |
| 118 | |
| 119 | if caller.__name__ == 'check_output': |
| 120 | try: |
| 121 | result = subprocess.run(call_args, |
| 122 | env=new_env, |
| 123 | check=True, |
| 124 | capture_output=True, |
| 125 | **kwargs) |
| 126 | return result.stdout.decode('utf-8') |
| 127 | except subprocess.CalledProcessError as e: |
| 128 | raise RuntimeError(f"Failed to run `{shlex.join(e.cmd)}`:\n" |
| 129 | f"Stdout: {e.stdout.decode()}\n" |
| 130 | f"Stderr: {e.stderr.decode()}\n") |
| 131 | else: |
| 132 | print(f"Start subprocess with {caller}({call_args}, env={new_env})") |
| 133 | return caller(call_args, env=new_env, **kwargs) |
| 134 | |
| 135 | def install_packages(self, packages): |
| 136 | """Install Python packages by name.""" |