Use dynamic versioning
Some checks failed
/ test (push) Successful in 26s
/ build (push) Failing after 8s

This commit is contained in:
phil 2025-03-15 17:43:07 +01:00
parent c7deb34bae
commit ea7e3087cd
5 changed files with 157 additions and 150 deletions

View file

@ -1,3 +1,8 @@
from treetrail.utils import get_version
import importlib.metadata
__version__ = get_version()
try:
from dunamai import Version, Style
__version__ = Version.from_git().serialize(style=Style.SemVer, dirty=True)
except ImportError:
__version__ = importlib.metadata.version("treetrail-backend")

View file

@ -7,19 +7,19 @@ import pandas as pd
from sqlalchemy.ext.declarative import DeclarativeMeta
from sqlalchemy.engine.row import Row
from sqlalchemy.sql.selectable import Select
import geopandas as gpd # type: ignore
import geopandas as gpd # type: ignore
from treetrail.config import conf
logger = logging.getLogger(__name__)
class AlchemyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
# an SQLAlchemy class
fields = {}
for field in [x for x in dir(obj)
if not x.startswith('_') and x != 'metadata']:
for field in [x for x in dir(obj) if not x.startswith("_") and x != "metadata"]:
data = obj.__getattribute__(field)
try:
# this will fail on non-encodable values, like other classes
@ -50,24 +50,30 @@ def get_attachment_root(type: str):
def get_attachment_tree_root():
return get_attachment_root('tree')
return get_attachment_root("tree")
def get_attachment_trail_root():
return get_attachment_root('trail')
return get_attachment_root("trail")
def get_attachment_poi_root():
return get_attachment_root('poi')
return get_attachment_root("poi")
def pandas_query(session, query):
return pd.read_sql_query(query, session.connection())
def geopandas_query(session, query: Select, model, *,
# simplify_tolerance: float|None=None,
crs=None, cast=True,
):
def geopandas_query(
session,
query: Select,
model,
*,
# simplify_tolerance: float|None=None,
crs=None,
cast=True,
):
## XXX: I could not get the add_columns work without creating a subquery,
## so moving the simplification to geopandas - see in _get_df
# if simplify_tolerance is not None:
@ -78,34 +84,11 @@ def geopandas_query(session, query: Select, model, *,
# query = query.add_columns(new_column)
return gpd.GeoDataFrame.from_postgis(query, session.connection(), crs=crs)
def mkdir(dir: Path | str) -> Path:
path = Path(dir)
if not path.is_dir():
logger.info(f'Create directory {path}')
logger.info(f"Create directory {path}")
path.mkdir(parents=True, exist_ok=True)
return path
def get_version() -> str:
version_file_src = Path(__file__).parent / 'version.txt'
version_file_in_container = Path("/app") / 'version.txt'
if version_file_src.exists():
with open(version_file_src) as version:
return version.read().strip()
if version_file_in_container.exists():
with open(version_file_in_container) as version:
return version.read().strip()
else:
logger.debug('No version file, using git')
try:
from subprocess import run
git_version_cmd = run(['git', 'describe', '--broken', '--tags', '--always', '--dirty'],
capture_output=True)
if git_version_cmd.returncode == 0:
return git_version_cmd.stdout.strip().decode()
else:
logger.debug('git returns with the error below, version set as 0.0.0')
logger.debug(git_version_cmd.stderr.decode())
return '0.0.0'
except FileNotFoundError as err:
logger.debug('git not found: version set as 0.0.0')
return '0.0.0'