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