Skip to content

Commit 9239549

Browse files
authored
donate-cpu-server.py: added support for basic information reports / some cleanups (#5014)
* donate-cpu-server.py: bumped version * donate-cpu-server.py: fixed some PyCharm inspection warnings * donate-cpu-server.py: use `os.path.join()` * donate-cpu-server.py: added support for basic `information` reports
1 parent 1cd1cba commit 9239549

1 file changed

Lines changed: 74 additions & 32 deletions

File tree

tools/donate-cpu-server.py

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
2727
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
2828
# changes)
29-
SERVER_VERSION = "1.3.39"
29+
SERVER_VERSION = "1.3.40"
3030

3131
OLD_VERSION = '2.10'
3232

33+
HEAD_MARKER = 'head results:'
34+
INFO_MARKER = 'info messages:'
35+
3336

3437
# Set up logging
3538
logger = logging.getLogger()
@@ -82,6 +85,7 @@ def overviewReport() -> str:
8285
html += '<a href="stale.html">Stale report</a><br>\n'
8386
html += '<a href="diff.html">Diff report</a><br>\n'
8487
html += '<a href="head.html">HEAD report</a><br>\n'
88+
html += '<a href="headinfo.html">HEAD (info) report</a><br>\n'
8589
html += '<a href="latest.html">Latest results</a><br>\n'
8690
html += '<a href="time_lt.html">Time report (improved)</a><br>\n'
8791
html += '<a href="time_gt.html">Time report (regressed)</a> - <a href="time_gt.html?pkgs=1">packages.txt</a><br>\n'
@@ -537,7 +541,7 @@ def diffMessageIdTodayReport(resultPath: str, messageId: str) -> str:
537541
return text
538542

539543

540-
def headReportFromDict(out: dict, today: str) -> str:
544+
def summaryReportFromDict(out: dict, prefix: str, today: str) -> str:
541545
html = '<pre>\n'
542546
html += '<b>MessageID Count</b>\n'
543547
sumTotal = 0
@@ -550,7 +554,7 @@ def headReportFromDict(out: dict, today: str) -> str:
550554
while len(line) < 48 - len(c):
551555
line += ' '
552556
line += c + ' '
553-
line = '<a href="head' + today + '-' + messageId + '">' + messageId + '</a>' + line[line.find(' '):]
557+
line = '<a href="' + prefix + today + '-' + messageId + '">' + messageId + '</a>' + line[line.find(' '):]
554558
html += line + '\n'
555559

556560
# Sum
@@ -565,7 +569,7 @@ def headReportFromDict(out: dict, today: str) -> str:
565569
return html
566570

567571

568-
def headReport(resultsPath: str) -> str:
572+
def summaryReport(resultsPath: str, name: str, prefix: str, marker: str) -> str:
569573
out = {}
570574
outToday = {}
571575
today = strDateTime()[:10]
@@ -575,7 +579,7 @@ def headReport(resultsPath: str) -> str:
575579
continue
576580
uploadedToday = False
577581
firstLine = True
578-
headResults = False
582+
inResults = False
579583
for line in open(filename, 'rt'):
580584
if firstLine:
581585
if line.startswith(today):
@@ -590,13 +594,13 @@ def headReport(resultsPath: str) -> str:
590594
else:
591595
# Current package, parse on
592596
continue
593-
if line.startswith('head results:'):
594-
headResults = True
597+
if line.startswith(marker):
598+
inResults = True
595599
continue
596600
if line.startswith('diff:'):
597-
if headResults:
601+
if inResults:
598602
break
599-
if not headResults:
603+
if not inResults:
600604
continue
601605
if not line.endswith(']'):
602606
continue
@@ -623,30 +627,38 @@ def headReport(resultsPath: str) -> str:
623627
html += '<html><head><title>HEAD report</title></head><body>\n'
624628
html += '<h1>HEAD report</h1>\n'
625629
html += '<h2>Uploaded today</h2>'
626-
html += headReportFromDict(outToday, 'today')
630+
html += summaryReportFromDict(outToday, prefix, 'today')
627631
html += '<h2>All</h2>'
628-
html += headReportFromDict(out, '')
632+
html += summaryReportFromDict(out, prefix, '')
629633

630634
return html
631635

632636

633-
def headMessageIdReport(resultPath: str, messageId: str, query_params: dict) -> str:
637+
def headReport(resultsPath: str) -> str:
638+
return summaryReport(resultsPath, 'HEAD', 'head', HEAD_MARKER)
639+
640+
641+
def infoReport(resultsPath: str) -> str:
642+
return summaryReport(resultsPath, 'HEAD (info)', 'headinfo', INFO_MARKER)
643+
644+
645+
def messageIdReport(resultPath: str, marker: str, messageId: str, query_params: dict) -> str:
634646
pkgs = '' if query_params.get('pkgs') == '1' else None
635647
text = messageId + '\n'
636648
e = '[' + messageId + ']\n'
637649
for filename in sorted(glob.glob(resultPath + '/*')):
638650
if not os.path.isfile(filename) or filename.endswith('.diff'):
639651
continue
640652
url = None
641-
headResults = False
653+
inResults = False
642654
for line in open(filename, 'rt'):
643655
if line.startswith('ftp://'):
644656
url = line
645-
elif line.startswith('head results:'):
646-
headResults = True
647-
elif not headResults:
657+
elif line.startswith(marker):
658+
inResults = True
659+
elif not inResults:
648660
continue
649-
elif headResults and line.startswith('diff:'):
661+
elif inResults and line.startswith('diff:'):
650662
break
651663
elif line.endswith(e):
652664
if url:
@@ -660,15 +672,23 @@ def headMessageIdReport(resultPath: str, messageId: str, query_params: dict) ->
660672
return text
661673

662674

663-
def headMessageIdTodayReport(resultPath: str, messageId: str) -> str:
675+
def headMessageIdReport(resultPath: str, messageId: str, query_params: dict) -> str:
676+
return messageIdReport(resultPath, HEAD_MARKER, messageId, query_params)
677+
678+
679+
def infoMessageIdReport(resultPath: str, messageId: str, query_params: dict) -> str:
680+
return messageIdReport(resultPath, INFO_MARKER, messageId, query_params)
681+
682+
683+
def messageIdTodayReport(resultPath: str, messageId: str, marker: str) -> str:
664684
text = messageId + '\n'
665685
e = '[' + messageId + ']\n'
666686
today = strDateTime()[:10]
667687
for filename in sorted(glob.glob(resultPath + '/*')):
668688
if not os.path.isfile(filename) or filename.endswith('.diff'):
669689
continue
670690
url = None
671-
headResults = False
691+
inResults = False
672692
firstLine = True
673693
for line in open(filename, 'rt'):
674694
if firstLine:
@@ -677,11 +697,11 @@ def headMessageIdTodayReport(resultPath: str, messageId: str) -> str:
677697
break
678698
if line.startswith('ftp://'):
679699
url = line
680-
elif line.startswith('head results:'):
681-
headResults = True
682-
elif not headResults:
700+
elif line.startswith(marker):
701+
inResults = True
702+
elif not inResults:
683703
continue
684-
elif headResults and line.startswith('diff:'):
704+
elif inResults and line.startswith('diff:'):
685705
break
686706
elif line.endswith(e):
687707
if url:
@@ -691,7 +711,16 @@ def headMessageIdTodayReport(resultPath: str, messageId: str) -> str:
691711
return text
692712

693713

694-
def timeReport(resultPath: str, show_gt: bool, query_params: dict) -> str:
714+
def headMessageIdTodayReport(resultPath: str, messageId: str) -> str:
715+
return messageIdTodayReport(resultPath, messageId, HEAD_MARKER)
716+
717+
718+
def infoMessageIdTodayReport(resultPath: str, messageId: str) -> str:
719+
return messageIdTodayReport(resultPath, messageId, INFO_MARKER)
720+
721+
722+
# TODO: needs to dinicate that it returns 'tuple[str, str]' but that isn't supported until Python 3.9
723+
def timeReport(resultPath: str, show_gt: bool, query_params: dict):
695724
# no need for package report support in "improved" report
696725
pkgs = '' if show_gt and query_params and query_params.get('pkgs') == '1' else None
697726
factor = float(query_params.get('factor')) if query_params and 'factor' in query_params else 2.0
@@ -991,9 +1020,11 @@ def __init__(self, connection: socket.socket, cmd: str, resultPath: str, latestR
9911020
self.connection = connection
9921021
self.cmd = cmd[:cmd.find('\r\n')]
9931022
self.resultPath = resultPath
1023+
self.infoPath = os.path.join(self.resultPath, 'info_output')
9941024
self.latestResults = latestResults
9951025

9961026
# TODO: use a proper parser
1027+
@staticmethod
9971028
def parse_req(cmd):
9981029
req_parts = cmd.split(' ')
9991030
if len(req_parts) != 3 or req_parts[0] != 'GET' or not req_parts[2].startswith('HTTP'):
@@ -1005,7 +1036,7 @@ def run(self):
10051036
try:
10061037
cmd = self.cmd
10071038
print_ts(cmd)
1008-
url, queryParams = HttpClientThread.parse_req(cmd)
1039+
url, queryParams = self.parse_req(cmd)
10091040
if url is None:
10101041
print_ts('invalid request: {}'.format(cmd))
10111042
self.connection.close()
@@ -1039,14 +1070,25 @@ def run(self):
10391070
elif url == '/head.html':
10401071
html = headReport(self.resultPath)
10411072
httpGetResponse(self.connection, html, 'text/html')
1073+
elif url == '/headinfo.html':
1074+
html = infoReport(self.infoPath)
1075+
httpGetResponse(self.connection, html, 'text/html')
10421076
elif url.startswith('/headtoday-'):
10431077
messageId = url[len('/headtoday-'):]
10441078
text = headMessageIdTodayReport(self.resultPath, messageId)
10451079
httpGetResponse(self.connection, text, 'text/plain')
1080+
elif url.startswith('/headinfotoday-'):
1081+
messageId = url[len('/headinfotoday-'):]
1082+
text = infoMessageIdTodayReport(self.infoPath, messageId)
1083+
httpGetResponse(self.connection, text, 'text/plain')
10461084
elif url.startswith('/head-'):
10471085
messageId = url[len('/head-'):]
10481086
text = headMessageIdReport(self.resultPath, messageId, queryParams)
10491087
httpGetResponse(self.connection, text, 'text/plain')
1088+
elif url.startswith('/headinfo-'):
1089+
messageId = url[len('/headinfo-'):]
1090+
text = infoMessageIdReport(self.infoPath, messageId, queryParams)
1091+
httpGetResponse(self.connection, text, 'text/plain')
10501092
elif url == '/time_lt.html':
10511093
text, mime = timeReport(self.resultPath, False, queryParams)
10521094
httpGetResponse(self.connection, text, mime)
@@ -1057,20 +1099,20 @@ def run(self):
10571099
text = timeReportSlow(self.resultPath)
10581100
httpGetResponse(self.connection, text, 'text/html')
10591101
elif url == '/check_library_function_report.html':
1060-
text = check_library_report(self.resultPath + '/' + 'info_output', message_id='checkLibraryFunction')
1102+
text = check_library_report(self.infoPath, message_id='checkLibraryFunction')
10611103
httpGetResponse(self.connection, text, 'text/html')
10621104
elif url == '/check_library_noreturn_report.html':
1063-
text = check_library_report(self.resultPath + '/' + 'info_output', message_id='checkLibraryNoReturn')
1105+
text = check_library_report(self.infoPath, message_id='checkLibraryNoReturn')
10641106
httpGetResponse(self.connection, text, 'text/html')
10651107
elif url == '/check_library_use_ignore_report.html':
1066-
text = check_library_report(self.resultPath + '/' + 'info_output', message_id='checkLibraryUseIgnore')
1108+
text = check_library_report(self.infoPath, message_id='checkLibraryUseIgnore')
10671109
httpGetResponse(self.connection, text, 'text/html')
10681110
elif url == '/check_library_check_type_report.html':
1069-
text = check_library_report(self.resultPath + '/' + 'info_output', message_id='checkLibraryCheckType')
1111+
text = check_library_report(self.infoPath, message_id='checkLibraryCheckType')
10701112
httpGetResponse(self.connection, text, 'text/html')
10711113
elif url.startswith('/check_library-'):
10721114
function_name = url[len('/check_library-'):]
1073-
text = check_library_function_name(self.resultPath + '/' + 'info_output', function_name)
1115+
text = check_library_function_name(self.infoPath, function_name)
10741116
httpGetResponse(self.connection, text, 'text/plain')
10751117
else:
10761118
filename = resultPath + url
@@ -1092,8 +1134,8 @@ def run(self):
10921134

10931135
def read_data(connection, cmd, pos_nl, max_data_size, check_done, cmd_name, timeout=10):
10941136
data = cmd[pos_nl+1:]
1137+
t = 0.0
10951138
try:
1096-
t = 0.0
10971139
while (len(data) < max_data_size) and (not check_done or not data.endswith('\nDONE')) and (timeout > 0 and t < timeout):
10981140
bytes_received = connection.recv(1024)
10991141
if bytes_received:
@@ -1115,7 +1157,7 @@ def read_data(connection, cmd, pos_nl, max_data_size, check_done, cmd_name, time
11151157
print_ts('Socket error occurred ({}): {}'.format(cmd_name, e))
11161158
data = None
11171159

1118-
if (timeout > 0 and t >= timeout):
1160+
if timeout > 0 and t >= timeout:
11191161
print_ts('Timeout occurred ({}).'.format(cmd_name))
11201162
data = None
11211163

0 commit comments

Comments
 (0)