Skip to content

Commit 1f79fac

Browse files
committed
get installed modules
1 parent 859d50b commit 1f79fac

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import ast
4+
import importlib.metadata
45
import os
56
import re
67
import shutil
@@ -10,6 +11,7 @@
1011
from functools import lru_cache
1112
from pathlib import Path
1213
from tempfile import TemporaryDirectory
14+
from typing import TYPE_CHECKING
1315

1416
import tomlkit
1517

@@ -19,8 +21,12 @@
1921
ImportErrorPattern = re.compile(r"ModuleNotFoundError.*$", re.MULTILINE)
2022

2123

24+
if TYPE_CHECKING:
25+
from collections.abc import Generator
26+
27+
2228
@contextmanager
23-
def custom_addopts() -> None:
29+
def custom_addopts() -> Generator[None, None, None]:
2430
pyproject_file = find_pyproject_toml()
2531
original_content = None
2632
non_blacklist_plugin_args = ""
@@ -58,7 +64,7 @@ def custom_addopts() -> None:
5864

5965

6066
@contextmanager
61-
def add_addopts_to_pyproject() -> None:
67+
def add_addopts_to_pyproject() -> Generator[None, None, None]:
6268
pyproject_file = find_pyproject_toml()
6369
original_content = None
6470
try:
@@ -220,3 +226,32 @@ def exit_with_message(message: str, *, error_on_exit: bool = False) -> None:
220226
paneled_text(message, panel_args={"style": "red"})
221227

222228
sys.exit(1 if error_on_exit else 0)
229+
230+
231+
blacklist_installed_pkgs = {
232+
"codeflash",
233+
"pytest",
234+
"coverage",
235+
"__", # this is for private packages or ones that contain "__" in order to mangle names i.e 3204bda914b7f2c6f497__mypyc
236+
"setuptools",
237+
"pip",
238+
"wheel",
239+
"importlib_metadata",
240+
"importlib_resources",
241+
"isort",
242+
"black",
243+
"tomlkit",
244+
"stubs",
245+
}
246+
247+
248+
def get_installed_packages() -> set[str]:
249+
pkgs = importlib.metadata.packages_distributions().keys()
250+
return {
251+
pkg
252+
for pkg in pkgs
253+
if not any(blacklisted in pkg for blacklisted in blacklist_installed_pkgs) and not pkg.startswith("_")
254+
}
255+
256+
257+
print(get_installed_packages())

0 commit comments

Comments
 (0)