Skip to content

Commit

Permalink
Merge pull request #152 from Guts/packaging/improve-metadata
Browse files Browse the repository at this point in the history
Packaging: complete metadata and test it
  • Loading branch information
Guts authored Sep 6, 2023
2 parents 84deaa2 + 267f234 commit 7e46ead
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 19 deletions.
14 changes: 11 additions & 3 deletions dicogis/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@
__copyright__ = f"2014 - {date.today().year}, {__author__}"
__email__ = "[email protected]"
__executable_name__ = "DicoGIS.exe"
__package_name__ = "dicogis"
__keywords__ = ["GIS", "metadata", "INSPIRE", "GDAL", "OGR", "data management"]
__license__ = "GNU General Public License v3.0"
__summary__ = "Create Excel spreadsheet describing geographical data."
__summary__ = (
"Create Excel spreadsheet describing geographical data from a PostGIS "
"Database or a file tree structure."
)
__title__ = "DicoGIS"
__title_clean__ = "".join(e for e in __title__ if e.isalnum())
__uri__ = "https://github.com/Guts/DicoGIS/"
__uri_doc__ = "https://guts.github.io/DicoGIS/"
__uri_homepage__ = "https://guts.github.io/DicoGIS/"
__uri_repository__ = "https://github.com/Guts/DicoGIS/"
__uri_tracker__ = f"{__uri_repository__}/issues/"
__uri__ = __uri_repository__


__version__ = "3.0.0"
__version_info__ = tuple(
Expand Down
168 changes: 168 additions & 0 deletions dicogis/utils/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#! python3 # noqa: E265

"""Helpers to retrieve information from execution environment like software versions."""

# ############################################################################
# ########## Libraries #############
# ##################################

# standard library
import logging
import re
import subprocess
from os import getenv

try:
from osgeo import gdal, osr

GDAL_IS_AVAILABLE: bool = True
except ImportError:
logging.info("GDAL (ou ses bindings Python) n'est pas installé.")
gdal = osr = None
GDAL_IS_AVAILABLE: bool = False

try:
import pyproj

PYPROJ_IS_AVAILABLE: bool = True
except ImportError:
logging.info("PyProj n'est pas installé.")
pyproj = None
PYPROJ_IS_AVAILABLE: bool = False

# ############################################################################
# ########### Globals ##############
# ##################################

# logs
logger = logging.getLogger(__name__)

# ############################################################################
# ########### Functions ############
# ##################################


def get_gdal_version() -> str:
"""Return GDAL version from gdal-config cmd or from environment variable GDAL_VERSION.
Returns:
str: version of GDAL
"""
gdal_fallback_version = "3.*"

# if available use,
if GDAL_IS_AVAILABLE:
logger.debug(
f"Version de GDAL obtenue depuis les bindings Python : {gdal.__version__}"
)
return gdal.__version__

# try to get it from gdalinfo (installed with gdal)
try:
returned_output: bytes = subprocess.check_output(
"gdalinfo --version", shell=True
)
logger.debug(
f"GDAL version retrieved from gdalinfo: {returned_output.decode('utf-8')}"
)
return returned_output[5:10].decode("utf-8")
except Exception:
pass

# try to get it from gdal-config (installed with gdaldev)
try:
returned_output: bytes = subprocess.check_output(
"gdal-config --version", shell=True
)
logger.debug(
f"GDAL version retrieved from gdal-config: {returned_output.decode('utf-8')}"
)
return returned_output.decode("utf-8")
except Exception:
pass

# first check environment variable
gdal_version = getenv("GDAL_VERSION")
if gdal_version:
return gdal_version

logging.warning(
"Unable to retrieve GDAL version from command-line or environment var. "
f"Using default version: {gdal_fallback_version}"
)
return gdal_fallback_version


def get_proj_version() -> str | None:
"""Get PROJ version, using GDAL, PyProj, installed proj binaries or environment
variable.
Returns:
str | None: PROJ's version as Major.Minor.Micro or None if not found
"""
proj_version = None

# from GDAL bindings
if osr is not None and GDAL_IS_AVAILABLE:
try:
proj_version = (
f"{osr.GetPROJVersionMajor()}."
f"{osr.GetPROJVersionMinor()}."
f"{osr.GetPROJVersionMicro()}"
)
logger.debug(
f"Version de PROJ obtenue depuis les bindings Python de GDAL : {proj_version}"
)
return proj_version
except Exception:
pass

# from PyProj
if pyproj is not None and PYPROJ_IS_AVAILABLE:
try:
proj_version = pyproj.__proj_version__
logger.debug(f"Version de PROJ obtenue depuis PyProj : {proj_version}")
return proj_version
except Exception:
pass

# from PROJ command-line
try:
# Exécute la commande "proj" en utilisant subprocess
result = subprocess.check_output(["proj"], stderr=subprocess.STDOUT, text=True)

# Recherche de la version dans la sortie à l'aide d'une expression régulière
if version_match := re.search(r"Rel\. ([0-9.]+)", result):
proj_version = version_match.group(1)
logger.debug(
f"Version de PROJ obtenue depuis le binaire proj : {proj_version}"
)
return proj_version
else:
logger.error(
"PROJ est bien installé mais impossible de trouver la version en regex."
)
except FileNotFoundError as err:
logger.info(f"Proj n'est pas installé. Trace : {err}")
except subprocess.CalledProcessError as e:
logger.info(f"Erreur lors de l'exécution de la commande : {e}")

# from environment variable
if proj_version := getenv("PROJ_VERSION"):
return proj_version

logging.warning(
"Impossible de déterminer la version de PROJ depuis les bindings GDAL, PyProj, "
"les binaires proj ou la variable d'environnement PROJ_VERSION. "
)
return proj_version


# ############################################################################
# ##### Stand alone program ########
# ##################################

if __name__ == "__main__":
"""Standalone execution."""
print(get_gdal_version())
print(get_proj_version())
5 changes: 3 additions & 2 deletions requirements/testing.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytest-cov>=3,<4.1
semver>=2.13,<2.14
pytest-cov>=4,<5
packaging>=20,<24
validators>=0.19,<0.21
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ use_parentheses = True
[tool:pytest]
addopts =
--junitxml=junit/test-results.xml
--cov-config=.coveragerc
--cov-config=setup.cfg
--cov=dicogis
--cov-report=xml
--cov-report=html
--cov-report=term
--cov-report=xml
--cov-append tests/
--ignore=tests/_wip/
junit_family = xunit2
Expand Down
62 changes: 50 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
# ##################################

# standard library
import pathlib
from pathlib import Path

# 3rd party
from setuptools import find_packages, setup

# package (to get version)
from dicogis import __about__
from dicogis.utils.environment import get_gdal_version

# ############################################################################
# ########## Globals #############
# ##############################

HERE = pathlib.Path(__file__).parent
README = (HERE / "README.md").read_text()
HERE = Path(__file__).parent


with open(HERE / "requirements/base.txt") as f:
requirements = [
Expand All @@ -42,24 +43,57 @@
if not line.startswith(("#", "-")) and len(line)
]

# ############################################################################
# ########### Functions ############
# ##################################


def load_requirements(requirements_files: Path | list[Path]) -> list:
"""Helper to load requirements list from a path or a list of paths.
Args:
requirements_files (Path | list[Path]): path or list to paths of requirements
file(s)
Returns:
list: list of requirements loaded from file(s)
"""
out_requirements = []

if isinstance(requirements_files, Path):
requirements_files = [
requirements_files,
]

for requirements_file in requirements_files:
with requirements_file.open(encoding="UTF-8") as f:
out_requirements += [
line
for line in f.read().splitlines()
if not line.startswith(("#", "-")) and len(line)
]

return out_requirements


# ############################################################################
# ########## Setup #############
# ##############################

setup(
name="dicogis",
name=__about__.__package_name__,
version=__about__.__version__,
author=__about__.__author__,
author_email=__about__.__email__,
description=__about__.__summary__,
license="GPL3",
long_description=README,
long_description=(HERE / "README.md").read_text(),
long_description_content_type="text/markdown",
url=__about__.__uri__,
project_urls={
"Docs": __about__.__uri_doc__,
"Bug Reports": f"{__about__.__uri__}issues/",
"Source": __about__.__uri__,
"Docs": __about__.__uri_homepage__,
"Bug Reports": __about__.__uri_tracker__,
"Source": __about__.__uri_repository__,
},
py_modules=["dicogis"],
# packaging
Expand All @@ -70,29 +104,33 @@
# dependencies
python_requires=">=3.9, <4",
extras_require={
"dev": requirements_dev,
"doc": requirements_docs,
"dev": load_requirements(HERE / "requirements/development.txt"),
"doc": load_requirements(HERE / "requirements/documentation.txt"),
"test": load_requirements(HERE / "requirements/testing.txt"),
"gdal": f"gdal=={get_gdal_version()}",
},
install_requires=requirements,
install_requires=load_requirements(HERE / "requirements/base.txt"),
# run
entry_points={
"gui_scripts": [
"dicogis = dicogis.DicoGIS:__main__",
]
},
# metadata
keywords="GIS metadata INSPIRE GDAL",
keywords=__about__.__keywords__,
classifiers=[
"Intended Audience :: End Users/Desktop",
"Intended Audience :: Information Technology",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: CPython",
"Development Status :: 5 - Production/Stable",
"Environment :: Win32 (MS Windows)",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
"Operating System :: Microsoft :: Windows :: Windows 10",
"Topic :: Scientific/Engineering :: GIS",
],
)
Loading

0 comments on commit 7e46ead

Please sign in to comment.