diff --git a/catfish/utils/sockets.py b/catfish/utils/sockets.py index 3be14b5..bd1b9eb 100644 --- a/catfish/utils/sockets.py +++ b/catfish/utils/sockets.py @@ -2,10 +2,11 @@ import os import select import shutil import tempfile +from pathlib import Path import ujson -BASE_SOCKET_DIR = os.path.join(tempfile.gettempdir(), "catfish") +BASE_SOCKET_DIR = Path(tempfile.gettempdir()).joinpath("catfish") BUFFER_SIZE = 4096 DEFAULT_SOCKET_READ_TIMEOUT = 0.01 @@ -41,5 +42,5 @@ def delete_base_socket_dir(): pass -def stdout_socket_for_pid(pid: int) -> str: - return os.path.join(BASE_SOCKET_DIR, "{}.stdout.sock".format(pid)) +def stdout_socket_for_pid(pid: int) -> Path: + return BASE_SOCKET_DIR.joinpath("{}.stdout.sock".format(pid)) diff --git a/catfish/worker/__init__.py b/catfish/worker/__init__.py index 7f032d8..0a081f7 100644 --- a/catfish/worker/__init__.py +++ b/catfish/worker/__init__.py @@ -1,5 +1,4 @@ import asyncio -import os import time import psutil @@ -10,13 +9,13 @@ from catfish.utils.sockets import BASE_SOCKET_DIR from .server import WORKER_SERVER_SOCKET, start_server -PID_FILE = os.path.join(BASE_SOCKET_DIR, "catfish.pid") +PID_FILE = BASE_SOCKET_DIR.joinpath("catfish.pid") def is_running(): - if not os.path.exists(PID_FILE): + if not PID_FILE.exists(): return False - if not os.path.exists(WORKER_SERVER_SOCKET): + if not WORKER_SERVER_SOCKET.exists(): return False try: get_running_process() diff --git a/catfish/worker/server.py b/catfish/worker/server.py index 2dcd6fa..131c900 100644 --- a/catfish/worker/server.py +++ b/catfish/worker/server.py @@ -4,6 +4,7 @@ import os import shlex import socket import subprocess +from pathlib import Path import click import zmq @@ -18,20 +19,20 @@ from catfish.utils.sockets import ( stdout_socket_for_pid, ) -WORKER_SERVER_SOCKET = os.path.join(BASE_SOCKET_DIR, "catfish.sock") +WORKER_SERVER_SOCKET = BASE_SOCKET_DIR.joinpath("catfish.sock") def send_to_server(payload): with socket.socket(socket.AF_UNIX, type=socket.SOCK_STREAM) as sock: - sock.connect(WORKER_SERVER_SOCKET) + sock.connect(str(WORKER_SERVER_SOCKET)) sock.sendall(ujson.dumps(payload).encode() + NEW_LINE) return read_all_from_socket(sock) -def read_from_stdout_socket(socket_path): +def read_from_stdout_socket(socket_path: Path): ctx = zmq.Context() sock = ctx.socket(zmq.SUB) - sock.connect("ipc://" + socket_path) + sock.connect("ipc://" + str(socket_path)) sock.setsockopt_string(zmq.SUBSCRIBE, "") while True: yield sock.recv_string().strip() @@ -46,7 +47,7 @@ async def publish_stdout(process): sock = ctx.socket(zmq.PUB) socket_path = stdout_socket_for_pid(process.pid) - sock.bind("ipc://" + socket_path) + sock.bind("ipc://" + str(socket_path)) while True: output = await process.stdout.readline() if not output: diff --git a/tests/__init__.py b/tests/__init__.py index ee7b914..9a4b5e6 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,6 +2,7 @@ import functools import os import shutil import subprocess +from pathlib import Path from unittest import TestCase from aiohttp.test_utils import AioHTTPTestCase, unused_port @@ -19,8 +20,9 @@ from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir class BaseTestCase(TestCase): - DUMMY_EXE = os.path.join(os.path.dirname(__file__), "dummy_program.py") - EXAMPLE_DIR = os.path.join(os.path.dirname(__file__), "../example") + TESTS_DIR = Path(os.path.dirname(__file__)) + DUMMY_EXE = TESTS_DIR.joinpath("dummy_program.py") + EXAMPLE_DIR = TESTS_DIR.parent.joinpath("example") def setUp(self): create_base_socket_dir() @@ -31,7 +33,7 @@ class BaseTestCase(TestCase): def terminate_dummy_processes(self): dummy_processes = [] for process in get_root_process().children(recursive=True): - if self.DUMMY_EXE in " ".join(process.cmdline()): + if str(self.DUMMY_EXE) in " ".join(process.cmdline()): dummy_processes.append(process) terminate_processes(dummy_processes) diff --git a/tests/test_project/test_project.py b/tests/test_project/test_project.py index d454776..89d596d 100644 --- a/tests/test_project/test_project.py +++ b/tests/test_project/test_project.py @@ -21,4 +21,4 @@ class ProjectTestCase(BaseTestCase): self.assertEqual(process.command, "python -m http.server $PORT") def test_name(self): - self.assertEqual(self.project.name, 'example') + self.assertEqual(self.project.name, "example") diff --git a/tests/test_router/test_utils.py b/tests/test_router/test_utils.py index 7e629d2..77fc1f9 100644 --- a/tests/test_router/test_utils.py +++ b/tests/test_router/test_utils.py @@ -13,7 +13,7 @@ class ProcessForHostnameTestCase(BaseTestCase): def test_finds_process(self): proc = subprocess.Popen( - self.DUMMY_EXE, + str(self.DUMMY_EXE), stdout=subprocess.PIPE, env={**os.environ, utils.HOSTNAME_ENV_VAR: "localhost"}, ) @@ -22,7 +22,7 @@ class ProcessForHostnameTestCase(BaseTestCase): def test_finds_process_with_multiple_hostnames(self): proc = subprocess.Popen( - self.DUMMY_EXE, + str(self.DUMMY_EXE), stdout=subprocess.PIPE, env={**os.environ, utils.HOSTNAME_ENV_VAR: "localhost,test.local"}, ) diff --git a/tests/test_utils/test_processes.py b/tests/test_utils/test_processes.py index 2ed376f..3fc5d31 100644 --- a/tests/test_utils/test_processes.py +++ b/tests/test_utils/test_processes.py @@ -23,7 +23,7 @@ class TerminateProcessesTestCase(BaseTestCase): created_processes = [] for _ in range(10): created_processes.append( - subprocess.Popen(self.DUMMY_EXE, stdout=subprocess.PIPE) + subprocess.Popen(str(self.DUMMY_EXE), stdout=subprocess.PIPE) ) for proc in created_processes: diff --git a/tests/test_worker/test_server.py b/tests/test_worker/test_server.py index bbf1000..92c387c 100644 --- a/tests/test_worker/test_server.py +++ b/tests/test_worker/test_server.py @@ -1,5 +1,3 @@ -import os - from catfish.utils.processes import is_process_running from catfish.utils.sockets import stdout_socket_for_pid from catfish.worker.server import read_from_stdout_socket, send_to_server @@ -8,7 +6,7 @@ from tests import BaseWorkerTestCase class WorkerServerTestCase(BaseWorkerTestCase): def test_server_creates_process(self): - response = send_to_server({"type": "process", "command": self.DUMMY_EXE}) + response = send_to_server({"type": "process", "command": str(self.DUMMY_EXE)}) self.assertTrue(is_process_running(response["pid"])) def test_unknown_command_type(self): @@ -22,12 +20,12 @@ class WorkerServerTestCase(BaseWorkerTestCase): class ProcessLogsTestCase(BaseWorkerTestCase): def test_creates_socket(self): - response = send_to_server({"type": "process", "command": self.DUMMY_EXE}) + response = send_to_server({"type": "process", "command": str(self.DUMMY_EXE)}) stdout_socket = stdout_socket_for_pid(response["pid"]) - self.assertTrue(os.path.exists(stdout_socket)) + self.assertTrue(stdout_socket.exists()) def test_gets_logs(self): - response = send_to_server({"type": "process", "command": self.DUMMY_EXE}) + response = send_to_server({"type": "process", "command": str(self.DUMMY_EXE)}) stdout_socket = stdout_socket_for_pid(response["pid"]) stdout_iter = read_from_stdout_socket(stdout_socket) for i in range(3):