BAPSicle/helpers/os_environment.py

48 lines
1.2 KiB
Python
Raw Normal View History

2020-10-28 22:03:48 +00:00
import sys
import os
# Check if we're running inside a pyinstaller bundled (it's an exe)
def isBundelled():
2021-04-08 19:53:51 +00:00
return getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS")
2020-10-28 22:03:48 +00:00
def isWindows():
2021-04-08 19:53:51 +00:00
return sys.platform.startswith("win32")
2020-10-28 22:03:48 +00:00
def isLinux():
2021-04-08 19:53:51 +00:00
return sys.platform.startswith("linux")
2020-10-28 22:03:48 +00:00
def isMacOS():
2021-04-08 19:53:51 +00:00
return sys.platform.startswith("darwin")
2020-10-28 22:03:48 +00:00
# This must be used to that relative file paths resolve inside the bundled versions.
2020-12-19 14:57:37 +00:00
def resolve_local_file_path(relative_path: str):
2020-10-28 22:03:48 +00:00
""" Get absolute path to resource, works for dev and for PyInstaller """
2021-04-18 02:52:34 +00:00
if relative_path.startswith("/"):
relative_path = relative_path[1:]
2020-10-28 22:03:48 +00:00
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
2021-04-08 19:53:51 +00:00
base_path: str = sys._MEIPASS
2021-04-08 21:32:16 +00:00
except Exception:
2020-10-28 22:03:48 +00:00
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
2021-04-08 19:53:51 +00:00
2020-10-28 22:03:48 +00:00
# Use this to resolve paths to resources not bundled within the bundled exe.
2020-12-19 14:57:37 +00:00
def resolve_external_file_path(relative_path: str):
2021-04-08 19:53:51 +00:00
if not relative_path.startswith("/"):
2020-10-28 22:03:48 +00:00
relative_path = "/" + relative_path
# Pass through abspath to correct any /'s with \'s on Windows
return os.path.abspath(os.getcwd() + relative_path)