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