-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathboot.py
More file actions
45 lines (37 loc) · 1.21 KB
/
boot.py
File metadata and controls
45 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import importlib
import pkgutil
COMMANDS_PACKAGE = 'commands'
def load_commands():
commands = {}
package = importlib.import_module(COMMANDS_PACKAGE)
for loader, name, is_pkg in pkgutil.iter_modules(package.__path__):
if name.startswith('_'):
continue
mod = importlib.import_module(f'{COMMANDS_PACKAGE}.{name}')
if hasattr(mod, 'run'):
commands[name] = mod
return commands
def main():
os.system('cls' if os.name == 'nt' else 'clear')
print('Booting PyOS...')
commands = load_commands()
print(f'Found commands ({len(commands)}): {", ".join(sorted(commands))}')
while True:
try:
cmdline = input(f'{os.getcwd()} > ').strip()
if not cmdline:
continue
parts = cmdline.split()
name, args = parts[0], parts[1:]
if name in commands:
if name == 'exit':
commands[name].run(args)
break
commands[name].run(args)
else:
print(f'Unknown command: {name}')
except KeyboardInterrupt:
print('\nUse \'exit\' to quit.')
if __name__ == '__main__':
main()