|
2 | 2 | import shlex |
3 | 3 | import sys |
4 | 4 | from pathlib import Path |
| 5 | +from re import MULTILINE, search |
5 | 6 | from subprocess import PIPE, STDOUT, run |
6 | 7 |
|
7 | 8 | ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"]) |
| 9 | +GITHUB_OUTPUT = Path(os.environ["GITHUB_OUTPUT"]) |
8 | 10 | ENV_PATH = ACTION_PATH / ".black-env" |
9 | 11 | ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin") |
10 | 12 | OPTIONS = os.getenv("INPUT_OPTIONS", default="") |
|
13 | 15 | BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="") |
14 | 16 | VERSION = os.getenv("INPUT_VERSION", default="") |
15 | 17 |
|
| 18 | +_is_formatted_re = r"\s?(?P<changed_files>[0-9]+)\sfiles?\sreformatted(\.|,)\s?" |
| 19 | + |
| 20 | +_outputs = {"is_formatted": "0", "changed_files": "0"} |
| 21 | + |
16 | 22 | run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True) |
17 | 23 |
|
18 | 24 | version_specifier = VERSION |
|
38 | 44 | base_cmd = [str(ENV_BIN / "black")] |
39 | 45 | if BLACK_ARGS: |
40 | 46 | # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS. |
41 | | - proc = run([*base_cmd, *shlex.split(BLACK_ARGS)]) |
| 47 | + proc = run([*base_cmd, *shlex.split(BLACK_ARGS)], stdout=PIPE, stderr=STDOUT) |
42 | 48 | else: |
43 | | - proc = run([*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)]) |
| 49 | + proc = run( |
| 50 | + [*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)], |
| 51 | + stdout=PIPE, |
| 52 | + stderr=STDOUT, |
| 53 | + ) |
| 54 | + |
| 55 | +_output = proc.stdout.decode("utf-8") |
| 56 | + |
| 57 | +matches = search(_is_formatted_re, _output, MULTILINE) |
| 58 | +if matches: |
| 59 | + _outputs["is_formatted"] = "1" |
| 60 | + _outputs["changed_files"] = str(matches.group("changed_files")) |
| 61 | + |
| 62 | +with GITHUB_OUTPUT.open("a+", encoding="utf-8") as f: |
| 63 | + for k, v in _outputs.items(): |
| 64 | + f.write(f"{k}={v}\n") |
44 | 65 |
|
45 | 66 | sys.exit(proc.returncode) |
0 commit comments