Skip to content

Commit

Permalink
fix: use flexible paths for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
builder555 committed Jan 24, 2024
1 parent acb4eab commit 996cbd2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
13 changes: 11 additions & 2 deletions backend/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import sys


def resource_path(relative_path):
def get_resource_path(relative_path, max_levels=3):
"""Get the absolute path to the resource, works for development and for PyInstaller"""
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path: str = getattr(sys, "_MEIPASS", os.path.abspath("."))
base_path = getattr(sys, "_MEIPASS", os.path.abspath("."))
level = 0
while base_path != '/' and level < max_levels:
file_path = os.path.join(base_path, relative_path)
if os.path.exists(file_path):
break
base_path = os.path.dirname(base_path)
level += 1
else:
raise FileNotFoundError(f"Could not find resource {relative_path}")
return os.path.join(base_path, relative_path)


Expand Down
11 changes: 8 additions & 3 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ws_server import CommandProcessor, WebSocketHandler
from version_checker import VersionChecker
import logging
from io_utils import parse_cmd_args, resource_path
from io_utils import parse_cmd_args, get_resource_path

LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper()
timestamp_format = "%H:%M:%S"
Expand All @@ -26,15 +26,20 @@ async def main(stop_event=asyncio.Event()):
pinesam_url = "https://api.github.com/repos/builder555/PineSAM/releases/latest"
ironos_url = "https://api.github.com/repos/Ralim/IronOS/releases/latest"
app_version_manager = VersionChecker(
api_url=pinesam_url, file_path=resource_path("version.txt")
api_url=pinesam_url, file_path=get_resource_path("version.txt")
)
ironos_version_manager = VersionChecker(api_url=ironos_url)

pinecil_finder = PinecilFinder()
command_processor = CommandProcessor(
pinecil_finder, app_version_manager, ironos_version_manager
)
ws_handler = WebSocketHandler(command_processor, ui_path=resource_path("gui"))
try:
ui_path = get_resource_path("gui", max_levels=2)
except FileNotFoundError:
logging.warning("gui directory not found. You will need to serve UI separately")
ui_path = ''
ws_handler = WebSocketHandler(command_processor, ui_path=ui_path)
pinecil_monitor = PinecilMonitor(pinecil_finder, ws_handler.broadcast)
tasks = [
asyncio.create_task(ws_handler.serve(host, port)),
Expand Down
2 changes: 2 additions & 0 deletions backend/ws_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def _get_content_type(self, filepath):
return content_type or "application/octet-stream"

def _handle_http_request(self, path):
if not ui_path:
return http.HTTPStatus.NOT_FOUND, [], b"404 Not Found"
if path == "/" or path == "":
path = "/index.html"
filepath = os.path.join(ui_path, path.lstrip("/"))
Expand Down

0 comments on commit 996cbd2

Please sign in to comment.