Skip to content

Commit 5da596f

Browse files
authored
Importing initial code from izaitsevfb/autorevert-history and introduced lots of BE changes (#6819)
Importing the code from `izaitsevfb/autorevert-history` to the lambda, and doing lots of BE effort to make it closer to production-ready code.
1 parent b289607 commit 5da596f

17 files changed

+1048
-56
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[flake8]
22
max-line-length = 120
3-
ignore = G004,B028,W504,W503,EXE001,G003
3+
ignore = G004,B028,W504,W503,EXE001,G003,E402
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ClickHouse Configuration
2+
CLICKHOUSE_HOST=your_clickhouse_host
3+
CLICKHOUSE_PORT=9000
4+
CLICKHOUSE_USER=default
5+
CLICKHOUSE_PASSWORD=your_password
6+
CLICKHOUSE_DATABASE=pytorch
7+
8+
# GitHub Configuration
9+
GITHUB_TOKEN=your_github_token_here
10+
GITHUB_REPO_OWNER=pytorch
11+
GITHUB_REPO_NAME=pytorch
12+
13+
# Script Configuration
14+
LOG_LEVEL=INFO
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
all: run-local
22

3+
.PHONY: clean
34
clean:
45
rm -rf deployment
5-
rm -rf venv
66
rm -rf deployment.zip
7+
rm -rf venv
8+
# it makes sense for this to be the last one, or at least after rm -rf venv
9+
find . -name __pycache__ -type d | xargs rm -rf
710

811
venv/bin/python:
912
virtualenv venv
1013
venv/bin/pip install -r requirements.txt
1114

15+
venv/bin/lintrunner: venv/bin/python
16+
venv/bin/pip install lintrunner==0.12.5 boto3-stubs==1.34.51
17+
# lintrunner only works properly with virtualenv if you activate it first
18+
. venv/bin/activate && lintrunner init --config ../../../.lintrunner.toml
19+
1220
.PHONY: run-local
1321
run-local: venv/bin/python
14-
venv/bin/python auto_revert.py --max-hours 70 --worker-pool-size 8 # --rebuild-table
22+
venv/bin/python -m pytorch_auto_revert
23+
24+
.PHONY: run-local-workflows
25+
run-local-workflows: venv/bin/python
26+
venv/bin/python -m pytorch_auto_revert workflows pull.yml
1527

1628
deployment.zip:
1729
mkdir -p deployment
18-
cp auto_revert.py ./deployment/.
30+
cp -a pytorch_auto_revert ./deployment/.
31+
cp -a __init__.py ./deployment/.
1932
pip3.10 install -r requirements.txt -t ./deployment/. --platform manylinux2014_x86_64 --only-binary=:all: --implementation cp --python-version 3.10 --upgrade
2033
cd ./deployment && zip -q -r ../deployment.zip .
2134

2235
.PHONY: create-deployment-package
2336
create-deployment-package: deployment.zip
37+
38+
.PHONY: lintrunner
39+
lintrunner: venv/bin/lintrunner
40+
# lintrunner only works properly with virtualenv if you activate it first
41+
. venv/bin/activate && lintrunner -a -v --force-color --config ../../../.lintrunner.toml --paths-cmd='git grep -Il .'

aws/lambda/pytorch-auto-revert/__init__.py

Whitespace-only changes.

aws/lambda/pytorch-auto-revert/auto_revert.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

aws/lambda/pytorch-auto-revert/pytorch_auto_revert/__init__.py

Whitespace-only changes.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import logging
5+
import os
6+
7+
from dotenv import load_dotenv
8+
9+
from .clickhouse_client_helper import CHCliFactory
10+
from .github_client_helper import GHClientFactory
11+
from .testers.autorevert import autorevert_checker
12+
from .testers.do_restart import do_restart_workflow
13+
from .testers.restart_checker import workflow_restart_checker
14+
15+
16+
def setup_logging(log_level: str) -> None:
17+
"""Set up logging configuration."""
18+
numeric_level = getattr(logging, log_level.upper(), None)
19+
if not isinstance(numeric_level, int):
20+
raise ValueError(f"Invalid log level: {log_level}")
21+
logging.basicConfig(level=numeric_level)
22+
23+
24+
def get_opts() -> argparse.Namespace:
25+
parser = argparse.ArgumentParser()
26+
27+
# General options and configurations
28+
parser.add_argument(
29+
"--log-level",
30+
default=os.environ.get("LOG_LEVEL", "INFO"),
31+
choices=["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
32+
help="Set the logging level for the application.",
33+
)
34+
parser.add_argument(
35+
"--clickhouse-host", default=os.environ.get("CLICKHOUSE_HOST", "")
36+
)
37+
parser.add_argument(
38+
"--clickhouse-port",
39+
type=int,
40+
default=int(os.environ.get("CLICKHOUSE_PORT", "8443")),
41+
)
42+
parser.add_argument(
43+
"--clickhouse-username", default=os.environ.get("CLICKHOUSE_USERNAME", "")
44+
)
45+
parser.add_argument(
46+
"--clickhouse-password", default=os.environ.get("CLICKHOUSE_PASSWORD", "")
47+
)
48+
parser.add_argument(
49+
"--clickhouse-database",
50+
default=os.environ.get("CLICKHOUSE_DATABASE", "default"),
51+
)
52+
parser.add_argument(
53+
"--github-access-token", default=os.environ.get("GITHUB_TOKEN", "")
54+
)
55+
parser.add_argument("--github-app-id", default=os.environ.get("GITHUB_APP_ID", ""))
56+
parser.add_argument(
57+
"--github-app-secret", default=os.environ.get("GITHUB_APP_SECRET", "")
58+
)
59+
parser.add_argument(
60+
"--github-installation-id",
61+
type=int,
62+
default=int(os.environ.get("GITHUB_INSTALLATION_ID", "0")),
63+
)
64+
65+
# no subcommand runs the lambda flow
66+
subparsers = parser.add_subparsers(dest="subcommand")
67+
68+
# autorevert subcommand
69+
workflow_parser = subparsers.add_parser(
70+
"autorevert", help="Analyze workflows looking for autorevert patterns"
71+
)
72+
workflow_parser.add_argument(
73+
"workflows",
74+
nargs="+",
75+
help="Workflow name(s) to analyze - single name or comma/space separated"
76+
+ ' list (e.g., "pull" or "pull,trunk,inductor")',
77+
)
78+
workflow_parser.add_argument(
79+
"--hours", type=int, default=48, help="Lookback window in hours (default: 48)"
80+
)
81+
workflow_parser.add_argument(
82+
"--verbose",
83+
"-v",
84+
action="store_true",
85+
help="Show detailed output including commit summaries",
86+
)
87+
88+
# workflow-restart-checke subcommand
89+
workflow_restart_parser = subparsers.add_parser(
90+
"workflow-restart-checke", help="Check for restarted workflows"
91+
)
92+
workflow_restart_parser.add_argument(
93+
"workflow",
94+
help="Workflow file name (e.g., trunk.yml)",
95+
)
96+
workflow_restart_parser.add_argument(
97+
"--commit",
98+
help="Check specific commit SHA",
99+
)
100+
workflow_restart_parser.add_argument(
101+
"--days",
102+
type=int,
103+
default=7,
104+
help="If no `--commit` specified, look back days for bulk query (default: 7)",
105+
)
106+
107+
# do-restart subcommand
108+
do_restart_parser = subparsers.add_parser(
109+
"do-restart", help="Restart a workflow for a specific commit"
110+
)
111+
do_restart_parser.add_argument(
112+
"workflow",
113+
help="Workflow file name to restart (e.g., trunk.yml)",
114+
)
115+
do_restart_parser.add_argument(
116+
"commit",
117+
help="Commit SHA to restart the workflow for",
118+
)
119+
120+
return parser.parse_args()
121+
122+
123+
def main(*args, **kwargs) -> None:
124+
load_dotenv()
125+
opts = get_opts()
126+
setup_logging(opts.log_level)
127+
CHCliFactory.setup_client(
128+
opts.clickhouse_host,
129+
opts.clickhouse_port,
130+
opts.clickhouse_username,
131+
opts.clickhouse_password,
132+
opts.clickhouse_database,
133+
)
134+
GHClientFactory.setup_client(
135+
opts.github_app_id,
136+
opts.github_app_secret,
137+
opts.github_installation_id,
138+
opts.github_access_token,
139+
)
140+
141+
if not CHCliFactory().connection_test():
142+
raise RuntimeError(
143+
"ClickHouse connection test failed. Please check your configuration."
144+
)
145+
146+
if opts.subcommand == "lambda":
147+
print("TODO: run lambda flow")
148+
elif opts.subcommand == "workflows":
149+
autorevert_checker(opts.workflows, hours=opts.hours, verbose=opts.verbose)
150+
elif opts.subcommand == "workflow-restart-checker":
151+
workflow_restart_checker(opts.workflow, commit=opts.commit, days=opts.days)
152+
elif opts.subcommand == "do-restart":
153+
do_restart_workflow(opts.workflow, commit=opts.commit)
154+
155+
156+
if __name__ == "__main__":
157+
main()

0 commit comments

Comments
 (0)