diff --git a/scripts/commands.py b/scripts/commands.py deleted file mode 100644 index 9be0a27..0000000 --- a/scripts/commands.py +++ /dev/null @@ -1,33 +0,0 @@ -import subprocess - - -def quitCondition(command: str) -> bool: - return command in ["q", "quit", "exit", "stop"] - - -def helpCondition(command: str) -> bool: - return command in ["h", "help"] - - -def clearCondition(command: str) -> bool: - return command in ["c", "clear", "cls"] - - -def systemCommand(command: str) -> str: - words = command[slice(1, len(command))].split() - if words[0] == "": - return "Command not found. Write 'h' for help." - return subprocess.run( - f'docker exec -it webscraper {" ".join(words)}', - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ).stdout.decode() - - -def restartCondition(command: str) -> bool: - return command in ["r", "restart"] - - -def runCondition(command: str) -> bool: - return command in ["run"] diff --git a/scripts/get_path.py b/scripts/get_path.py new file mode 100644 index 0000000..0624574 --- /dev/null +++ b/scripts/get_path.py @@ -0,0 +1,30 @@ +import os +from run_command import run_command + + +def get_path(): + pwd = run_command("pwd") + splitted = pwd.split("/") + splitted[-1] = splitted[-1].replace("\n", "") + + print(splitted[: splitted.index("webscraper") + 1]) + + if splitted.count("webscraper") > 1 and "webscraper" in splitted: + for i in range(len(splitted) - 1, -1, -1): + potential_path = "/".join(splitted[: i + 1]) + if "webscraper" in potential_path: + script_path = f"{potential_path}/scripts" + if os.path.isdir(script_path): + return potential_path + else: + return "This is not a valid webscraper project." + else: + return "/".join(splitted[: splitted.index("webscraper") + 1]) + + +def run_main(): + print(get_path()) + + +if __name__ == "__main__": + run_main() diff --git a/scripts/run_command.py b/scripts/run_command.py new file mode 100644 index 0000000..df64010 --- /dev/null +++ b/scripts/run_command.py @@ -0,0 +1,13 @@ +import subprocess +import sys + + +def run_command(command: str) -> str: + process = subprocess.run( + command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + if process.returncode != 0: + print(f"Error running command: {command}") + print(process.stderr.decode()) + sys.exit(process.returncode) + return process.stdout.decode() diff --git a/scripts/start.py b/scripts/start.py index 8260e81..39c3859 100644 --- a/scripts/start.py +++ b/scripts/start.py @@ -1,31 +1,25 @@ import subprocess import os -import sys - - -def run_command(command: str) -> str: - process = subprocess.run( - command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - if process.returncode != 0: - print(f"Error running command: {command}") - print(process.stderr.decode()) - sys.exit(process.returncode) - return process.stdout.decode() +from run_command import run_command def main(): - docker_compose_file = os.getenv("DOCKER_COMPOSE_FILE", "./app/docker-compose.yaml") + docker_compose_file = os.getenv( + "DOCKER_COMPOSE_FILE", "$WEBSCRAPER/app/docker-compose.yaml" + ) service_name = os.getenv("SERVICE_NAME", "webscraper") script_name = os.getenv("SCRIPT_NAME", "main.py") + try: + print("Starting Docker Compose services...\n") + run_command(f"docker compose -f {docker_compose_file} up -d") - print("Starting Docker Compose services...\n") - run_command(f"docker compose -f {docker_compose_file} up -d") + print(run_command(f"docker exec {service_name} python {script_name}")) - print(run_command(f"docker exec {service_name} python {script_name}")) - - print("Stopping and removing Docker Compose services...") - run_command(f"docker compose -f {docker_compose_file} down") + print("Stopping and removing Docker Compose services...") + run_command(f"docker compose -f {docker_compose_file} down") + except subprocess.CalledProcessError as e: + print("An error occurred while running the script.") + print(e) if __name__ == "__main__": diff --git a/scripts/threads/commands.py b/scripts/threads/commands.py new file mode 100644 index 0000000..1e19e54 --- /dev/null +++ b/scripts/threads/commands.py @@ -0,0 +1,57 @@ +from run_command import run_command + + +def quitCondition(command: str) -> bool: + return command in ["q", "quit", "exit", "stop"] + + +def quitService(path: str): + print("Stopping and removing Docker Compose services...") + run_command(f"docker compose -f {path}/app/docker-compose.yaml down") + return None + + +def helpCondition(command: str) -> bool: + return command in ["h", "help"] + + +def clearCondition(command: str) -> bool: + return command in ["c", "clear", "cls"] + + +def systemCommand(command: str) -> str: + words = command[1:].split() + if words[0] == "": + return "Command not found. Write 'h' for help." + print( + run_command( + f'docker exec -it webscraper {" ".join(words)}', + ) + ) + return None + + +def restartCondition(command: str) -> bool: + return command in ["r", "restart"] + + +def restartService(path: str): + print("Restarting Docker Compose services...") + run_command(f"docker compose -f {path}/app/docker-compose.yaml down") + run_command(f"docker compose -f {path}/app/docker-compose.yaml up -d") + print("Composed!") + return None + + +def runCondition(command: str) -> bool: + return command in ["run"] + + +def runService(): + print("Running main.py...") + print( + run_command( + "docker exec -it webscraper python main.py", + ) + ) + return None diff --git a/scripts/threads/help_list.py b/scripts/threads/help_list.py new file mode 100644 index 0000000..3db09a2 --- /dev/null +++ b/scripts/threads/help_list.py @@ -0,0 +1,9 @@ +def help_list(): + return """ +["h", "help"], - for help. +["q", "quit", "exit", "stop"], - to stop program. +["c", "clear", "cls"], - to clear console. +["r", "restart"], - to restart Docker Compose services. +["run"], - to run main.py in docker container. +["$..."], - to evaluate command in docker container. +""" diff --git a/scripts/threads/prompt.py b/scripts/threads/prompt.py index d7ffe7e..75a1416 100644 --- a/scripts/threads/prompt.py +++ b/scripts/threads/prompt.py @@ -1,65 +1,30 @@ -import commands -import subprocess import sys -import threading +from threads.commands import * +from run_command import run_command +from get_path import get_path +from threads.help_list import help_list def prompt(): while True: command = input("> ") - if commands.quitCondition(command): - print("Stopping and removing Docker Compose services...") - subprocess.run( - "docker compose -f ../app/docker-compose.yaml down", - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) + if quitCondition(command): + quitService(get_path()) break - if commands.helpCondition(command): - print( - """ -["h", "help"], - for help. -["q", "quit", "exit", "stop"], - to stop program. -["c", "clear", "cls"], - to clear console. -["r", "restart"], - to restart Docker Compose services. -["run"], - to run main.py in docker container. -["$..."], - to evaluate command in docker container. -""" - ) + if helpCondition(command): + print(help_list()) continue - if commands.clearCondition(command): - print("\n" * 100) + if clearCondition(command): + run_command("clear") continue if command.startswith("$"): - print(commands.systemCommand(command)) + systemCommand(command) continue - if commands.restartCondition(command): - print("Restarting Docker Compose services...") - subprocess.run( - "docker compose -f ../app/docker-compose.yaml down", - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - subprocess.run( - "docker compose -f ../app/docker-compose.yaml up -d", - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - print("Composed!") + if restartCondition(command): + restartService(get_path()) continue - if commands.runCondition(command): - print("Running main.py...") - print( - subprocess.run( - "docker exec -it webscraper python main.py", - shell=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ).stdout.decode() - ) + if runCondition(command): + runService() continue if command == "": continue diff --git a/scripts/watch.py b/scripts/watch.py index 06e3c32..f13e166 100644 --- a/scripts/watch.py +++ b/scripts/watch.py @@ -1,26 +1,20 @@ -import subprocess import time import os import threading from threads.prompt import prompt - - -def run_command(command: str) -> str: - process = subprocess.run( - command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - if process.returncode != 0: - print(f"Error running command: {command}") - return process.stderr.decode() - return process.stdout.decode() - +from run_command import run_command +from get_path import get_path thread = threading.Thread(target=prompt) def main(): + path = get_path() + docker_compose_file = os.getenv( + "DOCKER_COMPOSE_FILE", f"{path}/app/docker-compose.yaml" + ) print("Starting Docker Compose services...") - run_command("docker compose -f ../app/docker-compose.yaml up -d") + run_command(f"docker compose -f {docker_compose_file} up -d") print("Composed!\n") print("Running main.py...") print(run_command("docker exec -it webscraper python main.py")) @@ -31,10 +25,9 @@ def main(): print("\nWatching for changes...") thread.start() - path_to_watch = "/home/paprykdev/uni/webscraper/app" before = { - f: os.stat(os.path.join(path_to_watch, f)).st_mtime - for f in os.listdir(path_to_watch) + f: os.stat(os.path.join(path, "app", f)).st_mtime + for f in os.listdir(os.path.join(path, "app")) if f.endswith(".py") } @@ -43,8 +36,8 @@ def main(): break time.sleep(1) after = { - f: os.stat(os.path.join(path_to_watch, f)).st_mtime - for f in os.listdir(path_to_watch) + f: os.stat(os.path.join(path, "app", f)).st_mtime + for f in os.listdir(os.path.join(path, "app")) if f.endswith(".py") } for f in before: