You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.4 KiB

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