Skip to content

Commit

Permalink
[misc] Use black!
Browse files Browse the repository at this point in the history
  • Loading branch information
feisuzhu committed Apr 16, 2023
1 parent 0f00871 commit ee0abd5
Show file tree
Hide file tree
Showing 396 changed files with 13,082 additions and 10,773 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

import ti_build.entry

if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(ti_build.entry.main())
105 changes: 56 additions & 49 deletions .github/workflows/scripts/ghstack-perm-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,91 +12,98 @@

def main():
gh = requests.Session()
gh.headers.update({
'Authorization': f'Bearer {os.environ["GITHUB_TOKEN"]}',
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
})
gh.headers.update(
{
"Authorization": f'Bearer {os.environ["GITHUB_TOKEN"]}',
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
)
EV = json.loads(sys.stdin.read())
REPO = EV['repository']
PR = EV['event']['client_payload']['pull_request']
NUMBER = PR['number']
REPO = EV["repository"]
PR = EV["event"]["client_payload"]["pull_request"]
NUMBER = PR["number"]

def must(cond, msg):
if not cond:
print(msg)
gh.post(
f'https://api.github.com/repos/{REPO}/issues/{NUMBER}/comments',
f"https://api.github.com/repos/{REPO}/issues/{NUMBER}/comments",
json={
'body': f'ghstack bot failed: {msg}',
})
"body": f"ghstack bot failed: {msg}",
},
)
exit(1)

head_ref = PR['head']['ref']
must(head_ref and re.match(r'^gh/[A-Za-z0-9-]+/[0-9]+/head$', head_ref),
'Not a ghstack PR')
orig_ref = head_ref.replace('/head', '/orig')
print(':: Fetching newest master...')
must(os.system('git fetch origin master') == 0, "Can't fetch master")
print(':: Fetching orig branch...')
head_ref = PR["head"]["ref"]
must(
os.system(f'git fetch origin {orig_ref}') == 0,
"Can't fetch orig branch")
head_ref and re.match(r"^gh/[A-Za-z0-9-]+/[0-9]+/head$", head_ref),
"Not a ghstack PR",
)
orig_ref = head_ref.replace("/head", "/orig")
print(":: Fetching newest master...")
must(os.system("git fetch origin master") == 0, "Can't fetch master")
print(":: Fetching orig branch...")
must(os.system(f"git fetch origin {orig_ref}") == 0, "Can't fetch orig branch")

proc = subprocess.Popen(
'git log FETCH_HEAD...$(git merge-base FETCH_HEAD origin/master)',
"git log FETCH_HEAD...$(git merge-base FETCH_HEAD origin/master)",
stdout=subprocess.PIPE,
shell=True)
shell=True,
)
out, _ = proc.communicate()
must(proc.wait() == 0, '`git log` command failed!')
must(proc.wait() == 0, "`git log` command failed!")

pr_numbers = re.findall(
r'Pull Request resolved: https://github.com/.*?/pull/([0-9]+)',
out.decode('utf-8'))
r"Pull Request resolved: https://github.com/.*?/pull/([0-9]+)",
out.decode("utf-8"),
)
pr_numbers = list(map(int, pr_numbers))
must(pr_numbers and pr_numbers[0] == NUMBER,
'Extracted PR numbers not seems right!')
must(
pr_numbers and pr_numbers[0] == NUMBER, "Extracted PR numbers not seems right!"
)

for n in pr_numbers:
print(f':: Checking PR status #{n}... ', end='')
resp = gh.get(f'https://api.github.com/repos/{REPO}/pulls/{n}')
must(resp.ok, 'Error Getting PR Object!')
print(f":: Checking PR status #{n}... ", end="")
resp = gh.get(f"https://api.github.com/repos/{REPO}/pulls/{n}")
must(resp.ok, "Error Getting PR Object!")
pr_obj = resp.json()

resp = gh.get(
f'https://api.github.com/repos/{REPO}/pulls/{NUMBER}/reviews')
must(resp.ok, 'Error Getting PR Reviews!')
resp = gh.get(f"https://api.github.com/repos/{REPO}/pulls/{NUMBER}/reviews")
must(resp.ok, "Error Getting PR Reviews!")
reviews = resp.json()
idmap = {}
approved = False
for r in reviews:
s = r['state']
if s not in ('COMMENTED', ):
idmap[r['user']['login']] = r['state']
s = r["state"]
if s not in ("COMMENTED",):
idmap[r["user"]["login"]] = r["state"]

for u, cc in idmap.items():
approved = approved or cc == 'APPROVED'
must(cc in ('APPROVED', 'DISMISSED'),
f'@{u} has stamped PR #{n} `{cc}`, please resolve it first!')
approved = approved or cc == "APPROVED"
must(
cc in ("APPROVED", "DISMISSED"),
f"@{u} has stamped PR #{n} `{cc}`, please resolve it first!",
)

must(approved, f'PR #{n} is not approved yet!')
must(approved, f"PR #{n} is not approved yet!")

resp = gh.get(
f'https://api.github.com/repos/{REPO}/commits/{pr_obj["head"]["sha"]}/check-runs'
)
must(resp.ok, 'Error getting check runs status!')
must(resp.ok, "Error getting check runs status!")
checkruns = resp.json()
for cr in checkruns['check_runs']:
status = cr.get('conclusion', cr['status'])
name = cr['name']
for cr in checkruns["check_runs"]:
status = cr.get("conclusion", cr["status"])
name = cr["name"]
must(
status == 'success',
f'PR #{n} check-run `{name}`\'s status `{status}` is not success!'
status == "success",
f"PR #{n} check-run `{name}`'s status `{status}` is not success!",
)
print('SUCCESS!')
print("SUCCESS!")

print(':: All PRs are ready to be landed!')
print(":: All PRs are ready to be landed!")


if __name__ == '__main__':
if __name__ == "__main__":
main()
90 changes: 47 additions & 43 deletions .github/workflows/scripts/post-benchmark-to-github-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import os
import sqlite3

# -- stdlib --
from pathlib import Path

Expand All @@ -15,101 +16,103 @@

# -- code --
gh = requests.Session()
gh.headers.update({
'Authorization': f'Bearer {os.environ["GITHUB_TOKEN"]}',
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
})
gh.headers.update(
{
"Authorization": f'Bearer {os.environ["GITHUB_TOKEN"]}',
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
)


def post_comment(repo, number, msg):
gh.post(
f'https://api.github.com/repos/{repo}/issues/{number}/comments',
json={'body': msg},
f"https://api.github.com/repos/{repo}/issues/{number}/comments",
json={"body": msg},
)


def get_db():
db = sqlite3.connect(':memory:')
db.execute('CREATE TABLE release (name TEXT, value REAL)')
db.execute('CREATE UNIQUE INDEX release_name ON release (name)')
db.execute('CREATE TABLE current (name TEXT, value REAL)')
db.execute('CREATE UNIQUE INDEX current_name ON current (name)')
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE release (name TEXT, value REAL)")
db.execute("CREATE UNIQUE INDEX release_name ON release (name)")
db.execute("CREATE TABLE current (name TEXT, value REAL)")
db.execute("CREATE UNIQUE INDEX current_name ON current (name)")
return db


IGNORE_TAGS = {'type', 'release', 'impl'}
IGNORE_TAGS = {"type", "release", "impl"}


def flatten_metric(m):
tags = [f'{k}={v}' for k, v in m['tags'].items() if k not in IGNORE_TAGS]
tags = ','.join(sorted(tags))
return (f'{m["name"]}@{tags}', m['value'])
tags = [f"{k}={v}" for k, v in m["tags"].items() if k not in IGNORE_TAGS]
tags = ",".join(sorted(tags))
return (f'{m["name"]}@{tags}', m["value"])


def fmt(v):
if 0 < abs(v) < 1:
return f'{v:.3g}'
return f"{v:.3g}"
else:
return f'{v:.3f}'
return f"{v:.3f}"


def render_result(baseline, sha, rs):
texts = []
_ = texts.append
_(f'## Benchmark Report')
_(f'Baseline: `{baseline}`')
_(f'Current: `{sha}`')
_('')
_('| Item | Baseline | Current | Change |')
_('| --- | --- | --- | --- |')
_(f"## Benchmark Report")
_(f"Baseline: `{baseline}`")
_(f"Current: `{sha}`")
_("")
_("| Item | Baseline | Current | Change |")
_("| --- | --- | --- | --- |")
for name, cv, rv, rate in rs:
cmap = ['red', 'green']
if ':wall_time@' in name:
cmap = ["red", "green"]
if ":wall_time@" in name:
cv *= 1000
rv *= 1000
cmap = ['green', 'red']
cmap = ["green", "red"]

if rate > 5.0:
color = cmap[1]
elif rate < -5.0:
color = cmap[0]
else:
color = 'gray'
color = "gray"

rate = f'{" +"[rate > 0]}{rate:.2f}'

_(fr'| {name} | {fmt(rv)} | {fmt(cv)} | $\textcolor{{{color}}}{{\textsf{{{rate}\\%}}}}$ |'
)
_(
rf"| {name} | {fmt(rv)} | {fmt(cv)} | $\textcolor{{{color}}}{{\textsf{{{rate}\\%}}}}$ |"
)

return '\n'.join(texts)
return "\n".join(texts)


def main():
parser = argparse.ArgumentParser()
parser.add_argument('event')
parser.add_argument('result')
parser.add_argument("event")
parser.add_argument("result")
options = parser.parse_args()

db = get_db()

current = json.loads(Path(options.result).read_text())
for item in current:
db.execute('INSERT OR IGNORE INTO current VALUES (?, ?)',
flatten_metric(item))
db.execute("INSERT OR IGNORE INTO current VALUES (?, ?)", flatten_metric(item))

ver = requests.get(
'https://benchmark.taichi-lang.cn/releases?order=vnum.desc&limit=1'
).json()[0]['version']
"https://benchmark.taichi-lang.cn/releases?order=vnum.desc&limit=1"
).json()[0]["version"]
release = requests.get(
f'https://benchmark.taichi-lang.cn/taichi_benchmark?tags->>type=eq.release&tags->>release=eq.{ver}'
f"https://benchmark.taichi-lang.cn/taichi_benchmark?tags->>type=eq.release&tags->>release=eq.{ver}"
).json()

for item in release:
db.execute('INSERT OR IGNORE INTO release VALUES (?, ?)',
flatten_metric(item))
db.execute("INSERT OR IGNORE INTO release VALUES (?, ?)", flatten_metric(item))

rs = db.execute('''
rs = db.execute(
"""
SELECT
c.name AS name,
c.value AS cv,
Expand All @@ -119,7 +122,8 @@ def main():
current c
LEFT JOIN release r ON (r.name = c.name)
ORDER BY name
''')
"""
)

event = json.loads(Path(options.event).read_text())
sha = event["client_payload"]["pull_request"]["head"]["sha"]
Expand All @@ -132,5 +136,5 @@ def main():
)


if __name__ == '__main__':
if __name__ == "__main__":
main()

0 comments on commit ee0abd5

Please sign in to comment.