Advertisement
s243a

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

Apr 6th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 16.43 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. #The trap_stack lets us nest multiple trap statments. See:
  8. # https://stackoverflow.com/questions/16115144/save-and-restore-trap-state-easy-way-to-manage-multiple-handlers-for-traps
  9. # https://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
  10. process_pet_specs(){
  11.     local specs_txt=`cat "$1" | head --lines=1`
  12.     #https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
  13.     #http://www.linuxquestions.org/questions/programming-9/bash-shell-script-split-array-383848/#post3270796
  14.     specs=(${specs_txt//|/ })
  15.     process_pet_specs_FULL_NAME=specs[0]   
  16.     process_pet_specs_PKG_NAME=[1]
  17.     process_pet_specs_VERSION=specs[2]
  18.     process_pet_CATEGORY=specs[4]
  19.     process_pet_SIZE=specs[5]  
  20.     process_pet_BRANCH=specs[6] #Repository Folder
  21.     process_pet_FILE_NAME=specs[7] #File Namer
  22.     process_pet_FILE_DEPENDS=specs[8]
  23.     process_pet_FILE_SHORT_DESC=specs[9]
  24.     process_pet_FILE_COMPILED WITHIN=specs[10]
  25.     process_pet_FILE_COMPAT_DISTRO_VER=specs[11]   
  26.     process_pet_REPO_NAME=specs[12]
  27. }
  28.          
  29. mk_tazpkg_receipt(){
  30.   parse_pkg_dirname "$1"
  31.   local pkg_path="$path__ppd"
  32.   local pkg_name="$pkg_name__ppd"
  33.   local pkg_short_name="$pkg_short_ppd"
  34.   local pkg_version="$pkg_version__ppd"
  35.  
  36.     if [ -f "$pkg_path/receipt" ]; then
  37.       process_pet_specs
  38.   echo 'PACKAGE="$process_pet_specs_PKG_NAME"
  39. VERSION="$process_pet_specs_VERSION"
  40. CATEGORY="$process_pet_CATEGORY"
  41. SHORT_DESC="$process_pet_SHORT_DESC"
  42. WEB_SITE="$web_site"
  43. MAINTAINER="$maintainer"
  44. DEPENDS="$process_pet_FILE_DEPENDS"
  45. ' > $pkg_path/receipt #TODO add che for pkgmanager type and suport for toher package managers    
  46.     fi
  47. }
  48. delete_petspec_fm_file(){
  49.  
  50.   local field_val=$1
  51.   local pkg_rootfs=${2:-"$s_rootfs"}
  52.   local spec_list_name=${3:-'*-packages'}
  53.   if [ -z "$4" ]; then
  54.     if [[ $field_val == *"-"* ]]; then
  55.       local field=1
  56.     else
  57.       local field=2
  58.     fi
  59.   else
  60.     local field="$4"
  61.   fi
  62.   #TODO if prk_rootfs="/" or "" give warning
  63.   #TODO Give the option to compare more than one field. #https://www.gnu.org/software/gawk/manual/html_node/Boolean-Ops.html
  64.   #TODO consider how to deal with miskes like dashes when there should be
  65.   AWK_PRG="BEGIN{FS=\"|\"}
  66.  {
  67.    if ( \$$field != \"$field_val\" ) {
  68.     print
  69.    }
  70.  }"
  71.   #TODO we probably need logic to expand wildards
  72.    
  73.  
  74.   local file_spec_lists=( `ls -1 "$pkg_rootfs/root/.packages/$spec_list_name"` ) 2>/dev/null
  75.   mkdir -p /tmp/trim_puppy/
  76.   for spec_list in "${file_spec_lists[@]}"; do
  77.     spec_name=`basename "$spec_list"`
  78.     cat "$spec_list" | awk "$AWK_PRG"  >> "/tmp/trim_puppy/$spec_name"
  79.     echo "about to"
  80.     echo "rm \"$spec_list\"
  81.    mv  \"/tmp/trim_puppy/$spec_name\" \"$spec_list\"
  82.    "
  83.     #read -p "Press enter to continue"
  84.     set -x    
  85.     rm "$spec_list"
  86.     mv  "/tmp/trim_puppy/$spec_name" "$spec_list"
  87.     set +x    
  88.     #read -p "Press enter to continue"
  89.   done
  90.  
  91. }
  92. parse_pkg_dirname(){
  93.   path__ppd=$1
  94.   pkg_name__ppd=${pkg_name%.extracted}
  95.   pkg_name__ppd=${pkg_name%.tazpkg}    
  96.   pkg_name__ppd=${pkg_name%.pet}  
  97.   pkg_name__ppd=${pkg_name%.txz}
  98.   pkg_short_name__ppd=${pkg_name%%-*}
  99.   pkg_version__ppd=${pkg_name#*-}
  100. }
  101. inst_pkg_fm_anywhere(){
  102.   set -x
  103.   parse_pkg_dirname $1
  104.   local source_dir="$path__ppd"
  105.   local pkg_name="$pkg_name__ppd"
  106.   local pkg_short_name="$pkg_short_name__ppd"
  107.   local dest_root=${2:-"$curdir/$s_rootfs"} #TODO make the default more robust with regards to slashes
  108.   if [ "$dest_root" = "/" ]; then
  109.      echo "warning about to install package into system rootfs"
  110.      #read -p "Press enter to continue"
  111.   fi
  112.  
  113.   local pkg_name=${3:-`basename $1`} #TODO add logic for different location of files.list
  114.   pkg_name=${pkg_name%%.*}
  115.   if [ -d "$source_dir/fs" ]; then
  116.     source_dir_fs="$source_dir/fs"
  117.   else
  118.     source_dir_fs="$source_dir"
  119.   fi
  120.   system_root_check_and_warn \
  121.       $source_dir_fs -m="source_dir_fs=$source_dir_fs" \
  122.       $dest_root -m="dest_root=$dest_root"
  123.   set +x
  124.   echo "  cp --remove-destination -arf $source_dir_fs/*  $dest_root/ 2>/dev/null"
  125.   #read -p "Press enter to continue"
  126.   cp --remove-destination -arf $source_dir_fs/* \
  127.                                  $dest_root/ 2>/dev/null
  128.  
  129.   cd "$source_dir"
  130.   find . -mindepth 1 -name '*' > "$source_dir/$pkg_name" #"$dest_file_list_prefixed"
  131.   update_pkg_specs "$source_dir" "$dest_root"
  132. }
  133.  
  134. update_pkg_specs(){
  135.   parse_pkg_dirname "$1"
  136.   local source_dir="$path__ppd"
  137.   local pkg_name="$pkg_name__ppd"
  138.   local pkg_short_name="$pkg_short_name__ppd"
  139.   local pkg_version="$pkg_version__ppd"
  140.   local post_install_script_run=0 #Consider having different install modes
  141.   #For example maybe we want to run both a pinstall.sh as well as the tazpkg
  142.   #post install script
  143.   POST_INSTALL_MODE=${POST_INSTALL_MODE:-first} #e.g (first, all, non
  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. }
  429. do_post_inst(){
  430.   local a_rootfs=$1
  431.  
  432.   initialize_rootfs "$a_rootfs"
  433. }
  434. resolve_rootfs_for_chroot(){
  435.   local a_rootfs=""
  436.   local the_rootfs=""
  437.  
  438.   for a_rootfs in "$1" "$sandbox" "$s_rootfs" "$rootfs"; do
  439.     if [ ! -z "$a_rootfs" ]
  440.       if [ "${a_rootfs:0:1}" = "/" ] && [ ${#a_rootfs} -gt 1 ]; then
  441.         if [ -d "$a_rootfs" ]; then
  442.           the_rootfs="$a_rootfs"
  443.           break
  444.         fi
  445.       elif [ "${a_rootfs:0:2}" = "." ]; then
  446.         a_rootfs="`pwd`/$a_rootfs"
  447.         a_rootfs="`realpath the_rootfs`"
  448.         if [ -d "$a_rootfs" ]; then
  449.           the_rootfs="$a_rootfs"
  450.           break
  451.         fi
  452.       else
  453.         a_rootfs="$curdir/$a_rootfs"
  454.         if [ -d "$a_rootfs" ]; then
  455.           the_rootfs="$a_rootfs"
  456.           break
  457.         fi
  458.       fi
  459.     fi
  460.   done
  461.   resolve_rootfs_for_chroot_rtn="$the_rootfs"
  462. }
  463. initialize_default__rootfs(){
  464.    local a_rootfs="$1"
  465.    cd "$1"
  466.    bind_dirs sys dev proc
  467. }
  468. initialize_rootfs(){
  469.   resolve_rootfs_for_chroot "$1"
  470.   local a_rootfs="$resolve_rootfs_for_chroot_rtn"
  471.   local rfs_name="`basename $1`"
  472.   local init_fn="${ROOTFS_INIT_FNs[$a_rfs_name]}"
  473.   if [ -z "$init_fn" ]; then
  474.     #If the function exsits then evaluate it
  475.     declare -f -F "$init_fn" > /dev/null && \
  476.       eval "$init_fn" || \
  477.       default_int_rootfs_or_sandbox "$a_rootfs"
  478.   else
  479.       default_int_rootfs_or_sandbox "$a_rootfs"
  480.   fi
  481.   cp -f /etc/resolv.conf $curdir/$rootfs/etc/resolv.conf
  482.    
  483. }
  484. default_int_rootfs_or_sandbox(){
  485.   local a_rootfs=`realpath "$1"`
  486.   local a_curdir=`realpath "$curdir"`
  487.   if [ "$a_rootfs" = "$a_curdir" ]; then
  488.     initialize_default_sandbox "$a_rootfs"
  489.   else
  490.     initialize_default_rootfs "$a_rootfs"
  491.   fi
  492. }
  493. initialize_default_sandbox(){
  494.    local source_rootfs="$1"
  495.    local target_rootfs=${2:-"$curdir"}
  496.    local system_rootfs=${3:-"/"}
  497.    cd "$target_rootfs"
  498.    bind_dirs sys dev proc tmp run
  499.    
  500.    declare -a  source_dirs_to_bind=\
  501.    ( "bin" "etc" "home" "lib" "media" "mnt" \
  502.      "opt" "root" "sbin" "usr" "var"  \
  503.    )
  504.    for a_dir in "$source_dirs_to_bind" do
  505.       mkdir -p "$a_dir"
  506.       mount -o bind $source_rootfs/a_dir "$a_dir"
  507.    done
  508.    cp -f /etc/resolv.conf $curdir/$rootfs/etc/resolv.conf
  509. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement