cutekit/osdk/__init__.py

217 lines
5.5 KiB
Python
Raw Normal View History

2022-06-26 07:02:15 +00:00
import importlib
2022-06-25 22:22:53 +00:00
import shutil
import sys
2022-06-26 07:02:15 +00:00
from types import ModuleType
2022-06-25 22:22:53 +00:00
2022-06-26 06:31:43 +00:00
import osdk.build as build
import osdk.utils as utils
2022-06-26 08:24:13 +00:00
import osdk.targets as targets
2022-06-26 09:36:26 +00:00
import osdk.manifests as manifests
2022-06-25 22:22:53 +00:00
2022-10-14 09:10:06 +00:00
__version__ = "0.3.0"
2022-07-26 20:15:37 +00:00
2022-06-25 22:22:53 +00:00
CMDS = {}
2022-08-12 22:02:33 +00:00
2022-06-25 22:22:53 +00:00
def parseOptions(args: list[str]) -> dict:
result = {
'opts': {},
'args': []
}
for arg in args:
if arg.startswith("--"):
if "=" in arg:
key, value = arg[2:].split("=", 1)
result['opts'][key] = value
else:
result['opts'][arg[2:]] = True
else:
result['args'].append(arg)
return result
def propsFromOptions(opt: dict) -> dict:
result = {}
for key in opt:
if key.startswith("prop:"):
result[key[5:]] = opt[key]
return result
2022-06-25 22:22:53 +00:00
def runCmd(opts: dict, args: list[str]) -> None:
props = propsFromOptions(opts)
2022-06-25 22:22:53 +00:00
if len(args) == 0:
2022-07-21 22:32:45 +00:00
print(f"Usage: osdk run <component>")
2022-06-25 22:22:53 +00:00
sys.exit(1)
out = build.buildOne(opts.get('target', 'default'), args[0], props)
2022-06-25 22:22:53 +00:00
2022-07-23 22:46:58 +00:00
print()
2022-06-26 06:31:43 +00:00
print(f"{utils.Colors.BOLD}Running: {args[0]}{utils.Colors.RESET}")
2022-06-25 22:22:53 +00:00
utils.runCmd(out, *args[1:])
print()
print(f"{utils.Colors.GREEN}Process exited with success{utils.Colors.RESET}")
2022-06-25 22:22:53 +00:00
2022-07-27 16:53:58 +00:00
def debugCmd(opts: dict, args: list[str]) -> None:
props = propsFromOptions(opts)
if len(args) == 0:
print(f"Usage: osdk debug <component>")
sys.exit(1)
out = build.buildOne(opts.get('target', 'default:debug'), args[0], props)
print()
print(f"{utils.Colors.BOLD}Debugging: {args[0]}{utils.Colors.RESET}")
2022-08-12 22:02:33 +00:00
utils.runCmd("/usr/bin/lldb", "-o", "run", out, *args[1:])
2022-07-27 16:53:58 +00:00
print()
print(f"{utils.Colors.GREEN}Process exited with success{utils.Colors.RESET}")
2022-06-25 22:22:53 +00:00
def buildCmd(opts: dict, args: list[str]) -> None:
props = propsFromOptions(opts)
2022-07-06 21:11:10 +00:00
allTargets = opts.get('all-targets', False)
targetName = opts.get('target', 'default')
2022-06-26 06:31:43 +00:00
2022-07-06 21:11:10 +00:00
if allTargets:
for target in targets.available():
if len(args) == 0:
build.buildAll(target, props)
2022-07-06 21:11:10 +00:00
else:
for component in args:
build.buildOne(target, component, props)
2022-06-25 22:22:53 +00:00
else:
2022-07-06 21:11:10 +00:00
if len(args) == 0:
build.buildAll(targetName, props)
2022-07-06 21:11:10 +00:00
else:
for component in args:
build.buildOne(targetName, component, props)
2022-06-25 22:22:53 +00:00
2022-06-26 09:36:26 +00:00
def listCmd(opts: dict, args: list[str]) -> None:
props = propsFromOptions(opts)
targetName = opts.get('target', 'default')
target = targets.load(targetName, props)
2022-06-26 09:36:26 +00:00
components = manifests.loadAll("src", target)
print(f"Available components for target '{targetName}':")
componentsNames = list(components.keys())
componentsNames.sort()
for component in componentsNames:
2022-07-12 15:12:00 +00:00
if components[component]["enabled"]:
print(" " + component)
2022-06-26 09:36:26 +00:00
print("")
2022-06-25 22:22:53 +00:00
def cleanCmd(opts: dict, args: list[str]) -> None:
shutil.rmtree(".osdk/build", ignore_errors=True)
2022-06-25 22:22:53 +00:00
def nukeCmd(opts: dict, args: list[str]) -> None:
shutil.rmtree(".osdk", ignore_errors=True)
2022-06-25 22:22:53 +00:00
def helpCmd(opts: dict, args: list[str]) -> None:
2022-06-26 06:31:43 +00:00
print(f"Usage: osdk <command> [options...] [<args...>]")
2022-06-25 22:22:53 +00:00
print("")
print("Description:")
2022-06-26 06:31:43 +00:00
print(" Operating System Development Kit.")
2022-06-25 22:22:53 +00:00
print("")
print("Commands:")
for cmd in CMDS:
print(" " + cmd + " - " + CMDS[cmd]["desc"])
print("")
2022-06-26 08:24:13 +00:00
print("Targets:")
availableTargets = targets.available()
if len(availableTargets) == 0:
print(" No targets available")
2022-06-26 06:31:43 +00:00
else:
2022-06-26 08:24:13 +00:00
for targetName in targets.available():
print(" " + targetName)
2022-06-25 22:22:53 +00:00
print("")
print("Variants:")
2022-06-26 08:24:13 +00:00
for var in targets.VARIANTS:
2022-06-25 22:22:53 +00:00
print(" " + var)
print("")
2022-07-26 20:15:37 +00:00
def versionCmd(opts: dict, args: list[str]) -> None:
print("OSDK v" + __version__)
2022-06-25 22:22:53 +00:00
CMDS = {
"run": {
"func": runCmd,
"desc": "Run a component on the host",
},
2022-07-27 16:53:58 +00:00
"debug": {
"func": debugCmd,
"desc": "Run a component on the host in debug mode",
},
2022-06-25 22:22:53 +00:00
"build": {
"func": buildCmd,
"desc": "Build one or more components",
},
2022-06-26 09:36:26 +00:00
"list": {
"func": listCmd,
"desc": "List available components",
},
2022-06-25 22:22:53 +00:00
"clean": {
"func": cleanCmd,
"desc": "Clean the build directory",
},
"nuke": {
"func": nukeCmd,
"desc": "Clean the build directory and cache",
},
"help": {
"func": helpCmd,
"desc": "Show this help message",
2022-07-26 20:15:37 +00:00
},
"version": {
"func": versionCmd,
"desc": "Show current version",
2022-06-25 22:22:53 +00:00
},
}
2022-06-26 06:31:43 +00:00
2022-06-26 07:02:15 +00:00
def loadPlugin(path: str) -> ModuleType:
"""Load a plugin from a path"""
spec = importlib.util.spec_from_file_location("plugin", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
for files in utils.tryListDir("meta/plugins"):
if files.endswith(".py"):
plugin = loadPlugin(f"meta/plugins/{files}")
CMDS[plugin.__plugin__["name"]] = plugin.__plugin__
2022-06-26 06:31:43 +00:00
def main():
argv = sys.argv
2022-06-25 22:22:53 +00:00
try:
2022-06-26 06:31:43 +00:00
if len(argv) < 2:
2022-06-25 22:22:53 +00:00
helpCmd({}, [])
else:
2022-06-26 06:31:43 +00:00
o = parseOptions(argv[2:])
if not argv[1] in CMDS:
print(f"Unknown command: {argv[1]}")
2022-06-25 22:22:53 +00:00
print("")
2022-06-26 06:31:43 +00:00
print(f"Use '{argv[0]} help' for a list of commands")
return 1
CMDS[argv[1]]["func"](o['opts'], o['args'])
return 0
2022-06-25 22:22:53 +00:00
except utils.CliException as e:
print()
2022-06-26 06:31:43 +00:00
print(f"{utils.Colors.RED}{e.msg}{utils.Colors.RESET}")
return 1