Files
PyJail/main.py
2024-11-22 19:36:13 +01:00

59 lines
2.1 KiB
Python

"""
This is a sort of OS built in Python, not bootable, but creates a custom directory structure and path definition.
This is the "kernel", it hosts all features and runs all programs.
For safety reasons the kernel is isolated, which means that with every shell instance,
A new kernel instance will follow it. Same goes for every program, it will need to call upon a brand-new kernel instance.
Version: 0.1.1-nps3
"""
import os
import runpy
class PyJail:
"""
The "kernel" for PyNVOS
"""
def __init__(self):
self.rootpath = ""
self.rootpath = self.fs()
def run_program(self, path_to_bin):
"""
Runs a specified program.
"""
path_to_bin = self.fs(path_to_bin)
# print(path_to_bin)
# print(str(self.rootpath) + str(path_to_bin))
if path_to_bin == 3 or path_to_bin == 2:
print("An error has occurred launching the program.")
else:
runpy.run_path(path_to_bin)
def fs(self, check_path=None) -> str:
"""
Keeps track of the jailed filesystem and makes sure any calls to any
file get done in the jailed filesystem
"""
if check_path is not None:
if os.path.exists(f"{self.rootpath}{check_path}"):
if check_path.startswith("."):
check_path = check_path.lstrip(".")
return os.getcwd() + check_path if "vfs" in os.getcwd() else 2
return self.rootpath + check_path
elif self.rootpath in check_path:
print("ERR: Cannot parse rootpath, expected vfspath")
return 3
else:
# Path is not in the jailed fs, so we say it doesn't exist.
print("ERR: File/directory doesn't exist in vfspath")
return 2
else:
rootpath = os.getcwd() + "/vfs"
return rootpath
@staticmethod
def is_posix_compatible() -> bool:
"""
Returns if the kernel is POSIX compatible.
"""
return False