Skip to content

Instantly share code, notes, and snippets.

@aatchison
Created January 15, 2017 18:35
Show Gist options
  • Save aatchison/6356a9773e3bbd0c426eebabf34de1c1 to your computer and use it in GitHub Desktop.
Save aatchison/6356a9773e3bbd0c426eebabf34de1c1 to your computer and use it in GitHub Desktop.
Mycroft packaging sample with relocatable virtualenv and python bdists
#!/usr/bin/env bash
set -Ee
TOP=$(cd $(dirname $0)/.. && pwd -L)
VIRTUALENV_ROOT=${VIRTUALENV_ROOT:-"${HOME}/.virtualenvs/mycroft"}
MYCROFT_CORE_SRC=${TOP}/src/mycroft-core
if [ "$1" = "-q" ]; then
QUIET="echo"
fi
# clean
cd ${TOP}
rm -Rvf ${TOP}/src
mkdir -p ${TOP}/src
cd ${TOP}/src
rm -rf ${MYCROFT_CORE_SRC}/dist
function _run() {
if [[ "$QUIET" ]]; then
echo "$*"
else
eval "$@"
fi
}
function python_package() {
local SETUP_SCRIPT=$1
cp ${SETUP_SCRIPT} ${MYCROFT_CORE_SRC}/setup.py
python ${MYCROFT_CORE_SRC}/setup.py clean
python ${MYCROFT_CORE_SRC}/setup.py bdist_egg
python ${MYCROFT_CORE_SRC}/setup.py sdist
#_run python ${MYCROFT_CORE_SRC}/setup.py sdist upload -r mycroft
rm ${MYCROFT_CORE_SRC}/setup.py
}
git clone https://github.com/MycroftAi/mycroft-core.git
cd mycroft-core
VERSION="$(basename $(git for-each-ref --format="%(refname:short)" --sort=-authordate --count=1 refs/tags) | sed -e 's/v//g')"
#VERSION="0.8.0" # for hard coded version number, as sometimes git describe doesn't work
git checkout release/v${VERSION}
echo "version=\"${VERSION}\"" > ${MYCROFT_CORE_SRC}/mycroft/__version__.py
# create virtualenv, consistent with virtualenv-wrapper conventions
if [ ! -d ${VIRTUALENV_ROOT} ]; then
mkdir -p $(dirname ${VIRTUALENV_ROOT})
virtualenv -p python2.7 ${VIRTUALENV_ROOT}
fi
source ${VIRTUALENV_ROOT}/bin/activate
cd ${MYCROFT_CORE_SRC}
easy_install pip==7.1.2 # force version of pip
pip install -r requirements.txt
pip install -r test-requirements.txt
cd ${MYCROFT_CORE_SRC}
python_package mycroft-base-setup.py
python_package skills-sdk-setup.py
# build distributable virtualenv
ARCH="$(dpkg --print-architecture)"
SYSTEM_TARGET="/usr/local/"
ARTIFACT_BASE="mycroft-core-${ARCH}-${VERSION}"
MYCROFT_ARTIFACT_DIR=${MYCROFT_CORE_SRC}/build/${ARTIFACT_BASE}
virtualenv --always-copy --clear ${MYCROFT_ARTIFACT_DIR}
virtualenv --always-copy --clear --relocatable ${MYCROFT_ARTIFACT_DIR}
. ${MYCROFT_ARTIFACT_DIR}/bin/activate
pip install -r ${MYCROFT_CORE_SRC}/requirements.txt
cd ${MYCROFT_CORE_SRC}
python mycroft-base-setup.py install
${MYCROFT_CORE_SRC}/scripts/install-pygtk.sh
virtualenv --always-copy --relocatable ${MYCROFT_ARTIFACT_DIR}
mkdir -p ${MYCROFT_CORE_SRC}/dist
cd ${MYCROFT_CORE_SRC}/build
tar -czf ${MYCROFT_CORE_SRC}/dist/${ARTIFACT_BASE}.tar.gz ${ARTIFACT_BASE}
# package distributable virtualenv into deb
function replace() {
local FILE=$1
local PATTERN=$2
local VALUE=$3
local TMP_FILE="/tmp/$$.replace"
cat ${FILE} | sed -e "s/${PATTERN}/${VALUE}/g" > ${TMP_FILE}
mv ${TMP_FILE} ${FILE}
}
DEB_BASE="mycroft-core-${ARCH}_${VERSION}-1"
DEB_DIR=${MYCROFT_CORE_SRC}/build/${DEB_BASE}
mkdir -p ${DEB_DIR}/DEBIAN
echo "Creating debian control file"
# setup control file
CONTROL_FILE=${DEB_DIR}/DEBIAN/control
cp ${TOP}/publish-core-release/deb_base/control.template ${CONTROL_FILE}
replace ${CONTROL_FILE} "%%PACKAGE%%" "mycroft-core"
replace ${CONTROL_FILE} "%%VERSION%%" "${VERSION}"
replace ${CONTROL_FILE} "%%ARCHITECTURE%%" "${ARCH}"
replace ${CONTROL_FILE} "%%DESCRIPTION%%" "mycroft-core"
replace ${CONTROL_FILE} "%%DEPENDS%%" "dnsmasq, avrdude, jq, portaudio19-dev, libglib2.0-0, flac, mpg123, mimic, pulseaudio"
echo "Creating debian preinst file"
PREINST_FILE=${DEB_DIR}/DEBIAN/preinst
cp ${TOP}/publish-core-release/deb_base/preinst.template ${PREINST_FILE}
replace ${PREINST_FILE} "%%INSTALL_USER%%" "mycroft"
chmod 0755 ${PREINST_FILE}
echo "Creating debian postinst file"
POSTINST_FILE=${DEB_DIR}/DEBIAN/postinst
cp ${TOP}/publish-core-release/deb_base/postinst.template ${POSTINST_FILE}
replace ${POSTINST_FILE} "%%INSTALL_USER%%" "mycroft"
chmod 0755 ${POSTINST_FILE}
echo "Creating debian prerm file"
PRERM_FILE=${DEB_DIR}/DEBIAN/prerm
cp ${TOP}/publish-core-release/deb_base/prerm.template ${PRERM_FILE}
replace ${PRERM_FILE} "%%INSTALL_USER%%" "mycroft"
chmod 0755 ${PRERM_FILE}
echo "Creating debian postrm file"
POSTRM_FILE=${DEB_DIR}/DEBIAN/postrm
cp ${TOP}/publish-core-release/deb_base/postrm.template ${POSTRM_FILE}
replace ${POSTRM_FILE} "%%INSTALL_USER%%" "mycroft"
chmod 0755 ${POSTRM_FILE}
# setup init scripts
function setup_init_script() {
local NAME=$1
echo "Creating init script for ${NAME}"
INIT_SCRIPT=${DEB_DIR}/etc/init.d/${NAME}
mkdir -p $(dirname ${INIT_SCRIPT})
cp ${TOP}/publish-core-release/deb_base/init.template ${INIT_SCRIPT}
replace ${INIT_SCRIPT} "%%NAME%%" "${NAME}"
replace ${INIT_SCRIPT} "%%DESCRIPTION%%" "${NAME}"
replace ${INIT_SCRIPT} "%%COMMAND%%" "\/usr\/local\/bin\/${NAME}"
replace ${INIT_SCRIPT} "%%USERNAME%%" "mycroft"
chmod a+x ${INIT_SCRIPT}
}
# setup init script to start the enclosure-client
function setup_init_script_enclosure() {
local NAME=$1
echo "Creating init script for ${NAME}"
INIT_SCRIPT=${DEB_DIR}/etc/init.d/${NAME}
mkdir -p $(dirname ${INIT_SCRIPT})
cp ${TOP}/publish-core-release/deb_base/init.enclosure.template ${INIT_SCRIPT}
replace ${INIT_SCRIPT} "%%NAME%%" "${NAME}"
replace ${INIT_SCRIPT} "%%DESCRIPTION%%" "${NAME}"
replace ${INIT_SCRIPT} "%%COMMAND%%" "\/usr\/local\/bin\/${NAME}"
replace ${INIT_SCRIPT} "%%USERNAME%%" "mycroft"
chmod a+x ${INIT_SCRIPT}
}
# setup root user init scripts
function setup_init_script_root() {
local NAME=$1
echo "Creating init script for ${NAME}"
INIT_SCRIPT=${DEB_DIR}/etc/init.d/${NAME}
mkdir -p $(dirname ${INIT_SCRIPT})
cp ${TOP}/publish-core-release/deb_base/init.template ${INIT_SCRIPT}
replace ${INIT_SCRIPT} "%%NAME%%" "${NAME}"
replace ${INIT_SCRIPT} "%%DESCRIPTION%%" "${NAME}"
replace ${INIT_SCRIPT} "%%COMMAND%%" "\/usr\/local\/bin\/${NAME}"
replace ${INIT_SCRIPT} "%%USERNAME%%" "root"
chmod a+x ${INIT_SCRIPT}
}
setup_init_script "mycroft-messagebus"
setup_init_script "mycroft-skills"
setup_init_script "mycroft-speech-client"
if [ ${ARCH} = "armhf" ]; then
setup_init_script_enclosure "mycroft-enclosure-client"
setup_init_script_root "mycroft-wifi-setup-client"
fi
mkdir -p ${DEB_DIR}/${SYSTEM_TARGET}
cp -rf ${MYCROFT_CORE_SRC}/build/${ARTIFACT_BASE}/* ${DEB_DIR}/${SYSTEM_TARGET}
mkdir -p ${DEB_DIR}/home/mycroft/.mycroft/
# write installed config file
cat > ${DEB_DIR}/home/mycroft/.mycroft/mycroft.conf << EOM
{
"tts": {
"module": "mimic",
"mimic": {
"voice": "ap",
"path":"/usr/local/bin/mimic"
}
}
}
EOM
echo "Copy polkit power permissions file"
POLKIT_DIR=${DEB_DIR}/etc/polkit-1/localauthority/50-local.d
POLKIT_POWER_FILE=${TOP}/publish-core-release/polkit/allow_user_to_shutdown_reboot.pkla
mkdir -vp ${POLKIT_DIR}
cp -v ${POLKIT_POWER_FILE} ${POLKIT_DIR}
# ensures enclosure version
ENCLOSURE_DIR=${DEB_DIR}/opt/mycroft/
mkdir -p ${ENCLOSURE_DIR}
ENCLOSURE_VERSION=$(cat ${MYCROFT_CORE_SRC}/mycroft/client/enclosure/version.txt)
cat > ${DEB_DIR}/opt/mycroft/version.json << EOM
{
"coreVersion": "${VERSION}",
"enclosureVersion": "$ENCLOSURE_VERSION"
}
EOM
# create /opt/mycroft/skills directory and chmod 777
mkdir -p ${DEB_DIR}/opt/mycroft/skills
chmod -v 777 ${DEB_DIR}/opt/mycroft/skills
cd $(dirname ${DEB_DIR})
dpkg-deb --build ${DEB_BASE}
mv *.deb ${MYCROFT_CORE_SRC}/dist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment