|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +set -eu |
| 20 | + |
| 21 | +resolve_path() { |
| 22 | + CDPATH= cd -- "$1" && pwd |
| 23 | +} |
| 24 | + |
| 25 | +ensure_uv() { |
| 26 | + if command -v uv >/dev/null 2>&1; then |
| 27 | + return 0 |
| 28 | + fi |
| 29 | + |
| 30 | + uv_install_dir=${DEVLAKE_UV_INSTALL_DIR:-${HOME:-$(pwd)}/.local/bin} |
| 31 | + if [ -x "$uv_install_dir/uv" ]; then |
| 32 | + PATH="$uv_install_dir:$PATH" |
| 33 | + export PATH |
| 34 | + return 0 |
| 35 | + fi |
| 36 | + mkdir -p "$uv_install_dir" |
| 37 | + curl -LsSf https://astral.sh/uv/install.sh | env UV_UNMANAGED_INSTALL="$uv_install_dir" sh |
| 38 | + PATH="$uv_install_dir:$PATH" |
| 39 | + export PATH |
| 40 | +} |
| 41 | + |
| 42 | +sync_project() { |
| 43 | + project_dir=$(resolve_path "$1") |
| 44 | + ensure_uv |
| 45 | + cd "$project_dir" |
| 46 | + if [ -d .venv ] && [ ! -x .venv/bin/python ]; then |
| 47 | + rm -rf .venv |
| 48 | + fi |
| 49 | + if [ -x .venv/bin/python ] && ! .venv/bin/python -c "import sys" >/dev/null 2>&1; then |
| 50 | + rm -rf .venv |
| 51 | + fi |
| 52 | + if [ ! -x .venv/bin/python ]; then |
| 53 | + uv venv --python "${DEVLAKE_PYTHON_VERSION:-3.9}" .venv |
| 54 | + fi |
| 55 | + uv pip install --python .venv/bin/python -e . |
| 56 | +} |
| 57 | + |
| 58 | +ensure_project_python() { |
| 59 | + project_dir=$(resolve_path "$1") |
| 60 | + if [ ! -x "$project_dir/.venv/bin/python" ]; then |
| 61 | + sync_project "$project_dir" |
| 62 | + else |
| 63 | + ensure_uv |
| 64 | + fi |
| 65 | + printf '%s/.venv/bin/python\n' "$project_dir" |
| 66 | +} |
| 67 | + |
| 68 | +run_python() { |
| 69 | + project_dir=$(resolve_path "$1") |
| 70 | + shift |
| 71 | + python_bin=$(ensure_project_python "$project_dir") |
| 72 | + exec "$python_bin" "$@" |
| 73 | +} |
| 74 | + |
| 75 | +run_pytest() { |
| 76 | + project_dir=$(resolve_path "$1") |
| 77 | + shift |
| 78 | + ensure_uv |
| 79 | + python_bin=$(ensure_project_python "$project_dir") |
| 80 | + uv pip install --python "$python_bin" pytest |
| 81 | + exec "$python_bin" -m pytest "$@" |
| 82 | +} |
| 83 | + |
| 84 | +usage() { |
| 85 | + echo "Usage: $0 {sync|python|pytest} <project-dir> [args...]" >&2 |
| 86 | + exit 1 |
| 87 | +} |
| 88 | + |
| 89 | +command_name=${1:-} |
| 90 | +[ -n "$command_name" ] || usage |
| 91 | +shift |
| 92 | + |
| 93 | +case "$command_name" in |
| 94 | + sync) |
| 95 | + [ $# -eq 1 ] || usage |
| 96 | + sync_project "$1" |
| 97 | + ;; |
| 98 | + python) |
| 99 | + [ $# -ge 2 ] || usage |
| 100 | + project_dir=$1 |
| 101 | + shift |
| 102 | + run_python "$project_dir" "$@" |
| 103 | + ;; |
| 104 | + pytest) |
| 105 | + [ $# -ge 1 ] || usage |
| 106 | + project_dir=$1 |
| 107 | + shift |
| 108 | + run_pytest "$project_dir" "$@" |
| 109 | + ;; |
| 110 | + *) |
| 111 | + usage |
| 112 | + ;; |
| 113 | +esac |
0 commit comments