Files
LorenEteval_Furious/Furious/Utility/SystemRuntime.py
T
2025-06-22 11:07:16 +08:00

126 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Copyright (C) 2024–present Loren Eteval & contributors <loren.eteval@proton.me>
#
# This file is part of Furious.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
from Furious.Utility.Constants import *
from Furious.Utility.AppSettings import AppSettings
from Furious.Utility.Utility import runExternalCommand
import os
import sys
import ctypes
import functools
import subprocess
__all__ = [
'getPythonVersion',
'getUbuntuRelease',
'getAppImagePath',
'isAdministrator',
'isTUNMode',
'isScriptMode',
'isPythonw',
'isWindows7',
]
@functools.lru_cache(None)
def getPythonVersion():
return '.'.join(str(info) for info in sys.version_info)
@functools.lru_cache(None)
def getUbuntuRelease() -> str:
try:
if PLATFORM != 'Linux':
return ''
result = runExternalCommand(
['cat', '/etc/lsb-release'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
values = dict(
list(line.split('='))
for line in filter(lambda x: x != '', result.stdout.decode().split('\n'))
)
if values['DISTRIB_ID'] == 'Ubuntu':
return values['DISTRIB_RELEASE']
else:
return ''
except Exception:
# Any non-exit exceptions
return ''
def getAppImagePath() -> str:
if PLATFORM != 'Linux':
return ''
if 'APPIMAGE' in os.environ:
return os.environ['APPIMAGE']
else:
return ''
@functools.lru_cache(None)
def isAdministrator() -> bool:
if PLATFORM == 'Windows':
return ctypes.windll.shell32.IsUserAnAdmin() == 1
else:
return os.geteuid() == 0
def isTUNMode() -> bool:
return isAdministrator() and AppSettings.isStateON_('VPNMode')
@functools.lru_cache(None)
def isScriptMode() -> bool:
return sys.argv[0].endswith('.py')
@functools.lru_cache(None)
def isPythonw() -> bool:
def isRealFile(file):
if not hasattr(file, 'fileno'):
return False
try:
tmp = os.dup(file.fileno())
except Exception:
# Any non-exit exceptions
return False
else:
os.close(tmp)
return True
# pythonw.exe. Also applies to packed GUI application on Windows
return not isRealFile(sys.__stdout__) or not isRealFile(sys.__stderr__)
@functools.lru_cache(None)
def isWindows7() -> bool:
return PLATFORM == 'Windows' and PLATFORM_RELEASE == '7'