|
2 | 2 |
|
3 | 3 | import ast |
4 | 4 | import os |
| 5 | +import re |
5 | 6 | import shutil |
6 | 7 | import site |
| 8 | +from contextlib import contextmanager |
7 | 9 | from functools import lru_cache |
8 | 10 | from pathlib import Path |
9 | 11 | from tempfile import TemporaryDirectory |
10 | 12 |
|
| 13 | +import tomlkit |
| 14 | + |
11 | 15 | from codeflash.cli_cmds.console import logger |
| 16 | +from codeflash.code_utils.config_parser import find_pyproject_toml |
| 17 | + |
| 18 | + |
| 19 | +@contextmanager |
| 20 | +def custom_addopts() -> None: |
| 21 | + pyproject_file = find_pyproject_toml() |
| 22 | + original_content = None |
| 23 | + non_blacklist_plugin_args = "" |
| 24 | + |
| 25 | + try: |
| 26 | + # Read original file |
| 27 | + if pyproject_file.exists(): |
| 28 | + with Path.open(pyproject_file, encoding="utf-8") as f: |
| 29 | + original_content = f.read() |
| 30 | + data = tomlkit.parse(original_content) |
| 31 | + # Backup original addopts |
| 32 | + original_addopts = data.get("tool", {}).get("pytest", {}).get("ini_options", {}).get("addopts", "") |
| 33 | + # nothing to do if no addopts present |
| 34 | + if original_addopts != "": |
| 35 | + original_addopts = [x.strip() for x in original_addopts] |
| 36 | + non_blacklist_plugin_args = re.sub(r"-n(?: +|=)\S+", "", " ".join(original_addopts)).split(" ") |
| 37 | + non_blacklist_plugin_args = [x for x in non_blacklist_plugin_args if x != ""] |
| 38 | + if non_blacklist_plugin_args != original_addopts: |
| 39 | + data["tool"]["pytest"]["ini_options"]["addopts"] = non_blacklist_plugin_args |
| 40 | + # Write modified file |
| 41 | + with Path.open(pyproject_file, "w", encoding="utf-8") as f: |
| 42 | + f.write(tomlkit.dumps(data)) |
| 43 | + |
| 44 | + yield |
| 45 | + |
| 46 | + finally: |
| 47 | + # Restore original file |
| 48 | + if ( |
| 49 | + original_content |
| 50 | + and pyproject_file.exists() |
| 51 | + and tuple(original_addopts) not in {(), tuple(non_blacklist_plugin_args)} |
| 52 | + ): |
| 53 | + with Path.open(pyproject_file, "w", encoding="utf-8") as f: |
| 54 | + f.write(original_content) |
| 55 | + |
| 56 | + |
| 57 | +@contextmanager |
| 58 | +def add_addopts_to_pyproject() -> None: |
| 59 | + pyproject_file = find_pyproject_toml() |
| 60 | + original_content = None |
| 61 | + try: |
| 62 | + # Read original file |
| 63 | + if pyproject_file.exists(): |
| 64 | + with Path.open(pyproject_file, encoding="utf-8") as f: |
| 65 | + original_content = f.read() |
| 66 | + data = tomlkit.parse(original_content) |
| 67 | + data["tool"]["pytest"] = {} |
| 68 | + data["tool"]["pytest"]["ini_options"] = {} |
| 69 | + data["tool"]["pytest"]["ini_options"]["addopts"] = [ |
| 70 | + "-n=auto", |
| 71 | + "-n", |
| 72 | + "1", |
| 73 | + "-n 1", |
| 74 | + "-n 1", |
| 75 | + "-n auto", |
| 76 | + ] |
| 77 | + with Path.open(pyproject_file, "w", encoding="utf-8") as f: |
| 78 | + f.write(tomlkit.dumps(data)) |
| 79 | + |
| 80 | + yield |
| 81 | + |
| 82 | + finally: |
| 83 | + # Restore original file |
| 84 | + with Path.open(pyproject_file, "w", encoding="utf-8") as f: |
| 85 | + f.write(original_content) |
12 | 86 |
|
13 | 87 |
|
14 | 88 | def encoded_tokens_len(s: str) -> int: |
|
0 commit comments