Advertisement
s243a

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

Apr 2nd, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.11 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. delete_petspec_fm_file(){
  14.  
  15.   local field_val=$1
  16.   local pkg_rootfs=${2:-"$s_rootfs"}
  17.   local spec_list_name=${3:-'*-packages'}
  18.   if [ -z "$4" ]; then
  19.     if [[ $field_val == *"-"* ]]; then
  20.       local field=1
  21.     else
  22.       local field=2
  23.     fi
  24.   else
  25.     local field="$4"
  26.   fi
  27.   #TODO if prk_rootfs="/" or "" give warning
  28.   #TODO Give the option to compare more than one field. #https://www.gnu.org/software/gawk/manual/html_node/Boolean-Ops.html
  29.   #TODO consider how to deal with miskes like dashes when there should be
  30.   AWK_PRG="BEGIN{FS=\"|\"}
  31.  {
  32.    if ( \$$field != \"$field_val\" ) {
  33.     print
  34.    }
  35.  }"
  36.   #TODO we probably need logic to expand wildards
  37.    
  38.  
  39.   local file_spec_lists=( `ls -1 "$pkg_rootfs/root/.packages/$spec_list_name"` ) 2>/dev/null
  40.   mkdir -p /tmp/trim_puppy/
  41.   for spec_list in "${file_spec_lists[@]}"; do
  42.     spec_name=`basename "$spec_list"`
  43.     cat "$spec_list" | awk "$AWK_PRG"  >> "/tmp/trim_puppy/$spec_name"
  44.     echo "about to"
  45.     echo "rm \"$spec_list\"
  46.    mv  \"/tmp/trim_puppy/$spec_name\" \"$spec_list\"
  47.    "
  48.     #read -p "Press enter to continue"
  49.     set -x    
  50.     rm "$spec_list"
  51.     mv  "/tmp/trim_puppy/$spec_name" "$spec_list"
  52.     set +x    
  53.     #read -p "Press enter to continue"
  54.   done
  55.  
  56. }
  57.  
  58. inst_builtin_fm_dir(){
  59.   local source_dir=$1
  60.   local dest_root=${2:-"$curdir/$s_rootfs"} #TODO make the default more robust with regards to slashes
  61.   if [ "$dest_root" = "/" ]; then
  62.      echo "warning about to install package into system rootfs"
  63.      #read -p "Press enter to continue"
  64.   fi
  65.  
  66.   local pkg_name=${3:-`basename $1`} #TODO add logic for different location of files.list
  67.   pkg_name={pkg_name%%.*}
  68.   if [ -d "$source_dir/fs" ]; then
  69.     source_dir_fs="$source_dir/fs"
  70.   else
  71.     source_dir_fs="$source_dir"
  72.   fi
  73.   source_file_list_prefixed="$source_dir_fs/root/.packages/builtin_files/$pkg_name"
  74.   dest_file_list_prefixed="$dest_root/root/.packages/builtin_files/$pkg_name"
  75.   local spec_list_name=${4:-"woof-installed-packages"}
  76.   local file_spec_list_path="$dest_root/root/.packages/$spec_list_name"
  77.   #echo "about to:"
  78.   #echo "  cp --remove-destination -arf $source_dir_fs/*
  79.   #                               $dest_root/ 2>/dev/null"
  80.   #read -p "Press enter to continue"
  81.   cp --remove-destination -arf $source_dir_fs/* \
  82.                                  $dest_root/ 2>/dev/null
  83.   if [ ! -f "$source_file_list_prefixed" ]; then
  84.      cd "$source_dir"
  85.      mkdir -p `dirname "$dest_file_list_prefixed"`
  86.      find . -mindepth 1 -name '*' > "$dest_file_list_prefixed"
  87.   fi
  88.   update_pkg_specs "$source_dir" "$dest_root"
  89. }
  90. update_pkg_specs(){
  91.   local source_dir=$1
  92.   local dest_root=${2:-"$curdir/$s_rootfs"} #TODO make the default more robust with regards to slashes
  93.   if [ ! -z "$3" ]; then
  94.     spec_types_arry_name=$3
  95.   else
  96.     spec_types_arry=( "pet" "tazpkg" )
  97.     spec_types_arry_name="spec_types_arry"
  98.   fi
  99.   for spec_type in $(eval 'echo "${'$spec_types_arry_name'[@]}"'); do
  100.       case "$spec_type" in
  101.       pet|.pet)
  102.         pet_specs_fm_dir "$source_dir"
  103.         ;;
  104.       tazpkg)
  105.         tazpkg_specs_fm_dir "$source_dir"
  106.         #cp
  107.         ;;
  108.       esac
  109.   done
  110. }
  111. process_pet_specs(){
  112.     local specs_txt=`cat "$1" | head --lines=1`
  113.     #https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
  114.     #http://www.linuxquestions.org/questions/programming-9/bash-shell-script-split-array-383848/#post3270796
  115.     specs=(${specs_txt//|/ })
  116.     process_pet_specs_FULL_NAME=specs[0]   
  117.     process_pet_specs_PKG_NAME=[1]
  118.     process_pet_specs_VERSION=specs[2]
  119.     process_pet_CATEGORY=specs[4]
  120.     process_pet_SIZE=specs[5]  
  121.     process_pet_BRANCH=specs[6] #Repository Folder
  122.     process_pet_FILE_NAME=specs[7] #File Namer
  123.     process_pet_FILE_DEPENDS=specs[8]
  124.     process_pet_FILE_SHORT_DESC=specs[9]
  125.     process_pet_FILE_COMPILED WITHIN=specs[10]
  126.     process_pet_FILE_COMPAT_DISTRO_VER=specs[11]   
  127.     process_pet_REPO_NAME=specs[12]
  128. }
  129. mk_tazpkg_receipt(){
  130.    local pkg_path=$1
  131.   local pkg_short_name=${pkg_name%%-*}
  132.   local pkg_version=${pkg_name#*-}
  133.   local pkg_name=`basename $pkg_path`
  134.     if [ -f "$pkg_path/receipt" ]; then
  135.       process_pet_specs
  136.   echo 'PACKAGE="$process_pet_specs_PKG_NAME"
  137. VERSION="$process_pet_specs_VERSION"
  138. CATEGORY="$process_pet_CATEGORY"
  139. SHORT_DESC="$process_pet_SHORT_DESC"
  140. WEB_SITE="$web_site"
  141. MAINTAINER="$maintainer"
  142. DEPENDS="$process_pet_FILE_DEPENDS"
  143. ' > $pkg_path/receipt #TODO add che for pkgmanager type and suport for toher package managers    
  144.     fi
  145. }
  146. tazpkg_specs_fm_dir(){
  147.   local pkg_path=$1
  148.   local receipt_target_root=${2:-"$curdir/$s_rootfs"}
  149.   local pkg_name=`basename $pkg_path`  
  150.   pkg_name=${pkg_name%.extracted}
  151.   pkg_name=${pkg_name%.tazpkg}    
  152.   pkg_name=${pkg_name%.pet}  
  153.   pkg_name=${pkg_name%.txz}
  154.   local pkg_short_name=${pkg_name%%-*}
  155.   local pkg_version=${pkg_name#*-} #TODO append arch to version
  156.  
  157.   if [ ! "$pkg_path/receipt" ]; then
  158.     mk_tazpkg_receipt $pkg_path
  159.   fi
  160.   #https://unix.stackexchange.com/questions/266627/bash-source-without-polluting-own-namespace-get-variables-from-other-scripts/477443
  161.   set +x
  162.   eval $(source "$pkg_path/recepit" &> /dev/null;
  163.        echo pkg_short_name=${PACKAGE:-"$pkg_short_name"};
  164.        echo pkg_version=${VERSION:-"$pkg_version"};)
  165.   set -x
  166.   local pkg_name2="$pkg_short_name"
  167.   if [ ! -z "$receipt_target_root" ]; then
  168.     if [ "$receipt_target_root" = "/" ]; then
  169.       echo "Warning: installing package receipt to system rootfs"
  170.       #read -p "Press enter to continue"
  171.     fi
  172.     receipt_target=$receipt_target_root/var/lib/tazpkg/installed/$pkg_name2/receipt
  173.     mkdir -p `dirname "$receipt_target"`
  174.     cp -f "$pkg_path/receipt" "$receipt_target"
  175.   fi
  176.   set +x
  177. }
  178. pet_specs_fm_dir(){
  179.   set -x
  180.   local source_dir=$1
  181.   local receipt_target_root=${2:-"$curdir/$s_rootfs"}
  182.   if [ -z "$3" ]; then
  183.     local target_pet_spec_path="$file_spec_list_path"
  184.   else
  185.     local installer=${3:-woof}
  186.     local target_pet_spec_path="$receipt_target_root/root/.packages/$installer"'-installed-packages'
  187.   fi
  188.   local pkg_name=`basename $source_dir`  
  189.   pkg_name=${pkg_name%.extracted}
  190.   pkg_name=${pkg_name%.tazpkg}    
  191.   pkg_name=${pkg_name%.pet}  
  192.   pkg_name=${pkg_name%.txz}
  193.   local pkg_short_name=${pkg_name%%-*}
  194.   local pkg_version=${pkg_name#*-} #TODO append arch to version
  195.  
  196.   #if [ ! "$pkg_path/receipt" ]; then
  197.   #  mk_tazpkg_receipt $pkg_path
  198.   #fi  
  199.   #set -x
  200.   if [ ! -f "$source_dir/pet.specs" ]; then
  201.      cd "$source_dir/.."
  202.      old_name=`basename "$source_dir"`
  203.      
  204.      if [ -f "$source_dir/receipt" ]; then
  205.        eval $(source "$source_dir/receipt" &> /dev/null;
  206.               echo pkg_short_name=${PACKAGE:-"$pkg_short_name"};
  207.               echo pkg_version=${VERSION:-"$pkg_version"};
  208.               echo pkg_category=$CATEGORY;
  209.               echo pkg_depends=$DEPENDS;)
  210.        mv "$old_name" "$pkg_short_name"-"$pkg_version"
  211.        dir2pet -g -s -p="$pkg_name" -c="$pkg_category" #-d="$pkg_depends" TODO Need to fix the format of depends option    
  212.        mv "$pkg_short_name"-"$pkg_version" "$old_name"
  213.  
  214.      else
  215.      
  216.        mv "$old_name" "$pkg_name"
  217.        dir2pet -g -s -p="$pkg_name"
  218.        mv "$pkg_name" "$old_name"
  219.      fi
  220.   fi  
  221.   set +x
  222.   #match_cnt=$( grep -cF "$spec" "$inst_pkg_specs_target" )
  223.   #if [ $match_cnt -eq 0 ]; then
  224.   #  spec >> "$inst_pkg_specs_target"
  225.   #fi
  226.   if [ -f "$source_dir/pet.specs" ] && [ -d "$receipt_target_root/var" ]; then
  227.     #All args here except the first are default
  228.     #echo "about to:"
  229.     #echo "  delete_petspec_fm_file \"$pkg_name\" \"$dest_root\" \"$spec_list_name\" \"1\""
  230.     #read -p "Press enter to continue"
  231.     if [ -f "$target_pet_spec_path" ]; then
  232.       echo "TODO: maybe we need a forced option if the package exists"
  233.       #TODO maybe remove old package before deleting.
  234.       delete_petspec_fm_file "$pkg_name" "$dest_root" "$spec_list_name" #"1" We don't need to specify the field
  235.     else
  236.       mkdir -p "$receipt_target_root/root/.packages"
  237.       touch "$target_pet_spec_path"
  238.     fi
  239.     cat "$source_dir/pet.specs" >> "$target_pet_spec_path"
  240.   fi
  241.   #if [ -d "$receipt_target_root/var" ]; then
  242.   #  mkdir -p "$receipt_target_root/root/.packages"
  243.   #  cat "$source_dir/pet.specs" >> "$receipt_target_root/root/.packages/$installer"'-installed-packages'
  244.   #fi
  245. }
  246. copy_pet_specs(){
  247.   AWK_PRG="BEGIN{FS=\"|\"}
  248.  {
  249.    if ( \$2 == \"$app\" ) {
  250.     print
  251.    }
  252.  }"
  253.   file_spec_list=( "$rootfs_full/root/.packages/"*"-installed-packages" )
  254.   #cat "${file_spec_list[@]}" | awk -F'|' "$AWK_PRG" #'{print $2;}'
  255.   spec=$( cat "${file_spec_list[@]}" | awk "$AWK_PRG"  >> "$inst_pkg_specs_target" )
  256.   match_cnt=$( grep -cF "$spec" "$inst_pkg_specs_target" )
  257.   if [ $match_cnt -eq 0 ]; then
  258.     spec >> "$inst_pkg_specs_target"
  259.   fi
  260. }
  261. copy_built_in(){
  262.   set -x
  263.   app=$1
  264.   file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  265.   if [ ! -f "$file_list" ] && [ $retry -eq 1 ]; then
  266.     file_list="$alt_s_rootfs/root/.packages/builtin_files/$app"
  267.     using_alt_list=1
  268.   else
  269.     using_alt_list=0
  270.   fi
  271.   if [ "${target:0:1}" = "/" ]; then
  272.     target_root="$target"      
  273.   else
  274.     target_root="$curdir/$target"    
  275.   fi  
  276.   target_file_list="$target_root/root/.packages/builtin_files/$app"  
  277.   echo "file_list=$file_list"
  278.   mkdir -p /tmp/trim_puppy
  279.   exec 10<> /tmp/trim_puppy/fd_10
  280.   subdir="/"
  281.   mkdir -p /tmp/trim_puppy/
  282.   rm "/tmp/trim_puppy/$app"
  283.   touch "/tmp/trim_puppy/$app"
  284.  
  285.   #mkdir -p
  286.   inst_pkg_specs_target="$target_root/root/.packages/"$arr_name"-installed-packages"
  287.   mkdir -p `dirname "$inst_pkg_specs_target"`
  288.   copy_pet_specs
  289.  
  290.   while IFS=$'\n' read  -r -d $'\n' -u10 line ; do
  291.        line=`echo "$line" | tr -d '[:space:]'`
  292.        if [ ! "${line:0:1}" = "/" ]; then
  293.            line=$subdir$line
  294.        fi
  295.        echo $line>>"/tmp/trim_puppy/$app"
  296.        if [ "${target:0:1}" = "/" ]; then
  297.          target_prefixed="$target$line"
  298.          #target_root="$target" #Defined above      
  299.        else
  300.          #target_root="$curdir/$target" #Defined above
  301.          target_prefixed="$curdir/$target$line"      
  302.        fi  
  303.  
  304.        if [ "${s_rootfs:0:1}" = "/" ]; then
  305.          source_prefixed="$s_rootfs$line"
  306.          cd "$s_rootfs" #this is necessary for the cpio command below    
  307.        else
  308.          source_prefixed="$curdir/$s_rootfs$line"    
  309.          cd "$curdir/$s_rootfs"  #this is necessary for the cpio command below
  310.        fi  
  311.  
  312.     if [ -d "$source_prefixed" ]; then
  313.        subdir="$line"
  314.        echo ".$line" | cpio -pd "$target_root"
  315.        subdir=${subdir%/}/
  316.     else
  317.        target_dir=`dirname $target_prefixed`
  318.        if [ ! -d "$target_dir" ]; then
  319.         source_dir=`dirname $line`
  320.         echo ".$source_dir" | cpio -pd "$target_root"      
  321.        fi
  322.        if [ ! -f "$source_prefixed" ] && [ $retry -eq 1 ]; then
  323.            source_prefixed="$alt_s_rootfs$line"
  324.            cp -a -u "$source_prefixed" "$target_prefixed"
  325.        elif [ ! "$arr_action" = "mv" ]; then
  326.          cp -a -u "$source_prefixed" "$target_prefixed"
  327.        else
  328.          mv -uf "$source_prefixed" "$target_prefixed"
  329.          if [ -f "$source_prefixed" ]; then
  330.            rm "$source_prefixed"
  331.          fi
  332.        fi
  333.     fi
  334.    
  335.   done 10< <( cat "$file_list" )
  336.   exec 10>&-
  337.   set +x
  338.   if [ ! "$arr_action" = "mv" ]; then
  339.     mkdir -p `dirname "$target_file_list"`
  340.     cp -a -u "$file_list" "$target_file_list"
  341.   else
  342.     mv -uf "$file_list" "$target_file_list"
  343.     sed -i "\%|${app}|%d" "$curdir/$s_rootfs/root/.packages/"*"-installed-packages"
  344.     if [ -f "$file_list" ]; then
  345.       rm "$file_list"
  346.     fi
  347.   fi
  348. }
  349. dissect_rootfs(){
  350.   #arr_names=${1:-ALL_ArrNames}
  351.   arry_names_name=${1:-ALL_ArrNames}
  352.   for arr_name in $(eval 'echo "${'$arry_names_name'[@]}"'); do
  353.     eval "arr=( \"\${"$arr_name"[@]}\" )"
  354.     eval "arr_action=\$"$arr_name"_action"
  355.     arr_action=${arr_action:-"$action"}
  356.     #Move is faster but perhaps copy is safer
  357.     arr_action=${arr_action:-cp}
  358.     set -x
  359.     eval 'target="$'$arr_name'_target"'
  360.     target=${target:-"$curdir/$arr_name"}
  361.     set +x
  362.     if [ "${s_rootfs:0:1}" = "/" ]; then
  363.       s_rootfs_prefixed="$s_rootfs"    
  364.     else
  365.       s_rootfs_prefixed="$curdir/$s_rootfs"    
  366.     fi  
  367.     for app in "${arr[@]}"; do
  368.       echo "arr_action=$arr_action"
  369.       case "$arr_action" in
  370.       cp|mv|retry)  
  371.           echo "app=$app"
  372.           copy_built_in "$app"        
  373.           ;;
  374.       #mv)
  375.       #    move_built_in()
  376.       #;;
  377.       #pet) #We might also want to convert to other package formats
  378.       #    mk_pet_fm_built_in()
  379.       #;;
  380.       esac
  381.       if [ -f "/tmp/trim_puppy/$app" ] && [ $chroot_remove_builtin -eq 1 ]; then
  382.         file_list="$curdir/$s_rootfs/root/.packages/builtin_files/$app"
  383.         rm "$file_list"
  384.         cp "/tmp/trim_puppy/$app" "$file_list"
  385.       fi
  386.       #chroot "$s_rootfs_prefixed" remove_builtin "$app"
  387.     done
  388.   done
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement