Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_free_port removed and its individual functionalities used #194

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions clashroyalebuildabot/emulator/emulator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# pylint: disable=R1732

import atexit
from contextlib import contextmanager
import os
Expand All @@ -11,7 +9,6 @@
import zipfile

import av
from get_free_port import get_dynamic_ports
from loguru import logger
import requests
import yaml
Expand All @@ -25,8 +22,6 @@


class KThread(threading.Thread):
"""A subclass of threading.Thread, with a kill() method."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._kill = threading.Event()
Expand All @@ -43,6 +38,12 @@ def kill(self):
self._kill.set()


def get_free_port():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0))
return s.getsockname()[1]


def kill_pid(pid):
try:
os.kill(pid, 9)
Expand All @@ -62,12 +63,11 @@ def kill_process_children_parents(pid):
def get_child_processes(pid):
try:
child_pids = []
ps_command = subprocess.Popen(
with subprocess.Popen(
["ps", "-o", "pid", "--ppid", str(pid), "--noheaders"],
stdout=subprocess.PIPE,
)
ps_output = ps_command.stdout.read()
ps_command.stdout.close()
) as ps_command:
ps_output = ps_command.stdout.read()
for line in ps_output.splitlines():
child_pids.append(int(line))
return child_pids
Expand Down Expand Up @@ -108,7 +108,7 @@ def __init__(self):
self.frame = None
self.scrcpy_proc = None
self.codec = av.codec.CodecContext.create("h264", "r")
self.forward_port = get_dynamic_ports(qty=1)[0]
self.forward_port = get_free_port()

self._install_adb()
self.width, self.height = self._get_width_and_height()
Expand Down Expand Up @@ -220,12 +220,13 @@ def _start_scrcpy(self):
"raw_video_stream=true",
f"max_size={self.width}",
]
self.scrcpy_proc = subprocess.Popen(
with subprocess.Popen(
command,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
cwd=EMULATOR_DIR,
)
) as proc:
self.scrcpy_proc = proc

def _forward_port(self):
self._run_command(
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ dependencies = [
"PyYAML",
"pybind11>=2.12",
"requests>=2.25.1",
"av",
"get_free_port"
"av"
]

[project.optional-dependencies]
Expand Down