-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyv.sh
executable file
·635 lines (552 loc) · 17 KB
/
pyv.sh
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/bin/sh
# -*- mode: sh -*-
PYV_DIR="${PYV_DIR:-${HOME_ALT:+$HOME_ALT/local/pyv}}"
PYV_DIR="${PYV_DIR:-$HOME/local/pyv}"
PYV_DISTS_DIR="$PYV_DIR/distributions"
PYV_REPOS_DIR="$PYV_DIR/repositories"
PYV_VENVS_DIR="$PYV_DIR/venvs"
PYV_DIST_DEFAULT_FILE="$PYV_DIR/default_distribution"
PYV_VENV_DEFAULT_FILE="$PYV_DIR/default_virtualenv"
# used/set by pyv later in the script
#PYV_VENV_CUR=
PYV_LINK_TARGET=venv
PYTHON_DIST_URL='https://www.python.org/ftp/python/${version}/Python-${version}.tgz'
_pyv_create_venvs_dir()
{
mkdir -p "$PYV_VENVS_DIR" && return 0
echo >&2 "unable to create '$PYV_VENVS_DIR'"
return 1
}
_pyvenv_create_repos_dir()
{
mkdir -p "$PYV_REPOS_DIR" && return 0
echo >&2 "unable to create '$PYV_REPOS_DIR'"
return 1
}
### distributions
_pyv_dist()
{
if [ $# -eq 0 ]; then
_pyv_dist_list
else
_pyv_dist_set_default "$@"
fi
}
_pyv_dist_list()
{
_pyv_dist_list_from_path
_pyv_dist_list_repositories
}
_pyv_print_python()
(
printf "%s%s:%s:%s\n" "${4:- }" "$1" "$2" "$3"
)
_pyv_dist_list_repositories()
(
if cd "$PYV_DISTS_DIR" 2>/dev/null; then
for d in *; do
[ '*' = "$d" ] && continue
_pyv_python_env "$d" "$PYV_DISTS_DIR" &&
_pyv_print_python "$_pyv_python_env__name" \
"$_pyv_python_env__version" \
"$_pyv_python_env__path" \
"$_pyv_python_env__cur"
done
fi
)
_pyv_dist_list_from_path()
(
_pyv_dist_list_from_path__python=`which python 2>/dev/null`
[ $? -ne 0 ] && return 0
_pyv_dist_list_from_path__path="${_pyv_dist_list_from_path__python%%/python}"
_pyv_dist_list_from_path__version="`$_pyv_dist_list_from_path__python --version 2>&1`" ||
_pyv_dist_list_from_path__version='undefined'
cur=' '
[ "from_path" = "$(_pyv_dist_default)" ] && cur='*'
_pyv_print_python "from_path" "$_pyv_dist_list_from_path__version" \
"$_pyv_dist_list_from_path__path" "$cur"
)
_pyv_dist_default()
{
_pyv_dist_default=`cat "$PYV_DIST_DEFAULT_FILE" 2>/dev/null`
_pyv_dist_default="${_pyv_dist_default:-from_path}"
echo "$_pyv_dist_default"
}
_pyv_dist_set_default()
{
if [ "$1" = 'from_path' ]; then
rm -f "$PYV_DIST_DEFAULT_FILE"
else
[ -d "$PYV_DISTS_DIR/$1" ] || return 1
echo "$1" > "$PYV_DIST_DEFAULT_FILE"
fi
}
_pyv_dist_exec()
(
_pyv_dist_exec__dist="${1:-$(_pyv_dist_default)}"
shift
_pyv_dist_exec__path="$PATH"
# _pyv_dist_exec__ld_path="$LD_LIBRARY_PATH"
if [ "$_pyv_dist_exec__dist" != 'from_path' ]; then
_pyv_dist_exec__dist_p="$PYV_DISTS_DIR/$_pyv_dist_exec__dist"
_pyv_dist_exec__path="$_pyv_dist_exec__dist_p/bin:$_pyv_dist_exec__path"
# _pyv_dist_exec__ld_path="$_pyv_dist_exec__dist_p/lib:$_pyv_dist_exec__ld_path"
if [ ! -x "$_pyv_dist_exec__dist_p/bin/python" ]; then
printf >&2 "python command is not available at $_pyv_dist_exec__dist_p/bin/python\n"
return 1
fi
fi
PATH="$_pyv_dist_exec__path"
# LD_LIBRARY_PATH="$_pyv_dist_exec__ld_path"
eval "$@"
)
_pyv_dist_cur_exec()
{
_pyv_dist_exec "$(_pyv_dist_default)" "$@"
}
_pyv_dist_fix()
(
for dist in "$PYV_DISTS_DIR"/*; do
if cd "$dist/bin" 2>/dev/null; then
if [ -x 'python' ] || [ -L 'python' ]; then
continue
fi
echo $PWD: ln -s python[0-9]*.*[0-9] python
ln -s python[0-9]*.*[0-9] python
fi
done
)
_pyv_dist_build()
(
_pyv_dist_build__tmpdir=`mktemp -d` || return 1
cd "$_pyv_dist_build__tmpdir" &&
_pyv_dist_build_process "$@"
set +e
rm -Rf "$_pyv_dist_build__tmpdir"
)
_pyv_dist_build_process()
(
if [ -n "$LDFLAGS" ]; then
echo >&2 "WARN: LDFLAGS is set to \"$LDFLAGS\""
echo >&2 " it will override build mechanism"
fi
_pyv_dist_build_process__incs=
_pyv_dist_build_process__libs=
_pyv_dist_build_process__pkgs=
_pyv_dist_build_process__ssl=
_pyv_dist_build_process__bifs="$IFS"
IFS=":"
for _pyv_dist_build_process__dist in $PYV_BUILD_DISTS; do
[ -z "$_pyv_dist_build_process__dist" ] && continue
[ -d "$_pyv_dist_build_process__dist" ] || {
echo >&2 "WARN: $_pyv_dist_build_process__dist in PYV_BUILD_DISTS doest not exist"
continue
}
if [ -d "${_pyv_dist_build_process__dist}/include" ]; then
_pyv_dist_build_process__incs="${_pyv_dist_build_process__incs} -I${_pyv_dist_build_process__dist}/include"
fi
if [ -d "${_pyv_dist_build_process__dist}/lib" ]; then
_pyv_dist_build_process__libs="${_pyv_dist_build_process__libs} -Wl,-rpath=${_pyv_dist_build_process__dist}/lib"
fi
if [ -d "${_pyv_dist_build_process__dist}/lib/pkgconfig" ]; then
_pyv_dist_build_process__pkgs="${_pyv_dist_build_process__pkgs:+$_pyv_dist_build_process__pkgs:}$_pyv_dist_build_process__dist/lib/pkgconfig"
fi
case $_pyv_dist_build_process__dist in
*openssl*) _pyv_dist_build_process__ssl="$_pyv_dist_build_process__dist" ;;
esac
done
IFS="$_pyv_dist_build_process__bifs"
set -e
version="$1"
eval _pyv_dist_build__url="\"$PYTHON_DIST_URL\""
_pyv_dist_build__file="${_pyv_dist_build__url##*/}"
curl -s -S -L "$_pyv_dist_build__url" -o "$_pyv_dist_build__file"
tar -xzf "$_pyv_dist_build__file" &&
rm "$_pyv_dist_build__file"
cd *
# LDFLAGS="${LDFLAGS} ${PYV_OPENSSL:+-Wl,-rpath=$PYV_OPENSSL/lib}"
PKG_CONFIG_PATH="${_pyv_dist_build_process__pkgs}${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" \
CPPFLAGS="${_pyv_dist_build_process__incs}${CPPFLAGS:+:$CPPFLAGS}" \
LDFLAGS="${_pyv_dist_build_process__libs}${LDFLAGS:+:$LDFLAGS}" \
./configure --prefix "$PYV_DISTS_DIR/${PWD##*/}" ${_pyv_dist_build_process__ssl:+--with-openssl=$_pyv_dist_build_process__ssl} &&
make &&
make install
_pyv_dist_fix
)
_pyv_dist_delete()
{
[ -r "$PYV_DISTS_DIR"/"$1" ] && rm -Rf "$PYV_DISTS_DIR"/"$1"
}
### virtualenv
_pyv_venv_get_or_cur()
{
_pyv_venv_get_or_cur="${1:-$PYV_VENV_CUR}"
[ -z "$_pyv_venv_get_or_cur" ] && return 1
return 0
}
_pyv_venv_get_or_path()
{
_pyv_venv_get_or_path="${1:-${PWD##*/}}"
[ -z "$_pyv_venv_get_or_path" ] && return 1
return 0
}
_pyv_python_env()
{
_pyv_python_env__cur=
_pyv_python_env__version=
_pyv_python_env__dist=
_pyv_python_env__name="$1"
[ -n "$_pyv_python_env__name" ] || return 1
_pyv_python_env__path="${2:-$PYV_VENVS_DIR}/$_pyv_python_env__name"
[ -d "$_pyv_python_env__path" ] || return 1
_pyv_python_env__version="`$_pyv_python_env__path/bin/python --version 2>&1`" ||
_pyv_python_env__version='undefined'
_pyv_python_env__version="${_pyv_python_env__version%% \(*}"
_pyv_python_env__dist="`readlink -e $_pyv_python_env__path/bin/python`"
_pyv_python_env__dist="${_pyv_python_env__dist%/bin/python*}"
if [ "$_pyv_python_env__dist" = "$_pyv_python_env__path" ]; then
[ "$_pyv_python_env__name" = "$(_pyv_dist_default)" ] && _pyv_python_env__cur=*
else
[ "$_pyv_python_env__name" = "$PYV_VENV_CUR" ] && _pyv_python_env__cur=*
fi
return 0
}
_pyv_list_venvs()
(
if cd "$PYV_VENVS_DIR"; then
for venv in *; do
[ '*' = "$venv" ] && continue
_pyv_python_env "$venv" &&
_pyv_print_python "$_pyv_python_env__name" \
"$_pyv_python_env__version" \
"$_pyv_python_env__path" \
"$_pyv_python_env__cur"
done
fi
)
_pyv_venv_set_cur()
{
PYV_VENV_CUR="$1"
VIRTUAL_ENV="$PYV_VENVS_DIR/$1"
export VIRTUAL_ENV
}
_pyv_venv_unset_cur()
{
PYV_VENV_CUR=
VIRTUAL_ENV=
export VIRTUAL_ENV
}
_pyv_venv_load_default()
{
_pyv_venv_load_default=`cat "$PYV_VENV_DEFAULT_FILE" 2>/dev/null`
[ -n "$_pyv_venv_load_default" ] && _pyv_set "$_pyv_venv_load_default"
}
_pyv_venv_set_default()
{
[ -d "$PYV_VENVS_DIR/$1" ] || return 1
echo "$1" > "$PYV_VENV_DEFAULT_FILE"
if [ -z "$PYV_VENV_CUR" ]; then
_pyv_venv_load_default
fi
}
_pyv_venv()
{
if [ $# -eq 0 ]; then
_pyv_list_venvs
else
_pyv_venv_set_default "$@"
fi
}
_pyv_list()
{
printf "# distributions\n"
_pyv_dist_list
printf "\n"
printf "# virtualenvs\n"
_pyv_list_venvs
}
_pyv_venv_info()
(
_pyv_venv_get_or_cur "$1" || return 1
_pyv_python_env "$_pyv_venv_get_or_cur" || return 1
printf "%13s=%s\n" \
venv_name "$_pyv_python_env__name" \
venv_version "$_pyv_python_env__version" \
venv_path "$_pyv_python_env__path" \
venv_dist "$_pyv_python_env__dist"
)
_pyv_create()
{
[ -z "$1" ] && return 1
_pyv_create="$PYV_VENVS_DIR/$1"
if [ -e "$_pyv_create" ]; then
echo >&2 "$_pyv_create already exists"
return 1
fi
_pyv_create_venvs_dir || return 1
_pyv_dist_exec "${2:-$(_pyv_dist_default)}" "python -m venv --clear \"$_pyv_create\""
# _pyv_dist_exec "${2:-$(_pyv_dist_default)}" "pyvenv \"$_pyv_create\""
_pyv_venv_fix "$_pyv_create"
}
_pyv_venv_is_current()
{
[ "$PYV_VENV_CUR" = "$1" ]
}
_pyv_venv_fix()
(
[ -z "$1" ] && return 1
cd "$1/bin" || return 1
version=`./python --version`
version="${version#* }"
version="${version%% *}"
while [ -n "$version" ]; do
bin="python$version"
[ -x "$bin" ] || ln -s python "$bin"
sub_version="${version##*.}"
version="${version%$sub_version}"
version="${version%.}"
done
)
_pyv_recreate()
{
_pyv_venv_get_or_cur "$1" || return 1
_pyv_recreate="$_pyv_venv_get_or_cur"
_pyv_recreate__version="$2"
if [ -z "$_pyv_recreate__version" ]; then
_pyv_python_env "$_pyv_recreate" &&
_pyv_recreate__version="${_pyv_python_env__dist##*/}"
fi
_pyv_recreate__create_cmd=_pyv_create
_pyv_venv_is_current "$_pyv_recreate" && _pyv_recreate__create_cmd=_pyv_set_or_create_set
_pyv_delete "$_pyv_recreate"
$_pyv_recreate__create_cmd "$_pyv_recreate" "$_pyv_recreate__version"
}
_pyv_delete()
{
_pyv_venv_get_or_cur "$1" || return 1
_pyv_delete="$_pyv_venv_get_or_cur"
_pyv_delete__dir="$PYV_VENVS_DIR/$_pyv_delete"
[ -d "$_pyv_delete__dir" ] || return 1
rm -Rf "$_pyv_delete__dir"
if _pyv_venv_is_current "$_pyv_delete"; then
_pyv_unset
_pyv_venv_load_default
fi
}
_pyv_add_path()
{
_pyv_add_path=$(cd "$1" 2>/dev/null && pwd -P)
[ -z "$_pyv_add_path" ] && return 1
for _pyv_add_path__d in sbin bin; do
_pyv_add_path__d="$_pyv_add_path/$_pyv_add_path__d"
[ -d "$_pyv_add_path__d" ] &&
PATH="$_pyv_add_path__d:$PATH"
done
# for _pyv_add_path__d in lib; do
# _pyv_add_path__d="$_pyv_add_path/$_pyv_add_path__d"
# [ -d "$_pyv_add_path__d" ] &&
# LD_LIBRARY_PATH="$_pyv_add_path__d:$LD_LIBRARY_PATH"
# done
export PATH # LD_LIBRARY_PATH
}
_pyv_remove_path()
{
_pyv_remove_path=$(cd "$1" 2>/dev/null && pwd)
for _pyv_remove_path__d in bin sbin; do
_pyv_remove_path__d="$_pyv_remove_path/$_pyv_remove_path__d"
_pyv_remove_path__front="${PATH%%$_pyv_remove_path__d*}"
_pyv_remove_path__back="${PATH#$_pyv_remove_path__front}"
_pyv_remove_path__back="${_pyv_remove_path__back#$_pyv_remove_path__d}"
_pyv_remove_path__front="${_pyv_remove_path__front%:}"
_pyv_remove_path__back="${_pyv_remove_path__back#:}"
PATH="$_pyv_remove_path__front"
[ -n "$_pyv_remove_path__back" ] &&
PATH="${PATH:+$PATH:}$_pyv_remove_path__back"
done
# for _pyv_remove_path__d in lib; do
# _pyv_remove_path__d="$_pyv_remove_path/$_pyv_remove_path__d"
# _pyv_remove_path__front="${LD_LIBRARY_PATH%%$_pyv_remove_path__d*}"
# _pyv_remove_path__back="${LD_LIBRARY_PATH#$_pyv_remove_path__front}"
# _pyv_remove_path__back="${_pyv_remove_path__back#$_pyv_remove_path__d}"
# _pyv_remove_path__front="${_pyv_remove_path__front%:}"
# _pyv_remove_path__back="${_pyv_remove_path__back#:}"
# LD_LIBRARY_PATH="$_pyv_remove_path__front"
# [ -n "$_pyv_remove_path__back" ] &&
# LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$_pyv_remove_path__back"
# done
export PATH # LD_LIBRARY_PATH
}
_pyv_set()
{
_pyv_venv_get_or_path "$1" || return 1
_pyv_set__short="$_pyv_venv_get_or_path"
[ "$_pyv_set__short" = "$PYV_VENV_CUR" ] && return 0
_pyv_set="$PYV_VENVS_DIR/$_pyv_set__short"
if [ ! -e "$_pyv_set" ]; then
#echo >&2 "$_pyv_set doesn't exist"
return 1
fi
_pyv_unset
_pyv_add_path "$_pyv_set" &&
_pyv_venv_set_cur "$_pyv_set__short"
}
_pyv_unset()
{
_pyv_venv_get_or_cur "$1" || return 1
_pyv_unset__short="$_pyv_venv_get_or_cur"
[ -z "$_pyv_unset__short" ] && return 0
_pyv_unset="$PYV_VENVS_DIR/$_pyv_unset__short"
_pyv_remove_path "$_pyv_unset" &&
_pyv_venv_unset_cur
}
_pyv_create_set()
{
_pyv_venv_get_or_path "$1" || return 1
_pyv_create_set__id="$_pyv_venv_get_or_path"
[ $# -gt 0 ] && shift
_pyv_create "$_pyv_create_set__id" "$@" &&
_pyv_set "$_pyv_create_set__id" "$@"
}
_pyv_set_or_create_set()
{
_pyv_venv_get_or_path "$1"
_pyv_set_or_create_set__id="$_pyv_venv_get_or_path"
[ $# -gt 0 ] && shift
_pyv_set "$_pyv_set_or_create_set__id" "$@" 2>/dev/null && return 0
_pyv_create_set "$_pyv_set_or_create_set__id" "$@"
}
_pyv_link()
{
_pyv_target="${1:-$PYV_LINK_TARGET}"
_pyv_venv_get_or_cur "$2" || return 1
_pyv_link__short="$_pyv_venv_get_or_cur"
[ -z "$_pyv_link__short" ] && return 1
_pyv_link="$PYV_VENVS_DIR/$_pyv_link__short"
ln -sf "$_pyv_link" "$_pyv_target"
}
### help
_pyv_help()
{
cat <<EOF
Usage is :
pyv <action> <parameters>
With distribution action in :
D | dist | dists : list distributions
D | dist | dists <version> : set default distribution
fd | fix_dists : fix distributions
bd | build | build_dist <version> : build specified version
dd | delete_dist <version> : delete specified version
With virtualenv action in :
v | venv | venvs : list virtual env
v | venv | venvs <virtualenv> : set default virtual env
l | list : list distributions and virtualenv
i | info : display info for virtualenv
c | create <virtualenv> : create virtualenv
rc | recreate <virtualenv> : recreate virtualenv
d | delete <virtualenv> : delete virtualenv
s | set <virtualenv> : set virtualenv
u | unset <virtualenv> : unset virtualenv
cs | create_set <virtualenv> : create and set virtualenv
sc | set_or_create_set <virtualenv> : set or create and set virtualenv
Default action is to :
set_or_create_set <virtualenv>
EOF
}
### pyv
_pyv()
{
_pyv=
case $1 in
D|dist|dists|distribution|distributions)
shift
_pyv_dist "$@"
_pyv__ret=$?
;;
[Ff][Dd]|fix_dist|fix_dists|dist_fix|dists_fix)
_pyv_dist_fix
_pyv__ret=$?
;;
build|build_dist|[Bb]|[Bb][Dd])
shift
_pyv_dist_build "$@"
_pyv__ret=$?
;;
delete_dist|[Dd][Dd])
shift
_pyv_dist_delete "$@"
_pyv__ret=$?
;;
v|venv|venvs)
shift
_pyv_venv "$@"
_pyv__ret=$?
;;
l|list|-l)
_pyv_list
_pyv__ret=$?
;;
i|info)
shift
_pyv_venv_info "$@"
_pyv__ret=$?
;;
c|create)
shift
_pyv_create "$@"
_pyv__ret=$?
;;
rc|recreate)
shift
_pyv_recreate "$@"
_pyv__ret=$?
;;
d|delete)
shift
_pyv_delete "$@"
_pyv__ret=$?
;;
s|'set')
shift
_pyv_set "$@"
_pyv__ret=$?
;;
u|'unset')
shift
_pyv_unset "$@"
_pyv__ret=$?
;;
cs|create_set)
shift
_pyv_create_set "$@"
_pyv__ret=$?
;;
sc|set_or_create)
shift
_pyv_set_or_create_set "$@"
_pyv__ret=$?
;;
link)
shift
_pyv_link "$@"
_pyv__ret=$?
;;
-h|h|--help|help)
_pyv_help "$@"
_pyv__ret=$?
;;
*)
_pyv_set_or_create_set "$@"
_pyv__ret=$?
;;
esac
return $_pyv__ret
}
pyv()
{
_pyv "$@"
pyv="$?"
[ -n "$_pyv" ] && printf "%s\n" "$_pyv"
return $pyv
}
_pyv_venv_load_default
: