This repository was archived by the owner on Jun 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathutils.py
More file actions
85 lines (67 loc) · 2.79 KB
/
utils.py
File metadata and controls
85 lines (67 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import contextlib
import os
import sys
import platform
import subprocess
import tempfile
import time
from pathlib import Path
import psutil
# Path to the test data folder.
TEST_DATA_DIR = Path(__file__).parent / "data"
def get_process_listening_port(proc):
conn = None
if platform.system() == "Windows":
current_process = psutil.Process(proc.pid)
children = []
while children == []:
time.sleep(0.01)
children = current_process.children(recursive=True)
if (3, 6) <= sys.version_info < (3, 7):
children = [current_process]
for child in children:
while child.connections() == [] and not any(conn.status == "LISTEN" for conn in child.connections()):
time.sleep(0.01)
conn = next(filter(lambda conn: conn.status == "LISTEN", child.connections()))
else:
psutil_proc = psutil.Process(proc.pid)
while not any(conn.status == "LISTEN" for conn in psutil_proc.connections()):
time.sleep(0.01)
conn = next(filter(lambda conn: conn.status == "LISTEN", psutil_proc.connections()))
return conn.laddr.port
@contextlib.contextmanager
def get_eel_server(example_py, start_html):
"""Run an Eel example with the mode/port overridden so that no browser is launched and a random port is assigned"""
test = None
try:
with tempfile.NamedTemporaryFile(mode='w', dir=os.path.dirname(example_py), delete=False) as test:
# We want to run the examples unmodified to keep the test as realistic as possible, but all of the examples
# want to launch browsers, which won't be supported in CI. The below script will configure eel to open on
# a random port and not open a browser, before importing the Python example file - which will then
# do the rest of the set up and start the eel server. This is definitely hacky, and means we can't
# test mode/port settings for examples ... but this is OK for now.
test.write(f"""
import eel
eel._start_args['mode'] = None
eel._start_args['port'] = 0
import {os.path.splitext(os.path.basename(example_py))[0]}
""")
proc = subprocess.Popen(
[sys.executable, test.name],
cwd=os.path.dirname(example_py),
)
eel_port = get_process_listening_port(proc)
yield f"http://localhost:{eel_port}/{start_html}"
proc.terminate()
finally:
if test:
try:
os.unlink(test.name)
except FileNotFoundError:
pass
def get_console_logs(driver, minimum_logs=0):
console_logs = driver.get_log('browser')
while len(console_logs) < minimum_logs:
console_logs += driver.get_log('browser')
time.sleep(0.1)
return console_logs