| 1 |
import os |
| 2 |
import re |
| 3 |
import shlex |
| 4 |
import shutil |
| 5 |
import sys |
| 6 |
from pathlib import Path |
| 7 |
from subprocess import PIPE, STDOUT, run |
| 8 |
|
| 9 |
ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"]) |
| 10 |
ENV_PATH = ACTION_PATH / ".black-env" |
| 11 |
ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin") |
| 12 |
OPTIONS = os.getenv("INPUT_OPTIONS", default="") |
| 13 |
SRC = os.getenv("INPUT_SRC", default="") |
| 14 |
JUPYTER = os.getenv("INPUT_JUPYTER") == "true" |
| 15 |
BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="") |
| 16 |
VERSION = os.getenv("INPUT_VERSION", default="") |
| 17 |
USE_PYPROJECT = os.getenv("INPUT_USE_PYPROJECT") == "true" |
| 18 |
OUTPUT_FILE = os.getenv("OUTPUT_FILE", default="") |
| 19 |
|
| 20 |
BLACK_VERSION_RE = re.compile( |
| 21 |
r"^black((?:\s*(?:~=|==|!=|<=|>=|<|>|===)\s*[A-Za-z0-9*+._-]+)" |
| 22 |
r"(?:\s*,\s*(?:~=|==|!=|<=|>=|<|>|===)\s*[A-Za-z0-9*+._-]+)*)\s*$", |
| 23 |
re.IGNORECASE, |
| 24 |
) |
| 25 |
EXTRAS_RE = re.compile(r"\[.*\]") |
| 26 |
EXPORT_SUBST_FAIL_RE = re.compile(r"\$Format:.*\$") |
| 27 |
|
| 28 |
|
| 29 |
def determine_version_specifier() -> str: |
| 30 |
"""Determine the version of Black to install. |
| 31 |
|
| 32 |
The version can be specified either via the `with.version` input or via the |
| 33 |
pyproject.toml file if `with.use_pyproject` is set to `true`. |
| 34 |
""" |
| 35 |
if USE_PYPROJECT and VERSION: |
| 36 |
print( |
| 37 |
"::error::'with.version' and 'with.use_pyproject' inputs are " |
| 38 |
"mutually exclusive.", |
| 39 |
file=sys.stderr, |
| 40 |
flush=True, |
| 41 |
) |
| 42 |
sys.exit(1) |
| 43 |
if USE_PYPROJECT: |
| 44 |
return read_version_specifier_from_pyproject() |
| 45 |
elif VERSION and VERSION[0] in "0123456789": |
| 46 |
return f"=={VERSION}" |
| 47 |
else: |
| 48 |
return VERSION |
| 49 |
|
| 50 |
|
| 51 |
def read_version_specifier_from_pyproject() -> str: |
| 52 |
if sys.version_info < (3, 11): |
| 53 |
print( |
| 54 |
"::error::'with.use_pyproject' input requires Python 3.11 or later.", |
| 55 |
file=sys.stderr, |
| 56 |
flush=True, |
| 57 |
) |
| 58 |
sys.exit(1) |
| 59 |
|
| 60 |
import tomllib # type: ignore[import-not-found,unreachable] |
| 61 |
|
| 62 |
try: |
| 63 |
with Path("pyproject.toml").open("rb") as fp: |
| 64 |
pyproject = tomllib.load(fp) |
| 65 |
except FileNotFoundError: |
| 66 |
print( |
| 67 |
"::error::'with.use_pyproject' input requires a pyproject.toml file.", |
| 68 |
file=sys.stderr, |
| 69 |
flush=True, |
| 70 |
) |
| 71 |
sys.exit(1) |
| 72 |
|
| 73 |
version = pyproject.get("tool", {}).get("black", {}).get("required-version") |
| 74 |
if version is not None: |
| 75 |
# Match the two supported usages of `required-version`: |
| 76 |
if "." in version: |
| 77 |
return f"=={version}" |
| 78 |
else: |
| 79 |
return f"~={version}.0" |
| 80 |
|
| 81 |
arrays = [ |
| 82 |
*pyproject.get("dependency-groups", {}).values(), |
| 83 |
pyproject.get("project", {}).get("dependencies"), |
| 84 |
*pyproject.get("project", {}).get("optional-dependencies", {}).values(), |
| 85 |
] |
| 86 |
for array in arrays: |
| 87 |
version = find_black_version_in_array(array) |
| 88 |
if version is not None: |
| 89 |
break |
| 90 |
|
| 91 |
if version is None: |
| 92 |
print( |
| 93 |
"::error::'black' dependency missing from pyproject.toml.", |
| 94 |
file=sys.stderr, |
| 95 |
flush=True, |
| 96 |
) |
| 97 |
sys.exit(1) |
| 98 |
|
| 99 |
return version |
| 100 |
|
| 101 |
|
| 102 |
def find_black_version_in_array(array: object) -> str | None: |
| 103 |
if not isinstance(array, list): |
| 104 |
return None |
| 105 |
try: |
| 106 |
for item in array: |
| 107 |
# Rudimentary PEP 508 parsing. |
| 108 |
item = item.split(";")[0] |
| 109 |
item = EXTRAS_RE.sub("", item).strip() |
| 110 |
if item == "black": |
| 111 |
print( |
| 112 |
"::error::Version specifier missing for 'black' dependency in " |
| 113 |
"pyproject.toml.", |
| 114 |
file=sys.stderr, |
| 115 |
flush=True, |
| 116 |
) |
| 117 |
sys.exit(1) |
| 118 |
elif m := BLACK_VERSION_RE.match(item): |
| 119 |
return m.group(1).strip() |
| 120 |
except TypeError: |
| 121 |
pass |
| 122 |
|
| 123 |
return None |
| 124 |
|
| 125 |
|
| 126 |
run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True) |
| 127 |
|
| 128 |
version_specifier = determine_version_specifier() |
| 129 |
if JUPYTER: |
| 130 |
extra_deps = "[colorama,jupyter]" |
| 131 |
else: |
| 132 |
extra_deps = "[colorama]" |
| 133 |
if version_specifier: |
| 134 |
req = f"black{extra_deps}{version_specifier}" |
| 135 |
else: |
| 136 |
describe_name = "" |
| 137 |
with open(ACTION_PATH / ".git_archival.txt", encoding="utf-8") as fp: |
| 138 |
for line in fp: |
| 139 |
if line.startswith("describe-name: "): |
| 140 |
describe_name = line[len("describe-name: ") :].rstrip() |
| 141 |
break |
| 142 |
if not describe_name: |
| 143 |
print("::error::Failed to detect action version.", file=sys.stderr, flush=True) |
| 144 |
sys.exit(1) |
| 145 |
# expected format is one of: |
| 146 |
# - 23.1.0 |
| 147 |
# - 23.1.0-51-g448bba7 |
| 148 |
# - $Format:%(describe:tags=true,match=*[0-9]*)$ (if export-subst fails) |
| 149 |
if ( |
| 150 |
describe_name.count("-") < 2 |
| 151 |
and EXPORT_SUBST_FAIL_RE.match(describe_name) is None |
| 152 |
): |
| 153 |
# the action's commit matches a tag exactly, install exact version from PyPI |
| 154 |
req = f"black{extra_deps}=={describe_name}" |
| 155 |
else: |
| 156 |
# the action's commit does not match any tag, install from the local git repo |
| 157 |
req = f".{extra_deps}" |
| 158 |
print(f"Installing {req}...", flush=True) |
| 159 |
pip_proc = run( |
| 160 |
[str(ENV_BIN / "python"), "-m", "pip", "install", req], |
| 161 |
stdout=PIPE, |
| 162 |
stderr=STDOUT, |
| 163 |
encoding="utf-8", |
| 164 |
cwd=ACTION_PATH, |
| 165 |
) |
| 166 |
if pip_proc.returncode: |
| 167 |
print(pip_proc.stdout) |
| 168 |
print("::error::Failed to install Black.", file=sys.stderr, flush=True) |
| 169 |
sys.exit(pip_proc.returncode) |
| 170 |
|
| 171 |
|
| 172 |
base_cmd = [str(ENV_BIN / "black")] |
| 173 |
if BLACK_ARGS: |
| 174 |
# TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS. |
| 175 |
proc = run( |
| 176 |
[*base_cmd, *shlex.split(BLACK_ARGS)], |
| 177 |
stdout=PIPE, |
| 178 |
stderr=STDOUT, |
| 179 |
encoding="utf-8", |
| 180 |
) |
| 181 |
else: |
| 182 |
proc = run( |
| 183 |
[*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)], |
| 184 |
stdout=PIPE, |
| 185 |
stderr=STDOUT, |
| 186 |
encoding="utf-8", |
| 187 |
) |
| 188 |
shutil.rmtree(ENV_PATH, ignore_errors=True) |
| 189 |
|
| 190 |
# Write output to file if specified |
| 191 |
if OUTPUT_FILE: |
| 192 |
try: |
| 193 |
with open(OUTPUT_FILE, "w", encoding="utf-8") as f: |
| 194 |
f.write(proc.stdout) |
| 195 |
print(f"Black output written to {OUTPUT_FILE}") |
| 196 |
except Exception as e: |
| 197 |
print(f"::error::Failed to write output to {OUTPUT_FILE}: {e}", file=sys.stderr) |
| 198 |
sys.exit(1) |
| 199 |
|
| 200 |
print(proc.stdout) |
| 201 |
sys.exit(proc.returncode) |