#!/bin/sh
#
# This tool can be used to install a new package into MicroPython
# library location (for unix port, default behavior), or produce
# complete library snapshot to be deployed on a device for baremetal
# ports (if PIP_MICROPY_DEST environment var is set).
#
# Currently supported usage:
#
# pip-micropython install [-t/--target
]
# parse command
if [ "$1" != "install" ]; then
echo "Only install command is supported currently"
exit 1
fi
shift
# parse options
targetdest=''
if [ "$1" == "-t" -o "$1" == "--target" ]; then
targetdest="$2"
shift
shift
fi
if [ -z "$TMPDIR" ]; then
TMPDIR=/tmp
fi
TMPVENV="$TMPDIR/pip-micropy-venv"
if [ -n "$targetdest" ]; then
dest="$targetdest"
echo "Destination snapshot directory: $dest"
elif [ -n "$PIP_MICROPY_DEST" ]; then
dest="$PIP_MICROPY_DEST"
echo "Destination snapshot directory: $dest"
elif [ -n "$MICROPYPATH" ]; then
libdest=$(echo "$MICROPYPATH" | awk -F: ' {print $1}')
echo "Destination library directory: $libdest"
else
echo "Warning: MICROPYPATH is not set, assuming default value"
libdest=~/.micropython/lib
echo "Destination library directory: $libdest"
fi
# Due to bugs in pip, installation should happen with active virtualenv
# The issue (at least with pip 1.0 which is still what's shipped with many
# distros) is that even if --ignore-installed is used, package is not
# installed if it's already installed for main python distribution.
if [ ! -d "$TMPVENV" ]; then
virtualenv --no-site-packages "$TMPVENV"
# distutils, setuptools, pip are buggy and allow target packages affect
# their execution environment. For example, if distribution they install
# has re.py, they will import that instead of system re. So, we need
# to remove current dir from sys.path, but that appear to be quite uneasy
# with CPython, so we hook __import__ and exterminate it persistently.
# See also https://bitbucket.org/pypa/setuptools/issue/187/
cat > $(ls -1d "$TMPVENV"/lib/python*/)/sitecustomize.py <