-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (114 loc) · 4.49 KB
/
Copy pathapp.py
File metadata and controls
141 lines (114 loc) · 4.49 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
from flask import Flask, render_template, request, jsonify, send_from_directory
import subprocess
import re
import os
import json
import time
app = Flask(__name__)
PKGDB_PATH = os.path.join(os.path.dirname(__file__), "static", "pkgdb.json")
# ─── helpers ───────────────────────────────────────────
def run(cmd):
return subprocess.getoutput(cmd)
def get_packages():
"""Returns sorted list of {name, version, size}"""
raw = run("dpkg-query -W -f='${Package}\t${Version}\t${Installed-Size}\n' 2>/dev/null")
pkgs = []
for line in raw.splitlines():
parts = line.split('\t')
if len(parts) >= 3:
name = parts[0].strip()
version = parts[1].strip() or "unknown"
try:
size_kb = int(parts[2].strip())
size = f"{size_kb // 1024} MB" if size_kb >= 1024 else f"{size_kb} KB"
except:
size = "?"
pkgs.append({"name": name, "version": version, "size": size})
# fallback اگه dpkg-query نبود
if not pkgs:
raw2 = run("pkg list-installed 2>/dev/null")
for line in raw2.splitlines():
if "/" in line:
parts = line.split("/")
name = parts[0].strip()
ver = parts[1].strip().split(" ")[0] if len(parts) > 1 else "?"
pkgs.append({"name": name, "version": ver, "size": "?"})
return sorted(pkgs, key=lambda x: x["name"])
def system_info():
cpu = run("nproc").strip()
try:
mem = run("free -m").splitlines()[1].split()
ram_total = round(int(mem[1]) / 1024, 2)
ram_used = round(int(mem[2]) / 1024, 2)
ram_pct = int((ram_used / ram_total) * 100) if ram_total else 0
except:
ram_total = ram_used = ram_pct = 0
try:
disk = run("df -h /data").splitlines()[1].split()
storage = disk[1]
used = disk[2]
disk_pct = int(disk[4].replace('%', ''))
except:
storage = used = "?"
disk_pct = 0
uptime = run("uptime -p").strip().replace("up ", "")
return {
"cpu": cpu,
"ram_total": ram_total, "ram_used": ram_used, "ram_pct": ram_pct,
"storage": storage, "used": used, "disk_pct": disk_pct,
"uptime": uptime,
}
def pkgdb_meta():
"""Returns metadata of local pkgdb (count, updated_at) or None."""
if not os.path.exists(PKGDB_PATH):
return None
try:
with open(PKGDB_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
ts = data.get("updated_at", 0)
updated = time.strftime("%Y-%m-%d", time.localtime(ts)) if ts else "?"
return {"count": data.get("count", 0), "updated": updated}
except:
return None
# ─── routes ────────────────────────────────────────────
@app.route("/")
def home():
return render_template(
"index.html",
pkgs=get_packages(),
sys=system_info(),
pkgdb=pkgdb_meta(),
)
@app.route("/pkgdb.json")
def serve_pkgdb():
"""فایل pkgdb.json رو از پوشه static سرو میکنه"""
if not os.path.exists(PKGDB_PATH):
return jsonify({"error": "pkgdb not found. run: python update_pkgdb.py"}), 404
return send_from_directory(
os.path.join(os.path.dirname(__file__), "static"),
"pkgdb.json",
mimetype="application/json"
)
@app.route("/remove", methods=["POST"])
def remove():
pkg = request.json.get("pkg", "").strip()
if not pkg or not re.match(r'^[a-zA-Z0-9_\-\+\.]+$', pkg):
return jsonify({"ok": False, "error": "invalid package name"})
run(f"pkg uninstall -y {pkg}")
return jsonify({"ok": True})
@app.route("/update", methods=["POST"])
def update():
pkg = request.json.get("pkg", "").strip()
if not pkg or not re.match(r'^[a-zA-Z0-9_\-\+\.]+$', pkg):
return jsonify({"ok": False, "error": "invalid package name"})
run(f"pkg upgrade -y {pkg}")
return jsonify({"ok": True})
@app.route("/install", methods=["POST"])
def install():
pkg = request.json.get("pkg", "").strip()
if not pkg or not re.match(r'^[a-zA-Z0-9_\-\+\.]+$', pkg):
return jsonify({"ok": False, "error": "invalid package name"})
out = run(f"pkg install -y {pkg}")
return jsonify({"ok": True, "output": out[-300:]})
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8080, debug=True)