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

Standardise newlines after module-level docstrings (#3932)
[etc/vim.git] / action / main.py
1 import os
2 import shlex
3 import shutil
4 import sys
5 from pathlib import Path
6 from subprocess import PIPE, STDOUT, run
7
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="")
16
17 run([sys.executable, "-m", "venv", str(ENV_PATH)], check=True)
18
19 version_specifier = VERSION
20 if VERSION and VERSION[0] in "0123456789":
21     version_specifier = f"=={VERSION}"
22 if JUPYTER:
23     extra_deps = "[colorama,jupyter]"
24 else:
25     extra_deps = "[colorama]"
26 if version_specifier:
27     req = f"black{extra_deps}{version_specifier}"
28 else:
29     describe_name = ""
30     with open(ACTION_PATH / ".git_archival.txt", encoding="utf-8") as fp:
31         for line in fp:
32             if line.startswith("describe-name: "):
33                 describe_name = line[len("describe-name: ") :].rstrip()
34                 break
35     if not describe_name:
36         print("::error::Failed to detect action version.", file=sys.stderr, flush=True)
37         sys.exit(1)
38     # expected format is one of:
39     # - 23.1.0
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}"
44     else:
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)
48 pip_proc = run(
49     [str(ENV_BIN / "python"), "-m", "pip", "install", req],
50     stdout=PIPE,
51     stderr=STDOUT,
52     encoding="utf-8",
53     cwd=ACTION_PATH,
54 )
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)
59
60
61 base_cmd = [str(ENV_BIN / "black")]
62 if BLACK_ARGS:
63     # TODO: remove after a while since this is deprecated in favour of SRC + OPTIONS.
64     proc = run(
65         [*base_cmd, *shlex.split(BLACK_ARGS)],
66         stdout=PIPE,
67         stderr=STDOUT,
68         encoding="utf-8",
69     )
70 else:
71     proc = run(
72         [*base_cmd, *shlex.split(OPTIONS), *shlex.split(SRC)],
73         stdout=PIPE,
74         stderr=STDOUT,
75         encoding="utf-8",
76     )
77 shutil.rmtree(ENV_PATH, ignore_errors=True)
78 print(proc.stdout)
79 sys.exit(proc.returncode)