Skip to content

Commit

Permalink
Merge pull request #194 from Leviaria/main
Browse files Browse the repository at this point in the history
get_free_port removed and its individual functionalities used
  • Loading branch information
Leviaria authored Jun 30, 2024
2 parents 1644e0c + e172573 commit 95b079c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
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

0 comments on commit 95b079c

Please sign in to comment.