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 select
import shutil import shutil
import tempfile import tempfile
from pathlib import Path
import ujson import ujson
BASE_SOCKET_DIR = os.path.join(tempfile.gettempdir(), "catfish") BASE_SOCKET_DIR = Path(tempfile.gettempdir()).joinpath("catfish")
BUFFER_SIZE = 4096 BUFFER_SIZE = 4096
DEFAULT_SOCKET_READ_TIMEOUT = 0.01 DEFAULT_SOCKET_READ_TIMEOUT = 0.01
@ -41,5 +42,5 @@ def delete_base_socket_dir():
pass pass
def stdout_socket_for_pid(pid: int) -> str: def stdout_socket_for_pid(pid: int) -> Path:
return os.path.join(BASE_SOCKET_DIR, "{}.stdout.sock".format(pid)) return BASE_SOCKET_DIR.joinpath("{}.stdout.sock".format(pid))

View file

@ -1,5 +1,4 @@
import asyncio import asyncio
import os
import time import time
import psutil import psutil
@ -10,13 +9,13 @@ from catfish.utils.sockets import BASE_SOCKET_DIR
from .server import WORKER_SERVER_SOCKET, start_server 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(): def is_running():
if not os.path.exists(PID_FILE): if not PID_FILE.exists():
return False return False
if not os.path.exists(WORKER_SERVER_SOCKET): if not WORKER_SERVER_SOCKET.exists():
return False return False
try: try:
get_running_process() get_running_process()

View file

@ -4,6 +4,7 @@ import os
import shlex import shlex
import socket import socket
import subprocess import subprocess
from pathlib import Path
import click import click
import zmq import zmq
@ -18,20 +19,20 @@ from catfish.utils.sockets import (
stdout_socket_for_pid, 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): def send_to_server(payload):
with socket.socket(socket.AF_UNIX, type=socket.SOCK_STREAM) as sock: 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) sock.sendall(ujson.dumps(payload).encode() + NEW_LINE)
return read_all_from_socket(sock) return read_all_from_socket(sock)
def read_from_stdout_socket(socket_path): def read_from_stdout_socket(socket_path: Path):
ctx = zmq.Context() ctx = zmq.Context()
sock = ctx.socket(zmq.SUB) sock = ctx.socket(zmq.SUB)
sock.connect("ipc://" + socket_path) sock.connect("ipc://" + str(socket_path))
sock.setsockopt_string(zmq.SUBSCRIBE, "") sock.setsockopt_string(zmq.SUBSCRIBE, "")
while True: while True:
yield sock.recv_string().strip() yield sock.recv_string().strip()
@ -46,7 +47,7 @@ async def publish_stdout(process):
sock = ctx.socket(zmq.PUB) sock = ctx.socket(zmq.PUB)
socket_path = stdout_socket_for_pid(process.pid) socket_path = stdout_socket_for_pid(process.pid)
sock.bind("ipc://" + socket_path) sock.bind("ipc://" + str(socket_path))
while True: while True:
output = await process.stdout.readline() output = await process.stdout.readline()
if not output: if not output:

View file

@ -2,6 +2,7 @@ import functools
import os import os
import shutil import shutil
import subprocess import subprocess
from pathlib import Path
from unittest import TestCase from unittest import TestCase
from aiohttp.test_utils import AioHTTPTestCase, unused_port 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): class BaseTestCase(TestCase):
DUMMY_EXE = os.path.join(os.path.dirname(__file__), "dummy_program.py") TESTS_DIR = Path(os.path.dirname(__file__))
EXAMPLE_DIR = os.path.join(os.path.dirname(__file__), "../example") DUMMY_EXE = TESTS_DIR.joinpath("dummy_program.py")
EXAMPLE_DIR = TESTS_DIR.parent.joinpath("example")
def setUp(self): def setUp(self):
create_base_socket_dir() create_base_socket_dir()
@ -31,7 +33,7 @@ class BaseTestCase(TestCase):
def terminate_dummy_processes(self): def terminate_dummy_processes(self):
dummy_processes = [] dummy_processes = []
for process in get_root_process().children(recursive=True): 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) dummy_processes.append(process)
terminate_processes(dummy_processes) terminate_processes(dummy_processes)

View file

@ -21,4 +21,4 @@ class ProjectTestCase(BaseTestCase):
self.assertEqual(process.command, "python -m http.server $PORT") self.assertEqual(process.command, "python -m http.server $PORT")
def test_name(self): 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): def test_finds_process(self):
proc = subprocess.Popen( proc = subprocess.Popen(
self.DUMMY_EXE, str(self.DUMMY_EXE),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
env={**os.environ, utils.HOSTNAME_ENV_VAR: "localhost"}, env={**os.environ, utils.HOSTNAME_ENV_VAR: "localhost"},
) )
@ -22,7 +22,7 @@ class ProcessForHostnameTestCase(BaseTestCase):
def test_finds_process_with_multiple_hostnames(self): def test_finds_process_with_multiple_hostnames(self):
proc = subprocess.Popen( proc = subprocess.Popen(
self.DUMMY_EXE, str(self.DUMMY_EXE),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
env={**os.environ, utils.HOSTNAME_ENV_VAR: "localhost,test.local"}, env={**os.environ, utils.HOSTNAME_ENV_VAR: "localhost,test.local"},
) )

View file

@ -23,7 +23,7 @@ class TerminateProcessesTestCase(BaseTestCase):
created_processes = [] created_processes = []
for _ in range(10): for _ in range(10):
created_processes.append( 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: for proc in created_processes:

View file

@ -1,5 +1,3 @@
import os
from catfish.utils.processes import is_process_running from catfish.utils.processes import is_process_running
from catfish.utils.sockets import stdout_socket_for_pid from catfish.utils.sockets import stdout_socket_for_pid
from catfish.worker.server import read_from_stdout_socket, send_to_server from catfish.worker.server import read_from_stdout_socket, send_to_server
@ -8,7 +6,7 @@ from tests import BaseWorkerTestCase
class WorkerServerTestCase(BaseWorkerTestCase): class WorkerServerTestCase(BaseWorkerTestCase):
def test_server_creates_process(self): 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"])) self.assertTrue(is_process_running(response["pid"]))
def test_unknown_command_type(self): def test_unknown_command_type(self):
@ -22,12 +20,12 @@ class WorkerServerTestCase(BaseWorkerTestCase):
class ProcessLogsTestCase(BaseWorkerTestCase): class ProcessLogsTestCase(BaseWorkerTestCase):
def test_creates_socket(self): 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"]) 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): 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_socket = stdout_socket_for_pid(response["pid"])
stdout_iter = read_from_stdout_socket(stdout_socket) stdout_iter = read_from_stdout_socket(stdout_socket)
for i in range(3): for i in range(3):