-
Notifications
You must be signed in to change notification settings - Fork 0
/
pylinks
executable file
·95 lines (83 loc) · 2.41 KB
/
pylinks
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
#!/usr/bin/env bash
#
# pylinks
# set up symlinks in $2 (default /usr/local) to installed python root $1
#
# notes:
# - aborts on attempt to clobber any regular files in target
# - symlinks allowed to exist already in the target, get overwritten
# - audits at finish to make sure all files represented
#
# https://github.com/smemsh/utiladm/
# https://spdx.org/licenses/GPL-2.0
#
set -e
errtrap () { echo "$FUNCNAME: code $1, line: $2, cmd: \"$3\""; exit $1; }
trap 'errtrap $? $LINENO "$BASH_COMMAND"' ERR
if [[ $1 == -t || $1 == --test ]]
then run=echo; shift
else ((EUID == 0))
fi
ln --help |& grep -wPq -- --relative && let ++linkrel || true
pyroot=${1:?}
pyrootdir=${pyroot%/*}
pyrootbase=${pyroot##*/}
[[ $pyroot == $(readlink -f $pyroot) ]]
[[ $pyroot == $pyrootdir/$pyrootbase ]]
test -d $pyroot
pyver=${pyrootbase#*-}
[[ $pyver =~ ^([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)$ ]]
pymajor=${BASH_REMATCH[1]}
pyminor=${BASH_REMATCH[2]}
pypatch=${BASH_REMATCH[3]}
pymmver=python$pymajor.$pyminor
pylibrel=lib/lib$pymmver.a
[[ $pyver == $pymajor.$pyminor.$pypatch ]]
destroot=${2:-/usr/local}
manreldir=share/man/man1
pcreldir=lib/pkgconfig
for dir in bin $pcreldir $manreldir include; do
testdir=$destroot/$dir
if ! test -d $testdir
then mkdir -p $testdir; fi
[[ $(readlink -f $testdir) == $testdir ]]
done
for dir in {include,lib}/$pymmver; do
destpath=$destroot/$dir
pypath=$pyroot/$dir
test -d $pypath
[[ $(readlink -f $pypath) == $pypath ]]
test -L $destpath || ! test -e $destpath
$run ln ${linkrel:+-r} -nsf $pypath $destpath
done
for dir in bin lib/pkgconfig; do
pydir=$pyroot/$dir
destdir=$destroot/$dir
test -d $pydir
for path in $pydir/*; do
file=${path##*/}
destfile=$destdir/$file
test -L $destfile || ! test -e $destfile
$run ln ${linkrel:+-r} -nsf $path $destdir
done
done
destmandir=$destroot/$manreldir
for path in $pyroot/$manreldir/*; do
file=${path##*/}
destfile=$destmandir/$file
test -L $destfile || ! test -e $destfile
$run ln ${linkrel:+-r} -nsf $path $destmandir
done
pylibdest=$destroot/$pylibrel
pylibpath=$pyroot/$pylibrel
test -L $pylibdest || ! test -e $pylibdest
ln ${linkrel:+-r} -nsf $pylibpath $pylibdest
disjuncts=($(comm -1 -3 \
<(find $destroot/ -follow -mindepth 1 -printf '%P\n' | sort) \
<(find $pyroot/ -mindepth 1 -printf '%P\n' | sort) \
))
if ((${#disjuncts[@]})); then
echo "warning: disjunction detected:" >&2
fmt -1 <<< "${disjuncts[@]}"
fi