From 0f62e0dfba52d730f05c8b5f2b9b91d39fb5ae3d Mon Sep 17 00:00:00 2001 From: Jakub Kaczmarzyk Date: Wed, 21 Jun 2023 17:43:12 -0400 Subject: [PATCH] ENH: set local path based on whether pyinstaller is running (#4) * set _here based on whether we are in pyinstaller runtime * add schemas into the installed binary * wrap --add-data arg in quotes for windows build * remove debugging line * convert a print statement to warning --- .github/workflows/build-binary.yaml | 8 ++++---- wsinfer_zoo/client.py | 11 +++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-binary.yaml b/.github/workflows/build-binary.yaml index 6251114..432aa97 100644 --- a/.github/workflows/build-binary.yaml +++ b/.github/workflows/build-binary.yaml @@ -12,8 +12,8 @@ jobs: with: python-version: 3.9 - run: python -m pip install . pyinstaller - - run: pyinstaller --onefile --name wsinfer_zoo-win64 wsinfer_zoo/__main__.py - - run: ./dist/wsinfer_zoo-win64 --help + - run: pyinstaller --onefile --name wsinfer-zoo-win64 --add-data "wsinfer_zoo/schemas;schemas" wsinfer_zoo/__main__.py + - run: ./dist/wsinfer-zoo-win64 --help - uses: actions/upload-artifact@v2 with: path: dist/* @@ -39,8 +39,8 @@ jobs: with: python-version: 3.9 - run: python -m pip install . pyinstaller - - run: pyinstaller --onefile --name wsinfer_zoo-MacOSX-x86_64 wsinfer_zoo/__main__.py - - run: ./dist/wsinfer_zoo-MacOSX-x86_64 --help + - run: pyinstaller --onefile --name wsinfer-zoo-MacOSX-x86_64 --add-data wsinfer_zoo/schemas:schemas wsinfer_zoo/__main__.py + - run: ./dist/wsinfer-zoo-MacOSX-x86_64 --help - uses: actions/upload-artifact@v2 with: path: dist/* diff --git a/wsinfer_zoo/client.py b/wsinfer_zoo/client.py index fabe5be..cb9b198 100644 --- a/wsinfer_zoo/client.py +++ b/wsinfer_zoo/client.py @@ -4,7 +4,9 @@ import json from datetime import datetime from pathlib import Path +import sys from typing import Any, Dict, List, Optional, Sequence +import warnings import jsonschema import requests @@ -24,7 +26,12 @@ # The path to the registry file. WSINFER_ZOO_REGISTRY_DEFAULT_PATH = Path.home() / ".wsinfer-zoo-registry.json" -_here = Path(__file__).parent.resolve() +# In pyinstaller runtime for one-file executables, the root path +# is the path to the executable. +if getattr(sys, "frozen", False) and getattr(sys, "_MEIPASS", False): + _here = Path(sys._MEIPASS).resolve() # type: ignore +else: + _here = Path(__file__).parent.resolve() class WSInferZooException(Exception): @@ -307,4 +314,4 @@ def _download_registry_if_necessary(): r = requests.get(WSINFER_ZOO_REGISTRY_URL) WSINFER_ZOO_REGISTRY_DEFAULT_PATH.write_bytes(r.content) except requests.RequestException as e: - print(f"Could not download most recent registry, error: {e}") + warnings.warn(f"Could not download most recent registry, error: {e}")