|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import concurrent.futures |
| 6 | +import json |
| 7 | +import os.path |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | + |
| 11 | +EXCLUDED = frozenset(( |
| 12 | + ('windows-latest', 'docker'), |
| 13 | + ('windows-latest', 'docker_image'), |
| 14 | + ('windows-latest', 'lua'), |
| 15 | + ('windows-latest', 'swift'), |
| 16 | +)) |
| 17 | + |
| 18 | + |
| 19 | +def _lang_files(lang: str) -> frozenset[str]: |
| 20 | + prog = f'''\ |
| 21 | +import json |
| 22 | +import os.path |
| 23 | +import sys |
| 24 | +
|
| 25 | +import pre_commit.languages.{lang} |
| 26 | +import tests.languages.{lang}_test |
| 27 | +
|
| 28 | +modules = sorted( |
| 29 | + os.path.relpath(v.__file__) |
| 30 | + for k, v in sys.modules.items() |
| 31 | + if k.startswith(('pre_commit.', 'tests.', 'testing.')) |
| 32 | +) |
| 33 | +print(json.dumps(modules)) |
| 34 | +''' |
| 35 | + out = json.loads(subprocess.check_output((sys.executable, '-c', prog))) |
| 36 | + return frozenset(out) |
| 37 | + |
| 38 | + |
| 39 | +def main() -> int: |
| 40 | + parser = argparse.ArgumentParser() |
| 41 | + parser.add_argument('--all', action='store_true') |
| 42 | + args = parser.parse_args() |
| 43 | + |
| 44 | + langs = [ |
| 45 | + os.path.splitext(fname)[0] |
| 46 | + for fname in sorted(os.listdir('pre_commit/languages')) |
| 47 | + if fname.endswith('.py') and fname != '__init__.py' |
| 48 | + ] |
| 49 | + |
| 50 | + if not args.all: |
| 51 | + with concurrent.futures.ThreadPoolExecutor(os.cpu_count()) as exe: |
| 52 | + by_lang = { |
| 53 | + lang: files |
| 54 | + for lang, files in zip(langs, exe.map(_lang_files, langs)) |
| 55 | + } |
| 56 | + |
| 57 | + diff_cmd = ('git', 'diff', '--name-only', 'origin/main...HEAD') |
| 58 | + files = set(subprocess.check_output(diff_cmd).decode().splitlines()) |
| 59 | + |
| 60 | + langs = [ |
| 61 | + lang |
| 62 | + for lang, lang_files in by_lang.items() |
| 63 | + if lang_files & files |
| 64 | + ] |
| 65 | + |
| 66 | + matched = [ |
| 67 | + {'os': os, 'language': lang} |
| 68 | + for os in ('windows-latest', 'ubuntu-latest') |
| 69 | + for lang in langs |
| 70 | + if (os, lang) not in EXCLUDED |
| 71 | + ] |
| 72 | + |
| 73 | + with open(os.environ['GITHUB_OUTPUT'], 'a') as f: |
| 74 | + f.write(f'languages={json.dumps(matched)}\n') |
| 75 | + return 0 |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + raise SystemExit(main()) |
0 commit comments