-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.py
319 lines (290 loc) · 13.3 KB
/
validation.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from base64 import b64decode
from contextlib import ExitStack, redirect_stdout
import os
import sys
from tempfile import NamedTemporaryFile
from time import sleep, time
from typing import AnyStr, Iterable, Mapping, Optional, overload, TypeVar, Union
try:
from typing import Literal
except ImportError:
from typing_extensions import Literal
import regex
from selenium.common.exceptions import JavascriptException, TimeoutException, WebDriverException
from selenium.webdriver import Chrome, Edge, Firefox, chrome, edge, firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from webdriver_manager.firefox import GeckoDriverManager
if not __package__:
import default_vars, text_prep, webify
else:
# noinspection PyPackages
from . import default_vars, text_prep, webify
default_browser = 'chrome'
default_timeout = 60
default_by = By.TAG_NAME
default_element = 'body'
webdriver_paths_filename = 'webdriver_paths.txt'
os.environ['WDM_LOG'] = '0'
drivers = dict(chrome=[Chrome, chrome, ChromeDriverManager],
edge=[Edge, edge, EdgeChromiumDriverManager],
firefox=[Firefox, firefox, GeckoDriverManager]
)
BrowserType = Union[str, WebDriver]
critical_error_strings = ['executable needs to be', 'unable to find binary', 'unexpectedly']
FilenameOrBytes = TypeVar('FilenameOrBytes', str, bytes)
def full_path(filename: str) -> str:
return f"file:///{os.path.realpath(filename).replace(os.sep, '/')}"
def get_browser(browser: BrowserType,
stack: Optional[ExitStack] = None
) -> WebDriver:
if isinstance(browser, WebDriver):
return browser
options = drivers[browser][1].options.Options()
options.headless = True
options.add_argument('--no-sandbox')
if hasattr(options, 'add_experimental_option'):
options.add_experimental_option('excludeSwitches', ['enable-logging'])
try:
with redirect_stdout(None):
service = drivers[browser][2]().install()
folder = os.path.dirname(webdriver_paths_filename)
if folder:
os.makedirs(folder, exist_ok=True)
with open(webdriver_paths_filename, 'a', encoding='utf8') as f:
f.write(f'{browser},{service}\n')
except Exception:
with open(webdriver_paths_filename, encoding='utf8') as f:
for line in reversed(f.read().splitlines()):
b, service = line.split(',', 1)
if b == browser:
break
while isinstance(browser, str):
try:
browser = drivers[browser][0](service=drivers[browser][1].service.Service(service, log_path=os.devnull), options=options)
except WebDriverException as e:
if any(s in e.msg for s in critical_error_strings):
raise
print(e, file=sys.stderr)
sleep(30)
if stack:
browser = stack.enter_context(browser)
return browser
@overload
def render_html(file: FilenameOrBytes, by: str = ..., element: str = ...,
raw: bool = ..., image: Literal[True] = ...,
browser: str = ..., timeout: int = ..., content_var: str = ...
) -> Optional[bytes]: ...
@overload
def render_html(file: FilenameOrBytes, by: str = ..., element: str = ...,
raw: bool = ..., image: Literal[False] = ...,
browser: str = ..., timeout: int = ..., content_var: str = ...
) -> Optional[str]: ...
@overload
def render_html(file: FilenameOrBytes, by: str = ..., element: str = ...,
raw: bool = ..., image: bool = ...,
browser: str = ..., timeout: int = ..., content_var: str = ...
) -> Optional[AnyStr]: ...
def render_html(file,
by=default_by,
element=default_element,
raw=False,
image=False,
browser=default_browser,
timeout=default_timeout,
content_var=''
):
assert not raw or not image
if not by:
by = default_by
if not element:
element = default_element
with ExitStack() as stack:
browser = get_browser(browser, stack)
if isinstance(file, str):
filename = file
else:
with NamedTemporaryFile(suffix='.html', delete=False) as f: # See https://github.com/python/cpython/issues/88221
f.write(file)
filename = f.name
browser.get(full_path(filename))
if isinstance(file, bytes):
try:
os.remove(filename)
except PermissionError:
pass
try:
wait = WebDriverWait(browser, timeout)
if image:
if by == By.TAG_NAME and element == 'body':
data_url = wait.until(lambda x:
regex.sub('^none$', '',
x.find_element(by, element)
.value_of_css_property('background-image')))
else:
data_url = wait.until(lambda x:
x.find_element(by, element)
.get_property('src'))
assert isinstance(data_url, str), type(data_url)
if ';base64,' in data_url:
return b64decode(data_url.split(';base64,', 1)[1].split('"', 1)[0], validate=True)
image_data = browser.execute_script(f'return {content_var or default_vars.bytearray}')
if isinstance(image_data, dict): # Needed for or Firefox, see: https://github.com/SeleniumHQ/selenium/issues/11070
image_data = [v for k, v in sorted(image_data.items(), key=lambda x: int(x[0]))]
return bytes(image_data)
if raw:
sleep(0.1)
get_text = lambda x: x.execute_script(f'return {content_var or default_vars.text}')
else:
get_text = lambda x: x.find_element(by, element).get_property('innerText')
try:
text = wait.until(get_text)
except JavascriptException:
sleep(1)
text = wait.until(get_text)
assert isinstance(text, str), type(text)
return text
except TimeoutException:
return None
except Exception:
print(f'\nError: {browser.name} failed on {full_path(filename)}', file=sys.stderr)
raise
def find_first_diff(rendered: AnyStr, data: AnyStr, verbose: bool = True) -> int:
i = -1
for i, (r, t) in enumerate(zip(rendered, data)):
if r != t:
break
else:
i += 1
if verbose:
print(f'\nFirst difference found at {i} / {len(rendered)}', file=sys.stderr)
print(f'Original: {data[max(i - 30, 0) : i]!r} -> {data[i : i + 50]!r}', file=sys.stderr)
print(f'Rendered: {rendered[max(i - 30, 0) : i]!r} -> {rendered[i : i + 50]!r}\n', file=sys.stderr)
return i
def validate_html(file: FilenameOrBytes, # Don't use AnyStr as it does not have to be the same type as data
data: AnyStr,
caps: str = text_prep.default_caps,
by: str = default_by,
element: str = default_element,
raw: bool = False,
browser: BrowserType = default_browser,
timeout: int = default_timeout,
unicode_A: int = 0,
ignore_regex: str = '',
content_var: str = '',
verbose: bool = True
) -> Optional[bool]:
image = isinstance(data, bytes)
assert data, 'Error: Cannot validate against empty data'
rendered = render_html(file, by, element, raw, image, browser, timeout, content_var)
if rendered is None:
return None
if not image:
if caps == 'lower':
data = data.lower()
elif caps == 'upper':
data = data.upper()
elif caps == 'simple':
data = text_prep.decode_caps_simple(data.lower())
if not raw:
if unicode_A:
rendered = regex.sub(r'[^\p{Z}\p{C}]', lambda m: chr(ord(m[0]) - unicode_A + 65 + (6 if ord(m[0]) - unicode_A + 65 > 90 else 0)), rendered)
rendered = regex.sub(ignore_regex, '', rendered)
if rendered == data:
return True
if verbose:
find_first_diff(rendered, data)
return False
def validate_files(filenames: Mapping[str, str],
data: Optional[AnyStr] = None,
reduce_whitespace: bool = False,
unix_newline: bool = True,
fix_punct: bool = False,
remove_bom: bool = True,
caps: str = text_prep.default_caps,
by: str = default_by,
element: str = default_element,
raw: bool = False,
image: bool = False,
browsers: Optional[Union[BrowserType, Iterable[BrowserType]]] = None,
timeout: int = default_timeout,
unicode_A: int = 0,
ignore_regex: str = '',
content_var: str = '',
validate: bool = True,
verbose: bool = True
) -> bool:
error = False
if browsers is None:
browsers = list(drivers)
elif isinstance(browsers, (str, WebDriver)):
browsers = [browsers]
with ExitStack() as stack:
if validate:
browsers = [get_browser(browser, stack) for browser in browsers]
raw_size = None
no_overhead_size = None
for label, filename in sorted(filenames.items(), key=lambda x: (x[0] != 'raw', x[0] != 'base64_html')):
ext = os.path.splitext(filename)[-1][1:].lower()
if raw_size is not None and ext != 'html':
continue
if data is None or label == 'raw':
if ext in webify.raw_extensions:
raw = True
elif ext in webify.image_extensions:
image = True
assert not image or (not raw and not isinstance(data, str))
if data is None:
with open(filename, 'rb') as f:
data = f.read()
if raw_size is None:
raw_size = len(data.encode() if isinstance(data, str) else data)
if not image and isinstance(data, bytes):
data = text_prep.normalize(data.decode(), reduce_whitespace, unix_newline, fix_punct, remove_bom) # Assumes raw text file is utf8. Otherwise, pass it as a data argument
if verbose:
size = os.path.getsize(filename)
if label == 'base64_html':
no_overhead_size = size * 3 / 4
stats = []
if raw_size:
stats.append(f'ratio={round(size / raw_size * 100, 1)}%')
if no_overhead_size:
stats.append(f'overhead={round((size/no_overhead_size-1) * 100, 1)}%')
if ext == 'html' and label not in ['raw', 'base64_html']:
with open(filename, 'rb') as f:
html = f.read()
matches = regex.findall(webify.literals_regex.encode(), html)
payload = max(matches, key=len, default=b'').split(b'`', 1)[1].rsplit(b'`', 1)[0]
html = html.replace(payload, b'')
stats.append(f'code: {len(html):,} B = {round(len(html) / 1024, 1):,} kB')
stats = ' '.join(stats)
if stats:
stats = f' ({stats})'
mb = size / 1024 ** 2
if mb >= 0.1:
stats = f' = {round(mb, 1):,} MB{stats}'
kb = size / 1024
if kb >= 0.1:
stats = f' = {round(kb, 1):,} kB{stats}'
print(f"{full_path(filename)} {size:,} B{stats}", end='' if validate and ext == 'html' and label != 'raw' else None, file=sys.stderr)
if validate and ext == 'html' and label != 'raw':
for i, browser in enumerate(browsers):
start_time = time()
valid = validate_html(filename, data, caps, by, element,
raw, browser, timeout, unicode_A,
ignore_regex, content_var, verbose)
assert valid is not False, filename
if not valid:
error = True
if verbose:
if i == 0:
print(f' rendering secs:', end='', file=sys.stderr)
print(f' {browser.name}=' + (f'{time() - start_time :.1f}' if valid else f'{timeout}(TIMEOUT)'), end='', file=sys.stderr)
if verbose:
print(file=sys.stderr)
if verbose and validate:
print('Note: above rendering times from Selenium are much longer than actual browser rendering.', file=sys.stderr)
return error