Chinaunix

标题: yum for cygwin [打印本页]

作者: damcool    时间: 2014-04-03 17:28
标题: yum for cygwin
本帖最后由 damcool 于 2014-04-03 19:26 编辑

前些日子在用apt-cyg/cyg-apt来更新自己的cygwin安装包,太TMD让人心焦了。于是自己写了一个"yum for cygwin",功能还行吧,我需要的都有了。分享一下。
  1. #!/bin/bash


  2. function check_commands()
  3. {
  4.         local command
  5.         local ERR=0
  6.         for command in $@; do
  7.                 [ "$(which $command 2> /dev/null)" = "" ] && prompt_echo "$command is not available for $PROGNAME"
  8.         done
  9.         [ $ERR -gt 0 ] && exit 1
  10. }

  11. function cache_commands()
  12. {
  13.         local command
  14.         mkdir -p $CACHE_CMD
  15.         for command in $@; do
  16.                 cp -f $(which $command) $CACHE_CMD
  17.         done

  18. }

  19. function yes_or_no()
  20. {
  21.     local prompts="$1"
  22.     local default="$2"
  23.     local timeout="$3"
  24.     local result='?'

  25.     while [ $result != "Y" ] && [ $result != "N" ]; do
  26.         read -n1 -t $timeout -p "$prompts" result
  27.         [ -z $result ] && result=$default
  28.         result=$(echo $result|tr 'a-z' 'A-Z')
  29.         echo
  30.     done
  31.     [[ "$result" == "Y" ]] && return 1
  32.     [[ "$result" == "N" ]] && return 0
  33. }

  34. function help_usage(){
  35. cat <<EOF
  36. YUM for cygwin is a shell script helping cygwin user to install/update/search
  37. packages of cygwin in the manners of similar to redhat yum functions.

  38. Usage: $PROGNAME [COMMAND] [OPTION] [PARAMETERS]

  39. Commands:
  40.   check-updates       List all possible updates of installed package(s)
  41.                       for cygwin as of now.
  42.   clean               Clean all cache files and refresh them with latest
  43.                       data.
  44.   
  45.   info [pkgs..]       Display the briefing information of the given package(s)
  46.   info installed      Display the briefing information of installed package(s)
  47.   info updates        Display the briefing information of updating package(s)

  48.   install [pkgs..]    Install the given package(s) with required package(s)
  49.   
  50.   list [pkgs..]       List the detail information of the given package(s)
  51.   list installed      List the detail information of the installed package(s)
  52.   list updates        List the detail information of the updating package(s)
  53.   
  54.   remove [pkgs..]     Remove the installed package(s) given by user as well
  55.                       as the orphan package(s) relatives.
  56.                      
  57.   search key-word     Search the package database for any package(s) relevant
  58.                       the given keyword.
  59.   
  60.   update [pkgs..]     Update all packages or the package(s) given by user.
  61.   
  62.   full-cache          Retrive all information from cygwin in cache, it takes
  63.                       long time to complete while internet connection is slow.               

  64. Options:                     
  65.   -y, --yes    Always say yes to queries
  66.   -h, --help   Show current help message of the script usages
  67.   
  68. Please Report Script Bugs to $AUTHOR_MAIL


  69. EOF
  70. exit 1
  71. }

  72. function error_exit()
  73. {
  74.         echo "$@" 1>&2
  75.         exit
  76. }

  77. function prompt_echo()
  78. {
  79.         echo "$@" 1>&2
  80. }

  81. function get_mirror_site()
  82. {
  83. #        MIRRORS=($(curl -s http://cygwin.com/mirrors.html|grep -o -E 'href="([^"#]+)/cygwin/"' |awk -F"\"" '{print $2}'|grep -v "http://www.redhat.com/"|grep -v "rsync://"))
  84.         MIRROR="http://mirrors.163.com/cygwin/"
  85. }

  86. function check_md5()
  87. {
  88.         local remote_md5="$(curl -s "$(dirname $2)/md5.sum"|grep -E " $(basename $2)$"|awk '{print $1}')"
  89.         local local_md5="$(md5sum $1|awk '{print $1}')"
  90.         [ "$local_md5" == "$remote_md5" ] && return 0 || return 1
  91. }


  92. function refresh_setup_ini()
  93. {
  94.         prompt_echo "Refreshing setup.ini from [$(echo $MIRROR|awk -F"/" '{print $3}')] via Protocol [$(echo $MIRROR|awk -F":" '{print $1}')]..."
  95.         cp -f $SETUP_INI "$SETUP_INI.last" &>/dev/null
  96.         touch $SETUP_INI "$SETUP_INI.last"
  97.         curl -# -o $SETUP_INI "$MIRROR$ARCH/setup.ini" || error_exit "Curl Download Error with Error Code [$?]!"
  98.         check_md5 $SETUP_INI "$MIRROR$ARCH/setup.ini" || error_exit "Curl Download MD5 not Matching!"
  99.         SETUP_VER="$(awk '/setup-version:/{print$2;exit}' "$SETUP_INI")"
  100.         SETUP_TIMESTAMP="$(awk '/setup-timestamp:/{print$2;exit}' "$SETUP_INI")"
  101. }

  102. function refresh_setup_db()
  103. {
  104.         local force=$#
  105.         [[ "$SETUP_TIMESTAMP" == "$LAST_TIMESTAMP" && "$SETUP_VER" == "$LAST_VER" && $force -eq 0 ]] && return
  106.         awk -F"|" '$3!=""{printf "%s|%s\n",$1,$13}' $SETUP_DB >$CACHE_DIR/tmp.db
  107.         awk '
  108.         /^@/{if (p>0) {for(i=p;i<13;i++) printf"|";printf "\n"};p=1;printf "%s",$2}
  109.         /^\[prev\]$/ || /^\[test\]$/{if (p>0) {for(i=p;i<13;i++) printf"|";printf "\n"};p=0}
  110.         /^sdesc:/{for(i=p;i<2;i++) printf"|";p=2;gsub(/^sdesc: /,"");gsub("\"","");printf "%s",$0}
  111.         /^ldesc:/{for(i=p;i<3;i++) printf"|";p=3;gsub(/^ldesc: /,"");c=$0;l="";do {l=l""c" ";getline c} while (index(c,":")==0);gsub(/"/,"",l);printf"%s",l;$0=c}
  112.         /^category:/{for(i=p;i<4;i++) printf"|";p=4;gsub(/^category: /,"");printf "%s",$0}
  113.         /^requires:/{for(i=p;i<5;i++) printf"|";p=5;gsub(/^requires: /,"");printf "%s",$0}
  114.         /^version:/{if (p>0){for(i=p;i<6;i++) printf"|";p=6;gsub(/^version: /,"");printf "%s",$0}}
  115.         /^install:/{if (p>0){for(i=p;i<7;i++) printf"|";p=9;gsub(/^install: /,"");gsub(" ","|");printf "%s",$0}}
  116.         /^source:/{if (p>0){for(i=p;i<10;i++) printf"|";p=13;gsub(/^source: /,"");gsub(" ","|");printf "%s|",$0}}' $SETUP_INI|grep -v -E "^$" >$SETUP_DB
  117.         cp -f $SETUP_DB ${SETUP_DB}-save
  118.         awk -F"|" 'NR==FNR{a[$1]=$2}NR>FNR{printf "%s",$0;if ($1 in a) printf"%s",a[$1];printf"\n"}' $CACHE_DIR/tmp.db "${SETUP_DB}-save" >$SETUP_DB
  119.         rm -f $CACHE_DIR/tmp.db

  120. }

  121. function refresh_update_db()
  122. {
  123.         local force=$#
  124.         [[ "$SETUP_TIMESTAMP" == "$LAST_TIMESTAMP" && "$SETUP_VER" == "$LAST_VER" && $force -eq 0 ]] && return
  125.         awk 'NR==FNR && NR>1{p[$1]=$2}NR>FNR{FS="|";if ($1 in p) {p[$1]=substr(p[$1],length($1)+1);gsub(/.tar.bz2$/,"",p[$1]);gsub(/^\-/,"",p[$1]);if ($6!=p[$1]) printf "%s|%s|%s\n",$1,p[$1],$6}}' $INSTALLED_DB $SETUP_DB > $UPDATE_DB
  126. }

  127. function check_yes()
  128. {
  129.         [ $YES -eq 0 ] && YES=1 && return
  130.         prompt_echo "$PROGNAME: error - duplicated switch '-y' found!"
  131.         help_usage;
  132.         exit 1       
  133. }

  134. function check_command()
  135. {
  136.         [ "$COMMAND" == "" ] && COMMAND=$1 && return
  137.         prompt_echo "$PROGNAME: error - duplicated command '$1' found!"
  138.         help_usage;
  139.         exit 1
  140. }

  141. function get_parameters()
  142. {
  143.         local param=""
  144.         PARAMS=()
  145.         for param in $@; do
  146.                 [[ "$param" =~ ^- ]] && return
  147.                 for item in ${COMMANDS[@]}; do [ "$param" == "$item" ] && return; done
  148.                 PARAMS=("${PARAMS[@]}" "$param")
  149.         done
  150. }

  151. function info_options()
  152. {
  153.         local opt="$1"
  154.         local opts=(installed updates)
  155.         local item
  156.         OPTION=""
  157.         for item in ${opts[@]}; do [ "$opt" == "$item" ] && OPTION=$opt;done
  158. }

  159. function install_options()
  160. {
  161.         local opt="$1"
  162.         local opts=(withupdates)
  163.         local item
  164.         OPTION=""
  165.         for item in ${opts[@]}; do [ "$opt" == "$item" ] && OPTION=$opt;done
  166. }

  167. function search_options()
  168. {
  169.         local opt="$1"
  170.         local opts=(requires)
  171.         local item
  172.         OPTION=""
  173.         for item in ${opts[@]}; do [ "$opt" == "$item" ] && OPTION=$opt;done
  174. }

  175. function list_options()
  176. {
  177.         local opt="$1"
  178.         local opts=(installed updates)
  179.         local item
  180.         OPTION=""
  181.         for item in ${opts[@]}; do [ "$opt" == "$item" ] && OPTION=$opt;done
  182. }

  183. function run_check_update()
  184. {
  185.         info_items "$(find_updates)"
  186.         prompt_echo "Found $(cat $UPDATE_DB|wc -l) Updates!"
  187.         return
  188. }

  189. function info_items()
  190. {
  191.         local pkgs=" $@ "
  192.         echo "Package Name             Description                        Version     Size"
  193.         echo "------------------------ ---------------------------------- ----------- -------"
  194.         awk -F"|" -v t="$pkgs" 'BEGIN{split("BKMGTP",h,"")}function hn(n,i){while(++i && n>=1000 && i<=length(h)) n/=1024;return sprintf("%3.2f%s",n,h[i])}{if (match(t," "$1" ") || t==""){$1=(length($1)>24)?substr($1,1,22)">>":$1;$2=(length($2)>34)?substr($2,1,32)">>":$2;$6=(length($6)>11)?substr($6,1,9)">>":$6;printf "%-24s %-34s %-11s %7s\n",$1,$2,$6,hn($8)}}' $SETUP_DB
  195.         echo "-------------------------------------------------------------------------------"
  196. }

  197. function list_items()
  198. {
  199.         local pkgs=" $@ "
  200.         awk -F"|" -v t="$pkgs" 'BEGIN{split("BKMGTP",h,"")}
  201.         function hn(n,i){
  202.                 while(++i && n>=1000 && i<=length(h)) n/=1024;
  203.                 return sprintf("%3.2f%s",n,h[i])
  204.         }
  205.         {if (match(t," "$1" ") || t=="  "){
  206.                         for (i=0;i<79;i++) printf "-";printf "\n";
  207.                         printf "%-20s%s\n","Package Name:",$1
  208.                         if ($2!="") printf "%-20s%s\n","Briefing Info:",$2
  209.                         if ($3!=""){
  210.                                 d="Description:";n=split($3,a," ");m=80;
  211.                                 for (i=1;i<=n;i++){
  212.                                         if (m+length(a[i])>58) {if (m<60) printf "\n";printf "%-20s",d;d=" "};
  213.                                         m=(m+length(a[i])<59)?m+length(a[i])+1:length(a[i])+1;
  214.                                         printf "%s ",a[i];
  215.                                 }
  216.                                 printf "\n"
  217.                         }
  218.                         if ($6!="") printf "%-20s%s\n","Category:",$4
  219.                         if ($5!=""){
  220.                                 d="Required Package(s):";n=split($5,a," ");m=80;
  221.                                 for (i=1;i<=n;i++){
  222.                                         if (m+length(a[i])>58) {if (m<60) printf "\n";printf "%-20s",d;d=" "};
  223.                                         m=(m+length(a[i])<59)?m+length(a[i])+1:length(a[i])+1;
  224.                                         printf "%s ",a[i];
  225.                                 }
  226.                                 printf "\n"
  227.                         }
  228.                         if ($6!="")        printf "%-20s%s\n","Latest Version:",$6
  229.                         if ($7!="")        printf "%-20s%s\n","Binary Package:",$7;
  230.                         if ($8!="")        printf "%-20s%s\n","       Size:",hn($8);
  231.                         if ($9!="")        printf "%-20s%s\n","       MD5:",$9;
  232.                         if ($10!="")printf "%-20s%s\n","Source Package:",$10;
  233.                         if ($11!="")printf "%-20s%s\n","       Size:",hn($11);
  234.                         if ($12!="")printf "%-20s%s\n","       MD5:",$12;
  235.                 }
  236.         }' $SETUP_DB

  237.         echo "-------------------------------------------------------------------------------"
  238. }

  239. function run_full_cache()
  240. {
  241.     local dir_ifs=$IFS
  242.     local pkg
  243.     local url
  244.     local md5
  245.     local opt
  246.     local nbr=$(cat $SETUP_DB|wc -l)
  247.     local th10=$((nbr/10))
  248.     local th50=$((nbr/50))
  249.     local idx=0
  250.         cp -f "$SETUP_DB" "${SETUP_DB}-save"
  251.         IFS=\n';
  252.         rm -f $CACHE_DIR/tmp.db
  253.         prompt_echo "Caching $nbr Package(s)..."
  254.         for pkg in $(awk -F"|" '{print $1"|"$7"|"$9}' $SETUP_DB); do
  255.                 url=$MIRROR/$(echo $pkg|awk -F"|" '{print $2}');
  256.                 md5=$(echo $pkg|awk -F"|" '{print $3}');
  257.                 pkg=$(echo $pkg|awk -F"|" '{print $1}');
  258.                 if [ $((idx%th10)) -eq 0 ]; then
  259.                         echo -n ">" 1>&2
  260.                 else
  261.                         [ $((idx%th50)) -eq 0 ] && echo -n "." 1>&2
  262.                 fi
  263.                 idx=$((idx+1))
  264.                 curl -s -o $CACHE_DIR/$(basename $url) $url || continue
  265.                 [ "$(md5sum $CACHE_DIR/$(basename $url)|awk '{print $1}')" != "$md5" ] && rm -f $CACHE_DIR/$(basename $url) && continue
  266.                 opt="-tvjf"
  267.                 [[ $url =~ .xz ]] && opt="-tvJf"
  268.                 tar $opt $CACHE_DIR/$(basename $url)|awk -v p="$pkg" 'BEGIN{printf "%s|",p}!/\/$/{n=split($0,a,"/");printf "%s ",a[n]}END{printf "\n"}' >>$CACHE_DIR/tmp.db
  269.                 rm -f $CACHE_DIR/$(basename $url)
  270.         done
  271.         prompt_echo
  272.         awk -F"|" 'NR==FNR{a[$1]=$2}NR>FNR{printf "%s",$0;if ($1 in a) printf"%s",a[$1];printf"\n"}' $CACHE_DIR/tmp.db "${SETUP_DB}-save" >$SETUP_DB
  273. }

  274. function find_required_by()
  275. {
  276.         awk -v p="$1" 'NR==FNR{FS="|";if (match(" "$5" "," "p" ")) t[$1]=p}NR>FNR{FS=" ";if ($1 in t) printf "%s ",$1}' $SETUP_DB $INSTALLED_DB
  277. }

  278. function find_installed()
  279. {
  280.         awk 'NR>1{printf "%s ",$1}' $INSTALLED_DB
  281. }

  282. function find_updates()
  283. {
  284.         awk -F"|" '{printf "%s ",$1}' $UPDATE_DB
  285. }

  286. function find_requires()
  287. {
  288.         local requires=()
  289.         local item=" $@ "
  290.         local count=0
  291.         prompt_echo -n "Searching Requires."
  292.         requires=(${requires[@]} $(awk -F"|" -v t="$item" '{if (match(t," "$1" ")) printf "%s ",$5}' $SETUP_DB))
  293.         while [ ${#requires[@]} -ne $count ]; do
  294.                 count=${#requires[@]}
  295.                 item=" ${requires[@]} "
  296.                 requires=(${requires[@]} $(awk -F"|" -v t="$item" '{if (match(t," "$1" ")) printf "%s ",$5}' $SETUP_DB))
  297.                 requires=($(echo "${requires[@]}"|awk '{for(i=1;i<=NF;i++) if (!($i in a)) {a[$i]=$i;printf "%s ",$i}}'))
  298.                 prompt_echo -n "."
  299.         done
  300.         prompt_echo
  301.         echo ${requires[@]}
  302. }

  303. function find_orphans()
  304. {
  305.         local pkg="$(find_requires $@)"
  306.         local requires="$(find_requires $(find_installed))"
  307.         requires=($(awk -v p="$pkg" -v r="$requires" 'BEGIN{m=split(p,a," ");n=split(r,b," ");for(i=1;i<=m;i++)c[a[i]]=a[i];for(i=1;i<=n;i++) if (!(b[i] in c)) printf "%s ",b[i];}'))
  308.         requires="$(find_requires ${requires[@]})"
  309.         awk -v p="$pkg" -v r="$requires" 'BEGIN{m=split(p,a," ");n=split(r,b," ");for(i=1;i<=n;i++)c[b[i]]=b[i];for(i=1;i<=m;i++) if (!(a[i] in c)) printf "%s ",a[i];}'
  310. }

  311. function run_list()
  312. {
  313.         local missed
  314.         local items=()
  315.         local item
  316.         case "$OPTION" in
  317.                 (installed)        list_items $(awk 'NR>1{printf "%s ",$1}' $INSTALLED_DB);return;;
  318.                 (updates)        list_items $(awk -F"|" '{printf "%s ",$1}' $UPDATE_DB);return;;
  319.         esac
  320.         for item in ${PARAMS[@]}; do
  321.                 [ $(grep -E "^$item|" $SETUP_DB|wc -l) -ne 0 ] && items=(${items[@]} $item) || missed="$missed'$item' "
  322.         done;
  323.         [ ${#items[@]} -ne 0 ] && list_items ${items[@]}
  324.         [ "$missed" != "" ] && prompt_echo "Package(s):${missed}not found!"
  325. }

  326. function run_info()
  327. {
  328.         local missed
  329.         local items=()
  330.         local item
  331.         case "$OPTION" in
  332.                 (installed)        info_items $(awk 'NR>1{printf "%s ",$1}' $INSTALLED_DB);return;;
  333.                 (updates)        info_items $(awk -F"|" '{printf "%s ",$1}' $UPDATE_DB);return;;
  334.         esac
  335.         for item in ${PARAMS[@]}; do
  336.                 [ $(grep -E "^$item|" $SETUP_DB|wc -l) -ne 0 ] && items=(${items[@]} $item) || missed="$missed'$item' "
  337.         done;
  338.         [ ${#items[@]} -ne 0 ] && info_items ${items[@]}
  339.         [ "$missed" != "" ] && prompt_echo "Package(s):${missed}Not Found!"
  340. }

  341. function remove_orphans()
  342. {
  343.         local orphans=($@)
  344.         local item=""
  345.         if [ ${#orphans[@]} -ne 0 ]; then
  346.                 prompt_echo "Found ${#orphans[@]} Orphan Package(s)!"
  347.                 item="[${orphans[@]}]"
  348.                 [ $YES -eq 0 ] && [ "$COMMAND" != "update" ] && yes_or_no "Are you want to remove the orphan package(s) $item(y/N)?" "N" 30 && return 1
  349.                 YES=1
  350.                 for item in ${orphans[@]}; do
  351.                         remove_item "$item"
  352.                         echo "Orphan Package(s) '$item' Removed!"
  353.                 done
  354.         fi
  355. }

  356. function remove_item()
  357. {
  358.     local dir_ifs=$IFS
  359.         local pkg="$1"
  360.         local line
  361.         [ ! -e "/etc/setup/$pkg.lst.gz" ] && prompt_echo "Package Manifest Not Found, Cannot Remove $pkg." && return 1
  362.         prompt_echo "Removing Package '$pkg'..."
  363.         if [ -e "/etc/preremove/$pkg.sh" ]; then
  364.                 sh "/etc/preremove/$pkg.sh"
  365.                 rm -f "/etc/preremove/$pkg.sh"
  366.         fi
  367.         IFS=\n'
  368.         for line in $(gzip -dc "/etc/setup/$pkg.lst.gz" | awk '!/\/$/{print}'); do        rm -f "/$line";        done
  369.         for line in $(gzip -dc "/etc/setup/$pkg.lst.gz" | awk '/\/$/{n=split($0,a,"/");t[n]=t[n]"|"$0}END{n=asorti(t,a);for(i=n;i>0;i--){m=split(t[a[i]],b,"|");for(j=1;j<=m;j++) if (b[j]!="") print b[j]}}');do
  370.                 [ $(find "/$line" -maxdepth 1|wc -l) -eq 1 ] && rm -rf "/$line" && prompt_echo "Empty Folder /$line Deleted!"
  371.         done
  372.         IFS="$dir_ifs"
  373.         rm -f "/etc/setup/$pkg.lst.gz"
  374.         rm -f /etc/postinstall/$pkg.sh.done
  375.         cp -f $INSTALLED_DB "$INSTALLED_DB-save"
  376.         grep -v -E "^$pkg " "$INSTALLED_DB-save" > $INSTALLED_DB
  377. }

  378. function remove_items()
  379. {
  380.         local pkg=""
  381.         local orphans=()
  382.         [ $YES -eq 0 ] && [ "$COMMAND" != "update" ] && yes_or_no "Are you sure to remove package '${@/# /}'(y/N)?" "N" 30 && return 1
  383.         for pkg in $@; do
  384.                 remove_item "$pkg" && prompt_echo "Package $pkg removed!"       
  385.                 echo
  386.                 prompt_echo "Checking Orphan Packages..."
  387.                 orphans=("${orphans[@]}" "$(find_orphans $pkg)")
  388.         done
  389.         orphans=($(echo "${orphans[@]}"|awk '{for(i=1;i<=NF;i++) if (!($i in a)) {a[$i]=$i;printf "%s ",$i}}'))
  390.         remove_orphans ${orphans[@]}
  391. }

  392. function run_remove()
  393. {
  394.         local missed
  395.         local items=()
  396.         local item
  397.         local reqs
  398.         for item in ${PARAMS[@]}; do
  399.                 if [ $(grep -E "^$item " $INSTALLED_DB|wc -l) -ne 0 ]; then
  400.                         reqs="$(find_required_by $item)"
  401.                         reqs=${reqs/% /}
  402.                         [ "$reqs" != "" ] && prompt_echo "Package '$item' is Required by [$reqs]!" || items=(${items[@]} $item)
  403.                 else
  404.                         missed="$missed'$item' "
  405.                 fi
  406.         done;
  407.         [ "$missed" != "" ] && prompt_echo "Package(s):${missed}Not Installed!"
  408.         [ ${#items[@]} -ne 0 ] && remove_items "${items[@]}"
  409. }

  410. function install_item()
  411. {
  412.     local dir_ifs=$IFS
  413.     local pkg="$1"
  414.         local url="$(awk -F"|" -v t="$pkg" '{if($1==t){print $7;exit}}' $SETUP_DB)"
  415.         local md5_r="$(awk -F"|" -v t="$pkg" '{if($1==t){print $9;exit}}' $SETUP_DB)"
  416.         local md5_l
  417.         local opt
  418.         local file_name="$(basename $url)"
  419.         prompt_echo "Downloading Package '$pkg'..."
  420.         curl -# -o $CACHE_DIR/$file_name $MIRROR/$url || error_exit "Downloading Error in Installing Package of '$pkg'!"
  421.         md5_l="$(md5sum $CACHE_DIR/$file_name|awk '{print $1}')"
  422.         [ "$md5_l" != "$md5_r" ] && error_exit "MD5 Checking Error in Installing Package of '$pkg'!"
  423.         [[ $file_name =~ .bz2 ]] && opt="-xvjf"
  424.         [[ $file_name =~ .xz ]] && opt="-xvJf"
  425.         tar $opt $CACHE_DIR/$file_name -C / >/etc/setup/$pkg.lst
  426.         gzip -f "/etc/setup/$pkg.lst"
  427.         rm -f $CACHE_DIR/$file_name
  428.         [[ $file_name =~ .xz ]] && file_name=${file_name/%.xz}".bz2"
  429.         cp -f "$INSTALLED_DB" "$INSTALLED_DB-save"
  430.         awk -v p="$pkg" -v f="$file_name" '{if (p!="" && p<$1) {print p" "f" 0"; p=""};print}END{if (p!="") print p" "f" 0"}' "$INSTALLED_DB-save">"$INSTALLED_DB"
  431.         IFS=\n'
  432.         for file_name in /etc/postinstall/*.sh; do
  433.                 if [ "$file_name" != "/etc/postinstall/*.sh" ]; then
  434.                         prompt_echo "Running Script '$(basename $file_name)' of Package '$pkg'..."
  435.                         $file_name
  436.                         cat $file_name>$file_name.done
  437.                         rm -f $file_name
  438.                 fi
  439.         done
  440.         IFS="$dir_ifs"
  441. }

  442. function install_items()
  443. {
  444.         local pkgs=($@)
  445.         local pkg
  446.         prompt_echo "Found ${#pkgs[@]} Package(s) Ready for Installation!"
  447.         [ $YES -eq 0 ] && [ "$COMMAND" != "update" ] && yes_or_no "Are you sure to install the package(s)(y/N)?" "N" 30 && return 1
  448.         YES=1
  449.         for pkg in ${pkgs[@]}; do
  450.                 install_item "$pkg" && prompt_echo "Package $pkg Installed!"       
  451.                 echo
  452.         done
  453. }


  454. function run_install()
  455. {
  456.         local missed
  457.         local installed
  458.         local installing=()
  459.         local items=()
  460.         local item
  461.         local requires=()
  462.         for item in ${PARAMS[@]}; do
  463.                 [ $(grep -E "^$item " $INSTALLED_DB|wc -l) -ne 0 ] && installed="$installed'$item' " || items=(${items[@]} $item)
  464.         done;
  465.         prompt_echo "Checking Required Packages..."
  466.         for item in ${items[@]}; do
  467.                 if [ $(grep -E "^$item\|" $SETUP_DB|wc -l) -ne 0 ]; then
  468.                         installing=(${installing[@]} $item)
  469.                         requires=(${requires[@]} $(find_orphans $item))
  470.                 else
  471.                         missed="$missed'$item' "        
  472.                 fi
  473.         done
  474.         requires=($(echo "${requires[@]}"|awk '{for(i=1;i<=NF;i++) if (!($i in a)) {a[$i]=$i;printf "%s ",$i}}'))
  475.         item=" ${requires[@]} "
  476.         requires=($(awk -v t="$item" 'NR>1{gsub(" "$1" "," ",t)}END{print t}' $INSTALLED_DB))
  477.         installing=(${requires[@]} ${installing[@]})
  478.         [ "$installed" != "" ] && prompt_echo "Package(s):${installed}Installed Already!"
  479.         [ "$missed" != "" ] && prompt_echo "Package(s):${missed}Not Found!"       
  480.         [ ${#installing[@]} -ne 0 ] && info_items ${installing[@]} && install_items ${installing[@]}
  481. }

  482. function run_search()
  483. {
  484.         local res=($(awk -F"|" -v k="${PARAMS[@]}" '{for (i=1;i<=NF;i++) if (match($i,k)) {printf "%s ",$1;break}}' $SETUP_DB))
  485.         list_items ${res[@]}
  486. }

  487. function run_update()
  488. {
  489.         local missed
  490.         local updates=()
  491.         local item
  492.         [ ${#PARAMS[@]} -eq 0 ] && PARAMS=($(find_updates))
  493.         for item in ${PARAMS[@]}; do
  494.                 [ $(grep -E "^$item " $UPDATE_DB|wc -l) -ne 0 ] && updates=(${updates[@]} $item) || missed="$missed'$item' "
  495.         done;
  496.         [ "$missed" != ""] && prompt_echo "Not Found Package(s) of $missed!"
  497.         [ ${#updates[@]} -eq 0 ] && error_exit "No Package Required Update!"
  498.         prompt_echo "Found ${#updates[@]} Package(s) Required Update!"       
  499.         for item in ${updates[@]}; do
  500.                 remove_item $item
  501.                 install_item $item
  502.                 prompt_echo "Package '$item' Updated!"
  503.         done
  504. }

  505. function run_clean()
  506. {
  507.         refresh_setup_ini 1
  508.         refresh_setup_db 1
  509.         refresh_update_db 1
  510. }

  511. ################################################################################
  512. # Function Name:   Script Main Loop                                             
  513. ################################################################################
  514. CACHE_DIR="/etc/yum/cache"
  515. CACHE_CMD="$CACHE_DIR/commands"
  516. COMMANDS=(mkdir dirname basename curl bzip2 tar xz awk grep file md5sum find cat echo bash)
  517. check_commands "${COMMANDS[@]}"
  518. cache_commands "${COMMANDS[@]}"
  519. PATH="$CACHE_CMD:$PATH"
  520. BASE_DIR=$(cd "$(dirname "$0")" && pwd)
  521. PROGNAME=$(basename "$0")
  522. AUTHOR_MAIL="robin.hoo@hotmail.com"
  523. SETUP_INI="/etc/yum/setup.ini"
  524. INSTALLED_DB="/etc/setup/installed.db"
  525. UPDATE_DB="/etc/yum/cache/update.db"
  526. SETUP_DB="/etc/yum/cache/setup.db"
  527. SETUP_VER="$(grep -i 'setup-version:' "$SETUP_INI"|awk '{print $2}' 2>/dev/null)"
  528. LAST_VER="$SETUP_VER"
  529. SETUP_TIMESTAMP="$(grep -i 'setup-timestamp:' "$SETUP_INI"|awk '{print $2}'  2>/dev/null)"
  530. LAST_TIMESTAMP="$SETUP_TIMESTAMP"
  531. VERSION="1.0"
  532. MIRRORS=()
  533. MIRROR=""
  534. mkdir -p "$CACHE_DIR"
  535. PE="$(file -b "$(which file)"|awk '{print $1}')"
  536. ARCH="UNKNOWN"
  537. [ "$PE" == "PE32" ] && ARCH="x86"
  538. [ "$PE" == "PE32+" ] && ARCH="x86_64"
  539. [ "$ARCH" == "UNKOWN" ] && prompt_echo "$PROGRAM: Error - System Architecture is Unkown!" && help_usage
  540. OPTION=""
  541. PARAMS=()
  542. COMMAND=""
  543. COMMANDS=(check-update clean info install list remove search update full-cache)
  544. YES=0
  545. while [ $# -gt 0 ]
  546. do
  547.     case "$1" in
  548.             (check-update)        check_command "check-update";;
  549.             (full-cache)        check_command "full-cache";;
  550.             (clean)                        check_command "clean";;
  551.             (info)                        check_command "info";info_options "$2"; shift;[ "$OPTION" == "" ] && get_parameters "$@" && [ ${#PARAMS[@]} -ne 0 ] && shift ${#PARAMS[@]} && continue;;
  552.             (install)                check_command "install";install_options "$2";shift;get_parameters "$@";[ ${#PARAMS[@]} -ne 0 ] && shift ${#PARAMS[@]} && continue;;
  553.             (list)                        check_command "list";list_options "$2";shift; [ "$OPTION" == "" ] && get_parameters "$@" && [ ${#PARAMS[@]} -ne 0 ] && shift ${#PARAMS[@]}&& continue;;
  554.             (remove)                check_command "remove";shift;get_parameters "$@";[ ${#PARAMS[@]} -ne 0 ] && shift ${#PARAMS[@]} && continue;;
  555.             (search)                check_command "search";search_options "$2"; shift && get_parameters "$@";[ ${#PARAMS[@]} -ne 0 ] && shift ${#PARAMS[@]} && continue;;
  556.             (update)                check_command "update";shift;get_parameters "$@";[ ${#PARAMS[@]} -ne 0 ] && shift ${#PARAMS[@]} && continue;;
  557.                 (-y)                        check_yes;;                       
  558.                 (-h)                        help_usage;;
  559.                 (--yes)                        check_yes;;               
  560.             (--help)                help_usage;;
  561.             (*)                                prompt_echo "$PROGNAME: error - unrecognized option or parameter '$1'!"; help_usage;;
  562.     esac
  563.     shift
  564. done
  565. [ "$COMMAND" == "" ] && prompt_echo "$PROGNAME: error - missing option or parameters!" && help_usage
  566. [ "$COMMAND" == "info" ] && [ ${#PARAMS[@]} -eq 0 ] && [ "$OPTION" == "" ] && prompt_echo "$PROGNAME: error - missing parameters for command 'info' !" && help_usage
  567. [ "$COMMAND" == "list" ] && [ ${#PARAMS[@]} -eq 0 ] && [ "$OPTION" == "" ] && prompt_echo "$PROGNAME: error - missing parameters for command 'list' !" && help_usage
  568. [ "$COMMAND" == "remove" ] && [ ${#PARAMS[@]} -eq 0 ] && prompt_echo "$PROGNAME: error - missing parameters for command 'remove' !" && help_usage
  569. [ "$COMMAND" == "install" ] && [ ${#PARAMS[@]} -eq 0 ] && prompt_echo "$PROGNAME: error - missing parameters for command 'install' !" && help_usage
  570. [ "$COMMAND" == "search" ] && [ ${#PARAMS[@]} -eq 0 ] && prompt_echo "$PROGNAME: error - missing parameters for command 'search' !" && help_usage
  571. [ "$COMMAND" == "search" ] && [ ${#PARAMS[@]} -gt 1 ] && prompt_echo "$PROGNAME: error - too many parameters for command 'search' !" && help_usage

  572. get_mirror_site
  573. refresh_setup_ini
  574. refresh_setup_db
  575. refresh_update_db
  576. case "$COMMAND" in
  577.         (check-update)        run_check_update;;
  578.         (clean)                        run_clean;;
  579.         (info)                        run_info;;
  580.         (install)                run_install;;
  581.         (list)                        run_list;;
  582.         (remove)                run_remove;;
  583.         (search)                run_search;;
  584.         (update)                run_update;;
  585.         (full-cache)        run_full_cache;;
  586. esac

复制代码

作者: damcool    时间: 2014-04-03 17:31
没有写查询最快镜像的部分,有兴趣的朋友写好了,可以更新一下分享。
作者: yestreenstars    时间: 2014-04-03 17:37
牛X,进入膜拜模式~{:2_172:}
作者: rdcwayx    时间: 2014-04-03 20:35
感谢分享。

放到git里,publish一下?
作者: Herowinter    时间: 2014-04-03 21:30
回复 1# damcool
大神,咱们做朋友,好吗?

   
作者: damcool    时间: 2014-04-04 09:49
不会用git...
rdcwayx 发表于 2014-04-03 20:35
感谢分享。

放到git里,publish一下?

作者: 刺客阿地    时间: 2014-04-04 15:34
路过,膜拜一下大神!
作者: 银风冷月    时间: 2014-04-04 15:43
0.0  特地上号膜拜大神...
作者: dl_coyote    时间: 2023-04-13 08:38
最近试图使用cygwin ,安装linux 程序太费劲了。下你的代码试试。真牛。
作者: Shell_HAT    时间: 2023-07-26 17:33
感谢分享




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2