]> 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:

Fix a magical comment caused internal error (#3740)
[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 if version_specifier:
26     req = f"black{extra_deps}{version_specifier}"
27 else:
28     describe_name = ""
29     with open(ACTION_PATH / ".git_archival.txt", encoding="utf-8") as fp:
30         for line in fp:
31             if line.startswith("describe-name: "):
32                 describe_name = line[len("describe-name: ") :].rstrip()
33                 break
34     if not describe_name:
35         print("::error::Failed to detect action version.", file=sys.stderr, flush=True)
36         sys.exit(1)
37     # expected format is one of:
38     # - 23.1.0
39     # - 23.1.0-51-g448bba7
40     if describe_name.count("-") < 2:
41         # the action's commit matches a tag exactly, install exact version from PyPI
42         req = f"black{extra_deps}=={describe_name}"
43     else:
44         # the action's commit does not match any tag, install from the local git repo
45         req = f".{extra_deps}"
46 print(f"Installing {req}...", flush=True)
47 pip_proc = run(
48     [str(ENV_BIN / "python"), "-m", "pip", "install", req],
49     stdout=PIPE,
50     stderr=STDOUT,
51     encoding="utf-8",
52     cwd=ACTION_PATH,
53 )
54 if pip_proc.returncode:
55     print(pip_proc.stdout)
56     print("::error::Failed to install Black.", file=sys.stderr, flush=True)
57     sys.exit(pip_proc.returncode)
58
59
60 base_cmd = [str(ENV_BIN / "black")]
61 if BLACK_ARGS:
62     # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS.
63     proc = run(
64         [*base_cmd, *shlex.split(BLACK_ARGS)],
65         stdout=PIPE,
66         stderr=STDOUT,
67         encoding="utf-8",
68     )
69 else:
70     proc = run(
71         [*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)],
72         stdout=PIPE,
73         stderr=STDOUT,
74         encoding="utf-8",
75     )
76 print(proc.stdout)
77 sys.exit(proc.returncode)