mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-22 22:25:10 +08:00
[BE] Apply ufmt to run_test and GitHub Python util scripts (#97588)
This has been bugging me for a while as I'm working on these Python scripts and they are not tracked by ufmt linter. So I add these script into that linter. ``` [[linter]] code = 'UFMT' include_patterns = [ '.github/**/*.py', 'test/run_test.py', ``` This change should just work and not break anything as ufmt (black + usort) linter is very safe to use for standalone util scripts. Pull Request resolved: https://github.com/pytorch/pytorch/pull/97588 Approved by: https://github.com/kit1980
This commit is contained in:
71
.github/scripts/github_utils.py
vendored
71
.github/scripts/github_utils.py
vendored
@ -21,56 +21,69 @@ class GitHubComment:
|
||||
|
||||
|
||||
def gh_fetch_url(
|
||||
url: str, *,
|
||||
url: str,
|
||||
*,
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
data: Optional[Dict[str, Any]] = None,
|
||||
method: Optional[str] = None,
|
||||
reader: Callable[[Any], Any] = lambda x: x.read()
|
||||
reader: Callable[[Any], Any] = lambda x: x.read(),
|
||||
) -> Any:
|
||||
if headers is None:
|
||||
headers = {}
|
||||
token = os.environ.get("GITHUB_TOKEN")
|
||||
if token is not None and url.startswith('https://api.github.com/'):
|
||||
headers['Authorization'] = f'token {token}'
|
||||
if token is not None and url.startswith("https://api.github.com/"):
|
||||
headers["Authorization"] = f"token {token}"
|
||||
data_ = json.dumps(data).encode() if data is not None else None
|
||||
try:
|
||||
with urlopen(Request(url, headers=headers, data=data_, method=method)) as conn:
|
||||
return reader(conn)
|
||||
except HTTPError as err:
|
||||
if err.code == 403 and all(key in err.headers for key in ['X-RateLimit-Limit', 'X-RateLimit-Used']):
|
||||
print(f"""Rate limit exceeded:
|
||||
if err.code == 403 and all(
|
||||
key in err.headers for key in ["X-RateLimit-Limit", "X-RateLimit-Used"]
|
||||
):
|
||||
print(
|
||||
f"""Rate limit exceeded:
|
||||
Used: {err.headers['X-RateLimit-Used']}
|
||||
Limit: {err.headers['X-RateLimit-Limit']}
|
||||
Remaining: {err.headers['X-RateLimit-Remaining']}
|
||||
Resets at: {err.headers['x-RateLimit-Reset']}""")
|
||||
Resets at: {err.headers['x-RateLimit-Reset']}"""
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
def gh_fetch_json(
|
||||
url: str,
|
||||
params: Optional[Dict[str, Any]] = None,
|
||||
data: Optional[Dict[str, Any]] = None
|
||||
data: Optional[Dict[str, Any]] = None,
|
||||
) -> List[Dict[str, Any]]:
|
||||
headers = {'Accept': 'application/vnd.github.v3+json'}
|
||||
headers = {"Accept": "application/vnd.github.v3+json"}
|
||||
if params is not None and len(params) > 0:
|
||||
url += '?' + '&'.join(f"{name}={quote(str(val))}" for name, val in params.items())
|
||||
return cast(List[Dict[str, Any]], gh_fetch_url(url, headers=headers, data=data, reader=json.load))
|
||||
url += "?" + "&".join(
|
||||
f"{name}={quote(str(val))}" for name, val in params.items()
|
||||
)
|
||||
return cast(
|
||||
List[Dict[str, Any]],
|
||||
gh_fetch_url(url, headers=headers, data=data, reader=json.load),
|
||||
)
|
||||
|
||||
|
||||
def _gh_fetch_json_any(
|
||||
url: str,
|
||||
params: Optional[Dict[str, Any]] = None,
|
||||
data: Optional[Dict[str, Any]] = None
|
||||
data: Optional[Dict[str, Any]] = None,
|
||||
) -> Any:
|
||||
headers = {'Accept': 'application/vnd.github.v3+json'}
|
||||
headers = {"Accept": "application/vnd.github.v3+json"}
|
||||
if params is not None and len(params) > 0:
|
||||
url += '?' + '&'.join(f"{name}={quote(str(val))}" for name, val in params.items())
|
||||
url += "?" + "&".join(
|
||||
f"{name}={quote(str(val))}" for name, val in params.items()
|
||||
)
|
||||
return gh_fetch_url(url, headers=headers, data=data, reader=json.load)
|
||||
|
||||
|
||||
def gh_fetch_json_list(
|
||||
url: str,
|
||||
params: Optional[Dict[str, Any]] = None,
|
||||
data: Optional[Dict[str, Any]] = None
|
||||
data: Optional[Dict[str, Any]] = None,
|
||||
) -> List[Dict[str, Any]]:
|
||||
return cast(List[Dict[str, Any]], _gh_fetch_json_any(url, params, data))
|
||||
|
||||
@ -78,24 +91,38 @@ def gh_fetch_json_list(
|
||||
def gh_fetch_json_dict(
|
||||
url: str,
|
||||
params: Optional[Dict[str, Any]] = None,
|
||||
data: Optional[Dict[str, Any]] = None
|
||||
) -> Dict[str, Any] :
|
||||
data: Optional[Dict[str, Any]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
return cast(Dict[str, Any], _gh_fetch_json_any(url, params, data))
|
||||
|
||||
|
||||
def _gh_post_comment(url: str, comment: str, dry_run: bool = False) -> List[Dict[str, Any]]:
|
||||
def _gh_post_comment(
|
||||
url: str, comment: str, dry_run: bool = False
|
||||
) -> List[Dict[str, Any]]:
|
||||
if dry_run:
|
||||
print(comment)
|
||||
return []
|
||||
return gh_fetch_json_list(url, data={"body": comment})
|
||||
|
||||
|
||||
def gh_post_pr_comment(org: str, repo: str, pr_num: int, comment: str, dry_run: bool = False) -> List[Dict[str, Any]]:
|
||||
return _gh_post_comment(f'https://api.github.com/repos/{org}/{repo}/issues/{pr_num}/comments', comment, dry_run)
|
||||
def gh_post_pr_comment(
|
||||
org: str, repo: str, pr_num: int, comment: str, dry_run: bool = False
|
||||
) -> List[Dict[str, Any]]:
|
||||
return _gh_post_comment(
|
||||
f"https://api.github.com/repos/{org}/{repo}/issues/{pr_num}/comments",
|
||||
comment,
|
||||
dry_run,
|
||||
)
|
||||
|
||||
|
||||
def gh_post_commit_comment(org: str, repo: str, sha: str, comment: str, dry_run: bool = False) -> List[Dict[str, Any]]:
|
||||
return _gh_post_comment(f'https://api.github.com/repos/{org}/{repo}/commits/{sha}/comments', comment, dry_run)
|
||||
def gh_post_commit_comment(
|
||||
org: str, repo: str, sha: str, comment: str, dry_run: bool = False
|
||||
) -> List[Dict[str, Any]]:
|
||||
return _gh_post_comment(
|
||||
f"https://api.github.com/repos/{org}/{repo}/commits/{sha}/comments",
|
||||
comment,
|
||||
dry_run,
|
||||
)
|
||||
|
||||
|
||||
def gh_delete_comment(org: str, repo: str, comment_id: int) -> None:
|
||||
|
Reference in New Issue
Block a user