| 7 | |
| 8 | |
| 9 | def python_run( |
| 10 | cmdline, |
| 11 | returncode: Union[Literal["error"], Literal["any"], int], |
| 12 | cwd=None, |
| 13 | additional_env: Optional[Dict[str, str]] = None, |
| 14 | timeout=None, |
| 15 | ) -> CompletedProcess: |
| 16 | cp = os.environ.copy() |
| 17 | cp["PYTHONPATH"] = os.pathsep.join([x for x in sys.path if x]) |
| 18 | if additional_env: |
| 19 | cp.update(additional_env) |
| 20 | args = [sys.executable] + cmdline |
| 21 | result = subprocess.run(args, capture_output=True, env=cp, cwd=cwd, timeout=timeout) |
| 22 | |
| 23 | if returncode == "any": |
| 24 | return result |
| 25 | |
| 26 | if returncode == "error" and result.returncode: |
| 27 | return result |
| 28 | |
| 29 | if result.returncode == returncode: |
| 30 | return result |
| 31 | |
| 32 | # This is a bit too verbose, so, commented out for now. |
| 33 | # env_str = "\n".join(str(x) for x in sorted(cp.items())) |
| 34 | |
| 35 | raise AssertionError( |
| 36 | f"""Expected returncode: {returncode}. Found: {result.returncode}. |
| 37 | === stdout: |
| 38 | {result.stdout.decode('utf-8')} |
| 39 | |
| 40 | === stderr: |
| 41 | {result.stderr.decode('utf-8')} |
| 42 | |
| 43 | === Args: |
| 44 | {args} |
| 45 | |
| 46 | """ |
| 47 | ) |
| 48 | |
| 49 | |
| 50 | project_rootdir = os.path.abspath(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |