-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathutils.py
89 lines (72 loc) · 2.25 KB
/
utils.py
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
86
87
88
89
import os
import ssl
import subprocess
import sys
import time
from contextlib import suppress
from typing import Callable, Optional
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
CI = os.getenv("CI")
class Compose:
def __init__(self, project_name: str, env_file: str):
self.project_name = project_name
self.base_cmd = (
"docker",
"compose",
"-p",
project_name,
"--env-file",
env_file,
)
def __call__(self, *cmd: str) -> None:
file_args = [
"-f",
"compose.yaml",
"-f",
"overrides/compose.proxy.yaml",
"-f",
"overrides/compose.mariadb.yaml",
"-f",
"overrides/compose.redis.yaml",
]
if CI:
file_args += ("-f", "tests/compose.ci.yaml")
args = self.base_cmd + tuple(file_args) + cmd
subprocess.check_call(args)
def exec(self, *cmd: str) -> None:
if sys.stdout.isatty():
self("exec", *cmd)
else:
self("exec", "-T", *cmd)
def stop(self) -> None:
# Stop all containers in `test` project if they are running.
# We don't care if it fails.
with suppress(subprocess.CalledProcessError):
subprocess.check_call(self.base_cmd + ("down", "-v", "--remove-orphans"))
def bench(self, *cmd: str) -> None:
self.exec("backend", "bench", *cmd)
def check_url_content(
url: str, callback: Callable[[str], Optional[str]], site_name: str
):
request = Request(url, headers={"Host": site_name})
# This is needed to check https override
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
for _ in range(100):
try:
response = urlopen(request, context=ctx)
except HTTPError as exc:
if exc.code not in (404, 502):
raise
except URLError:
pass
else:
text: str = response.read().decode()
ret = callback(text)
if ret:
print(ret)
return
time.sleep(0.1)
raise RuntimeError(f"Couldn't ping {url}")