Skip to content

Commit

Permalink
fix: remove any linux specific implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunyoku committed Sep 24, 2023
1 parent 1380456 commit 00da2e6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ akatsuki-pp-py = "*"
cryptography = "*"
tenacity = "*"
httpx = "*"
py-cpuinfo = "*"

[dev-packages]
pytest = "*"
Expand Down
10 changes: 9 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/api/init_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def init_events(asgi_app: BanchoAPI) -> None:
async def on_startup() -> None:
app.state.loop = asyncio.get_running_loop()

if os.geteuid() == 0:
if app.utils.is_running_as_admin():
log(
"Running the server with root privileges is not recommended.",
Ansi.LRED,
Expand Down
19 changes: 7 additions & 12 deletions app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import time
import traceback
import uuid
from collections import Counter
from collections.abc import Awaitable
from collections.abc import Callable
from collections.abc import Mapping
Expand All @@ -27,6 +26,7 @@
from typing import TypedDict
from urllib.parse import urlparse

import cpuinfo
import psutil
import timeago
from pytimeparse.timeparse import timeparse
Expand Down Expand Up @@ -1227,18 +1227,13 @@ async def server(ctx: Context) -> str | None:
uptime = int(time.time() - proc.create_time())

# get info about our cpu
with open("/proc/cpuinfo") as f:
header = "model name\t: "
trailer = "\n"

model_names = Counter(
line[len(header) : -len(trailer)]
for line in f.readlines()
if line.startswith("model name")
)
cpu_info = cpuinfo.get_cpu_info()

# list of all cpus installed with thread count
cpus_info = " | ".join(f"{v}x {k}" for k, v in model_names.most_common())
thread_count = cpu_info["count"]
cpu_name = cpu_info["brand_raw"]

cpu_info_str = f"{thread_count}x {cpu_name}"

# get system-wide ram usage
sys_ram = psutil.virtual_memory()
Expand Down Expand Up @@ -1275,7 +1270,7 @@ async def server(ctx: Context) -> str | None:
return "\n".join(
(
f"{build_str} | uptime: {seconds_readable(uptime)}",
f"cpu(s): {cpus_info}",
f"cpu(s): {cpu_info_str}",
f"ram: {ram_info}",
f"search mirror: {mirror_search_url} | download mirror: {mirror_download_url}",
f"osu!api connection: {using_osuapi}",
Expand Down
32 changes: 19 additions & 13 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import ctypes
import inspect
import io
import ipaddress
Expand Down Expand Up @@ -50,6 +51,7 @@
"get_media_type",
"has_jpeg_headers_and_trailers",
"has_png_headers_and_trailers",
"is_running_as_admin",
)

T = TypeVar("T")
Expand Down Expand Up @@ -336,16 +338,6 @@ def escape_enum(

def ensure_supported_platform() -> int:
"""Ensure we're running on an appropriate platform for bancho.py."""
if sys.platform != "linux":
log("bancho.py currently only supports linux", Ansi.LRED)
if sys.platform == "win32":
log(
"you could also try wsl(2), i'd recommend ubuntu 18.04 "
"(i use it to test bancho.py)",
Ansi.LBLUE,
)
return 1

if sys.version_info < (3, 11):
log(
"bancho.py uses many modern python features, "
Expand Down Expand Up @@ -398,18 +390,32 @@ def _install_debugging_hooks() -> None:
runtime.setup()


def is_running_as_admin() -> bool:
try:
return os.geteuid() == 0 # type: ignore[attr-defined, unused-ignore]
except AttributeError:
pass

try:
return ctypes.windll.shell32.IsUserAdmin() == 1 # type: ignore[attr-defined, no-any-return]
except AttributeError:
raise Exception(
f"{sys.platform} is not currently supported on bancho.py, please create a github issue!",
)


def display_startup_dialog() -> None:
"""Print any general information or warnings to the console."""
if app.settings.DEVELOPER_MODE:
log("running in advanced mode", Ansi.LRED)
if app.settings.DEBUG:
log("running in debug mode", Ansi.LMAGENTA)

# running on root grants the software potentally dangerous and
# running on root/admin grants the software potentally dangerous and
# unnecessary power over the operating system and is not advised.
if os.geteuid() == 0:
if is_running_as_admin():
log(
"It is not recommended to run bancho.py as root, especially in production..",
"It is not recommended to run bancho.py as root/admin, especially in production..",
Ansi.LYELLOW,
)

Expand Down
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ ignore_missing_imports = True

[mypy-pytimeparse.*]
ignore_missing_imports = True

[mypy-cpuinfo.*]
ignore_missing_imports = True

0 comments on commit 00da2e6

Please sign in to comment.