s243a

mk-min-taz-functions.sh (5)

Apr 15th, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 16.08 KB | None | 0 0
  1.  
  2.  
  3. #The declair statment is used to check if a function exists
  4. #See: https://stackoverflow.com/questions/85880/determine-if-a-function-exists-in-bash
  5. declare -f -F trap_push > /dev/null && \
  6.   source trap_stack.sh  
  7.  
  8. source bind_functions.sh  
  9. initialize_chroot
  10. #The trap_stack lets us nest multiple trap statments. See:
  11. # https://stackoverflow.com/questions/16115144/save-and-restore-trap-state-easy-way-to-manage-multiple-handlers-for-traps
  12. # https://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
  13. process_pet_specs(){
  14.     local specs_txt=`cat "$1" | head --lines=1`
  15.     #https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
  16.     #http://www.linuxquestions.org/questions/programming-9/bash-shell-script-split-array-383848/#post3270796
  17.     specs=(${specs_txt//|/ })
  18.     process_pet_specs_FULL_NAME=specs[0]   
  19.     process_pet_specs_PKG_NAME=[1]
  20.     process_pet_specs_VERSION=specs[2]
  21.     process_pet_CATEGORY=specs[4]
  22.     process_pet_SIZE=specs[5]  
  23.     process_pet_BRANCH=specs[6] #Repository Folder
  24.     process_pet_FILE_NAME=specs[7] #File Namer
  25.     process_pet_FILE_DEPENDS=specs[8]
  26.     process_pet_FILE_SHORT_DESC=specs[9]
  27.     process_pet_FILE_COMPILED WITHIN=specs[10]
  28.     process_pet_FILE_COMPAT_DISTRO_VER=specs[11]   
  29.     process_pet_REPO_NAME=specs[12]
  30. }
  31.          
  32. mk_tazpkg_receipt(){
  33.   parse_pkg_dirname "$1"
  34.   local pkg_path="$path__ppd"
  35.   local pkg_name="$pkg_name__ppd"
  36.   local pkg_short_name="$pkg_short_ppd"
  37.   local pkg_version="$pkg_version__ppd"
  38.  
  39.     if [ -f "$pkg_path/receipt" ]; then
  40.       process_pet_specs
  41.   echo 'PACKAGE="$process_pet_specs_PKG_NAME"
  42. VERSION="$process_pet_specs_VERSION"
  43. CATEGORY="$process_pet_CATEGORY"
  44. SHORT_DESC="$process_pet_SHORT_DESC"
  45. WEB_SITE="$web_site"
  46. MAINTAINER="$maintainer"
  47. DEPENDS="$process_pet_FILE_DEPENDS"
  48. ' > $pkg_path/receipt #TODO add che for pkgmanager type and suport for toher package managers    
  49.     fi
  50. }
  51. delete_petspec_fm_file(){
  52.  
  53.   local field_val=$1
  54.   local pkg_rootfs=${2:-"$s_rootfs"}
  55.   local spec_list_name=${3:-'*-packages'}
  56.   if [ -z "$4" ]; then
  57.     if [[ $field_val == *"-"* ]]; then
  58.       local field=1
  59.     else
  60.       local field=2
  61.     fi
  62.   else
  63.     local field="$4"
  64.   fi
  65.   #TODO if prk_rootfs="/" or "" give warning
  66.   #TODO Give the option to compare more than one field. #https://www.gnu.org/software/gawk/manual/html_node/Boolean-Ops.html
  67.   #TODO consider how to deal with miskes like dashes when there should be
  68.   AWK_PRG="BEGIN{FS=\"|\"}
  69.  {
  70.    if ( \$$field != \"$field_val\" ) {
  71.     print
  72.    }
  73.  }"
  74.   #TODO we probably need logic to expand wildards
  75.    
  76.  
  77.   local file_spec_lists=( `ls -1 "$pkg_rootfs/root/.packages/$spec_list_name"` ) 2>/dev/null
  78.   mkdir -p /tmp/trim_puppy/
  79.   for spec_list in "${file_spec_lists[@]}"; do
  80.     spec_name=`basename "$spec_list"`
  81.     cat "$spec_list" | awk "$AWK_PRG"  >> "/tmp/trim_puppy/$spec_name"
  82.     echo "about to"
  83.     echo "rm \"$spec_list\"
  84.    mv  \"/tmp/trim_puppy/$spec_name\" \"$spec_list\"
  85.    "
  86.     #read -p "Press enter to continue"
  87.     set -x    
  88.     rm "$spec_list"
  89.     mv  "/tmp/trim_puppy/$spec_name" "$spec_list"
  90.     set +x    
  91.     #read -p "Press enter to continue"
  92.   done
  93.  
  94. }
  95. parse_pkg_dirname(){
  96.   path__ppd=$1
  97.   pkg_name__ppd=${pkg_name%.extracted}
  98.   pkg_type__ppd=${pkg_name__ppd##*.}
  99.   pkg_name__ppd=${pkg_name%"$pkg_type__ppd"}
  100.   pkg_name__ppd=${pkg_name%.tazpkg}    
  101.   pkg_name__ppd=${pkg_name%.pet}  
  102.   pkg_name__ppd=${pkg_name%.txz}
  103.   pkg_name__ppd=${pkg_name%.deb}
  104.   pkg_short_name__ppd=${pkg_name%%-*}
  105.   pkg_version__ppd=${pkg_name#*-}
  106. }
  107. inst_pkg_fm_anywhere(){
  108.   set -x
  109.   parse_pkg_dirname $1
  110.   source_dir="$path__ppd" #local
  111.   pkg_type="$pkg_type__ppd"
  112.   pkg_name="$pkg_name__ppd" #local
  113.   pkg_short_name="$pkg_short_name__ppd" #local
  114.   #local dest_root=...
  115.   dest_root=${2:-"$curdir/$s_rootfs"} #TODO make the default more robust with regards to slashes
  116.   if [ "$dest_root" = "/" ]; then
  117.      echo "warning about to install package into system rootfs"
  118.      #read -p "Press enter to continue"
  119.   fi
  120.  
  121.   local pkg_name=${3:-`basename $1`} #TODO add logic for different location of files.list
  122.   pkg_name=${pkg_name%%.*}
  123.   if [ -d "$source_dir/fs" ]; then
  124.     source_dir_fs="$source_dir/fs"
  125.   else
  126.     source_dir_fs="$source_dir"
  127.   fi
  128.   system_root_check_and_warn \
  129.       $source_dir_fs -m="source_dir_fs=$source_dir_fs" \
  130.       $dest_root -m="dest_root=$dest_root"
  131.   set +x
  132.   echo "  cp --remove-destination -arf $source_dir_fs/*  $dest_root/ 2>/dev/null"
  133.   #read -p "Press enter to continue"
  134.   cp --remove-destination -arf $source_dir_fs/* \
  135.                                  $dest_root/ 2>/dev/null
  136.  
  137.   cd "$source_dir"
  138.   find . -mindepth 1 -name '*' > "$source_dir/$pkg_name" #"$dest_file_list_prefixed"
  139.   update_pkg_specs "$source_dir" "$dest_root"
  140.  
  141.   run_in_chroot
  142. }
  143. do_post_inst(){
  144.   local a_rootfs=$1
  145.   local pkg_type=${pkg_type:-pet}
  146.   initialize_rootfs "$a_rootfs"
  147.   case pkg_type in
  148.   pet)
  149.     pinst_trys=( "pet" "tazpkg" "txz" "deb" )
  150.     ;;
  151.   tazpkg)
  152.     pinst_trys=( "tazpkg" "pet" "txz" "deb" )
  153.     ;;
  154.   txz)
  155.     pinst_trys=( "pet" "tazpkg" "txz" "deb" )
  156.     ;;
  157.   deb)
  158.     pinst_trys=( "pet" "tazpkg" "txz" "deb" )
  159.     ;;  
  160.   esac
  161.   for a_pinst in  "${pinst_trys[@]}"; do
  162.     case a_pinst in
  163.     pet) #A slitaz package
  164.       if [ -z "$source_dir/pinstall.sh" ]; then
  165.         cp $source_dir/pinstall.sh $dest_root/pinstall.sh
  166.         run_in_chroot $pinstall.sh
  167.         break
  168.       fi
  169.       ;;
  170.     tazpkg) #A slitaz package
  171.       if [ -z "$source_dir/receipt" ] && [ $(grep -cF "post_install()") -gt 0 ]; then
  172.         echo "
  173.          . /etc/slitaz/slitaz.conf #Not sure if this is needed (s243a)
  174.          . /etc/slitaz/tazpkg.conf #Not sure if this is needed (s243a)
  175.          . /lib/libtaz.sh          #Not sure if this is needed (s243a)
  176.          . /usr/lib/slitaz/libpkg.sh         #Not sure if this is needed (s243a)
  177.          . /usr/libexec/tazpkg/find-depends  #Not sure if this is needed (s243a)
  178.          source /var/lib/tazpkg/installed/$pkg_name/receipt #Not sure if this is needed (s243a)
  179.          post_instal" > $dest_root/start.sh
  180.         cp $source_dir/pinstall.sh $dest_root/pinstall.sh
  181.         chmod +x $dest_root/start.sh          
  182.         run_in_chroot start.sh
  183.         ;;
  184.         break  
  185.       fi        
  186.       ;;
  187.     txz) #A slackware package
  188.       #TODO
  189.       ;;
  190.     deb) #A debian package
  191.       #TODO        
  192.       ;;
  193.     esac  
  194.   done
  195. }
  196. update_pkg_specs(){
  197.   parse_pkg_dirname "$1"
  198.   local source_dir="$path__ppd"
  199.   local pkg_name="$pkg_name__ppd"
  200.   local pkg_short_name="$pkg_short_name__ppd"
  201.   local pkg_version="$pkg_version__ppd"
  202.   local post_install_script_run=0 #Consider having different install modes
  203.   #For example maybe we want to run both a pinstall.sh as well as the tazpkg
  204.   #post install script
  205.   POST_INSTALL_MODE=${POST_INSTALL_MODE:-first} #e.g (first, all, non
  206.   local dest_root=${2:-"$curdir/$s_rootfs"} #TODO make the default more robust with regards to slashes
  207.   if [ ! -z "$3" ]; then
  208.     spec_types_arry_name=$3
  209.   else
  210.     spec_types_arry=( "pet" "tazpkg" )
  211.     spec_types_arry_name="spec_types_arry"
  212.   fi
  213.  
  214.  
  215.   mkdir -p "$dest_root/root/.packages/builtin_files"
  216.   mkdir -p "$dest_root/var/lib/tazpkg/installed/$pkg_short_name"  
  217.   for spec_type in $(eval 'echo "${'$spec_types_arry_name'[@]}"'); do
  218.       case "$spec_type" in
  219.       pet|.pet)
  220.         pet_specs_fm_dir "$source_dir" "$dest_root"/
  221.         cp "$source_dir/pet.specs" \
  222.            "$dest_root/root/.packages/builtin_files/$pkg_short_name"
  223.         ;;
  224.       tazpkg)
  225.         tazpkg_specs_fm_dir "$source_dir"
  226.        
  227.         cp "$source_dir/$pkg_name" "$dest_root/var/lib/tazpkg/installed/$pkg_short_name/files.list"
  228.         ;;
  229.       esac
  230.   done
  231. }
  232. system_root_check_and_warn(){
  233.   args=()
  234.   #let count=0
  235.   let arg_count=0
  236.   for arg in "$@"; do
  237.      if [[ "$arg" = "-m="* ]]; then
  238.        message=${arg#-m=}
  239.        args[-1]="$message"
  240.      else  
  241.        args+=( "$arg" "arg$arg_count=$arg" )
  242.      fi
  243.      #let count++
  244.      let arg_count++
  245.   done
  246.   while read a b; do
  247.     if [ "$a" = "/" ]; then
  248.       echo "Warning! Action may involve system root:"
  249.       echo "$b"
  250.       read -p "Press enter to continue"
  251.     fi
  252.   done < <( "${args[@]}" )
  253. }
  254. tazpkg_specs_fm_dir(){
  255.   parse_pkg_dirname "$1"
  256.   local pkg_path="$path__ppd"
  257.   local pkg_name="$pkg_name__ppd"
  258.   local pkg_short_name="$pkg_short_name__ppd"
  259.   local pkg_version="$pkg_version__ppd"
  260.  
  261.   local receipt_target_root=${2:-"$curdir/$s_rootfs"}
  262.  
  263.   if [ ! "$pkg_path/receipt" ]; then
  264.     mk_tazpkg_receipt $pkg_path
  265.   fi
  266.   #https://unix.stackexchange.com/questions/266627/bash-source-without-polluting-own-namespace-get-variables-from-other-scripts/477443
  267.   set +x
  268.   eval $(source "$pkg_path/recepit" &> /dev/null;
  269.        echo pkg_short_name=${PACKAGE:-"$pkg_short_name"};
  270.        echo pkg_version=${VERSION:-"$pkg_version"};)
  271.   set -x
  272.   local pkg_name2="$pkg_short_name"
  273.   if [ ! -z "$receipt_target_root" ]; then
  274.     if [ "$receipt_target_root" = "/" ]; then
  275.       echo "Warning: installing package receipt to system rootfs"
  276.       #read -p "Press enter to continue"
  277.     fi
  278.     receipt_target=$receipt_target_root/var/lib/tazpkg/installed/$pkg_name2/receipt
  279.     mkdir -p `dirname "$receipt_target"`
  280.    
  281.     system_root_check_and_warn \
  282.       $pkg_path -m="pkg_path/receipt=$pkg_path/receipt" \
  283.       $receipt_target_root -m="receipt_target=$receipt_target"
  284.      
  285.     cp -f "$pkg_path/receipt" "$receipt_target"
  286.   fi
  287.   set +x
  288. }
  289. pet_specs_fm_dir(){
  290.   set -x
  291.   parse_pkg_dirname "$1"
  292.   local source_dir="$path__ppd"
  293.   local pkg_name="$pkg_name__ppd"
  294.   local pkg_short_name="$pkg_short_name__ppd"
  295.   local pkg_version="$pkg_version__ppd"
  296.  
  297.   local receipt_target_root=${2:-"$curdir/$s_rootfs"}
  298.   #if [ -z "$3" ]; then
  299.   #  local target_pet_spec_path="$file_spec_list_path"
  300.   #else
  301.   #  local installer=${3:-woof}
  302.   #  local target_pet_spec_path="$receipt_target_root/root/.packages/$installer"'-installed-packages'
  303.   #fi
  304.     local installer=${3:-woof}
  305.     local target_pet_spec_path="$receipt_target_root/root/.packages/$installer"'-installed-packages'
  306.   if [ ! -f "$source_dir/pet.specs" ]; then
  307.      cd "$source_dir/.."
  308.      old_name=`basename "$source_dir"`
  309.      
  310.      if [ -f "$source_dir/receipt" ]; then
  311.        eval $(source "$source_dir/receipt" &> /dev/null;
  312.               echo pkg_short_name=${PACKAGE:-"$pkg_short_name"};
  313.               echo pkg_version=${VERSION:-"$pkg_version"};
  314.               echo pkg_category=$CATEGORY;
  315.               echo pkg_depends=$DEPENDS;)
  316.        mv "$old_name" "$pkg_short_name"-"$pkg_version"
  317.        dir2pet -g -s -p="$pkg_name" -c="$pkg_category" #-d="$pkg_depends" TODO Need to fix the format of depends option    
  318.        mv "$pkg_short_name"-"$pkg_version" "$old_name"
  319.  
  320.      else
  321.      
  322.        mv "$old_name" "$pkg_name"
  323.        dir2pet -g -s -p="$pkg_name"
  324.        mv "$pkg_name" "$old_name"
  325.      fi
  326.   fi  
  327.   set +x
  328.  
  329.   if [ -f "$source_dir/pet.specs" ]; then #&& [ -d "$receipt_target_root/var" ]; then
  330.  
  331.     if [ -f "$target_pet_spec_path" ]; then
  332.       echo "TODO: maybe we need a forced option if the package exists"
  333.       #TODO maybe remove old package before deleting.
  334.       delete_petspec_fm_file "$pkg_name" "$dest_root" "$spec_list_name" #"1" We don't need to specify the field
  335.     else
  336.       mkdir -p "$receipt_target_root/root/.packages"
  337.       touch "$target_pet_spec_path"
  338.     fi
  339.     cat "$source_dir/pet.specs" >> "$target_pet_spec_path"
  340.   fi
  341.  
  342. }
  343. copy_pet_specs(){
  344.   local spec_target=$1
  345.   if [ -z "$spec_target" ]; then
  346.     spec_target="$inst_pkg_specs_target"
  347.   fi
  348.   AWK_PRG="BEGIN{FS=\"|\"}
  349.  {
  350.    if ( \$2 == \"$app\" ) {
  351.     print
  352.    }
  353.  }"
  354.   file_spec_list=( "$rootfs_full/root/.packages/"*"-installed-packages" )
  355.   #cat "${file_spec_list[@]}" | awk -F'|' "$AWK_PRG" #'{print $2;}'
  356.   spec=$( cat "${file_spec_list[@]}" | awk "$AWK_PRG"  >> "$spec_target" )
  357.   match_cnt=$( grep -cF "$spec" "$spec_target" )
  358.   if [ $match_cnt -eq 0 ]; then
  359.     echo "$spec" >> "$spec_target"
  360.   fi
  361. }
  362. copy_built_in(){
  363.   set -x
  364.   app=$1
  365.   file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  366.   if [ ! -f "$file_list" ] && [ $retry -eq 1 ]; then
  367.     file_list="$alt_s_rootfs/root/.packages/builtin_files/$app"
  368.     using_alt_list=1
  369.   else
  370.     using_alt_list=0
  371.   fi
  372.   if [ "${target:0:1}" = "/" ]; then
  373.     target_root="$target"      
  374.   else
  375.     target_root="$curdir/$target"    
  376.   fi  
  377.   target_file_list="$target_root/root/.packages/builtin_files/$app"  
  378.   echo "file_list=$file_list"
  379.   mkdir -p /tmp/trim_puppy
  380.   exec 10<> /tmp/trim_puppy/fd_10
  381.   subdir="/"
  382.   mkdir -p /tmp/trim_puppy/
  383.   rm "/tmp/trim_puppy/$app"
  384.   touch "/tmp/trim_puppy/$app"
  385.  
  386.   #mkdir -p
  387.   inst_pkg_specs_target="$target_root/root/.packages/"$arr_name"-installed-packages"
  388.   mkdir -p `dirname "$inst_pkg_specs_target"`
  389.   copy_pet_specs
  390.  
  391.   while IFS=$'\n' read  -r -d $'\n' -u10 line ; do
  392.        line=`echo "$line" | tr -d '[:space:]'`
  393.        if [ ! "${line:0:1}" = "/" ]; then
  394.            line=$subdir$line
  395.        fi
  396.        echo $line>>"/tmp/trim_puppy/$app"
  397.        if [ "${target:0:1}" = "/" ]; then
  398.          target_prefixed="$target$line"
  399.          #target_root="$target" #Defined above      
  400.        else
  401.          #target_root="$curdir/$target" #Defined above
  402.          target_prefixed="$curdir/$target$line"      
  403.        fi  
  404.  
  405.        if [ "${s_rootfs:0:1}" = "/" ]; then
  406.          source_prefixed="$s_rootfs$line"
  407.          cd "$s_rootfs" #this is necessary for the cpio command below    
  408.        else
  409.          source_prefixed="$curdir/$s_rootfs$line"    
  410.          cd "$curdir/$s_rootfs"  #this is necessary for the cpio command below
  411.        fi  
  412.  
  413.     if [ -d "$source_prefixed" ]; then
  414.        subdir="$line"
  415.        echo ".$line" | cpio -pd "$target_root"
  416.        subdir=${subdir%/}/
  417.     else
  418.        target_dir=`dirname $target_prefixed`
  419.        if [ ! -d "$target_dir" ]; then
  420.         source_dir=`dirname $line`
  421.         echo ".$source_dir" | cpio -pd "$target_root"      
  422.        fi
  423.        if [ ! -f "$source_prefixed" ] && [ $retry -eq 1 ]; then
  424.            source_prefixed="$alt_s_rootfs$line"
  425.            cp -a -u "$source_prefixed" "$target_prefixed"
  426.        elif [ ! "$arr_action" = "mv" ]; then
  427.          cp -a -u "$source_prefixed" "$target_prefixed"
  428.        else
  429.          mv -uf "$source_prefixed" "$target_prefixed"
  430.          if [ -f "$source_prefixed" ]; then
  431.            rm "$source_prefixed"
  432.          fi
  433.        fi
  434.     fi
  435.    
  436.   done 10< <( cat "$file_list" )
  437.   exec 10>&-
  438.   set +x
  439.   if [ ! "$arr_action" = "mv" ]; then
  440.     mkdir -p `dirname "$target_file_list"`
  441.     cp -a -u "$file_list" "$target_file_list"
  442.   else
  443.     mv -uf "$file_list" "$target_file_list"
  444.     sed -i "\%|${app}|%d" "$curdir/$s_rootfs/root/.packages/"*"-installed-packages"
  445.     if [ -f "$file_list" ]; then
  446.       rm "$file_list"
  447.     fi
  448.   fi
  449. }
  450. dissect_rootfs(){
  451.   #arr_names=${1:-ALL_ArrNames}
  452.   arry_names_name=${1:-ALL_ArrNames}
  453.   for arr_name in $(eval 'echo "${'$arry_names_name'[@]}"'); do
  454.     eval "arr=( \"\${"$arr_name"[@]}\" )"
  455.     eval "arr_action=\$"$arr_name"_action"
  456.     arr_action=${arr_action:-"$action"}
  457.     #Move is faster but perhaps copy is safer
  458.     arr_action=${arr_action:-cp}
  459.     set -x
  460.     eval 'target="$'$arr_name'_target"'
  461.     target=${target:-"$curdir/$arr_name"}
  462.     set +x
  463.     if [ "${s_rootfs:0:1}" = "/" ]; then
  464.       s_rootfs_prefixed="$s_rootfs"    
  465.     else
  466.       s_rootfs_prefixed="$curdir/$s_rootfs"    
  467.     fi  
  468.     for app in "${arr[@]}"; do
  469.       echo "arr_action=$arr_action"
  470.       case "$arr_action" in
  471.       cp|mv|retry)  
  472.           echo "app=$app"
  473.           copy_built_in "$app"        
  474.           ;;
  475.       #mv)
  476.       #    move_built_in()
  477.       #;;
  478.       #pet) #We might also want to convert to other package formats
  479.       #    mk_pet_fm_built_in()
  480.       #;;
  481.       esac
  482.       if [ -f "/tmp/trim_puppy/$app" ] && [ $chroot_remove_builtin -eq 1 ]; then
  483.         file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  484.         rm "$file_list"
  485.         cp "/tmp/trim_puppy/$app" "$file_list"
  486.       fi
  487.       #chroot "$s_rootfs_prefixed" remove_builtin "$app"
  488.     done
  489.   done
  490. }
Advertisement
Add Comment
Please, Sign In to add comment