-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathhost.py
More file actions
152 lines (136 loc) · 5.09 KB
/
host.py
File metadata and controls
152 lines (136 loc) · 5.09 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Run this file with `python host.py` to start this server to show information to Mr. Mortensen!
Make sure to first initialize `venv` and install necessary dependencies by running `./scripts/venv.sh`.
This file retrieves the following information:
- Operating system
- XCode developer tools installed (MacOS only)
- Checks if the following are installed, and their versions
- XCode developer tools (MacOS only)
- Homebrew (MacOS only)
- Git
- Python
- Pip
- Ruby
- Bundler
- RubyGems
- Jupyter
- Stored GitHub username/email
"""
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_restful import Api, Resource
import subprocess
import platform
import shutil
import os
import re
app = Flask(__name__)
CORS(app, supports_credentials=True, origins=[
"https://pages.opencodingsociety.com",
"http://localhost:4100",
"http://127.0.0.1:4100",
"http://localhost:4500", # ← Jekyll default port
"http://127.0.0.1:4500",
"http://localhost:4200", # ← common fallback
"http://127.0.0.1:4200",
"null", # ← for when the .ipynb is opened directly as a file
])
api = Api(app)
# --- API Resource ---
class HostAPI(Resource):
def get(self):
"""Return a structured JSON object with parsed versions and raw outputs.
For most developer tools we parse version number and also return the raw output. For a few commands (like Jupyter and git config values) we keep the full string as the primary value.
"""
def run_cmd(cmd, timeout=10):
try:
proc = subprocess.run(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=timeout,
)
stdout = proc.stdout.strip()
stderr = proc.stderr.strip()
combined = "\n".join([s for s in (stdout, stderr) if s])
return {
"cmd": cmd,
"returncode": proc.returncode,
"stdout": stdout,
"stderr": stderr,
"combined": combined,
}
except subprocess.TimeoutExpired:
return {"cmd": cmd, "error": "timeout", "returncode": None}
except Exception as e:
return {"cmd": cmd, "error": str(e), "returncode": None}
def parse_version(text):
if not text:
return None
# Find first semver-like token (e.g. 3, 3.1, 3.1.4)
m = re.search(r"(\d+(?:\.\d+){0,3})", text)
return m.group(1) if m else None
# Info about the person's system, also some other random stuff
host_data = {
"OS": platform.system(),
"OS Release": platform.release(),
"OS Version": platform.version(),
"Architecture": platform.machine(),
"Python Executable Path": os.path.abspath(shutil.which('python') or ''),
"checks": {},
}
# third param is a mode, basically if raw means return full string and version gets the first one
check_cmds = [
("python", "python --version", "version"),
("pip", "pip --version", "version"),
("ruby", "ruby -v", "version"),
("bundler", "bundle -v", "version"),
("gem", "gem -v", "version"),
("jupyter", "jupyter --version", "raw"),
("jupyter_kernelspecs", "jupyter kernelspec list", "raw"),
("git", "git --version", "version"),
("git_user_name", "git config --global user.name", "raw"),
("git_user_email", "git config --global user.email", "raw"),
("brew", "which brew && brew --version", "version"),
("xcode_select", "xcode-select -p", "raw"),
("uname", "uname -a", "raw"),
]
for name, cmd, mode in check_cmds:
# Skip macos-only checks if not on a mac
if (name == "brew" or name == "xcode_select") and host_data["OS"] != "Darwin":
continue
res = run_cmd(cmd)
installed = res.get("returncode") == 0 and bool(res.get("combined"))
raw = res.get("combined") or ""
if mode == "raw":
host_data[name] = {
"installed": installed,
"value": raw,
}
else:
version = parse_version(raw)
host_data[name] = {
"installed": installed,
"version": version,
"raw": raw,
}
return jsonify(host_data)
api.add_resource(HostAPI, '/api/host')
# Wee can use @app.route for HTML endpoints, this will be style for Admin UI
@app.route('/')
def say_hello():
html_content = """
<html>
<head>
<title>Hello</title>
</head>
<body>
<h2>Hello, World!</h2>
</body>
</html>
"""
return html_content
if __name__ == '__main__':
app.run(port=6741, debug=True)