diff --git a/scripts/commands.py b/scripts/commands.py new file mode 100644 index 0000000..9be0a27 --- /dev/null +++ b/scripts/commands.py @@ -0,0 +1,33 @@ +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/start.py b/scripts/start.py new file mode 100644 index 0000000..66ee960 --- /dev/null +++ b/scripts/start.py @@ -0,0 +1,25 @@ +import subprocess + + +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() + + +def main(): + print("Starting Docker Compose services...\n") + run_command("docker compose -f ../app/docker-compose.yaml up -d") + + print(run_command("docker exec -it webscraper python main.py")) + + print("Stopping and removing Docker Compose services...") + run_command("docker compose -f ../app/docker-compose.yaml down") + + +if __name__ == "__main__": + main() diff --git a/scripts/threads/prompt.py b/scripts/threads/prompt.py new file mode 100644 index 0000000..9fa3c73 --- /dev/null +++ b/scripts/threads/prompt.py @@ -0,0 +1,67 @@ +import commands +import subprocess +import sys +import threading + + +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, + ) + 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. +""" + ) + continue + if commands.clearCondition(command): + print("\n" * 100) + continue + if command.startswith("$"): + print(commands.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!") + 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() + ) + continue + else: + print("Command not found. Write 'h' for help.") + continue + sys.exit(0) diff --git a/scripts/watch.py b/scripts/watch.py new file mode 100644 index 0000000..06e3c32 --- /dev/null +++ b/scripts/watch.py @@ -0,0 +1,59 @@ +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() + + +thread = threading.Thread(target=prompt) + + +def main(): + print("Starting Docker Compose services...") + run_command("docker compose -f ../app/docker-compose.yaml up -d") + print("Composed!\n") + print("Running main.py...") + print(run_command("docker exec -it webscraper python main.py")) + print( + "\n\nWrite 'q' to stop program. Don't stop with 'Ctrl + C' otherwise docker container will be still on." + ) + print("For help write 'h'.") + 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) + if f.endswith(".py") + } + + while True: + if threading.active_count() == 1: + 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) + if f.endswith(".py") + } + for f in before: + if before[f] != after[f]: + print(f"\nDetected change in {f}") + print("Running main.py...") + print(run_command("docker exec -it webscraper python main.py")) + before[f] = after[f] + + +if __name__ == "__main__": + main() diff --git a/start.py b/start.py deleted file mode 100644 index 9d25bd4..0000000 --- a/start.py +++ /dev/null @@ -1,47 +0,0 @@ -import subprocess -import time - - -def run_command(command): - result = subprocess.run( - command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - if result.returncode != 0: - print(f"Error running command: {command}") - print(result.stderr.decode()) - return result.stdout.decode() - - -def wait_for_webscraper(): - while True: - result = run_command("docker compose ps -q webscraper") - container_id = result.strip() - - if not container_id: - print("Webscraper container not found.") - break - - status = run_command( - f"docker inspect --format '{{.State.Status}}' {container_id}" - ) - - if status.strip() == "exited": - print("Webscraper has finished.") - break - - print("Waiting for webscraper to finish...") - time.sleep(3) - - -def main(): - print("Starting Docker Compose services...") - run_command("docker compose up -d") - - wait_for_webscraper() - - print("Stopping and removing Docker Compose services...") - run_command("docker compose down") - - -if __name__ == "__main__": - main() diff --git a/start.sh b/start.sh deleted file mode 100755 index 635cdfc..0000000 --- a/start.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -docker compose up -d - -docker compose wait webscraper > /dev/null - -# docker compose down \ No newline at end of file