archive
/
catfish
Archived
1
Fork 0

Replace uses of os.path with pathlib

This commit is contained in:
Jake Howard 2018-12-18 15:49:20 +00:00
parent c63c709c79
commit 7af173c0cf
Signed by: jake
GPG Key ID: 57AFB45680EDD477
8 changed files with 26 additions and 25 deletions

View File

@ -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))

View File

@ -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()

View File

@ -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:

View File

@ -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)

View File

@ -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")

View File

@ -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"},
)

View File

@ -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:

View File

@ -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):