-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeofetch.py
executable file
·377 lines (349 loc) · 10.7 KB
/
geofetch.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#! /usr/bin/python3
import argparse
import ctypes
import os
import socket
import psutil
import platform
import subprocess
import re
import requests
if os.name.lower() == "posix":
import distro
if os.name == "nt":
import wmi
# Colors
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# Storage units
storageUnits = dict()
storageUnits["bytes"] = 1
storageUnits["KiB"] = 1024
storageUnits["MiB"] = 1024 ** 2
storageUnits["GiB"] = 1024 ** 3
storageUnits["TiB"] = 1024 ** 4
def pickStorageUnit(bytes: int) -> str:
pickedStorageUnitName = "bytes"
for storageUnitName in storageUnits.keys():
storageUnitInBytes = storageUnits[storageUnitName]
if bytes / storageUnitInBytes >= 1:
pickedStorageUnitName = storageUnitName
return pickedStorageUnitName
# Argument parser
parser = argparse.ArgumentParser(
prog='geofetch',
description='geofetch lets you get some info about your system, just like neofetch. Usually it outputs only general information, but you can optionally display more info.',
epilog='The hide option has more priority than the show option'
)
# It shouldn't be that hard to get CPU info...
def get_cpu_info():
if os.name == 'nt':
return platform.processor()
else:
try:
with open('/proc/cpuinfo', 'r') as f:
for line in f:
if line.strip().startswith('model name'):
return line.split(':')[1].strip()
except FileNotFoundError:
pass
try:
output = subprocess.check_output(['getprop', 'ro.soc.manufacturer'], stderr=subprocess.DEVNULL)
output2 = subprocess.check_output(['getprop', 'ro.soc.model'], stderr=subprocess.DEVNULL)
return output.strip().decode('utf-8', errors='ignore') + " " + output2.strip().decode('utf-8', errors='ignore')
except subprocess.CalledProcessError:
pass
return "Unknown"
def get_advertised_cpu_speed():
if os.name == 'nt':
# Windows method 1
libc = ctypes.windll.kernel32
freq = ctypes.c_uint64(0)
ret = libc.QueryPerformanceFrequency(ctypes.byref(freq))
if ret:
return f"{freq.value / 1000000:.2f} MHz"
return "Unknown"
# Linux method 1
try:
with open('/proc/cpuinfo', 'r') as f:
for line in f:
if line.strip().startswith('cpu MHz'):
return f"{float(line.split(':')[1].strip()):.2f} MHz"
except FileNotFoundError:
pass
# Linux method 2
try:
with open('/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq', 'r') as f:
return f"{int(f.read()) / 1000:.2f} MHz"
except FileNotFoundError:
pass
# Darwin method 1
libc = ctypes.CDLL(None)
buf = ctypes.create_string_buffer(128)
ret = libc.sysctlbyname(b'hw.cpufrequency_max', buf, ctypes.byref(ctypes.c_size_t(128)), None, 0)
if ret == 0:
max_freq = int.from_bytes(buf.raw, byteorder='little')
return f"{max_freq / 1000000:.2f} MHz"
return "Unknown"
# Setup arg parser
parser.add_argument(
'--show-username',
action='store_true',
help="Show username (default)"
)
parser.add_argument(
'--hide-username',
action='store_true',
help="Don't show username, overrides --show-username"
)
parser.add_argument(
'--show-hostname',
action='store_true',
help="Show hostname (default)"
)
parser.add_argument(
'--hide-hostname',
action='store_true',
help="Don't show hostname, overrides --show-hostname"
)
parser.add_argument(
'--show-memusage',
action='store_true',
help="Show RAM and swap usage (default)"
)
parser.add_argument(
'--hide-memusage',
action='store_true',
help="Don't show RAM and swap usage, overrides --show-memusage"
)
parser.add_argument(
'--show-cpu',
action='store_true',
help="Show CPU usage (default)"
)
parser.add_argument(
'--hide-cpu',
action='store_true',
help="Don't show CPU usage, overrides --hide-cpu"
)
parser.add_argument(
'--show-os',
action='store_true',
help="Show your OS (for Linux, shows distro too) (default)"
)
parser.add_argument(
'--hide-os',
action='store_true',
help="Don't show your OS (and distro on Linux), overrides --show-os"
)
parser.add_argument(
'--show-device-info',
action='store_true',
help="Show your device manufacturer and model, if possible (default)"
)
parser.add_argument(
'--hide-device-info',
action='store_true',
help="Don't show your device manufacturer and model, overrides --show-device-info"
)
parser.add_argument(
'--show-partitions',
action='store_true',
help="Show info about mounted partitions (hidden by default)"
)
parser.add_argument(
'--hide-partitions',
action='store_true',
help="Don't show info about mounted partitions, overrides --show-partitions"
)
parser.add_argument(
'--show-ip',
action='store_true',
help="Show your public IP (hidden by default)"
)
parser.add_argument(
'--hide-ip',
action='store_true',
help="Don't show your public IP, overrides --show-ip"
)
# Parse the arguments
args = parser.parse_args()
# Variables
hostname = True
username = True
memory_usage = True
cpu = True
showOS = True
deviceInfo = True
showPartitions = False
showIP = False
if args.show_hostname:
hostname = True
if args.hide_hostname:
hostname = False
if args.show_username:
username = True
if args.hide_username:
username = False
if args.show_memusage:
memory_usage = True
if args.hide_memusage:
memory_usage = False
if args.show_cpu:
cpu = True
if args.hide_cpu:
cpu = False
if args.show_os:
showOS = True
if args.hide_os:
showOS = False
if args.show_device_info:
deviceInfo = True
if args.hide_device_info:
deviceInfo = False
if args.show_partitions:
showPartitions = True
if args.hide_partitions:
showPartitions = False
if args.show_ip:
showIP = True
if args.hide_ip:
showIP = False
# username@hostname
print(bcolors.OKBLUE, end="")
if hostname and not username:
try:
print(socket.gethostname())
except:
print("<unknown hostname>")
if username and not hostname:
try:
print(os.getlogin())
except:
print("<unknown user>")
if username and hostname:
try:
login = os.getlogin()
except:
login = "<unknown user>"
try:
host = socket.gethostname()
except:
host = "<unknown hostname>"
print(f"{login}@{host}")
if username or hostname:
print(bcolors.HEADER, end="")
print("-" * 10)
print(bcolors.ENDC, end="")
# Memory usage
if memory_usage:
memory = psutil.virtual_memory()
storageUnit = storageUnits[pickStorageUnit(memory.total)]
print(f"{bcolors.WARNING}RAM usage: {bcolors.FAIL}{round(memory.used / storageUnit, 2)}/{round(memory.total / storageUnit, 2)} {pickStorageUnit(memory.total)} ({memory.percent}%) used{bcolors.ENDC}")
swap = psutil.swap_memory()
storageUnit = storageUnits[pickStorageUnit(swap.total)]
print(f"{bcolors.WARNING}Swap usage: {bcolors.FAIL}{round(swap.used / storageUnit, 2)}/{round(swap.total / storageUnit, 2)} {pickStorageUnit(swap.total)} ({swap.percent}%) used")
# CPU
if cpu:
cpu_freq_str = f" {get_advertised_cpu_speed()} advertised speed"
print(f"{bcolors.WARNING}CPU: {bcolors.FAIL}{get_cpu_info()} ({psutil.cpu_count()} cores){cpu_freq_str}{bcolors.ENDC}")
# OS
def is_running_on_android():
android_files = [
'/system/app',
'/system/priv-app',
'/system/vendor',
'/system/build.prop'
]
return any(os.path.exists(file_path) for file_path in android_files)
def get_android_version():
try:
output = subprocess.check_output(['getprop', 'ro.build.version.release'], stderr=subprocess.DEVNULL)
return output.strip().decode('utf-8', errors='ignore')
except:
pass
return "unknown version"
def get_device():
if is_running_on_android():
# Android method 1
output = subprocess.check_output(['getprop', 'ro.product.manufacturer'], stderr=subprocess.DEVNULL)
output2 = subprocess.check_output(['getprop', 'ro.product.model'], stderr=subprocess.DEVNULL)
return output.strip().decode('utf-8', errors='ignore') + " " + output2.strip().decode('utf-8', errors='ignore')
if os.name == "nt":
# Windows method 1
c = wmi.WMI()
my_system = c.Win32_ComputerSystem()[0]
return my_system.Manufacturer + " " + my_system.Model
if os.name == "posix":
if os.uname().sysname.lower() == "darwin":
# Darwin method 1
try:
output = subprocess.check_output(["system_profiler", "SPHardwareDataType"], universal_newlines=True)
manufacturer_line = [line for line in output.splitlines() if "Manufacturer" in line][0]
model_line = [line for line in output.splitlines() if "Model Name" in line][0]
return f"{manufacturer_line.split(':')[1].strip()} {model_line.split(':')[1].strip()}"
except subprocess.CalledProcessError:
pass
else:
# Linux method 1
if os.path.exists("/sys/devices/virtual/dmi/id/product_name") or os.path.exists("/sys/devices/virtual/dmi/id/product_version"):
ret = ""
if os.path.exists("/sys/devices/virtual/dmi/id/product_name"):
with open("/sys/devices/virtual/dmi/id/product_name", "r") as f:
ret = f.read().strip().strip("\n")
if os.path.exists("/sys/devices/virtual/dmi/id/product_version"):
if ret:
ret += " "
with open("/sys/devices/virtual/dmi/id/product_version", "r") as f:
ret += f.read().strip()
return ret
# Linux method 2
if os.path.exists("/sys/firmware/devicetree/base/model"):
with open("/sys/firmware/devicetree/base/model", "r") as f:
return f.read().strip()
# Linux method 3
if os.path.exists("/tmp/sysinfo/model"):
with open("/tmp/sysinfo/model", "r") as f:
return f.read().strip()
return "Unknown"
if showOS:
print(f"{bcolors.WARNING}OS kernel name and version: {bcolors.FAIL}{platform.platform()}{bcolors.ENDC}")
if os.name.lower() == "posix":
if is_running_on_android():
print(f"{bcolors.WARNING}Linux distribution: {bcolors.FAIL}Android {get_android_version()}{bcolors.ENDC}")
elif os.uname().sysname.lower() != "darwin":
print(f"{bcolors.WARNING}Linux distribution: {bcolors.FAIL}{distro.name()} {distro.version()}{bcolors.ENDC}")
# Device
if deviceInfo:
print(f"{bcolors.WARNING}Host: {bcolors.FAIL}{get_device()}{bcolors.ENDC}")
# Disk usage of the root
if showPartitions:
partitions = psutil.disk_partitions(all=True)
for partition in partitions:
usageStr = ""
try:
if partition.mountpoint:
usage = psutil.disk_usage(partition.mountpoint)
storageUnitName = pickStorageUnit(usage.total)
storageUnit = storageUnits[storageUnitName]
usageStr += f" partition_usage={round(usage.used / storageUnit, 2)}/{round(usage.total / storageUnit, 2)} {storageUnitName} ({usage.percent}%)"
except:
usageStr = ""
print(f"{bcolors.WARNING}Partition {partition.device} (mounted at {partition.mountpoint}): {bcolors.FAIL}filesystem={partition.fstype} opts={bcolors.HEADER}'{partition.opts}'{bcolors.FAIL}{usageStr}{bcolors.ENDC}")
# storageUnitName = pickStorageUnit(usage.total)
# storageUnit = storageUnits[storageUnitName]
# IP
if showIP:
try:
resp = requests.get("https://icanhazip.com")
print(f"{bcolors.WARNING}Public IP: {bcolors.FAIL}{resp.text}{bcolors.ENDC}")
except:
print(f"{bcolors.WARNING}Public IP: {bcolors.FAIL}Error!{bcolors.ENDC}")