]> git.madduck.net Git - etc/vim.git/blob - action/main.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Vim plugin: allow using system black rather than virtualenv (#3309)
[etc/vim.git] / action / main.py
1 import os
2 import shlex
3 import sys
4 from pathlib import Path
5 from subprocess import PIPE, STDOUT, run
6
7 ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"])
8 ENV_PATH = ACTION_PATH / ".black-env"
9 ENV_BIN = ENV_PATH / ("Scripts" if sys.platform == "win32" else "bin")
10 OPTIONS = os.getenv("INPUT_OPTIONS", default="")
11 SRC = os.getenv("INPUT_SRC", default="")
12 JUPYTER = os.getenv("INPUT_JUPYTER") == "true"
13 BLACK_ARGS = os.getenv("INPUT_BLACK_ARGS", default="")
14 VERSION = os.getenv("INPUT_VERSION", default="")
15
16 run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True)
17
18 version_specifier = VERSION
19 if VERSION and VERSION[0] in "0123456789":
20     version_specifier = f"=={VERSION}"
21 if JUPYTER:
22     extra_deps = "[colorama,jupyter]"
23 else:
24     extra_deps = "[colorama]"
25 req = f"black{extra_deps}{version_specifier}"
26 pip_proc = run(
27     [str(ENV_BIN / "python"), "-m", "pip", "install", req],
28     stdout=PIPE,
29     stderr=STDOUT,
30     encoding="utf-8",
31 )
32 if pip_proc.returncode:
33     print(pip_proc.stdout)
34     print("::error::Failed to install Black.", flush=True)
35     sys.exit(pip_proc.returncode)
36
37
38 base_cmd = [str(ENV_BIN / "black")]
39 if BLACK_ARGS:
40     # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS.
41     proc = run([*base_cmd, *shlex.split(BLACK_ARGS)])
42 else:
43     proc = run([*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)])
44
45 sys.exit(proc.returncode)