Files
pytorch/.github/scripts/parse_ref.py
Huy Do 4c0dce50fd [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
2023-03-26 04:52:55 +00:00

30 lines
734 B
Python
Executable File

#!/usr/bin/env python3
import os
import re
def set_output(name: str, val: str) -> None:
if os.getenv("GITHUB_OUTPUT"):
with open(str(os.getenv("GITHUB_OUTPUT")), "a") as env:
print(f"{name}={val}", file=env)
else:
print(f"::set-output name={name}::{val}")
def main() -> None:
ref = os.environ["GITHUB_REF"]
m = re.match(r"^refs/(\w+)/(.*)$", ref)
if m:
category, stripped = m.groups()
if category == "heads":
set_output("branch", stripped)
elif category == "pull":
set_output("branch", "pull/" + stripped.split("/")[0])
elif category == "tags":
set_output("tag", stripped)
if __name__ == "__main__":
main()