robot0nfire/c0re
Archived
1
0
Fork 0
This repository has been archived on 2025-05-08. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
c0re/utils.py
2018-01-15 10:09:19 +01:00

74 lines
No EOL
1.6 KiB
Python

from traceback import print_exception
from sys import exc_info, byteorder
import platform
import os
import struct
import subprocess
import urllib
HIGHEST_PORT = 65535
class HostnameNotChangedError(PermissionError):
def __init__(self):
super(PermissionError, self).__init__("hostname could not be changed")
class NotSupportedOnPlatform(OSError):
def __init__(self):
super(OSError, self).__init__("feature not avaliable on OS")
class PlaybackFailure(OSError):
def __init__(self):
super(OSError, self).__init__("audio playback failed")
def is_wallaby():
return "3.18.21-custom" in platform.uname().release
def is_linux():
return platform.uname().system == "Linux"
def is_darwin():
return platform.uname().system == "Darwin"
def is_windows():
return platform.uname().system == "Windows"
def set_hostname(hostname):
if is_linux():
if os.geteuid() == 0:
open("/etc/hostname", "w").write(hostname)
else:
raise HostnameNotChangedError()
elif is_darwin():
if os.geteuid() == 0:
subprocess.check_call(["scutil", "--set", "HostName", hostname])
else:
raise HostnameNotChangedError()
else:
raise HostnameNotChangedError()
def get_hostname():
return platform.uname().node
def get_ip_from_url(url):
return urllib.parse.urlsplit(url).netloc.split(':')[0]
def play_sound(path):
if is_linux() or is_darwin():
try:
subprocess.check_call(["aplay" if is_linux() else "afplay", path])
except subprocess.CalledProcessError as e:
raise PlaybackFailure()
else:
raise NotSupportedOnPlatform()
def valid_port(port):
return type(port) is int and port <= HIGHEST_PORT