Skip to content

Commit 2e26984

Browse files
authored
Work harder to get a Python virtual environment. (#977)
* Work harder to get a Python virtual environment. * Fix variable name. * Use sed instead of awk. * Simplify search for activate & pio * activate should be next to pio * Update -V penv/venv help * Create penv if it doesn't exist. * Try to get workflow to run on my repo.
1 parent 72dab8f commit 2e26984

File tree

1 file changed

+102
-26
lines changed

1 file changed

+102
-26
lines changed

build.sh

Lines changed: 102 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
99
PIO_VENV_ROOT="${HOME}/.platformio/penv"
10+
PC_VENV_ROOT="${SCRIPT_DIR}/build/.venv"
1011

1112
BUILD_ALL=0
1213
RUN_BUILD=0
@@ -48,16 +49,6 @@ check_python_version() {
4849
fi
4950
}
5051

51-
# Check if "python" exists first since that's what PlatformIO names it
52-
PYTHON=python
53-
if ! check_python_version "${PYTHON}" ; then
54-
PYTHON=python3
55-
if ! check_python_version "${PYTHON}" ; then
56-
echo "Python 3 is not installed"
57-
exit 1
58-
fi
59-
fi
60-
6152
function display_board_names {
6253
while IFS= read -r piofile; do
6354
BOARD_NAME=$(echo $(basename $piofile) | sed 's#^platformio-##;s#.ini$##')
@@ -92,12 +83,13 @@ function show_help {
9283
echo " -c # run clean before build"
9384
echo " -p TGT # perform PC build instead of ESP, for given target (e.g. APPLE|ATARI)"
9485
echo " -g # enable debug in generated fujinet-pc exe"
95-
echo " -G GEN # Use GEN as the Generator for cmake (e.g. -G \"Unix Makefiles\" )"
86+
echo " -G GEN # Use GEN as the Generator for cmake (e.g. -G \"Unix Makefiles\" )"
9687
echo ""
97-
echo "other options:"
88+
echo "other options:"
9889
echo " -y # answers any questions with Y automatically, for unattended builds"
9990
echo " -h # this help"
10091
echo " -V # Override default Python virtual environment location (e.g. \"-V ~/.platformio/penv\")"
92+
echo " # Alternatively, this can be set with the shell env var VENV_ROOT"
10193
echo ""
10294
echo "Additional Args can be accepted to pass values onto sub processes where supported."
10395
echo " e.g. ./build.sh -p APPLE -- -DFOO=BAR"
@@ -140,13 +132,109 @@ do
140132
G) CMAKE_GENERATOR=${OPTARG} ;;
141133
y) ANSWER_YES=1 ;;
142134
z) ZIP_MODE=1 ;;
143-
V) PIO_VENV_ROOT=${OPTARG} ;;
135+
V) VENV_ROOT=${OPTARG} ;;
144136
h) show_help ;;
145137
*) show_help ;;
146138
esac
147139
done
148140
shift $((OPTIND - 1))
149141

142+
# Requirements:
143+
# - python3
144+
# - python3 can create venv - PlatformIO also needs this to install penv
145+
# - if doing ESP32 build:
146+
# - PlatformIO
147+
# - not ESP32 build:
148+
# - cmake
149+
150+
# Make sure we have python3 and it has the ability to create venvs
151+
PYTHON=python
152+
if ! check_python_version "${PYTHON}" ; then
153+
PYTHON=python3
154+
if ! check_python_version "${PYTHON}" ; then
155+
echo "Python 3 is not installed"
156+
exit 1
157+
fi
158+
fi
159+
160+
if ! ${PYTHON} -c "import venv, ensurepip" 2>/dev/null ; then
161+
echo "Error: Python venv module is not installed."
162+
exit 1
163+
fi
164+
165+
# if ! "$PYTHON" -m pip --version >/dev/null 2>&1; then
166+
# echo Error: pip is not installed.
167+
# exit 1
168+
# fi
169+
170+
if [ -z "${VENV_ROOT}" ] ; then
171+
if [ -n "${PC_TARGET}" ] ; then
172+
VENV_ROOT="${PC_VENV_ROOT}"
173+
else
174+
VENV_ROOT="${PIO_VENV_ROOT}"
175+
fi
176+
fi
177+
178+
ACTIVATE="${VENV_ROOT}/bin/activate"
179+
if [ -z "${PC_TARGET}" ] ; then
180+
# Doing a PlatformIO build, locate PlatformIO. It may or may not
181+
# already be in the users' path.
182+
if [ -e "${ACTIVATE}" ] ; then
183+
# Activate now in case pio isn't already in PATH
184+
source "${ACTIVATE}"
185+
fi
186+
PIO=$(command -v pio)
187+
if [ -z "${PIO}" ] ; then
188+
echo Please install platformio
189+
exit 1
190+
fi
191+
fi
192+
193+
# Let the user know about any required packages they need to install
194+
MISSING=""
195+
if [ -n "${PC_TARGET}" ] ; then
196+
for REQUIRED in g++ make cmake ; do
197+
if ! command -v ${REQUIRED} > /dev/null ; then
198+
MISSING="${REQUIRED} ${MISSING}"
199+
fi
200+
done
201+
fi
202+
if [ -n "${MISSING}" ] ; then
203+
echo The following commands need to be installed: ${MISSING}
204+
exit 1
205+
fi
206+
207+
if [[ "$VIRTUAL_ENV" != "$VENV_ROOT" ]] ; then
208+
if [ ! -e "${ACTIVATE}" ] ; then
209+
echo Creating venv at "${VENV_ROOT}"
210+
mkdir -p $(dirname "${VENV_ROOT}")
211+
${PYTHON} -m venv "${VENV_ROOT}" || exit 1
212+
fi
213+
if [ -e "${ACTIVATE}" ] ; then
214+
source "${ACTIVATE}"
215+
fi
216+
if [[ "$VIRTUAL_ENV" != "$VENV_ROOT" ]] ; then
217+
echo Unable to activate penv/venv
218+
exit 1
219+
fi
220+
fi
221+
222+
# If pio is the one installed by the system it runs the system
223+
# python instead of the penv python, blocking pip from installing
224+
# packages
225+
if [ -z "${PC_BUILD}" ] ; then
226+
PIO=$(command -v pio)
227+
if [ "${PIO}" != "${VENV_ROOT}/bin/pio" ] ; then
228+
pip install platformio
229+
fi
230+
fi
231+
232+
echo Virtual env: "${VIRTUAL_ENV}"
233+
echo venv root: "${VENV_ROOT}"
234+
echo Activate: "${ACTIVATE}"
235+
echo PATH: "${PATH}"
236+
command -v pio
237+
150238
if [ $SHOW_BOARDS -eq 1 ] ; then
151239
display_board_names
152240
exit 1
@@ -159,18 +247,6 @@ if [ $BUILD_ALL -eq 1 ] ; then
159247
exit $?
160248
fi
161249

162-
##############################################################
163-
# Set up the virtual environment if it exists
164-
if [[ -d "$PIO_VENV_ROOT" && "$VIRTUAL_ENV" != "$PIO_VENV_ROOT" ]] ; then
165-
echo "Activating virtual environment"
166-
[[ -z "$VIRTUAL_ENV" ]] && deactivate &>/dev/null
167-
source "$PIO_VENV_ROOT/bin/activate"
168-
elif [[ -d "$PIO_VENV_ROOT" && "$VIRTUAL_ENV" == "$PIO_VENV_ROOT" ]] ; then
169-
echo "Virtual environment already activated at $PIO_VENV_ROOT"
170-
else
171-
echo "Warning: Virtual environment not found in $PIO_VENV_ROOT. Continuing without it."
172-
fi
173-
174250
##############################################################
175251
# PC BUILD using cmake
176252
if [ ! -z "$PC_TARGET" ] ; then
@@ -372,7 +448,7 @@ if [ ${SHOW_MONITOR} -eq 1 ] ; then
372448
# device monitor hard codes to using platformio.ini, let's grab all the data it would use directly from our generated ini.
373449
MONITOR_PORT=$(grep ^monitor_port $INI_FILE | cut -d= -f2 | cut -d\; -f1 | awk '{print $1}')
374450
MONITOR_SPEED=$(grep ^monitor_speed $INI_FILE | cut -d= -f2 | cut -d\; -f1 | awk '{print $1}')
375-
MONITOR_FILTERS=$(grep ^monitor_filters $INI_FILE | cut -d= -f2 | cut -d\; -f1 | tr ',' '\n' | sed 's/^ *//; s/ *$//' | awk '{printf("-f %s ", $1)}')
451+
MONITOR_FILTERS=$(grep ^monitor_filters $INI_FILE | cut -d= -f2 | cut -d\; -f1 | tr ',' '\n' | sed 's/^ *//; s/ *$//' | awk '{printf("-f %s ", $1)}')
376452

377453
# warn the user if the build_board in platformio.ini (if exists) is not the same as INI_FILE version, as that means stacktrace will not work correctly
378454
# because the monitor does not allow an INI file to be set!!

0 commit comments

Comments
 (0)