Advertisement
s243a

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

Apr 3rd, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.79 KB | None | 0 0
  1. unmount_vfs(){
  2.  umount -l $curdir/$rootfs/dev 2>/dev/null
  3.  umount -l $curdir/$rootfs/sys 2>/dev/null
  4.  umount -l $curdir/$rootfs/proc 2>/dev/null
  5.  #umount /mnt/wktaz 2>/dev/null
  6.  #umount /mnt/wksfs 2>/dev/null
  7.  close_file_descriptors
  8.  
  9. }  
  10. close_file_descriptors(){
  11.   exec 10>&-
  12. }
  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_name__ppd=${pkg_name%.tazpkg}    
  99.   pkg_name__ppd=${pkg_name%.pet}  
  100.   pkg_name__ppd=${pkg_name%.txz}
  101.   pkg_short_name__ppd=${pkg_name%%-*}
  102.   pkg_version__ppd=${pkg_name#*-}
  103. }
  104. inst_pkg_fm_anywhere(){
  105.   set -x
  106.   parse_pkg_dirname $1
  107.   local source_dir="$path__ppd"
  108.   local pkg_name="$pkg_name__ppd"
  109.   local pkg_short_name="$pkg_short_name__ppd"
  110.   local dest_root=${2:-"$curdir/$s_rootfs"} #TODO make the default more robust with regards to slashes
  111.   if [ "$dest_root" = "/" ]; then
  112.      echo "warning about to install package into system rootfs"
  113.      #read -p "Press enter to continue"
  114.   fi
  115.  
  116.   local pkg_name=${3:-`basename $1`} #TODO add logic for different location of files.list
  117.   pkg_name=${pkg_name%%.*}
  118.   if [ -d "$source_dir/fs" ]; then
  119.     source_dir_fs="$source_dir/fs"
  120.   else
  121.     source_dir_fs="$source_dir"
  122.   fi
  123.   system_root_check_and_warn \
  124.       $source_dir_fs -m="source_dir_fs=$source_dir_fs" \
  125.       $dest_root -m="dest_root=$dest_root"
  126.   set +x
  127.   echo "  cp --remove-destination -arf $source_dir_fs/*  $dest_root/ 2>/dev/null"
  128.   #read -p "Press enter to continue"
  129.   cp --remove-destination -arf $source_dir_fs/* \
  130.                                  $dest_root/ 2>/dev/null
  131.  
  132.   cd "$source_dir"
  133.   find . -mindepth 1 -name '*' > "$source_dir/$pkg_name" #"$dest_file_list_prefixed"
  134.   update_pkg_specs "$source_dir" "$dest_root"
  135. }
  136.  
  137. update_pkg_specs(){
  138.   parse_pkg_dirname "$1"
  139.   local source_dir="$path__ppd"
  140.   local pkg_name="$pkg_name__ppd"
  141.   local pkg_short_name="$pkg_short_name__ppd"
  142.   local pkg_version="$pkg_version__ppd"
  143.  
  144.   local dest_root=${2:-"$curdir/$s_rootfs"} #TODO make the default more robust with regards to slashes
  145.   if [ ! -z "$3" ]; then
  146.     spec_types_arry_name=$3
  147.   else
  148.     spec_types_arry=( "pet" "tazpkg" )
  149.     spec_types_arry_name="spec_types_arry"
  150.   fi
  151.  
  152.  
  153.   mkdir -p "$dest_root/root/.packages/builtin_files"
  154.   mkdir -p "$dest_root/var/lib/tazpkg/installed/$pkg_short_name"  
  155.   for spec_type in $(eval 'echo "${'$spec_types_arry_name'[@]}"'); do
  156.       case "$spec_type" in
  157.       pet|.pet)
  158.         pet_specs_fm_dir "$source_dir" "$dest_root"/
  159.         cp "$source_dir/pet.specs" \
  160.            "$dest_root/root/.packages/builtin_files/$pkg_short_name"
  161.         ;;
  162.       tazpkg)
  163.         tazpkg_specs_fm_dir "$source_dir"
  164.        
  165.         cp "$source_dir/$pkg_name" "$dest_root/var/lib/tazpkg/installed/$pkg_short_name/files.list"
  166.         ;;
  167.       esac
  168.   done
  169. }
  170. system_root_check_and_warn(){
  171.   args=()
  172.   #let count=0
  173.   let arg_count=0
  174.   for arg in "$@"; do
  175.      if [[ "$arg" = "-m="* ]]; then
  176.        message=${arg#-m=}
  177.        args[-1]="$message"
  178.      else  
  179.        args+=( "$arg" "arg$arg_count=$arg" )
  180.      fi
  181.      #let count++
  182.      let arg_count++
  183.   done
  184.   while read a b; do
  185.     if [ "$a" = "/" ]; then
  186.       echo "Warning! Action may involve system root:"
  187.       echo "$b"
  188.       read -p "Press enter to continue"
  189.     fi
  190.   done < <( "${args[@]}" )
  191. }
  192. tazpkg_specs_fm_dir(){
  193.   parse_pkg_dirname "$1"
  194.   local pkg_path="$path__ppd"
  195.   local pkg_name="$pkg_name__ppd"
  196.   local pkg_short_name="$pkg_short_name__ppd"
  197.   local pkg_version="$pkg_version__ppd"
  198.  
  199.   local receipt_target_root=${2:-"$curdir/$s_rootfs"}
  200.  
  201.   if [ ! "$pkg_path/receipt" ]; then
  202.     mk_tazpkg_receipt $pkg_path
  203.   fi
  204.   #https://unix.stackexchange.com/questions/266627/bash-source-without-polluting-own-namespace-get-variables-from-other-scripts/477443
  205.   set +x
  206.   eval $(source "$pkg_path/recepit" &> /dev/null;
  207.        echo pkg_short_name=${PACKAGE:-"$pkg_short_name"};
  208.        echo pkg_version=${VERSION:-"$pkg_version"};)
  209.   set -x
  210.   local pkg_name2="$pkg_short_name"
  211.   if [ ! -z "$receipt_target_root" ]; then
  212.     if [ "$receipt_target_root" = "/" ]; then
  213.       echo "Warning: installing package receipt to system rootfs"
  214.       #read -p "Press enter to continue"
  215.     fi
  216.     receipt_target=$receipt_target_root/var/lib/tazpkg/installed/$pkg_name2/receipt
  217.     mkdir -p `dirname "$receipt_target"`
  218.    
  219.     system_root_check_and_warn \
  220.       $pkg_path -m="pkg_path/receipt=$pkg_path/receipt" \
  221.       $receipt_target_root -m="receipt_target=$receipt_target"
  222.      
  223.     cp -f "$pkg_path/receipt" "$receipt_target"
  224.   fi
  225.   set +x
  226. }
  227. pet_specs_fm_dir(){
  228.   set -x
  229.   parse_pkg_dirname "$1"
  230.   local source_dir="$path__ppd"
  231.   local pkg_name="$pkg_name__ppd"
  232.   local pkg_short_name="$pkg_short_name__ppd"
  233.   local pkg_version="$pkg_version__ppd"
  234.  
  235.   local receipt_target_root=${2:-"$curdir/$s_rootfs"}
  236.   #if [ -z "$3" ]; then
  237.   #  local target_pet_spec_path="$file_spec_list_path"
  238.   #else
  239.   #  local installer=${3:-woof}
  240.   #  local target_pet_spec_path="$receipt_target_root/root/.packages/$installer"'-installed-packages'
  241.   #fi
  242.     local installer=${3:-woof}
  243.     local target_pet_spec_path="$receipt_target_root/root/.packages/$installer"'-installed-packages'
  244.   if [ ! -f "$source_dir/pet.specs" ]; then
  245.      cd "$source_dir/.."
  246.      old_name=`basename "$source_dir"`
  247.      
  248.      if [ -f "$source_dir/receipt" ]; then
  249.        eval $(source "$source_dir/receipt" &> /dev/null;
  250.               echo pkg_short_name=${PACKAGE:-"$pkg_short_name"};
  251.               echo pkg_version=${VERSION:-"$pkg_version"};
  252.               echo pkg_category=$CATEGORY;
  253.               echo pkg_depends=$DEPENDS;)
  254.        mv "$old_name" "$pkg_short_name"-"$pkg_version"
  255.        dir2pet -g -s -p="$pkg_name" -c="$pkg_category" #-d="$pkg_depends" TODO Need to fix the format of depends option    
  256.        mv "$pkg_short_name"-"$pkg_version" "$old_name"
  257.  
  258.      else
  259.      
  260.        mv "$old_name" "$pkg_name"
  261.        dir2pet -g -s -p="$pkg_name"
  262.        mv "$pkg_name" "$old_name"
  263.      fi
  264.   fi  
  265.   set +x
  266.  
  267.   if [ -f "$source_dir/pet.specs" ]; then #&& [ -d "$receipt_target_root/var" ]; then
  268.  
  269.     if [ -f "$target_pet_spec_path" ]; then
  270.       echo "TODO: maybe we need a forced option if the package exists"
  271.       #TODO maybe remove old package before deleting.
  272.       delete_petspec_fm_file "$pkg_name" "$dest_root" "$spec_list_name" #"1" We don't need to specify the field
  273.     else
  274.       mkdir -p "$receipt_target_root/root/.packages"
  275.       touch "$target_pet_spec_path"
  276.     fi
  277.     cat "$source_dir/pet.specs" >> "$target_pet_spec_path"
  278.   fi
  279.  
  280. }
  281. copy_pet_specs(){
  282.   local spec_target=$1
  283.   if [ -z "$spec_target" ]; then
  284.     spec_target="$inst_pkg_specs_target"
  285.   fi
  286.   AWK_PRG="BEGIN{FS=\"|\"}
  287.  {
  288.    if ( \$2 == \"$app\" ) {
  289.     print
  290.    }
  291.  }"
  292.   file_spec_list=( "$rootfs_full/root/.packages/"*"-installed-packages" )
  293.   #cat "${file_spec_list[@]}" | awk -F'|' "$AWK_PRG" #'{print $2;}'
  294.   spec=$( cat "${file_spec_list[@]}" | awk "$AWK_PRG"  >> "$spec_target" )
  295.   match_cnt=$( grep -cF "$spec" "$spec_target" )
  296.   if [ $match_cnt -eq 0 ]; then
  297.     echo "$spec" >> "$spec_target"
  298.   fi
  299. }
  300. copy_built_in(){
  301.   set -x
  302.   app=$1
  303.   file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  304.   if [ ! -f "$file_list" ] && [ $retry -eq 1 ]; then
  305.     file_list="$alt_s_rootfs/root/.packages/builtin_files/$app"
  306.     using_alt_list=1
  307.   else
  308.     using_alt_list=0
  309.   fi
  310.   if [ "${target:0:1}" = "/" ]; then
  311.     target_root="$target"      
  312.   else
  313.     target_root="$curdir/$target"    
  314.   fi  
  315.   target_file_list="$target_root/root/.packages/builtin_files/$app"  
  316.   echo "file_list=$file_list"
  317.   mkdir -p /tmp/trim_puppy
  318.   exec 10<> /tmp/trim_puppy/fd_10
  319.   subdir="/"
  320.   mkdir -p /tmp/trim_puppy/
  321.   rm "/tmp/trim_puppy/$app"
  322.   touch "/tmp/trim_puppy/$app"
  323.  
  324.   #mkdir -p
  325.   inst_pkg_specs_target="$target_root/root/.packages/"$arr_name"-installed-packages"
  326.   mkdir -p `dirname "$inst_pkg_specs_target"`
  327.   copy_pet_specs
  328.  
  329.   while IFS=$'\n' read  -r -d $'\n' -u10 line ; do
  330.        line=`echo "$line" | tr -d '[:space:]'`
  331.        if [ ! "${line:0:1}" = "/" ]; then
  332.            line=$subdir$line
  333.        fi
  334.        echo $line>>"/tmp/trim_puppy/$app"
  335.        if [ "${target:0:1}" = "/" ]; then
  336.          target_prefixed="$target$line"
  337.          #target_root="$target" #Defined above      
  338.        else
  339.          #target_root="$curdir/$target" #Defined above
  340.          target_prefixed="$curdir/$target$line"      
  341.        fi  
  342.  
  343.        if [ "${s_rootfs:0:1}" = "/" ]; then
  344.          source_prefixed="$s_rootfs$line"
  345.          cd "$s_rootfs" #this is necessary for the cpio command below    
  346.        else
  347.          source_prefixed="$curdir/$s_rootfs$line"    
  348.          cd "$curdir/$s_rootfs"  #this is necessary for the cpio command below
  349.        fi  
  350.  
  351.     if [ -d "$source_prefixed" ]; then
  352.        subdir="$line"
  353.        echo ".$line" | cpio -pd "$target_root"
  354.        subdir=${subdir%/}/
  355.     else
  356.        target_dir=`dirname $target_prefixed`
  357.        if [ ! -d "$target_dir" ]; then
  358.         source_dir=`dirname $line`
  359.         echo ".$source_dir" | cpio -pd "$target_root"      
  360.        fi
  361.        if [ ! -f "$source_prefixed" ] && [ $retry -eq 1 ]; then
  362.            source_prefixed="$alt_s_rootfs$line"
  363.            cp -a -u "$source_prefixed" "$target_prefixed"
  364.        elif [ ! "$arr_action" = "mv" ]; then
  365.          cp -a -u "$source_prefixed" "$target_prefixed"
  366.        else
  367.          mv -uf "$source_prefixed" "$target_prefixed"
  368.          if [ -f "$source_prefixed" ]; then
  369.            rm "$source_prefixed"
  370.          fi
  371.        fi
  372.     fi
  373.    
  374.   done 10< <( cat "$file_list" )
  375.   exec 10>&-
  376.   set +x
  377.   if [ ! "$arr_action" = "mv" ]; then
  378.     mkdir -p `dirname "$target_file_list"`
  379.     cp -a -u "$file_list" "$target_file_list"
  380.   else
  381.     mv -uf "$file_list" "$target_file_list"
  382.     sed -i "\%|${app}|%d" "$curdir/$s_rootfs/root/.packages/"*"-installed-packages"
  383.     if [ -f "$file_list" ]; then
  384.       rm "$file_list"
  385.     fi
  386.   fi
  387. }
  388. dissect_rootfs(){
  389.   #arr_names=${1:-ALL_ArrNames}
  390.   arry_names_name=${1:-ALL_ArrNames}
  391.   for arr_name in $(eval 'echo "${'$arry_names_name'[@]}"'); do
  392.     eval "arr=( \"\${"$arr_name"[@]}\" )"
  393.     eval "arr_action=\$"$arr_name"_action"
  394.     arr_action=${arr_action:-"$action"}
  395.     #Move is faster but perhaps copy is safer
  396.     arr_action=${arr_action:-cp}
  397.     set -x
  398.     eval 'target="$'$arr_name'_target"'
  399.     target=${target:-"$curdir/$arr_name"}
  400.     set +x
  401.     if [ "${s_rootfs:0:1}" = "/" ]; then
  402.       s_rootfs_prefixed="$s_rootfs"    
  403.     else
  404.       s_rootfs_prefixed="$curdir/$s_rootfs"    
  405.     fi  
  406.     for app in "${arr[@]}"; do
  407.       echo "arr_action=$arr_action"
  408.       case "$arr_action" in
  409.       cp|mv|retry)  
  410.           echo "app=$app"
  411.           copy_built_in "$app"        
  412.           ;;
  413.       #mv)
  414.       #    move_built_in()
  415.       #;;
  416.       #pet) #We might also want to convert to other package formats
  417.       #    mk_pet_fm_built_in()
  418.       #;;
  419.       esac
  420.       if [ -f "/tmp/trim_puppy/$app" ] && [ $chroot_remove_builtin -eq 1 ]; then
  421.         file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  422.         rm "$file_list"
  423.         cp "/tmp/trim_puppy/$app" "$file_list"
  424.       fi
  425.       #chroot "$s_rootfs_prefixed" remove_builtin "$app"
  426.     done
  427.   done
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement