mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Changes by apply order: 1. Replace all `".."` and `os.pardir` usage with `os.path.dirname(...)`. 2. Replace nested `os.path.dirname(os.path.dirname(...))` call with `str(Path(...).parent.parent)`. 3. Reorder `.absolute()` ~/ `.resolve()`~ and `.parent`: always resolve the path first. `.parent{...}.absolute()` -> `.absolute().parent{...}` 4. Replace chained `.parent x N` with `.parents[${N - 1}]`: the code is easier to read (see 5.) `.parent.parent.parent.parent` -> `.parents[3]` 5. ~Replace `.parents[${N - 1}]` with `.parents[${N} - 1]`: the code is easier to read and does not introduce any runtime overhead.~ ~`.parents[3]` -> `.parents[4 - 1]`~ 6. ~Replace `.parents[2 - 1]` with `.parent.parent`: because the code is shorter and easier to read.~ Pull Request resolved: https://github.com/pytorch/pytorch/pull/129374 Approved by: https://github.com/justinchuby, https://github.com/malfet
75 lines
2.5 KiB
Python
Executable File
75 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
WORKFLOWS = REPO_ROOT / ".github" / "workflows"
|
|
EXPECTED_GROUP_PREFIX = (
|
|
"${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}"
|
|
)
|
|
EXPECTED_GROUP = (
|
|
EXPECTED_GROUP_PREFIX + "-${{ github.event_name == 'workflow_dispatch' }}"
|
|
)
|
|
|
|
|
|
def should_check(filename: Path) -> bool:
|
|
with open(filename) as f:
|
|
content = f.read()
|
|
|
|
data = yaml.safe_load(content)
|
|
on = data.get("on", data.get(True, {}))
|
|
return "pull_request" in on
|
|
|
|
|
|
if __name__ == "__main__":
|
|
errors_found = False
|
|
files = [f for f in WORKFLOWS.glob("*.yml") if should_check(f)]
|
|
names = set()
|
|
for filename in files:
|
|
with open(filename) as f:
|
|
data = yaml.safe_load(f)
|
|
|
|
name = data.get("name")
|
|
if name is not None and name in names:
|
|
print("ERROR: duplicate workflow name:", name, file=sys.stderr)
|
|
errors_found = True
|
|
names.add(name)
|
|
actual = data.get("concurrency", {})
|
|
if filename.name == "create_release.yml":
|
|
if not actual.get("group", "").startswith(EXPECTED_GROUP_PREFIX):
|
|
print(
|
|
f"'concurrency' incorrect or not found in '{filename.relative_to(REPO_ROOT)}'",
|
|
file=sys.stderr,
|
|
)
|
|
print(
|
|
f"concurrency group should start with {EXPECTED_GROUP_PREFIX} but found {actual.get('group', None)}",
|
|
file=sys.stderr,
|
|
)
|
|
errors_found = True
|
|
elif not actual.get("group", "").startswith(EXPECTED_GROUP):
|
|
print(
|
|
f"'concurrency' incorrect or not found in '{filename.relative_to(REPO_ROOT)}'",
|
|
file=sys.stderr,
|
|
)
|
|
print(
|
|
f"concurrency group should start with {EXPECTED_GROUP} but found {actual.get('group', None)}",
|
|
file=sys.stderr,
|
|
)
|
|
errors_found = True
|
|
if not actual.get("cancel-in-progress", False):
|
|
print(
|
|
f"'concurrency' incorrect or not found in '{filename.relative_to(REPO_ROOT)}'",
|
|
file=sys.stderr,
|
|
)
|
|
print(
|
|
f"concurrency cancel-in-progress should be True but found {actual.get('cancel-in-progress', None)}",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
if errors_found:
|
|
sys.exit(1)
|