Skip to content

Commit

Permalink
- switched to writing missing modules (with --write-requirements-to) …
Browse files Browse the repository at this point in the history
…as soon as they are

  installed, if installing missing modules, so that there is an easy way to reproduce the
  state if CoverUp aborts before finishing a run;
  • Loading branch information
jaltmayerpizzorno committed Sep 1, 2024
1 parent 08723c0 commit 546e8dd
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/coverup/coverup.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ def find_imports(python_code: str) -> T.List[str]:

module_available = dict()
def missing_imports(modules: T.List[str]) -> T.List[str]:
# TODO handle GPT sometimes generating 'from your_module import XYZ', asking us to modify

import importlib.util

for module in modules:
Expand All @@ -308,6 +310,8 @@ def missing_imports(modules: T.List[str]) -> T.List[str]:


def install_missing_imports(seg: CodeSegment, modules: T.List[str]) -> bool:
global args, module_available

all_ok = True
for module in modules:
try:
Expand All @@ -317,6 +321,10 @@ def install_missing_imports(seg: CodeSegment, modules: T.List[str]) -> bool:
module_available[module] = 2 # originally unavailable, but now added
print(f"Installed module {module}")
log_write(seg, f"Installed module {module}")

if args.write_requirements_to:
with args.write_requirements_to.open("a") as f:
f.write(f"{module}\n")
except subprocess.CalledProcessError as e:
log_write(seg, f"Unable to install module {module}:\n{str(e.stdout, 'UTF-8', errors='ignore')}")
all_ok = False
Expand All @@ -325,8 +333,8 @@ def install_missing_imports(seg: CodeSegment, modules: T.List[str]) -> bool:


def get_required_modules() -> T.List[str]:
"""Returns a list of the modules originally missing (i.e., even if we installed them)"""
return [m for m in module_available if module_available[m] != 1]
"""Returns a list of the modules found missing (and not installed)"""
return [m for m in module_available if not module_available[m]]


PROGRESS_COUNTERS=['G', 'F', 'U', 'R'] # good, failed, useless, retry
Expand Down Expand Up @@ -733,9 +741,7 @@ async def sem_coro(coro):
state.set_final_coverage(coverage)
state.save_checkpoint(args.checkpoint)

if required := get_required_modules():
# Sometimes GPT outputs 'from your_module import XYZ', asking us to modify
# FIXME move this to 'state'
if not args.install_missing_modules and (required := get_required_modules()):
print(f"Some modules seem to be missing: {', '.join(str(m) for m in required)}")
if args.write_requirements_to:
with args.write_requirements_to.open("a") as f:
Expand Down

0 comments on commit 546e8dd

Please sign in to comment.