Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update pixi env and hooks #126

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions .build/build.spec
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@ from fiat.util import generic_folder_check
import inspect
import os
import sys
from pathlib import Path
from pathlib import Path, PurePath

#Pre build event setup
app_name = "fiat"
sys.setrecursionlimit(5000)

_file = Path(inspect.getfile(lambda: None))
cwd = _file.parent
env_path = os.path.dirname(sys.executable)
generic_folder_check(Path(cwd, "../bin"))
mode = "Release"

proj = Path(os.environ["PROJ_LIB"])
# Get project root directory
project_root = _file.parents[1].absolute()

build_dir = project_root / ".build"

binaries = [
(Path(proj, 'proj.db'), './share'),
]
# check if bin folder is in build dir.
generic_folder_check(build_dir / "bin")
mode = "Release"

# Build event
a = Analysis(
[Path(cwd, "../src/fiat/cli/main.py")],
pathex=[Path(cwd, "../src"), Path(env_path, "lib/site-packages")],
binaries=binaries,
[Path(project_root, "src/fiat/cli/main.py")],
pathex=[Path(project_root, "src")],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hookspath=[".build"],
hooksconfig={},
runtime_hooks=[Path(cwd, 'runtime_hooks.py')],
runtime_hooks=[
Path(build_dir, "pyi_rth_fiat.py"),
],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
Expand Down
50 changes: 50 additions & 0 deletions .build/hook-fiat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Analysis hook for fiat installation.

Helps finding different dependencies for PyInstaller.
"""

import os
import sys

from PyInstaller.compat import is_conda, is_win
from PyInstaller.utils.hooks.conda import (
Distribution,
distribution,
)

datas = []

if hasattr(sys, "real_prefix"): # check if in a virtual environment
root_path = sys.real_prefix
else:
root_path = sys.prefix

if is_conda:
netcdf_dist: Distribution = distribution("libgdal-netcdf")
# append netcdf files to datas
datas += list(
map(
lambda path: (os.path.join(root_path, path), str(path.parent)),
netcdf_dist.files,
)
)
# a runtime hook defines the path for `GDAL_DRIVER_PATH`

# - conda-specific
if is_win:
tgt_proj_data = os.path.join("Library", "share", "proj")
src_proj_data = os.path.join(root_path, "Library", "share", "proj")

else: # both linux and darwin
tgt_proj_data = os.path.join("share", "proj")
src_proj_data = os.path.join(root_path, "share", "proj")

if is_conda:
if os.path.exists(src_proj_data):
datas.append((src_proj_data, tgt_proj_data))
else:
from PyInstaller.utils.hooks import logger

logger.warning("Datas for proj not found at:\n{}".format(src_proj_data))
# A runtime hook defines the path for `PROJ_LIB`
34 changes: 34 additions & 0 deletions .build/pyi_rth_fiat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Pyinstaller hook that sets mandatory env_vars.

Based on https://github.com/conda-forge/gdal-feedstock/blob/main/recipe/scripts/activate.sh
and pyproj hooks.
"""

import os
import sys

# Installing `osgeo` Conda packages with plugins requires to set `GDAL_DRIVER_PATH`
is_win = sys.platform.startswith("win")

if is_win:
gdal_plugins = os.path.join(sys._MEIPASS, "share", "gdalplugins")
if not os.path.exists(gdal_plugins):
gdal_plugins = os.path.join(sys._MEIPASS, "Library", "lib", "gdalplugins")
# last attempt, check if one of the required file is in the generic folder Library/data
if not os.path.exists(os.path.join(gdal_plugins, "gcs.csv")):
gdal_plugins = os.path.join(sys._MEIPASS, "Library", "lib")

else:
gdal_plugins = os.path.join(sys._MEIPASS, "lib", "gdalplugins")

if os.path.exists(gdal_plugins):
os.environ["GDAL_DRIVER_PATH"] = gdal_plugins

if is_win:
proj_data = os.path.join(sys._MEIPASS, "Library", "share", "proj")
else:
proj_data = os.path.join(sys._MEIPASS, "share", "proj")

if os.path.exists(proj_data):
os.environ["PROJ_LIB"] = proj_data
10 changes: 0 additions & 10 deletions .build/runtime_hooks.py

This file was deleted.

4 changes: 4 additions & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ quarto-quick = { cmd = ["quarto", "render", "docs"] }
quarto-render = { cmd = ["quarto", "render", "docs", "--execute"] }
serve = { cmd = ["python", "-m", "http.server", "8000", "-d", "docs/_site"] }

# Build
build = { cmd = "pyinstaller .build/build.spec --distpath .build/bin --workpath .build/intermediates", env = { "PROJ_LIB" = "$CONDA_PREFIX/share/proj" } }

# Testing
test = { cmd = ["pytest"] }
test-lf = { cmd = ["pytest", "--lf", "--tb=short"] }
Expand All @@ -55,6 +58,7 @@ clean-docs = { cmd = ["rm", "-rf", "docs/_site"] }
## Dependencies
[dependencies]
gdal = ">=3.5"
libgdal = "*"
numpy = "*"
regex = "*"
tomli = "*"
Expand Down