Skip to content

Commit

Permalink
Make the build-venv auto-activate fully instead of changing how zap-c…
Browse files Browse the repository at this point in the history
…li is located
  • Loading branch information
ksperling-apple committed May 6, 2023
1 parent 38575f2 commit b8edd5c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
8 changes: 7 additions & 1 deletion scripts/configure
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,15 @@ function check_build_env() {

function configure_python_env() {
progress "Setting up Python venv"
"$PYTHON" -m venv "$BUILD_ENV_PATH"
"$PYTHON" -m venv --clear "$BUILD_ENV_PATH"
info "$BUILD_ENV_PATH"

# Install our auto-loading venvactivate module so that running scripts via
# the venv python has the side-effect of fully activating the environment.
local sitepkgs=("${BUILD_ENV_PATH}/lib/python"*"/site-packages")
[[ -d "$sitepkgs" ]] || fail "Failed to locate venv site-packages"
cp "${CHIP_ROOT}/scripts/configure.venv/venvactivate".{pth,py} "${sitepkgs}/"

progress "Installing Python build dependencies"
"${BUILD_ENV_PATH}/bin/pip" install --require-virtualenv --quiet --upgrade pip wheel
"${BUILD_ENV_PATH}/bin/pip" install --require-virtualenv --quiet \
Expand Down
1 change: 1 addition & 0 deletions scripts/configure.venv/venvactivate.pth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import venvactivate
11 changes: 11 additions & 0 deletions scripts/configure.venv/venvactivate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Activates the current venv as if the activate script had been sourced
import collections
import os
import sys

# Prepend the venv bin to PATH (without introducing duplicate entries)
path = [os.path.join(sys.prefix, 'bin')] + os.environ['PATH'].split(':')
os.environ['PATH'] = ':'.join(collections.OrderedDict.fromkeys(path).keys())

# Set VIRTUAL_ENV to the venv directory
os.environ['VIRTUAL_ENV'] = sys.prefix
47 changes: 27 additions & 20 deletions scripts/tools/zap/zap_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@
#
MIN_ZAP_VERSION = '2023.4.27'

ZAP_INSTALL_BANNER = '*'*80 + """
* You may need to install zap from https://github.com/project-chip/zap/releases
* Please ensure one of these applies (see docs/guides/BUILDING.md for details):
* - `zap-cli` is in your Python venv or $PATH.
* - `zap-cli` is in $ZAP_INSTALL_PATH
* Use this option if you installed zap but do not want to update $PATH.
* - Point $ZAP_DEVELOPMENT_PATH to your local copy of zap that you develop on.
""" + '*'*80


class ZapTool:
def __init__(self):
Expand All @@ -56,15 +47,11 @@ def __init__(self):
os.environ['ZAP_SKIP_REAL_VERSION'] = '1'
self.check_version = False # cannot check versions without dirtying tree
elif 'ZAP_INSTALL_PATH' in os.environ:
self.zap_start = [os.path.join(os.environ['ZAP_INSTALL_PATH'], 'zap-cli')]
self.zap_start = [os.path.join(
os.environ['ZAP_INSTALL_PATH'], 'zap-cli')]
self.working_directory = None
else:
# If we're running within a Python venv, check for a zap-cli binary there
venv_zap_cli = os.path.join(sys.prefix, 'bin', 'zap-cli')
if sys.prefix != sys.base_prefix and os.path.isfile(venv_zap_cli):
self.zap_start = [venv_zap_cli]
else:
self.zap_start = ['zap-cli']
self.zap_start = ['zap-cli']
self.working_directory = None

def version_check(self, min_version=None):
Expand Down Expand Up @@ -94,8 +81,18 @@ def version_check(self, min_version=None):
f"Failed to find `Version: ....` line from {self.zap_start} --version")
sys.exit(1)
except FileNotFoundError as e:
print(f'FAILED TO EXECUTE ZAP GENERATION: {e.strerror} - "{e.filename}"')
print(ZAP_INSTALL_BANNER)
print(
f'FAILED TO EXECUTE ZAP GENERATION: {e.strerror} - "{e.filename}"')
print('*'*80)
print('* You may need to install zap. Please ensure one of these applies:')
print(
'* - `zap-cli` is in $PATH. Install from https://github.com/project-chip/zap/releases')
print('* see docs/guides/BUILDING.md for details')
print('* - `zap-cli` is in $ZAP_INSTALL_PATH. Use this option if you')
print('* installed zap but do not want to update $PATH')
print('* - Point $ZAP_DEVELOPMENT_PATH to your local copy of zap that you')
print('* develop on (to use a developer build of zap)')
print('*'*80)
sys.exit(1)

def parse_version_string(str):
Expand All @@ -112,6 +109,16 @@ def run(self, cmd: str, *extra_args: Tuple[str]):
subprocess.check_call(
self.zap_start + [cmd] + list(extra_args), cwd=self.working_directory)
except FileNotFoundError as e:
print(f'FAILED TO EXECUTE ZAP GENERATION: {e.strerror} - "{e.filename}"')
print(ZAP_INSTALL_BANNER)
print(
f'FAILED TO EXECUTE ZAP GENERATION: {e.strerror} - "{e.filename}"')
print('*'*80)
print('* You may need to install zap. Please ensure one of these applies:')
print(
'* - `zap-cli` is in $PATH. Install from https://github.com/project-chip/zap/releases')
print('* see docs/guides/BUILDING.md for details')
print('* - `zap-cli` is in $ZAP_INSTALL_PATH. Use this option if you')
print('* installed zap but do not want to update $PATH')
print('* - Point $ZAP_DEVELOPMENT_PATH to your local copy of zap that you')
print('* develop on (to use a developer build of zap)')
print('*'*80)
sys.exit(1)

0 comments on commit b8edd5c

Please sign in to comment.