Created
June 14, 2021 12:38
-
-
Save ndvbd/240153f18d9976fc2c32367efb8f63da to your computer and use it in GitHub Desktop.
Process multiple google chrome debugger tools code coverage jsons in ordet to extract effective source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import re | |
# code_coverage_filename = | |
# Will merge all this files | |
code_coverage_filenames = ['Coverage-20210613T181747.json', 'Coverage-20210613T1817427.json'] | |
specific_file_url = 'https://localhost/bootstrap.min.self.css?body=1' | |
p = re.compile("@media(.)*?{") | |
ranges = [] | |
source_text = None | |
medias_start_stop = None | |
for code_coverage_filename in code_coverage_filenames: | |
with open(code_coverage_filename) as f: | |
data = json.load(f) | |
for entry in data: | |
pass # print entry['url'] | |
if entry['url'] == specific_file_url: | |
ranges.extend(entry['ranges']) | |
if source_text is not None and entry['text'] != source_text: | |
print 'Warning! source texts of files are not the same!' | |
exit(1) | |
source_text = entry['text'] | |
ranges_to_take = [] | |
medias_start_stop = [] | |
for m in p.finditer(source_text): | |
# print(m.start(), m.group()) | |
start = m.start() | |
header_length = len(m.group()) | |
pointer = start + header_length | |
count_open_braces = 0 | |
while True: | |
pointer += 1 | |
char = source_text[pointer] | |
if char == "{": | |
count_open_braces += 1 | |
elif char == "}": | |
count_open_braces -= 1 | |
if count_open_braces < 0: | |
pointer += 1 | |
break | |
medias_start_stop.append((start, pointer)) | |
# print source_text[start:pointer] | |
print medias_start_stop | |
for range in ranges: | |
range_start = range['start'] | |
range_end = range['end'] | |
# if we are in the middle of a media query, take it all | |
inside = None | |
for media_start, media_end in medias_start_stop: | |
if source_text[range_start:range_end].startswith(".container"): | |
justdebug = 1 | |
if range_start>=media_start and range_end<=media_end: | |
inside = (media_start, media_end) | |
break | |
if inside is not None: | |
if inside not in ranges_to_take: | |
ranges_to_take.append(inside) | |
else: | |
pass # already taken before | |
else: | |
#text += entry['text'][int(range_start):int(range_end)]+"\n" | |
ranges_to_take.append((range_start, range_end)) | |
text = "" | |
for start, stop in ranges_to_take: | |
text += source_text[int(start):int(stop)]+"\n" | |
print text | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment