forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcreate_workflow_report.py
More file actions
executable file
·691 lines (595 loc) · 25.5 KB
/
create_workflow_report.py
File metadata and controls
executable file
·691 lines (595 loc) · 25.5 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
from itertools import combinations
import json
from datetime import datetime
import requests
import pandas as pd
from clickhouse_driver import Client
import boto3
from botocore.exceptions import NoCredentialsError
import pandas as pd
DATABASE_HOST_VAR = "CHECKS_DATABASE_HOST"
DATABASE_USER_VAR = "CHECKS_DATABASE_USER"
DATABASE_PASSWORD_VAR = "CHECKS_DATABASE_PASSWORD"
S3_BUCKET = "altinity-build-artifacts"
GITHUB_REPO = "Altinity/ClickHouse"
css = """
/* Base colors for Altinity */
:root {
--altinity-background: #000D45;
--altinity-accent: #189DCF;
--altinity-highlight: #FFC600;
--altinity-gray: #6c757d;
--altinity-light-gray: #f8f9fa;
--altinity-white: #ffffff;
}
/* Body and heading fonts */
body {
font-family: Arimo, "Proxima Nova", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 1rem;
background-color: var(--altinity-background);
color: var(--altinity-light-gray);
padding: 2rem;
}
h1, h2, h3, h4, h5, h6 {
font-family: Figtree, "Proxima Nova", "Helvetica Neue", Helvetica, Arial, sans-serif;
color: var(--altinity-white);
}
.logo {
width: auto;
height: 5em;
}
/* General table styling */
table {
min-width: min(900px, 98vw);
margin: 1rem 0;
border-collapse: collapse;
background-color: var(--altinity-white);
border: 1px solid var(--altinity-accent);
box-shadow: 0 0 8px rgba(0, 0, 0, 0.05);
color: var(--altinity-background);
}
/* Table header styling */
th {
background-color: var(--altinity-accent);
color: var(--altinity-white);
padding: 10px 16px;
text-align: left;
border: none;
border-bottom: 2px solid var(--altinity-background);
white-space: nowrap;
}
th.hth {
border-bottom: 1px solid var(--altinity-accent);
border-right: 2px solid var(--altinity-background);
}
/* Table header sorting styling */
th {
cursor: pointer;
}
th.no-sort {
pointer-events: none;
}
th::after,
th::before {
transition: color 0.2s ease-in-out;
font-size: 1.2em;
color: transparent;
}
th::after {
margin-left: 3px;
content: '\\025B8';
}
th:hover::after {
color: inherit;
}
th.dir-d::after {
color: inherit;
content: '\\025BE';
}
th.dir-u::after {
color: inherit;
content: '\\025B4';
}
/* Table body row styling */
tr:hover {
background-color: var(--altinity-light-gray);
}
/* Table cell styling */
td {
padding: 8px 8px;
border: 1px solid var(--altinity-accent);
}
/* Link styling */
a {
color: var(--altinity-accent);
text-decoration: none;
}
a:hover {
color: var(--altinity-highlight);
text-decoration: underline;
}
"""
script = """
<script>
document.addEventListener('click', function (e) {
try {
function findElementRecursive(element, tag) {
return element.nodeName === tag ? element :
findElementRecursive(element.parentNode, tag)
}
var descending_th_class = ' dir-d '
var ascending_th_class = ' dir-u '
var ascending_table_sort_class = 'asc'
var regex_dir = / dir-(u|d) /
var alt_sort = e.shiftKey || e.altKey
var element = findElementRecursive(e.target, 'TH')
var tr = findElementRecursive(element, 'TR')
var table = findElementRecursive(tr, 'TABLE')
function reClassify(element, dir) {
element.className = element.className.replace(regex_dir, '') + dir
}
function getValue(element) {
return (
(alt_sort && element.getAttribute('data-sort-alt')) ||
element.getAttribute('data-sort') || element.innerText
)
}
if (true) {
var column_index
var nodes = tr.cells
for (var i = 0; i < nodes.length; i++) {
if (nodes[i] === element) {
column_index = element.getAttribute('data-sort-col') || i
} else {
reClassify(nodes[i], '')
}
}
var dir = descending_th_class
if (
element.className.indexOf(descending_th_class) !== -1 ||
(table.className.indexOf(ascending_table_sort_class) !== -1 &&
element.className.indexOf(ascending_th_class) == -1)
) {
dir = ascending_th_class
}
reClassify(element, dir)
var org_tbody = table.tBodies[0]
var rows = [].slice.call(org_tbody.rows, 0)
var reverse = dir === ascending_th_class
rows.sort(function (a, b) {
var x = getValue((reverse ? a : b).cells[column_index])
var y = getValue((reverse ? b : a).cells[column_index])
return isNaN(x - y) ? x.localeCompare(y) : x - y
})
var clone_tbody = org_tbody.cloneNode()
while (rows.length) {
clone_tbody.appendChild(rows.splice(0, 1)[0])
}
table.replaceChild(clone_tbody, org_tbody)
}
} catch (error) {
}
});
</script>
"""
logo = """
<p><img class="logo" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIGlkPSJhIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0NjEuNTUgMTA1Ljk5Ij48ZGVmcz48c3R5bGU+LmJ7ZmlsbDojZmZmO30uY3tmaWxsOiMxOTlkY2Y7fTwvc3R5bGU+PC9kZWZzPjxnPjxwb2x5Z29uIGNsYXNzPSJjIiBwb2ludHM9Ii4wOSA1MC45NiA2Ni44NiAxMi4xMiA0NS44OCAwIDQ1Ljg4IC4wNCAyMi45NCAxMy4zIDIyLjkzIDEzLjMgMjIuOTMgMTMuMyAuMDkgMjYuNDkgLjA5IDI2LjQ5IC4wOSAyNi40OSAwIDI2LjU0IC4wOSAyNi41OSAuMDkgNTAuOTYiLz48cG9seWdvbiBjbGFzcz0iYyIgcG9pbnRzPSI0LjIxIDUzLjE5IDIyLjk0IDY0LjA4IDIyLjk0IDQyLjI5IDQuMjEgNTMuMTkiLz48cG9seWdvbiBjbGFz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DgsLjIxLC45LC41LDEuMjUsLjg2LC4zNSwuMzYsLjYzLC43OCwuODMsMS4yNywuMiwuNDgsLjMsMSwuMywxLjU0WiIvPjwvZz48L3N2Zz4=" alt="logo"/></p>
"""
def get_commit_statuses(sha: str) -> pd.DataFrame:
"""
Fetch commit statuses for a given SHA and return as a pandas DataFrame.
Handles pagination to get all statuses.
Args:
sha (str): Commit SHA to fetch statuses for.
Returns:
pd.DataFrame: DataFrame containing all statuses.
"""
headers = {
"Authorization": f"token {os.getenv('GITHUB_TOKEN')}",
"Accept": "application/vnd.github.v3+json",
}
url = f"https://api.github.com/repos/{GITHUB_REPO}/commits/{sha}/statuses"
all_data = []
while url:
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(
f"Failed to fetch statuses: {response.status_code} {response.text}"
)
data = response.json()
all_data.extend(data)
# Check for pagination links in the response headers
if "Link" in response.headers:
links = response.headers["Link"].split(",")
next_url = None
for link in links:
parts = link.strip().split(";")
if len(parts) == 2 and 'rel="next"' in parts[1]:
next_url = parts[0].strip("<>")
break
url = next_url
else:
url = None
# Parse relevant fields
parsed = [
{
"job_name": item["context"],
"job_status": item["state"],
"message": item["description"],
"results_link": item["target_url"],
}
for item in all_data
]
# Create DataFrame
df = pd.DataFrame(parsed)
# Drop duplicates keeping the first occurrence (newest status for each context)
# GitHub returns statuses in reverse chronological order
df = df.drop_duplicates(subset=["job_name"], keep="first")
# Sort by status and job name
return df.sort_values(
by=["job_status", "job_name"], ascending=[True, True]
).reset_index(drop=True)
def get_pr_info_from_number(pr_number: str) -> dict:
"""
Fetch pull request information for a given PR number.
Args:
pr_number (str): Pull request number to fetch information for.
Returns:
dict: Dictionary containing PR information.
"""
headers = {
"Authorization": f"token {os.getenv('GITHUB_TOKEN')}",
"Accept": "application/vnd.github.v3+json",
}
url = f"https://api.github.com/repos/{GITHUB_REPO}/pulls/{pr_number}"
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(
f"Failed to fetch pull request info: {response.status_code} {response.text}"
)
return response.json()
def get_checks_fails(client: Client, job_url: str):
"""
Get tests that did not succeed for the given job URL.
Exclude checks that have status 'error' as they are counted in get_checks_errors.
"""
query = f"""SELECT job_status, job_name, status as test_status, test_name, results_link
FROM (
SELECT
argMax(check_status, check_start_time) as job_status,
check_name as job_name,
argMax(test_status, check_start_time) as status,
test_name,
report_url as results_link,
task_url
FROM `gh-data`.checks
GROUP BY check_name, test_name, report_url, task_url
)
WHERE task_url LIKE '{job_url}%'
AND test_status IN ('FAIL', 'ERROR')
AND job_status!='error'
ORDER BY job_name, test_name
"""
return client.query_dataframe(query)
def get_checks_known_fails(client: Client, job_url: str, known_fails: dict):
"""
Get tests that are known to fail for the given job URL.
"""
if len(known_fails) == 0:
return pd.DataFrame()
query = f"""SELECT job_status, job_name, status as test_status, test_name, results_link
FROM (
SELECT
argMax(check_status, check_start_time) as job_status,
check_name as job_name,
argMax(test_status, check_start_time) as status,
test_name,
report_url as results_link,
task_url
FROM `gh-data`.checks
GROUP BY check_name, test_name, report_url, task_url
)
WHERE task_url LIKE '{job_url}%'
AND test_status='BROKEN'
AND test_name IN ({','.join(f"'{test}'" for test in known_fails.keys())})
ORDER BY job_name, test_name
"""
df = client.query_dataframe(query)
df.insert(
len(df.columns) - 1,
"reason",
df["test_name"]
.astype(str)
.apply(
lambda test_name: known_fails[test_name].get("reason", "No reason given")
),
)
return df
def get_checks_errors(client: Client, job_url: str):
"""
Get checks that have status 'error' for the given job URL.
"""
query = f"""SELECT job_status, job_name, status as test_status, test_name, results_link
FROM (
SELECT
argMax(check_status, check_start_time) as job_status,
check_name as job_name,
argMax(test_status, check_start_time) as status,
test_name,
report_url as results_link,
task_url
FROM `gh-data`.checks
GROUP BY check_name, test_name, report_url, task_url
)
WHERE task_url LIKE '{job_url}%'
AND job_status=='error'
ORDER BY job_name, test_name
"""
return client.query_dataframe(query)
def drop_prefix_rows(df, column_to_clean):
"""
Drop rows from the dataframe if:
- the row matches another row completely except for the specified column
- the specified column of that row is a prefix of the same column in another row
"""
to_drop = set()
reference_columns = [col for col in df.columns if col != column_to_clean]
for (i, row_1), (j, row_2) in combinations(df.iterrows(), 2):
if all(row_1[col] == row_2[col] for col in reference_columns):
if row_2[column_to_clean].startswith(row_1[column_to_clean]):
to_drop.add(i)
elif row_1[column_to_clean].startswith(row_2[column_to_clean]):
to_drop.add(j)
return df.drop(to_drop)
def get_regression_fails(client: Client, job_url: str):
"""
Get regression tests that did not succeed for the given job URL.
"""
# If you rename the alias for report_url, also update the formatters in format_results_as_html_table
# Nested SELECT handles test reruns
query = f"""SELECT arch, job_name, status, test_name, results_link
FROM (
SELECT
architecture as arch,
test_name,
argMax(result, start_time) AS status,
job_url,
job_name,
report_url as results_link
FROM `gh-data`.clickhouse_regression_results
GROUP BY architecture, test_name, job_url, job_name, report_url
ORDER BY length(test_name) DESC
)
WHERE job_url='{job_url}'
AND status IN ('Fail', 'Error')
"""
df = client.query_dataframe(query)
df = drop_prefix_rows(df, "test_name")
df["job_name"] = df["job_name"].str.title()
return df
def get_cves(pr_number, commit_sha):
s3_client = boto3.client("s3", endpoint_url=os.getenv("S3_URL"))
s3_prefix = f"{pr_number}/{commit_sha}/grype/"
results = []
response = s3_client.list_objects_v2(
Bucket=S3_BUCKET, Prefix=s3_prefix, Delimiter="/"
)
grype_result_dirs = [
content["Prefix"] for content in response.get("CommonPrefixes", [])
]
for path in grype_result_dirs:
file_key = f"{path}result.json"
file_response = s3_client.get_object(Bucket=S3_BUCKET, Key=file_key)
content = file_response["Body"].read().decode("utf-8")
results.append(json.loads(content))
rows = []
for scan_result in results:
for match in scan_result["matches"]:
rows.append(
{
"docker_image": scan_result["source"]["target"]["userInput"],
"severity": match["vulnerability"]["severity"],
"identifier": match["vulnerability"]["id"],
"namespace": match["vulnerability"]["namespace"],
}
)
if len(rows) == 0:
return pd.DataFrame()
df = pd.DataFrame(rows).drop_duplicates()
df = df.sort_values(
by="severity",
key=lambda col: col.str.lower().map(
{"critical": 1, "high": 2, "medium": 3, "low": 4, "negligible": 5}
),
)
return df
def url_to_html_link(url: str) -> str:
if not url:
return ""
text = url.split("/")[-1].replace("__", "_")
if not text:
text = "results"
return f'<a href="{url}">{text}</a>'
def format_test_name_for_linewrap(text: str) -> str:
"""Tweak the test name to improve line wrapping."""
return f'<span style="line-break: anywhere;">{text}</span>'
def format_test_status(text: str) -> str:
"""Format the test status for better readability."""
color = (
"red"
if text.lower().startswith("fail")
else "orange" if text.lower() in ("error", "broken") else "green"
)
return f'<span style="font-weight: bold; color: {color}">{text}</span>'
def format_results_as_html_table(results) -> str:
if len(results) == 0:
return "<p>Nothing to report</p>"
results.columns = [col.replace("_", " ").title() for col in results.columns]
html = results.to_html(
index=False,
formatters={
"Results Link": url_to_html_link,
"Test Name": format_test_name_for_linewrap,
"Test Status": format_test_status,
"Job Status": format_test_status,
"Status": format_test_status,
"Message": lambda m: m.replace("\n", " "),
"Identifier": lambda i: url_to_html_link(
"https://nvd.nist.gov/vuln/detail/" + i
),
},
escape=False,
).replace(' border="1"', "")
return html
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Create a combined CI report.")
parser.add_argument(
"--actions-run-url", required=True, help="URL of the actions run"
)
parser.add_argument(
"--pr-number", required=True, help="Pull request number for the S3 path"
)
parser.add_argument(
"--commit-sha", required=True, help="Commit SHA for the S3 path"
)
parser.add_argument(
"--no-upload", action="store_true", help="Do not upload the report"
)
parser.add_argument(
"--known-fails", type=str, help="Path to the file with known fails"
)
parser.add_argument(
"--cves", action="store_true", help="Get CVEs from Grype results"
)
parser.add_argument(
"--mark-preview", action="store_true", help="Mark the report as a preview"
)
return parser.parse_args()
def main():
args = parse_args()
db_client = Client(
host=os.getenv(DATABASE_HOST_VAR),
user=os.getenv(DATABASE_USER_VAR),
password=os.getenv(DATABASE_PASSWORD_VAR),
port=9440,
secure="y",
verify=False,
settings={"use_numpy": True},
)
fail_results = {
"job_statuses": get_commit_statuses(args.commit_sha),
"checks_fails": get_checks_fails(db_client, args.actions_run_url),
"checks_known_fails": [],
"checks_errors": get_checks_errors(db_client, args.actions_run_url),
"regression_fails": get_regression_fails(db_client, args.actions_run_url),
"docker_images_cves": (
[] if not args.cves else get_cves(args.pr_number, args.commit_sha)
),
}
if args.known_fails:
if not os.path.exists(args.known_fails):
print(f"Known fails file {args.known_fails} not found.")
exit(1)
with open(args.known_fails) as f:
known_fails = json.load(f)
if known_fails:
fail_results["checks_known_fails"] = get_checks_known_fails(
db_client, args.actions_run_url, known_fails
)
if args.pr_number == "0":
pr_info_html = "Release"
else:
try:
pr_info = get_pr_info_from_number(args.pr_number)
pr_info_html = f"""<a href="https://github.com/{GITHUB_REPO}/pull/{pr_info["number"]}">
#{pr_info.get("number")} ({pr_info.get("base", {}).get('ref')} <- {pr_info.get("head", {}).get('ref')}) {pr_info.get("title")}
</a>"""
except Exception as e:
pr_info_html = e
high_cve_count = 0
if len(fail_results["docker_images_cves"]) > 0:
high_cve_count = (
fail_results["docker_images_cves"]["severity"]
.str.lower()
.isin(("high", "critical"))
.sum()
)
title = "ClickHouse® CI Workflow Run Report"
html_report = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>{css}
</style>
<title>{title}</title>
</head>
<body>
{logo}
<h1>{title}</h1>
<table>
<tr>
<th class='hth no-sort'>Pull Request</th><td>{pr_info_html}</td>
</tr>
<tr>
<th class='hth no-sort'>Workflow Run</th><td><a href="{args.actions_run_url}">{args.actions_run_url.split('/')[-1]}</a></td>
</tr>
<tr>
<th class='hth no-sort'>Commit</th><td><a href="https://github.com/{GITHUB_REPO}/commit/{args.commit_sha}">{args.commit_sha}</a></td>
</tr>
<tr>
<th class='hth no-sort'>Date</th><td>{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')} UTC</td>
</tr>
</table>
<h2>Table of Contents</h2>
{'<p style="font-weight: bold;color: #F00;">This is a preview. FinishCheck has not completed.</p>' if args.mark_preview else ""}
<ul>
<li><a href="#ci-jobs-status">CI Jobs Status</a> ({sum(fail_results['job_statuses']['job_status'] != 'success')} fail/error)</li>
<li><a href="#checks-errors">Checks Errors</a> ({len(fail_results['checks_errors'])})</li>
<li><a href="#checks-fails">Checks New Fails</a> ({len(fail_results['checks_fails'])})</li>
<li><a href="#regression-fails">Regression New Fails</a> ({len(fail_results['regression_fails'])})</li>
<li><a href="#docker-images-cves">Docker Images CVEs</a> ({'N/A' if not args.cves else f'{high_cve_count} high/critical'})</li>
<li><a href="#checks-known-fails">Checks Known Fails</a> ({'N/A' if not args.known_fails else len(fail_results['checks_known_fails'])})</li>
</ul>
<h2 id="ci-jobs-status">CI Jobs Status</h2>
{format_results_as_html_table(fail_results['job_statuses'])}
<h2 id="checks-errors">Checks Errors</h2>
{format_results_as_html_table(fail_results['checks_errors'])}
<h2 id="checks-fails">Checks New Fails</h2>
{format_results_as_html_table(fail_results['checks_fails'])}
<h2 id="regression-fails">Regression New Fails</h2>
{format_results_as_html_table(fail_results['regression_fails'])}
<h2 id="docker-images-cves">Docker Images CVEs</h2>
{"<p>Not Checked</p>" if not args.cves else format_results_as_html_table(fail_results['docker_images_cves'])}
<h2 id="checks-known-fails">Checks Known Fails</h2>
{"<p>Not Checked</p>" if not args.known_fails else format_results_as_html_table(fail_results['checks_known_fails'])}
{script}
</body>
</html>
"""
report_name = "ci_run_report.html"
report_path = Path(report_name)
report_path.write_text(html_report, encoding="utf-8")
if args.no_upload:
print(f"Report saved to {report_path}")
exit(0)
report_destination_key = f"{args.pr_number}/{args.commit_sha}/{report_name}"
# Upload the report to S3
s3_client = boto3.client("s3", endpoint_url=os.getenv("S3_URL"))
try:
s3_client.put_object(
Bucket=S3_BUCKET,
Key=report_destination_key,
Body=html_report,
ContentType="text/html; charset=utf-8",
)
except NoCredentialsError:
print("Credentials not available for S3 upload.")
print(f"https://s3.amazonaws.com/{S3_BUCKET}/" + report_destination_key)
if __name__ == "__main__":
main()