All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
5 from pathlib import Path
6 from subprocess import PIPE, STDOUT, run
8 ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"])
9 ENV_PATH = ACTION_PATH / ".black-env"
10 ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin")
11 OPTIONS = os.getenv("INPUT_OPTIONS", default="")
12 SRC = os.getenv("INPUT_SRC", default="")
13 JUPYTER = os.getenv("INPUT_JUPYTER") == "true"
14 BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="")
15 VERSION = os.getenv("INPUT_VERSION", default="")
17 run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True)
19 version_specifier = VERSION
20 if VERSION and VERSION[0] in "0123456789":
21 version_specifier = f"=={VERSION}"
23 extra_deps = "[colorama,jupyter]"
25 extra_deps = "[colorama]"
27 req = f"black{extra_deps}{version_specifier}"
30 with open(ACTION_PATH / ".git_archival.txt", encoding="utf-8") as fp:
32 if line.startswith("describe-name: "):
33 describe_name = line[len("describe-name: ") :].rstrip()
36 print("::error::Failed to detect action version.", file=sys.stderr, flush=True)
38 # expected format is one of:
40 # - 23.1.0-51-g448bba7
41 if describe_name.count("-") < 2:
42 # the action's commit matches a tag exactly, install exact version from PyPI
43 req = f"black{extra_deps}=={describe_name}"
45 # the action's commit does not match any tag, install from the local git repo
46 req = f".{extra_deps}"
47 print(f"Installing {req}...", flush=True)
49 [str(ENV_BIN / "python"), "-m", "pip", "install", req],
55 if pip_proc.returncode:
56 print(pip_proc.stdout)
57 print("::error::Failed to install Black.", file=sys.stderr, flush=True)
58 sys.exit(pip_proc.returncode)
61 base_cmd = [str(ENV_BIN / "black")]
63 # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS.
65 [*base_cmd, *shlex.split(BLACK_ARGS)],
72 [*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)],
77 shutil.rmtree(ENV_PATH, ignore_errors=True)
79 sys.exit(proc.returncode)