Skip to content

Commit

Permalink
Reload modules after scratch install (#316)
Browse files Browse the repository at this point in the history
Changes:
- For any modules in scratch that replace exisitnng dependnecies,
`importlib.reload()` is called
- Dockerfile now includes a larger Python container with tools needed to
pip install
  • Loading branch information
callumforrester committed Oct 25, 2023
1 parent 789ec2d commit 566c34a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ WORKDIR /context
# install python package into /venv
RUN pip install ${PIP_OPTIONS}

FROM python:3.11-slim as runtime
FROM python:3.11 as runtime

# Add apt-get system dependecies for runtime here if needed

Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
url=f"https://github.com/{github_user}/{github_repo}/releases",
)
],
navigation_with_keys=True,
)

# A dictionary of values to pass into the template engine’s context for all pages
Expand Down
15 changes: 14 additions & 1 deletion src/blueapi/service/scratch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib
import logging
import os
import subprocess
Expand Down Expand Up @@ -44,6 +45,16 @@ def install_editable(
]
)

def reload_modules_within(self, root: Path) -> None:
for module in sys.modules.values():
if (
hasattr(module, "__file__")
and module.__file__ is not None
and root in Path(module.__file__).parents
):
logging.info(f"Reloading {module}")
importlib.reload(module)


class ScratchManager:
"""
Expand Down Expand Up @@ -82,7 +93,9 @@ def sync_packages(self) -> None:
self._pip.install_editable(directory, [])
except subprocess.CalledProcessError as ex:
logging.error(f"Unable to install {directory}", ex)
logging.info("Scratch packages installed")
logging.info("Scratch packages installed, reloading modules")
self._pip.reload_modules_within(self._root_path)
logging.info("Reload complete")

def _get_directories_in_scratch(self) -> Set[Path]:
self._check_scratch_exists()
Expand Down

0 comments on commit 566c34a

Please sign in to comment.