changed the functions, error handling altered, default uses install, install handles a list, picroft/mycroft handled now.
parent
a5592d8216
commit
f3453d0ffc
249
msm/msm
249
msm/msm
|
@ -23,114 +23,163 @@
|
|||
# This script assists in the installation and management of
|
||||
# skills loaded from Github.
|
||||
|
||||
set -e
|
||||
mycroft_skill_folder=${mycroft_skill_folder:-"/opt/mycroft/skills"}
|
||||
if [[ ! -d "${mycroft_skill_folder}" ]] ; then
|
||||
echo "Uname to access ${mycroft_skill_folder}!"
|
||||
exit 101
|
||||
fi
|
||||
|
||||
mycroft_skill_folder="/opt/mycroft/skills"
|
||||
mycroft_virtualenv=~/.virtualenvs/mycroft/bin/activate
|
||||
# picroft?
|
||||
if [[ "$(hostname)" == 'picroft' ]] || [[ -x /home/pi/bin/cli ]] ; then
|
||||
picroft=0
|
||||
else
|
||||
picroft=1
|
||||
if locate virtualenvwrapper.sh ; then
|
||||
source $(locate virtualenvwrapper.sh)
|
||||
else
|
||||
echo "Unable to locate virtualenvwrapper.sh, will not be able to install skills!"
|
||||
fi
|
||||
fi
|
||||
|
||||
default_skills="alarm audio-record configuration date-time desktop-launcher ip joke hello-world media npr-news naptime pairing personal reminder installer singing speak spelling stop stock volume weather wiki wolfram-alpha mark1-demo"
|
||||
|
||||
echo "####### Mycroft Skill Manager #######"
|
||||
|
||||
function help() {
|
||||
echo "msm: Mycroft Skill Manager"
|
||||
echo -e " Copyright (c) 2017 Mycroft AI, Inc. All rights reserved.\n"
|
||||
echo "usage: msm install <repository> or <name>"
|
||||
echo " Installs the given Skill into the /opt/mycroft/skills directory"
|
||||
echo " where <repository> is the address of the skill in Github."
|
||||
echo -e "example: msm install https://github.com/ethanaward/demo_skill.git\n"
|
||||
}
|
||||
|
||||
|
||||
function goto_skills_dir {
|
||||
if [ ! -d $mycroft_skill_folder ]; then
|
||||
echo "Couldn't install skill, $mycroft_skill_folder does not exist"
|
||||
exit 1
|
||||
fi
|
||||
cd $mycroft_skill_folder
|
||||
}
|
||||
|
||||
function install() {
|
||||
goto_skills_dir
|
||||
if [ -z "$2" ]; then
|
||||
echo "You must pass the git url or skill name"
|
||||
exit 1
|
||||
fi
|
||||
if [[ "$2" == "git@"* || "$2" == "https://"* || "$2" == "http://"* ]]; then
|
||||
repo=$2
|
||||
else
|
||||
skill_list="`curl -s "https://raw.githubusercontent.com/MycroftAI/mycroft-skills/master/.gitmodules"`"
|
||||
skills=`echo "$skill_list" | grep -n 'submodule' | sed 's/[[:space:]]//g' | sed 's/\[submodule"//g' | sed 's/"\]//g'`
|
||||
exact_match=`echo "$skills" | grep -i ".*:$2$"`
|
||||
skill=`echo "$skills" | grep -i ".*:.*$2.*"`
|
||||
if [ ! -z $exact_match ]; then
|
||||
skill=$exact_match
|
||||
fi
|
||||
git_line=`echo "$skill" | sed 's/\:.*//'`
|
||||
|
||||
if [[ $skill == *$'\n'* ]]; then
|
||||
echo -e "Your search has multiple choices\n\n$skill" | sed 's/.*://g'
|
||||
exit 2
|
||||
else
|
||||
if [ -z $git_line ]; then
|
||||
echo "Skill not found"
|
||||
exit 3
|
||||
fi
|
||||
repo_line=$(($git_line + 2))
|
||||
repo=`echo "$skill_list" | sed -n $repo_line'{p;q;}' | sed 's/[[:space:]]//g' | sed 's/url=//g'`
|
||||
fi
|
||||
fi
|
||||
git_name=`echo "$repo" | sed 's/.*\///'`
|
||||
name=`echo "$git_name" | sed 's/.git//'`
|
||||
echo "Cloning repository"
|
||||
git clone $repo >> /dev/null
|
||||
cd $name
|
||||
if [ -f "requirements.txt" ]; then
|
||||
echo "Installing libraries requirements"
|
||||
pip install -r requirements.txt
|
||||
fi
|
||||
echo "Skill installed!"
|
||||
}
|
||||
|
||||
function update() {
|
||||
goto_skills_dir
|
||||
for d in $(ls -d */); do
|
||||
if git -C "$d" rev-parse --git-dir > /dev/null 2>&1; then
|
||||
cd $d
|
||||
if [[ -z $(git status --porcelain) ]]; then
|
||||
git fetch
|
||||
git reset --hard origin/master
|
||||
fi
|
||||
cd ..
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
function install_defaults() {
|
||||
skills=( "alarm" "audio-record" "configuration" "date-time" "desktop-launcher" "ip" "joke" "hello-world" "media" "npr-news" "naptime" "pairing" "personal" "reminder" "installer" "singing" "speak" "spelling" "stop" "stock" "volume" "weather" "wiki" "wolfram-alpha" "mark1-demo" )
|
||||
for i in "${skills[@]}"
|
||||
do
|
||||
if [ ! -d "$mycroft_skill_folder/skill-$i" ]; then
|
||||
install "" "https://github.com/MycroftAI/skill-$i.git"
|
||||
fi
|
||||
done
|
||||
update
|
||||
echo "Installed!"
|
||||
exit 0
|
||||
echo "msm: Mycroft Skill Manager"
|
||||
echo -e " Copyright (c) 2017 Mycroft AI, Inc. All rights reserved.\n"
|
||||
echo "usage: msm install <repository> or <name>"
|
||||
echo " Installs the given Skill into the ${mycroft_skill_folder}"
|
||||
echo " where <repository> is the address of the skill in Github."
|
||||
echo "example: msm search rss-skill"
|
||||
echo -e "example: msm install https://github.com/ethanaward/demo_skill.git\n"
|
||||
exit 1
|
||||
}
|
||||
|
||||
function list() {
|
||||
curl -s "https://raw.githubusercontent.com/MycroftAI/mycroft-skills/master/.gitmodules" | grep 'submodule "' | sed 's/\[submodule "//g'| sed 's/"\]//g'
|
||||
if hash curl ; then
|
||||
if ! curl -s "https://raw.githubusercontent.com/MycroftAI/mycroft-skills/master/.gitmodules" ; then
|
||||
echo "Unable to pull master skills list!"
|
||||
exit 111
|
||||
fi
|
||||
else
|
||||
if ! wget -qO- "https://raw.githubusercontent.com/MycroftAI/mycroft-skills/master/.gitmodules" ; then
|
||||
echo "Unable to pull master skills list!"
|
||||
exit 112
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "$1" = "install" ]; then
|
||||
install $*
|
||||
elif [ "$1" = "list" ]; then
|
||||
echo -e "Searching...\n"
|
||||
list
|
||||
elif [ "$1" = "update" ]; then
|
||||
update
|
||||
elif [ "$1" = "default" ]; then
|
||||
install_defaults
|
||||
else
|
||||
help
|
||||
fi
|
||||
function install() {
|
||||
cd "${mycroft_skill_folder}"
|
||||
# loop through list of arguments
|
||||
while [[ $# -gt 0 ]] ; do
|
||||
iskill="${1}";
|
||||
shift;
|
||||
echo "Attempting to install ${iskill}..."
|
||||
if [[ "${iskill}" == "git@"* || "${iskill}" == "https://"* || "${iskill}" == "http://"* ]]; then
|
||||
repo="${iskill}"
|
||||
else
|
||||
skill_list=$(list)
|
||||
skills=$(echo "$skill_list" | grep -n 'submodule' | sed 's/[[:space:]]//g' | sed 's/\[submodule"//g' | sed 's/"\]//g')
|
||||
exact_match=$(echo "$skills" | grep -i ".*:${iskill}$")
|
||||
skill=$(echo "$skills" | grep -i ".*:.*${iskill}.*")
|
||||
if [[ ! -z "${exact_match}" ]]; then
|
||||
skill=${exact_match}
|
||||
fi
|
||||
git_line=$(echo "$skill" | sed 's/\:.*//')
|
||||
|
||||
if [[ "${skill}" == *$'\n'* ]]; then
|
||||
echo -e "Your search has multiple choices\n\n$skill" | sed 's/.*://g'
|
||||
return 3
|
||||
else
|
||||
if [[ -z "${git_line}" ]]; then
|
||||
echo "Skill not found"
|
||||
return 3
|
||||
fi
|
||||
repo_line=$(($git_line + 2))
|
||||
repo=$(echo "$skill_list" | sed -n $repo_line'{p;q;}' | sed 's/[[:space:]]//g' | sed 's/url=//g')
|
||||
fi
|
||||
fi
|
||||
git_name=$(echo "${repo}" | sed 's/.*\///')
|
||||
name=$(echo "$git_name" | sed 's/.git//')
|
||||
echo "Cloning repository"
|
||||
git clone "${repo}" >> /dev/null
|
||||
if ! cd "${name}" ; then
|
||||
echo "Unable to access directory ${name}!"
|
||||
return 102
|
||||
fi
|
||||
if [[ -f "requirements.txt" ]]; then
|
||||
echo "Installing libraries requirements"
|
||||
if [[ $picroft -eq 1 ]]; then
|
||||
if workon mycroft ; then
|
||||
if ! pip install -r requirements.txt ; then
|
||||
echo "Unable to install requirements for skill ${iskill}!"
|
||||
deactivate
|
||||
return 121
|
||||
fi
|
||||
else
|
||||
echo "Unable to activate mycroft virtualenv!"
|
||||
deactivate
|
||||
return 120
|
||||
fi
|
||||
else
|
||||
if ! pip install -r requirements.txt ; then
|
||||
echo "Unable to install requirements for skill ${iskill}!"
|
||||
return 121
|
||||
fi
|
||||
fi
|
||||
|
||||
fi
|
||||
echo "Skill ${iskill} has been installed!"
|
||||
done
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
cd "${mycroft_skill_folder}"
|
||||
for d in $(find "${mycroft_skill_folder}" -mindepth 1 -maxdepth 1 -type d |grep -v '.git'$ ); do
|
||||
if git -C "$d" rev-parse --git-dir > /dev/null 2>&1; then
|
||||
cd "${d}"
|
||||
if [[ -z $(git status --porcelain) ]]; then
|
||||
git fetch
|
||||
git reset --hard origin/master
|
||||
fi
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
function search() {
|
||||
skill_list=$(list)
|
||||
search_list=$(echo "$skill_list" | grep 'submodule "' | sed 's/\[submodule "//g'| sed 's/"\]//g')
|
||||
while [[ $# -gt 0 ]] ; do
|
||||
search_string=$1
|
||||
shift
|
||||
while read -r matches; do
|
||||
if [[ "${search_string}" == "${matches}" ]] ; then
|
||||
echo "Exact match found: ${matches}"
|
||||
else
|
||||
echo "Possible match: ${matches}"
|
||||
fi
|
||||
done < <(grep -i "${search_string}" <<< "${search_list}")
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
#### Main
|
||||
|
||||
OPT=$1
|
||||
shift
|
||||
case ${OPT} in
|
||||
"install") if [[ $# -gt 0 ]] ; then install "$*" ; else help ; fi;;
|
||||
"list") list | grep 'submodule "' | sed 's/\[submodule "//g'| sed 's/"\]//g' ;;
|
||||
"update") update ;;
|
||||
"default") install $(echo ${default_skills}) ;;
|
||||
"search") if [[ $# -gt 0 ]] ; then search "$*" ; else help ; fi;;
|
||||
*) help ;;
|
||||
esac
|
||||
|
||||
if [[ $? -gt 0 ]] ; then
|
||||
echo "Sorry I'm unable to complete the request! Check the error messages above for why."fs
|
||||
fi
|
||||
exit 0
|
Loading…
Reference in New Issue