Skip to content

Commit e21c453

Browse files
committed
Use sitecustomize.py for early MFC path patching
sitecustomize.py is loaded automatically at Python startup, ensuring our path overrides (MFC_ROOT_DIR, MFC_BUILD_DIR, MFC_LOCK_FILEPATH) are applied before any mfc.common imports happen. This fixes the lock.yaml write error that occurred when the patch loaded too late.
1 parent b235d62 commit e21c453

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed

packaging/homebrew/mfc.rb

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -122,42 +122,53 @@ def install
122122
# Copy only pyproject.toml (tiny file, prevents reinstall checks)
123123
cp "#{prefix}/toolchain/pyproject.toml" build/pyproject.toml
124124
125-
# Create a minimal patch file for build.py overrides and path redirection
126-
mkdir -p .mfc_patch
127-
cat > .mfc_patch/build_patch.py << 'PATCH_EOF'
125+
# Create a sitecustomize.py file that patches MFC paths before any imports
126+
# This runs automatically at Python startup and ensures paths are correct
127+
cat > sitecustomize.py << 'SITECUSTOMIZE_EOF'
128128
import sys
129129
import os
130+
131+
# Add toolchain to path
130132
sys.path.insert(0, "#{prefix}/toolchain")
131133
132-
# Override MFC_ROOT_DIR to point to our temporary directory
133-
# This ensures lock.yaml and other build artifacts are written to writable temp location
134-
import mfc.common
135-
mfc.common.MFC_ROOT_DIR = os.getcwd()
136-
mfc.common.MFC_BUILD_DIR = os.path.join(mfc.common.MFC_ROOT_DIR, "build")
137-
mfc.common.MFC_LOCK_FILEPATH = os.path.join(mfc.common.MFC_BUILD_DIR, "lock.yaml")
138-
# Keep toolchain and examples pointing to Homebrew installation
139-
mfc.common.MFC_TOOLCHAIN_DIR = "#{prefix}/toolchain"
140-
mfc.common.MFC_EXAMPLE_DIRPATH = "#{prefix}/examples"
141-
142-
from mfc.build import MFCTarget
143-
144-
# Override get_install_binpath to use pre-installed binaries
145-
_original_get_install_binpath = MFCTarget.get_install_binpath
146-
def _homebrew_get_install_binpath(self, case):
147-
return "#{bin}/" + self.name
148-
MFCTarget.get_install_binpath = _homebrew_get_install_binpath
149-
150-
# Override is_buildable to skip building main targets
151-
_original_is_buildable = MFCTarget.is_buildable
152-
def _homebrew_is_buildable(self):
153-
if self.name in ["pre_process", "simulation", "post_process", "syscheck"]:
154-
return False
155-
return _original_is_buildable(self)
156-
MFCTarget.is_buildable = _homebrew_is_buildable
157-
PATCH_EOF
158-
159-
# Set PYTHONPATH to load our patch before running mfc.sh
160-
export PYTHONPATH="${TMPDIR}/.mfc_patch:#{prefix}/toolchain:${PYTHONPATH:-}"
134+
# Patch MFC paths before mfc.common is imported anywhere
135+
_mfc_temp_root = os.getcwd()
136+
137+
def _patch_mfc_common():
138+
"""Patches mfc.common module to use temp directory for writable files."""
139+
import mfc.common
140+
mfc.common.MFC_ROOT_DIR = _mfc_temp_root
141+
mfc.common.MFC_BUILD_DIR = os.path.join(_mfc_temp_root, "build")
142+
mfc.common.MFC_LOCK_FILEPATH = os.path.join(mfc.common.MFC_BUILD_DIR, "lock.yaml")
143+
# Keep toolchain and examples pointing to Homebrew installation
144+
mfc.common.MFC_TOOLCHAIN_DIR = "#{prefix}/toolchain"
145+
mfc.common.MFC_EXAMPLE_DIRPATH = "#{prefix}/examples"
146+
147+
def _patch_mfc_build():
148+
"""Patches MFCTarget to use pre-installed binaries."""
149+
from mfc.build import MFCTarget
150+
151+
# Override get_install_binpath to use pre-installed binaries
152+
_original_get_install_binpath = MFCTarget.get_install_binpath
153+
def _homebrew_get_install_binpath(self, case):
154+
return "#{bin}/" + self.name
155+
MFCTarget.get_install_binpath = _homebrew_get_install_binpath
156+
157+
# Override is_buildable to skip building main targets
158+
_original_is_buildable = MFCTarget.is_buildable
159+
def _homebrew_is_buildable(self):
160+
if self.name in ["pre_process", "simulation", "post_process", "syscheck"]:
161+
return False
162+
return _original_is_buildable(self)
163+
MFCTarget.is_buildable = _homebrew_is_buildable
164+
165+
# Apply patches immediately
166+
_patch_mfc_common()
167+
_patch_mfc_build()
168+
SITECUSTOMIZE_EOF
169+
170+
# Set PYTHONPATH to include current directory so sitecustomize.py is found
171+
export PYTHONPATH="${TMPDIR}:#{prefix}/toolchain:${PYTHONPATH:-}"
161172
162173
# For 'mfc run', add --no-build flag to skip compilation
163174
if [ "${1-}" = "run" ]; then

0 commit comments

Comments
 (0)