s243a

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

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