Add files via upload

This commit is contained in:
blueShard-dev
2020-06-28 11:15:51 +00:00
committed by GitHub
parent b8e3072563
commit bc65ff575d
31 changed files with 6762 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

View File

@ -0,0 +1,228 @@
#!/usr/bin/python3
from shell_utils import *
import sys
sys.path.insert(0, "..")
help = """
Usage:
aion [command]
Commands:
start starts aion
run runs aion
install <skill / plugin> installs a skill or plugin
uninstall <skill / plugin> uninstalls a skill or plugin
remove <skill / plugin> removes a skill or plugin
update <skill / plugin> updates a skill or plugin
version <skill / plugin> version of a skill or plugin
save <name> saves the current aion_data directory (with a name)
load <version> [name] loads a saved aion_data directory (add optional name to save the current aion_data directory with this name)
saves shows all saved aion_data directory
pid shows the pid from the running aion process
kill kills aion
stop stops aion
pack <custom skill / plugin directory> packs the given directory with a custom skill or plugin into one standalone file for installation
"""
def main():
from argparse import ArgumentParser, RawTextHelpFormatter
import os
parser = ArgumentParser(description="Command line support for the 'aion' project", formatter_class=RawTextHelpFormatter, add_help=False)
parser.add_argument("command", nargs="+")
args = parser.parse_args()
try:
command = args.command[0].strip()
if os.path.isfile(command):
import __init__
__init__.ExecuteAionFile(command)
elif command == "help" or command == "-help" or command == "--help":
print(help)
elif command == "start" or command == "run":
os.system("python3 " + atils.aion_path + "/main.py")
elif command == "install":
errno = "77227"
arglen_check(args.command, 2)
package = args.command[1]
if package == "aion":
must_be_sudo()
Install.aion()
print("Installed aion")
elif package == "respeaker":
must_be_sudo()
Install.respeaker(yesno("Install in compatibility mode? (installs older kernel version)? (y/n): "))
print("Installed respeaker")
elif os.path.exists(package):
if package.strip() == "skill.aion":
Install.skill_from_aion_file(package)
elif package.strip() == "plugin.aion":
Install.plugin_from_aion_file(package)
elif package.endswith(".skill"):
Install.skill_from_skill_file(package)
elif package.endswith(".plugin"):
Install.plugin_from_plugin_file(package)
else:
AionShellError(package + " is an unknowing skill / plugin. Type 'aion help' for help", errno)
else:
AionShellError(package + " is an unknowing skill / plugin. Type 'aion help' for help", errno)
elif args.command in ["kill", "stop"]: # kist
arglen_check(args.command, 1)
is_aion_running(command)
import variable
import signal
avar = variable.Variable()
os.kill(int(avar.get_value("AION_PID")), signal.SIGKILL)
elif command == "load":
arglen_check(args.command, 2, 3)
import save as asave
if len(args.command) == 3:
asave.load_save(str(args.command[1]), str(args.command[2]))
else:
asave.load_save(str(args.command[1]))
elif command == "pack":
no_skill_plugin_file_errno = "27177"
no_dir_errno = "27178"
arglen_check(args.command, 2)
dir = args.command[1]
if os.path.isdir(dir):
if os.path.isfile(dir + "/skill.aion"):
Pack.skill(dir)
elif os.path.isfile(dir + "/plugin.aion"):
Pack.plugin(dir)
else:
AionShellError("couldn't find 'skill.aion' or 'plugin.aion' in " + dir + ". See 'create_skill_file' function in 'aionlib.skill' to create a 'skill.aion' file"
"or 'create_plugin_file' function in 'aionlib.plugin' to create a 'plugin.aion' file", no_skill_plugin_file_errno)
else:
AionShellError("couldn't find directory " + dir, no_dir_errno)
elif command == "pid":
arglen_check(args.command, 1)
is_aion_running(command)
import variable
avar = variable.Variable()
print(avar.get_value("AION_PID"))
elif command == "save":
arglen_check(args.command, 2)
import save as asave
asave.save(args.command[1])
elif command == "saves":
arglen_check(args.command, 1)
import save as asave
print(" ".join(asave.saves()))
elif command in ["remove", "uninstall"]: # UnRem
errno = "07340"
arglen_check(args.command, 2)
package = args.command[1]
if command == "uninstall":
name = "Uninstall"
else:
name = "Remove"
question = yesno(name + " " + package + "? (y/n): ")
if package == "aion":
if question is True:
question = yesno("Should your personal data ('/etc/aion_data/': custom skills / plugins, version saves, language files, ...) deleted as well? (y/n): ")
UnRem.aion(question)
print(name + "ed aion")
print("You should reboot now to complete the " + name.lower() + " process")
elif package == "aionlib":
if question is True:
UnRem.aionlib()
print(name + "ed aionlib")
elif package == "respeaker":
if question is True:
UnRem.respeaker()
print(name + "ed respeaker")
else:
package_type = which_package(package, "Type in the number of your package type: ")
if package_type == -1:
AionShellError("couldn't find skill / plugin " + package, errno)
elif package_type == 0:
UnRem.skill(package)
elif package_type == 1:
UnRem.run_after_plugin(package)
elif package_type == 2:
UnRem.run_before_plugin(package)
elif command in ["run", "start"]:
arglen_check(args.command, 1)
import start
from os import geteuid
if geteuid() == 0:
start(True)
elif geteuid() == 1000:
start(False)
elif command == "update":
errno = "43503"
arglen_check(args.command, 2)
package = args.command[1]
if package == "aion":
Update.aion()
elif package == "aionlib":
Update.aionlib()
else:
package_type = which_package(package, "Type in the number of your package type: ")
if package_type == -1:
AionShellError("couldn't find skill / plugin " + package, errno)
elif package_type == 0:
Update.skill(package)
elif package_type == 1:
Update.run_after_plugin(package)
elif package_type == 2:
Update.run_before_plugin(package)
elif command == "variable":
arglen_check(args.command, 2)
command_variable = args.command[1]
import variable
avar = variable.Variable()
print(avar.get_value(command_variable))
elif command == "version":
errno = "56297"
arglen_check(args.command, 2)
package = args.command[1]
if package == "aion":
Version.aion()
elif package == "aionlib":
Version.aionlib()
else:
package_type = which_package(package, "Type in the number of your package type: ")
if package_type == -1:
AionShellError("couldn't find skill / plugin " + package, errno)
elif package_type == 0:
Version.skill(package)
elif package_type == 1:
Version.run_after_plugin(package)
elif package_type == 2:
Version.run_before_plugin(package)
else:
errno = "12345"
AionShellError(" ".join(args.command) + " isn't a command. Type 'aion help' to get help", errno)
except KeyboardInterrupt:
exit(-1)

View File

@ -0,0 +1,612 @@
#!/usr/bin/python3
import sys
from glob import glob
sys.path.insert(1, glob("/usr/local/aion-*/aion_core/")[0])
import utils as atils
class AionShellError:
"""
base class for errors while using the 'aion' shell command (for the exact identification of the error)
"""
def __init__(self, errormsg: str, errno: str = "00000") -> None:
"""
the error 'function'
:param errormsg: str
message that should be printed out
syntax: <error message>
example: "An example error occured"
:param errno: str
error number
syntax: <error number>
example: "11112"
:return: None
:since: 0.1.0
"""
from colorama import Fore
print(Fore.RED + "AionShellError: [Errno " + str(errno) + "]: " + str(errormsg) + Fore.RESET)
def arglen_check(arg_list: list, arg_len: int, max_arg_len: int = None) -> None:
"""
checks if the length of a list is equal to a given length
:param arg_list: list
list you want to check if the length is equal to the given length
syntax: [<list content>]
example: ["example1", "example2"]
:param arg_len: int
length you want to check
syntax: <length>
example: 2
:param max_arg_len: int
if not None, it checks if the arg list length is between 'arg_len' and 'max_arg_len
syntax: <max length>
example: 4
:return: None
:since: 0.1.0
"""
if max_arg_len is None:
max_arg_len = arg_len
if len(arg_list) < arg_len or len(arg_list) > max_arg_len:
from colorama import Fore
if arg_len == max_arg_len:
if arg_len == 1:
print(Fore.RED + "Expected 1 argument, got " + str(len(arg_list)) + " (" + ", ".join(arg_list) + "). Type 'aion help' for help" + Fore.RESET)
else:
print(Fore.RED + "Expected " + str(arg_len) + " arguments, got " + str(len(arg_list)) + " (" + ", ".join(arg_list) + "). Type 'aion help' for help" + Fore.RESET)
else:
print(Fore.RED + "Expected " + str(arg_len) + " to " + str(max_arg_len) + " arguments, got " + str(len(arg_list)) + " (" + ", ".join(arg_list) + "). Type 'aion help' for help" + Fore.RESET)
exit(-1)
def which_package(package: str, input_sentence: str) -> int:
"""
if a user want to uninstall (or something else) a skill or plugin and there are more than one plugin / skill with this name, this function get called
:param package: str
name of the package
syntax: <package name>
example: "test_package
:param input_sentence: str
sentence the user should be asked for input
syntax: <input sentence>
example: "Type in the number of your package type: "
:return: int
returns the type of the package (0 = skill; 1 = run after plugin; 2 = run before plugin)
syntax: <package type>
example: 2
:since: 0.1.0
"""
from plugin import get_all_run_after_plugins, get_all_run_before_plugins
from skill import get_all_skills
in_package = []
if package in get_all_skills():
in_package.append("Skill: " + str(len(in_package)))
else:
for plugin in get_all_run_after_plugins().values():
if package in plugin:
in_package.append("Run after plugin: " + str(len(in_package)))
for plugin in get_all_run_before_plugins().values():
if package in plugin:
in_package.append("Run before plugin: " + str(len(in_package)))
if len(in_package) == 0:
return -1
elif len(in_package) == 1:
choose = 0
else:
for pa in in_package:
print(pa)
print("Found package '" + str(package) + "' multiple times")
while True:
input_choose = input(input_sentence)
if str(input_choose) not in [str(l) for l in range(len(in_package))]:
print("Your number must be in " + ", ".join([str(l) for l in range(len(in_package))]))
else:
choose = input_choose
break
if in_package[choose].startswith("Skill"):
return 0
elif in_package[choose].startswith("Run after"):
return 1
elif in_package[choose].startswith("Run before"):
return 2
def is_aion_running(command: str) -> None:
"""
checks if aion is running
:param command: str
command that the user has typed in
syntax: <command>
example: "pid"
:return: None
:since: 0.1.0
"""
import variable
from ast import literal_eval
from colorama import Fore
if literal_eval(variable.Variable().get_value(variable.IS_AION_RUNNING)) is False:
print(Fore.RED + command + " can only used if aion is running. Type 'aion run' or 'aion start' to start aion" + Fore.RESET)
exit(-1)
def must_be_sudo() -> None:
"""
checks if the user is root and if not it prints an warning message
:return: None
:since: 0.1.0
"""
from colorama import Fore
if atils.is_root() is False:
print(Fore.RED + "to execute the command, aion must be run as sudo" + Fore.RESET)
exit(-1)
def yesno(question: str) -> bool:
"""
asks a yes no question
:param question: str
question you want to ask
syntax: <question>
example: "Execute test command? (y/n): "
:return: bool
returns if the user entered y / yes (True) or n / no (False)
syntax: <boolean>
example: True
:since: 0.1.0
"""
while True:
yn = input(question)
if yn.lower().strip() == "y" or yn.lower().strip() == "yes":
return True
elif yn.lower().strip() == "n" or yn.lower().strip() == "no":
return False
else:
print("Please choose 'y' or 'n'")
class Install:
"""
base class for installation
"""
@staticmethod
def aion() -> None:
"""
installs aion
:return: None
:since: 0.1.0
"""
must_be_sudo()
from os import system
from shutil import rmtree
system("git clone https://github.com/blueShard/aion_project /tmp/aion_project")
system("bash /tmp/aion_project/install.sh")
rmtree("/tmp/aion_project", ignore_errors=True)
@staticmethod
def plugin_from_aion_file(fname: str) -> None:
"""
installs a plugin from a 'plugin.aion' file
:param fname: str
path of the file
syntax: <filename>
example: "/home/pi/test_plugin/plugin.aion"
:return: None
:since: 0.1.0
"""
must_be_sudo()
from plugin import execute_aion_file_type_plugin
execute_aion_file_type_plugin(fname)
@staticmethod
def plugin_from_plugin_file(fname: str) -> None:
"""
installs a plugin from a '.plugin' package
:param fname: str
path of the file
syntax: <filename>
example: "/home/pi/test_plugin/test.plugin"
:return: None
:since: 0.1.0
"""
must_be_sudo()
from plugin import execute_plugin_file
execute_plugin_file(fname)
@staticmethod
def skill_from_aion_file(fname: str) -> None:
"""
installs a skill from a 'skill.aion' file
:param fname: str
path of the file
syntax: <filename>
example: "/home/pi/test_skill/skill.aion"
:return: None
:since: 0.1.0
"""
must_be_sudo()
from skill import execute_aion_file_type_skill
execute_aion_file_type_skill(fname)
@staticmethod
def skill_from_skill_file(fname: str) -> None:
"""
installs a skill from a '.skill' package
:param fname: str
path of the file
syntax: <filename>
example: "/home/pi/test_skill/test.skill"
:return: None
:since: 0.1.0
"""
must_be_sudo()
from skill import execute_skill_file
execute_skill_file(fname)
@staticmethod
def respeaker(compatibility_mode: bool = False) -> None:
"""
installs respeaker
:param compatibility_mode:
installs an old kernel version which is definitely compatible with respeaker
(the current version may contain patches etc. which make respeaker not work properly anymore and to which the developers of respeaker have to adapt the software first)
syntax: <boolean>
example: False
:return: None
:since: 0.1.0
"""
must_be_sudo()
from os import system
from shutil import copy, rmtree
system("git clone https://github.com/respeaker/seeed-voicecard.git /tmp/seeed-voicecard")
copy("/tmp/seeed-voicecard/uninstall.sh", atils.aion_path + "/etc/respeaker_uninstall.sh")
if compatibility_mode:
system("cd /tmp/seeed-voicecard/; ./install.sh --compat-kernel")
else:
system("cd /tmp/seeed-voicecard/; ./install.sh")
system("amixer cset numid=3 2")
rmtree("/tmp/seeed-voicecard", ignore_errors=True)
class Pack:
"""
base class for packing plugin or skill directories to '.plugin' or '.skill' packages
"""
@staticmethod
def skill(directory: str) -> None:
"""
creates a skill package from given directory
:param directory: str
path of the directory
syntax: <filename>
example: "/home/pi/test_skill.skill"
:return: None
:since: 0.1.0
"""
from skill import create_skill_package
create_skill_package(directory)
@staticmethod
def plugin(directory: str) -> None:
"""
creates a plugin package from given directory
:param directory: str
path of the directory
syntax: <filename>
example: "/home/pi/test_plugin.plugin"
:return: None
:since: 0.1.0
"""
from plugin import create_plugin_package
create_plugin_package(directory)
class UnRem: # this class name combine the word uninstall and remove
@staticmethod
def aion(personal_data: bool = False) -> None:
"""
deletes aion
:param personal_data: bool
checks if all personal data should be deleted as well
syntax: <boolean>
example: True
:return: None
:since: 0.1.0
"""
must_be_sudo()
from glob import glob
from os import system
if personal_data is True:
system("bash " + glob("/usr/local/aion-*/etc/uninstall.sh")[0] + " --all")
else:
system("bash " + glob("/usr/local/aion-*/etc/uninstall.sh")[0])
@staticmethod
def aionlib() -> None:
"""
uninstalls aionlib
:return: None
:since: 0.1.0
"""
must_be_sudo()
from os import system
system("yes | pip3 uninstall aionlib")
@staticmethod
def run_after_plugin(name: str) -> None:
"""
uninstalls a run after plugin
:param name: str
name of the run after plugin
syntax: <run after plugin>
example: "test_run_after_plugin"
:return: None
:since: 0.1.0
"""
must_be_sudo()
from plugin import RUN_AFTER, remove_plugin
remove_plugin(name, RUN_AFTER)
@staticmethod
def run_before_plugin(name: str) -> None:
"""
uninstalls a run before plugin
:param name: str
name of the run before plugin
syntax: <run before plugin>
example: "test_run_before_plugin"
:return: None
:since: 0.1.0
"""
must_be_sudo()
from plugin import RUN_BEFORE, remove_plugin
remove_plugin(name, RUN_BEFORE)
@staticmethod
def skill(name: str) -> None:
"""
uninstalls a skill
:param name: str
name of the skill
syntax: <skill>
example: "test_skill"
:return: None
:since: 0.1.0
"""
must_be_sudo()
from skill import remove_skill
remove_skill(name)
@staticmethod
def respeaker() -> None:
"""
uninstalls respeaker (if installed)
:return: None
:since: 0.1.0
"""
must_be_sudo()
from os import path, system
if path.isfile(atils.aion_path + "/etc/respeaker_uninstall.sh") is False:
raise FileNotFoundError("can't find file '" + atils.aion_path + "/etc/respeaker_uninstall.sh' to uninstall respeaker")
else:
system("bash " + atils.aion_path + "/etc/respeaker_uninstall.sh")
class Update:
@staticmethod
def aion() -> None:
"""
updates aion
:return: None
:since: 0.1.0
"""
from colorama import Fore
print(Fore.RED + "The update command for aion isn't created yet" + Fore.RESET)
@staticmethod
def aionlib() -> None:
"""
updates aionlib
:return: None
:since: 0.1.0
"""
must_be_sudo()
from os import system
system("yes | pip3 install --upgrade aionlib")
@staticmethod
def run_after_plugin(name: str) -> None:
"""
updates a run after plugin
:param name: str
name of the plugin
syntax: <plugin name>
example: "test_run_after_plugin"
:return: None
:since: 0.1.0
"""
from colorama import Fore
print(Fore.RED + "The update command for 'run_after_plugin' isn't created yet")
@staticmethod
def run_before_plugin(name: str) -> None:
"""
updates a run before plugin
:param name: str
name of the run before plugin
syntax: <plugin name>
example: "test_run_before_plugin"
:return: None
:since: 0.1.0
"""
from colorama import Fore
print(Fore.RED + "The update command for 'run_before_plugin' isn't created yet")
@staticmethod
def skill(name: str) -> None:
"""
updates a skill
:param name: str
name if the skill
syntax: <skill name>
example: "test_skill"
:return: None
:since: 0.1.0
"""
from colorama import Fore
print(Fore.RED + "The update command for 'skill' isn't created yet")
class Version:
@staticmethod
def aion() -> None:
"""
prints the version of 'aion'
:return: None
:since: 0.1.0
"""
from glob import glob
print("".join(glob("/usr/local/aion-*").remove("/usr/local/aion-")))
@staticmethod
def aionlib() -> None:
"""
prints the version of aionlib (if installed)
:return: None
:since: 0.1.0
"""
try:
from aionlib import __version__ as aionlib_version
print(aionlib_version)
except ImportError:
print("'aionlib' isn't installed. To install aionlib, type 'sudo pip3 install aionlib'")
@staticmethod
def run_after_plugin(name: str) -> None:
"""
prints the version of a run after plugin
:param name: str
name of the plugin
syntax: <plugin name>
example: "test_run_after_plugin"
:return: None
:since: 0.1.0
"""
from plugin import get_run_after_plugin_infos
for key, item in get_run_after_plugin_infos(name):
if key == "version":
print(item)
break
@staticmethod
def run_before_plugin(name: str) -> None:
"""
prints the version of a run before plugin
:param name: str
name of the plugin
syntax: <plugin name>
example: "test_run_before_plugin"
:return: None
:since: 0.1.0
"""
from plugin import get_run_before_plugin_infos
for key, item in get_run_before_plugin_infos(name):
if key == "version":
print(item)
break
@staticmethod
def skill(name: str) -> None:
"""
prints the version of a skill
:param name: str
name of the skill
syntax: <plugin name>
example: "test_skill"
:return: None
:since: 0.1.0
"""
from skill import get_skill_infos
for key, item in get_skill_infos(name):
if key == "version":
print(item)
break