diff --git a/helper/helper.py b/helper/helper.py new file mode 100644 index 0000000..3cfcdb1 --- /dev/null +++ b/helper/helper.py @@ -0,0 +1,45 @@ +import ast +class color: + green = "\033[1;32m" + blue = "\033[1;34m" + white = "\033[0m" + green = "\033[1;32m" + red = "\033[1;31m" + white = "\033[0m" + blue = "\033[1;34m" + orange = "\033[1;33m" + +def update(self, packages): + with open(("./packages/pakk.conf"), "w") as f: + f.write(str(packages)) + +def toList(List: str) -> list: + + newList = ast.literal_eval(List) + if isinstance(newList, list): + return newList + +def install(name:str, Packages:list): + try: + + if name in [row[0] for row in Packages.packages]: + raise Exception("Package already installed") + + with open(("./packages/"+name+".py"), "r") as f: + line = f.readline() + + line = line.strip("#") + + # ... (existing code) + + print("Installing Package") + + + + + Packages.packages.append(toList(line)) + Packages.update(Packages.packages) + print(f"\n{color.green}Success:{color.white} Successfully installed {name}.") + + except Exception as e: + raise Exception(f"{color.red}{e}") \ No newline at end of file diff --git a/main.py b/main.py index e803a81..c0c1e10 100644 --- a/main.py +++ b/main.py @@ -11,12 +11,7 @@ if os.name != "nt": import threading import signal import requests - -def handler(signum, frame): - return - -# Set the signal handler -signal.signal(signal.SIGINT, handler) +import subprocess @@ -25,12 +20,18 @@ class color: green = "\033[1;32m" blue = "\033[1;34m" white = "\033[0m" - green = "\033[1;32m" red = "\033[1;31m" - white = "\033[0m" - blue = "\033[1;34m" orange = "\033[1;33m" +def restart_program(): + python = sys.executable + subprocess.call([python, __file__]) + sys.exit() + + + + + def clear(): os.system('cls' if os.name == 'nt' else 'clear') @@ -139,19 +140,25 @@ class packagemanager: def install(self, name: str) -> int: try: - print("Checking for package source.") - with open(("./packages/"+name+".py"), "r") as f: - f.close() + if not os.path.exists(f"./packages/{name}.py"): + raise FileNotFoundError(name) + + except FileNotFoundError: - try: + print("Downloading File from Source") url = f"https://raw.githubusercontent.com/OusmBlueNinja/TermPY/main/packages/{name}.py" r = requests.get(url) #print(r.text) - fileInstallingCreator = os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.basename(url)) + fileInstallingCreator = os.path.join(os.path.dirname(os.path.realpath(__file__)), "packages", name+".py") + print("Saving file to disk") with open(fileInstallingCreator, "w") as newFile: newFile.write(r.text) - except Exception as e: - raise Exception("Package could not be found, make sure package source is in [ packages ] folder.") + time.sleep(1) + call("helper.helper", "install", name, Packages) + print(f"{color.orange}WARNING: {color.white}Restarting Program") + restart_program() + except Exception as e: + raise Exception(f"Package could not be found, make sure package source is in [ packages ] folder. {e}") try: @@ -258,9 +265,10 @@ def main(): def start(): try: + print("") main() - except KeyboardInterrupt: - start() + except (KeyboardInterrupt, EOFError): + restart_program() if __name__ == '__main__': diff --git a/packages/pakk.conf b/packages/pakk.conf index 92b94cf..11ba7f3 100644 --- a/packages/pakk.conf +++ b/packages/pakk.conf @@ -1 +1 @@ -[['builtin', 'packages.builtin', ['echo', 'ls', 'rm', 'clear', 'cd', 'll']], ['upd', 'packages.update', ['upd']], ['pip', 'packages.pip', ['pip']]] \ No newline at end of file +[['builtin', 'packages.builtin', ['echo', 'ls', 'rm', 'clear', 'cd', 'll']], ['netget', 'packages.netget', ['netget']], ['netget', 'packages.netget', ['netget']], ['netget', 'packages.netget', ['netget']]] \ No newline at end of file