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.

104 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. SED="${SED:-sed -e}"
  57. find "$src_dir" -name "*\.exe" -exec rm -f {} \;
  58. process_filespec "$src_dir" "$dst_dir" "$filespec" || {
  59. echo "process filespec error-ed"
  60. exit 1
  61. }
  62. usr_bin_dir="$dst_dir/usr/bin"
  63. if [ -d "$usr_bin_dir" ] ; then
  64. $SED "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python${ver}," -i --follow-symlinks $usr_bin_dir/*
  65. fi
  66. if [ "$mode" == "sources" ] ; then
  67. # Copy only python source files
  68. find "$dst_dir" -not -type d -not -name "*\.py" -exec rm -f {} \;
  69. delete_empty_dirs "$dst_dir"
  70. exit 0
  71. fi
  72. legacy=
  73. [ "$ver" == "3" ] && legacy="-b"
  74. # XXX [So that you won't goof as I did]
  75. # Note: Yes, I tried to use the -O & -OO flags here.
  76. # However the generated byte-codes were not portable.
  77. # So, we just stuck to un-optimized byte-codes,
  78. # which is still way better/faster than running
  79. # Python sources all the time.
  80. $python -m compileall $legacy -d '/' "$dst_dir" || {
  81. echo "python -m compileall err-ed"
  82. exit 1
  83. }
  84. # Delete source files and pyc [ un-optimized bytecode files ]
  85. # We may want to make this optimization thing configurable later, but not sure atm
  86. find "$dst_dir" -type f -name "*\.py" -exec rm -f {} \;
  87. delete_empty_dirs "$dst_dir"
  88. exit 0