Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .basedpyright/baseline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files": {}
}
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
USER_GROUP := $(shell id -u):$(shell id -g)

ifeq ($(OS),Windows_NT)
detected_OS := Windows
else
detected_OS := $(shell uname -s)
endif

.PHONY: format
format: image
docker run --rm -u ${USER_GROUP} -v "$(CURDIR):/app" -w /app interuss/geospatial-utils uv run ruff format
docker run --rm -u ${USER_GROUP} -v "$(CURDIR):/app" -w /app interuss/geospatial-utils uv run ruff check --fix
docker run --rm -u ${USER_GROUP} -v "$(CURDIR):/app" -w /app interuss/geospatial-utils uv run basedpyright


.PHONY: lint
lint: shell-lint python-lint


.PHONY: python-lint
python-lint: image
docker run --rm -u ${USER_GROUP} -v "$(CURDIR):/app" -w /app interuss/geospatial-utils uv run ruff format --check || (echo "Linter didn't succeed. You can use the following command to fix python linter issues: make format" && exit 1)
docker run --rm -u ${USER_GROUP} -v "$(CURDIR):/app" -w /app interuss/geospatial-utils uv run ruff check || (echo "Linter didn't succeed. You can use the following command to fix python linter issues: make format" && exit 1)
shasum -b -a 256 .basedpyright/baseline.json > /tmp/baseline-before.hash
docker run --rm -u ${USER_GROUP} -v "$(CURDIR):/app" -w /app interuss/geospatial-utils uv run basedpyright || (echo "Typing check didn't succeed. Please fix issue and run make format to validate changes." && exit 1)
shasum -b -a 256 .basedpyright/baseline.json > /tmp/baseline-after.hash
diff /tmp/baseline-before.hash /tmp/baseline-after.hash || (echo "Basedpyright baseline changed, probably dues to issues that have been cleanup. Use the following command to update baseline: make format" && exit 1)


.PHONY: shell-lint
shell-lint:
find . -type f -name '*.sh' ! -path './.*' | xargs docker run --rm -v "$(CURDIR):/geospatial-utils" -w /geospatial-utils koalaman/shellcheck


.PHONY: image
image:
cd geospatial-utils && make image
68 changes: 68 additions & 0 deletions geospatial-utils/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Dockerfile for interuss/geospatial-utils
#
# The image generated by this Dockerfile (via ./build.sh) includes the entire
# `geospatial-utils` folder contents in /app/geospatial-utils and has installed dependencies
# necessary to run any of the geospatial-utils tools.
#
# This image is intended to be built from the repository root context/folder.

FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
# Not -alpine because: https://stackoverflow.com/a/58028091/651139

# Install system tools
# openssl: Provides TLS tools
# curl: Useful debugging utility
# gcc: Required to build various packages
# ca-certificates: Needed to accurately validate TLS connections
RUN apt-get update --fix-missing && apt-get install -y make openssl curl libgeos-dev gcc g++ && apt-get install ca-certificates

# Required to build in an ARM environment
# gevent: libffi-dev libssl-dev python3-dev build-essential
# lxml: libxml2-dev libxslt-dev
# h5py: pkg-config libhdf5-dev
RUN if [ "$(uname -m)" = "aarch64" ]; then \
apt-get install -y libffi-dev libssl-dev python3-dev build-essential libxml2-dev libxslt-dev pkg-config libhdf5-dev \
; fi

RUN mkdir -p /app/

# Install dependencies
ENV UV_LINK_MODE=copy
ENV UV_PROJECT_ENVIRONMENT=/venv/
ENV VIRTUAL_ENV=/venv/
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=./uv.lock,target=/app/uv.lock \
--mount=type=bind,source=./pyproject.toml,target=/app/pyproject.toml \
cd /app && \
uv sync --locked --no-install-project --compile-bytecode

# Ensure UV don't check dependencies or lock file, since we installed everything before
ENV UV_FROZEN=1

# Ensure the cache folder is present and writable by anyone
# (Some command run with limited privileges)
RUN mkdir -p /.cache/uv/ && chmod 777 /.cache/uv/
# Also fix the root folder where python is downloaded
RUN find /root/ -type d -exec chmod a+rx {} +

# Start in this folder
WORKDIR /app/geospatial-utils

# Add core content from repo
ADD ./geospatial-utils /app/geospatial-utils

# Discover `geospatial-utils` module in Python
ENV PYTHONPATH=/app

# Add venv to path
ENV PATH="/venv/bin/:$PATH"

# This image should be built by passing in `version` and `commit_hash` based on information from git (see `make image`)
# This version information becomes available in the environment variables specified below
ARG version
ARG commit_hash
ENV GEOSPATIAL_UTILS_VERSION=$version
ENV GIT_COMMIT_HASH=$commit_hash

# No entry point maximizes flexibility in the use of this image
ENTRYPOINT []
3 changes: 3 additions & 0 deletions geospatial-utils/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
image: ../uv.lock ../pyproject.toml $(shell find . -type f ! -name image ! -name *.pyc)
# Building image due to changes in the following files: $?
./build.sh
24 changes: 24 additions & 0 deletions geospatial-utils/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -eo pipefail

# Find and change to repo root directory
OS=$(uname)
if [[ "$OS" == "Darwin" ]]; then
# OSX uses BSD readlink
BASEDIR="$(dirname "$0")"
else
BASEDIR=$(readlink -e "$(dirname "$0")")
fi
cd "${BASEDIR}/.." || exit 1

TAG="${1:-interuss/geospatial-utils}"

docker image build \
-f geospatial-utils/Dockerfile \
-t "${TAG}" \
--build-arg version="$(scripts/git/version.sh geospatial-utils --long)" \
--build-arg commit_hash="$(git rev-parse HEAD)" \
. \
|| exit 1
echo "File created by geospatial-utils/build.sh to keep track of the latest build run date time." > geospatial-utils/image
1 change: 1 addition & 0 deletions geospatial-utils/image
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File created by geospatial-utils/build.sh to keep track of the latest build run date time.
13 changes: 13 additions & 0 deletions geospatial-utils/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os

from loguru import logger

version = os.environ.get("GEOSPATIAL_UTILS_VERSION", "unknown")


def main():
logger.info(f"Geospatial utils - {version}")


if __name__ == "__main__":
main()
39 changes: 39 additions & 0 deletions geospatial-utils/run_locally.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

set -eo pipefail

# Find and change to repo root directory
OS=$(uname)
if [[ "$OS" == "Darwin" ]]; then
# OSX uses BSD readlink
BASEDIR="$(dirname "$0")"
else
BASEDIR=$(readlink -e "$(dirname "$0")")
fi
cd "${BASEDIR}/.." || exit 1

(
cd geospatial-utils || exit 1
make image
)

# https://stackoverflow.com/a/9057392
# shellcheck disable=SC2124
OTHER_ARGS=${@:2}

if [ "$CI" == "true" ]; then
docker_args=""
else
docker_args="-it"
fi

# shellcheck disable=SC2086
docker run ${docker_args} --name geospatial-utils \
--rm \
--network interop_ecosystem_network \
--add-host=host.docker.internal:host-gateway \
-u "$(id -u):$(id -g)" \
-e PYTHONBUFFERED=1 \
-w /app/geospatial-utils \
interuss/geospatial-utils \
uv run main.py $OTHER_ARGS
30 changes: 30 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[project]
name = "geospatial-utils"
version = "0.1.0"
description = "Set of geospatial utilies"
readme = "README.md"
requires-python = "==3.13.5"
dependencies = [
"basedpyright>=1.31.1",
"loguru>=0.7.3",
"ruff",
]

[tool.ruff]
target-version = "py313"

# Default + isort + pyupgrade
lint.select = [
"E4", "E7", "E9", "F", "I", "UP"
]
extend-exclude = [
]
line-length = 88

[tool.basedpyright]
typeCheckingMode = "standard"

exclude = [
"**/__pycache__",
"**/.*",
]
28 changes: 28 additions & 0 deletions scripts/git/upstream_owner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

# This script attempts to print the organization of the upstream repository.

# The following strategies will be used to determine the organization name:
# 1. If a remote named `origin` exists, the organization name will be extracted from the
# remote URL assuming the format below.
# 2. If a remote named `interuss` exists, the organization name will be extracted from the
# remote URL assuming the format below.
# 3. If the upstream of the current branch exists, the organization name will be set to the
# upstream repo name.
# 4. Otherwise, the default organization name of "unknown" will be printed.

# The expected URL formats for remote URLs are:
# 1. git@github.com:interuss/geospatial-utils.git
# 2. git@github.com/interuss/geospatial-utils.git
# 3. https://github.com/interuss/geospatial-utils.git

# Determine what remote this branch is tracking, in case `origin` and `interuss` don't exist
BACKUP_REPO="https://github.com/$(git rev-parse --abbrev-ref @\{upstream\} 2> /dev/null || echo unknown/_)"

UPSTREAM_REPO=$(git remote get-url origin 2> /dev/null || git remote get-url interuss 2> /dev/null || echo "$BACKUP_REPO")
# Replace `:` by `/` to handle git@github.com:interuss/geospatial-utils.git remote reference.
UPSTREAM_REPO=${UPSTREAM_REPO//:/\/}
# Remove hostname part
UPSTREAM_OWNER=$(dirname "${UPSTREAM_REPO#*github.com/*}")

echo "$UPSTREAM_OWNER"
64 changes: 64 additions & 0 deletions scripts/git/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash

# This script prints the current version of a component in the repository based on the tags
# of the upstream repository (remote origin) matching the following convention:
# owner/component/version. Examples of values:
# - owner: interuss (automatically extracted from the remote origin url)
# - component: rid, scd, aux, uss_qualifier
# - version: v3.0.1[-hash][-dirty]
# - [-hash] (example: -8a493ef8 ) is added when commits have been added to the latest version tagged.
# - [-dirty] (example: -dirty) when the workspace is not clean.
# Only versions without [-hash] and without [-dirty] shall be released.

if [[ $# == 0 ]]; then
echo "Usage: $0 <COMPONENT> [--long]"
echo "Print the component's version number. (ie v0.0.1)"
echo "[--long]: Print the component's version using the long format including the upstream owner (ie interuss/scd/v0.0.1)."
exit 1
fi

COMPONENT=${1}

RELEASE_FORMAT=false
if [[ $2 == "--long" ]]; then
RELEASE_FORMAT=true
fi

# Set working directory
cd "$(dirname "$0")" || exit 1

UPSTREAM_ORG=$(./upstream_owner.sh)

# Look for the last tag of the component
LAST_VERSION_TAG=$(git describe --abbrev=1 --tags --match="${UPSTREAM_ORG}/${COMPONENT}/*" 2> /dev/null)
#echo "LAST_VERSION_TAG: $LAST_VERSION_TAG"

# Store in LAST_VERSION the version of the tag (ie v0.0.1)
LAST_VERSION=${LAST_VERSION_TAG##*/}
#echo "LAST_VERSION: $LAST_VERSION"

# Current commit
COMMIT=$(git rev-parse --short HEAD)

# If no version was found, use default v0.0.0.
if [[ -z "$LAST_VERSION" ]]; then
LAST_VERSION="v0.0.0-$COMMIT"
# Check if there are some commits on top of the tag by checking if an abbrev part is present.
elif [[ "$LAST_VERSION" == *"-"* ]]; then
# Remove abbrev part
LAST_VERSION=${LAST_VERSION%%-*}
# Append the commit hash
LAST_VERSION=${LAST_VERSION}-${COMMIT}
fi

# Set the dirty flag if the workspace is not clean.
DIRTY=""
if test -n "$(git status -s)"; then
DIRTY="-dirty"
fi

if [[ "$RELEASE_FORMAT" == "true" ]]; then
echo "${UPSTREAM_ORG}"/"${COMPONENT}"/"${LAST_VERSION}""${DIRTY}"
else
echo "${LAST_VERSION}""${DIRTY}"
fi
Loading