Merge pull request #8361 from jandelgado/add_udptunnel_package
[feed/packages.git] / lang / python / python-package-install.sh
1 #!/bin/sh
2 set -e
3
4 [ -z "$SOURCE_DATE_EPOCH" ] || {
5 PYTHONHASHSEED="$SOURCE_DATE_EPOCH"
6 export PYTHONHASHSEED
7 }
8
9 process_filespec() {
10 local src_dir="$1"
11 local dst_dir="$2"
12 local filespec="$3"
13 echo "$filespec" | (
14 IFS='|'
15 while read fop fspec fperm; do
16 local fop=`echo "$fop" | tr -d ' \t\n'`
17 if [ "$fop" = "+" ]; then
18 if [ ! -e "${src_dir}${fspec}" ]; then
19 echo "File not found '${src_dir}${fspec}'"
20 exit 1
21 fi
22 dpath=`dirname "$fspec"`
23 if [ -z "$fperm" ]; then
24 dperm=`stat -c "%a" ${src_dir}${dpath}`
25 fi
26 mkdir -p -m$dperm ${dst_dir}${dpath}
27 echo "copying: '$fspec'"
28 cp -fpR ${src_dir}${fspec} ${dst_dir}${dpath}/
29 if [ -n "$fperm" ]; then
30 chmod -R $fperm ${dst_dir}${fspec}
31 fi
32 elif [ "$fop" = "-" ]; then
33 echo "removing: '$fspec'"
34 rm -fR ${dst_dir}${fspec}
35 elif [ "$fop" = "=" ]; then
36 echo "setting permissions: '$fperm' on '$fspec'"
37 chmod -R $fperm ${dst_dir}${fspec}
38 fi
39 done
40 )
41 }
42
43 delete_empty_dirs() {
44 local dst_dir="$1"
45 if [ -d "$dst_dir/usr" ] ; then
46 find "$dst_dir/usr" -empty -type d -delete
47 fi
48 }
49
50 ver="$1"
51 src_dir="$2"
52 dst_dir="$3"
53 python="$4"
54 mode="$5"
55 filespec="$6"
56
57 SED="${SED:-sed -e}"
58
59 find "$src_dir" -name "*.exe" -delete
60
61 process_filespec "$src_dir" "$dst_dir" "$filespec" || {
62 echo "process filespec error-ed"
63 exit 1
64 }
65
66 usr_bin_dir="$dst_dir/usr/bin"
67
68 if [ -d "$usr_bin_dir" ] ; then
69 $SED "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python${ver}," -i --follow-symlinks $usr_bin_dir/*
70 fi
71
72 if [ "$mode" == "sources" ] ; then
73 # Copy only python source files
74 find "$dst_dir" -not -type d -not -name "*.py" -delete
75
76 delete_empty_dirs "$dst_dir"
77 exit 0
78 fi
79
80 legacy=
81 [ "$ver" == "3" ] && legacy="-b"
82 # default max recursion is 10
83 max_recursion_level=20
84
85 # XXX [So that you won't goof as I did]
86 # Note: Yes, I tried to use the -O & -OO flags here.
87 # However the generated byte-codes were not portable.
88 # So, we just stuck to un-optimized byte-codes,
89 # which is still way better/faster than running
90 # Python sources all the time.
91 $python -m compileall -r "$max_recursion_level" $legacy -d '/' "$dst_dir" || {
92 echo "python -m compileall err-ed"
93 exit 1
94 }
95
96 # Delete source files and pyc [ un-optimized bytecode files ]
97 # We may want to make this optimization thing configurable later, but not sure atm
98 find "$dst_dir" -type f -name "*.py" -delete
99
100 delete_empty_dirs "$dst_dir"
101
102 exit 0