Repository navigation
Expand file tree
/
Copy pathversions.py
More file actions
120 lines (103 loc) · 5.06 KB
/
Copy pathversions.py
File metadata and controls
120 lines (103 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
Collect the versions of all installed compilers and interpreters.
python3 versions.py > versions.json
The Dockerfile runs this at build time; server.py serves the result as the
`versions` service. Keys are the names of the @LIA.<key> shortcuts in README.md.
'''
import json
import re
import subprocess
from concurrent.futures import ThreadPoolExecutor
# key: (name, command), the version is the first "1.2.3"-like token of the output,
# or the first plain number if there is none (Erlang/OTP "24", Io "20260302")
LANGUAGES = {
"ada": ("Ada (GNAT)", "gnatmake --version"),
"algol": ("ALGOL 68 Genie", "a68g --version"),
"apl": ("Dyalog APL", "echo \"⎕←'#'⎕WG'APLVersion'\" > /tmp/v.apl && dyalog -script /tmp/v.apl"),
"awk": ("GNU Awk", "awk --version"),
"basic": ("Bywater BASIC", "dpkg-query -W -f='${Version}' bwbasic"),
"c": ("C (GCC)", "gcc --version"),
"clojure": ("Clojure CLI", "clojure --version"),
"cobol": ("GnuCOBOL", "cobc --version"),
"coq": ("Coq", "coqc --version"),
"cpp": ("C++ (G++)", "g++ --version"),
"d": ("D (GDC)", "gdc --version"),
"delphi": ("Free Pascal", "fpc -iV"),
"dotnet": (".NET SDK", "dotnet --version"),
"elixir": ("Elixir", "elixir --version | grep Elixir"),
"erlang": ("Erlang/OTP", "erl -noshell -eval 'io:format(\"~s\", [erlang:system_info(otp_release)]), halt().'"),
"forth": ("Gforth", "gforth --version"),
"fortran": ("GNU Fortran", "gfortran --version"),
"fsharp": ("F# (.NET SDK)", "dotnet --version"),
"gleam": ("Gleam", "gleam --version"),
"go": ("Go", "go version"),
"groovy": ("Groovy", "groovy --version"),
"haskell": ("GHC", "ghc --version"),
"haxe": ("Haxe", "haxe --version"),
"inform": ("Inform", "dpkg-query -W -f='${Version}' inform6-compiler"),
"io": ("Io", "io --version"),
"java": ("Java (JDK)", "java -version"),
"julia": ("Julia", "julia --version"),
"kotlin": ("Kotlin", "kotlinc -version"),
"lua": ("Lua", "lua -v"),
"modula2": ("GNU Modula-2", "gm2 --version"),
"mono": ("C# (Mono)", "mono --version"),
"nasm": ("NASM", "nasm -v"),
"nim": ("Nim", "nim --version"),
"nodejs": ("Node.js", "node --version"),
"objectivec": ("Objective-C (GCC)", "gcc --version"),
"ocaml": ("OCaml", "ocaml -version"),
"octave": ("GNU Octave", "octave --version"),
"pascal": ("Free Pascal", "fpc -iV"),
"perl": ("Perl", "perl -e 'print $^V'"),
"php": ("PHP", "php --version"),
"postscript": ("Ghostscript", "gs --version"),
"prolog": ("SWI-Prolog", "swipl --version"),
"python2": ("Python 2", "python2.7 --version"),
"python3": ("Python 3", "python3 --version"),
"qsharp": ("Q# (QDK)", "python3 -c \"from importlib.metadata import version; print(version('qdk'))\""),
"r": ("R", "Rscript --version"),
"racket": ("Racket", "racket --version"),
"rexx": ("Regina REXX", "rexx -v"),
"ruby": ("Ruby", "ruby --version"),
"rust": ("Rust", "rustc --version"),
"scala": ("Scala", "scala -version"),
"scheme": ("GNU Guile", "guile --version"),
"selectscript": ("SelectScript", "true"), # S2c has no version option, it starts a REPL
"sml": ("Standard ML of New Jersey", "dpkg-query -W -f='${Version}' smlnj"),
"smalltalk": ("GNU Smalltalk", "gst --version"),
"solidity": ("Solidity (solc)", "solc --version | tail -n 1"),
"tcl": ("Tcl", "echo 'puts [info patchlevel]' | tclsh"),
"v": ("V", "v version"),
"vala": ("Vala", "valac --version"),
"verilog": ("Icarus Verilog", "iverilog -V"),
"vhdl": ("GHDL", "ghdl --version"),
"zig": ("Zig", "zig version"),
}
VERSION = re.compile(r"\d+(?:\.\d+)+")
NUMBER = re.compile(r"\d+")
SHELL_ERROR = re.compile(r"^(/bin/)?sh: \d+: ")
def probe(name: str, cmd: str) -> dict:
'''Run one version command, never fail: a missing tool gives version None.'''
try:
out = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=60)
raw = (out.stdout + out.stderr).strip()
except Exception as error:
return {"name": name, "version": None, "raw": str(error)[:200]}
# 127: the shell did not find the command, i.e. the tool is not installed
if out.returncode == 127:
return {"name": name, "version": None, "raw": "not installed"}
# Drop errors of the shell itself ("sh: 1: tool: not found", e.g. in a pipe
# whose last command succeeded), their line number would be taken as version.
raw = "\n".join(line for line in raw.splitlines() if not SHELL_ERROR.match(line)).strip()
first = raw.splitlines()[0].strip() if raw else ""
match = VERSION.search(raw) or NUMBER.search(raw)
return {"name": name, "version": match.group(0) if match else None, "raw": first[:200]}
def collect() -> dict:
with ThreadPoolExecutor(max_workers=8) as pool:
futures = {key: pool.submit(probe, *entry) for key, entry in LANGUAGES.items()}
versions = {key: future.result() for key, future in futures.items()}
versions["python"] = versions["python3"]
return versions
if __name__ == "__main__":
print(json.dumps(collect(), indent=2, ensure_ascii=False))