rebuild-perl-packages.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/bin/bash -
  2. #===============================================================================
  3. #
  4. # FILE: rebuild-packages.sh
  5. #
  6. # USAGE: ./rebuild-packages.sh [OPTIONS]
  7. #
  8. # DESCRIPTION: Search and rebuild packages perl's packages
  9. #
  10. # OPTIONS: see -h
  11. # REQUIREMENTS: ---
  12. # BUGS: ---
  13. # NOTES: ---
  14. # AUTHOR: Piotr Rogoża (piecia), rogoza.piotr@gmail.com
  15. # LICENCE: Copyright (c) 2012, Piotr Rogoża
  16. # COMPANY: dracoRP
  17. # CREATED: 26.10.2012 20:15:17 CEST
  18. # VERSION:
  19. # REVISION: ---
  20. #===============================================================================
  21. #-------------------------------------------------------------------------------
  22. # Script variables
  23. #-------------------------------------------------------------------------------
  24. PROGRAM_NAME=$(basename $0)
  25. QUIET=''
  26. REMOVE=''
  27. PACKAGES=()
  28. SHORTOPTS='e:o:i:g::hqr'
  29. OPTIONS='[-e error_log] [-o output_log] [-i input_file] [-goutput_file] [-hqr]'
  30. REQUIRED_PROGRAMS=(yaourt pacman getopt awk)
  31. YAOURT='yaourt -Sa --noconfirm'
  32. # Command to generate a list of Perl packages with arch != any
  33. AWKFILE="/tmp/awk-$$"
  34. ( cat << EOF
  35. BEGIN {
  36. FS="\n";RS="";
  37. }
  38. \$0 !~ /:[[:space:]]+any/ {
  39. \$1 = gensub(/^Name[[:space:]]+:[[:space:]]+/,"", "g", \$1);
  40. print \$1;
  41. }
  42. EOF
  43. ) > $AWKFILE
  44. COMMAND="pacman -Qsq | grep ^perl- | xargs env LC_ALL=C pacman -Qi | awk -f $AWKFILE"
  45. #Another command that generates a list of packages
  46. #COMMAND=""
  47. # Cleaning
  48. trap "rm -f $AWKFILE" 0
  49. _usage (){ #{{{
  50. cat <<- EOT
  51. -e error_log - saves errors to an error_log file
  52. -o output_log - saves output to an output_log file
  53. -i input_file - reads packages from a file
  54. -goutput_file - generates a list of packages (and saves to a file - optional)
  55. don't use space between the parameter and the filename
  56. -q - is quiet
  57. -r - removes rebuild successfully package from the input file
  58. -h - show this help
  59. EOT
  60. } # ---------- end of function _usage ----------}}}
  61. _check_requirement (){ #{{{
  62. for program in ${REQUIRED[*]}; do
  63. if ! which $program &>/dev/null; then
  64. echo "The program $program is not found. Please install it first."
  65. exit
  66. fi
  67. done
  68. } # ---------- end of function _check_requirement ----------}}}
  69. _help (){ #{{{
  70. echo "Usage: $PROGRAM_NAME $OPTIONS"
  71. _usage
  72. } # ---------- end of function _help ----------}}}
  73. _generate_list (){ #{{{
  74. eval $COMMAND
  75. } # ---------- end of function _generate_list ----------}}}
  76. _check_requirement
  77. # Interpretation of positional parameters
  78. eval set -- $(getopt -o $SHORTOPTS -- "$@")
  79. while true; do
  80. case "$1" in
  81. -h)
  82. _help
  83. exit
  84. ;;
  85. -e)
  86. error_log_file="$2"
  87. :>"$error_log_file"
  88. shift 2
  89. ;;
  90. -o)
  91. output_log_file="$2"
  92. :>"$output_log_file"
  93. shift 2
  94. ;;
  95. -g)
  96. case "$2" in
  97. "")
  98. shift 2;
  99. ;;
  100. *)
  101. output_file="$2"
  102. exec 1>"$output_file"
  103. shift 2
  104. ;;
  105. esac
  106. _generate_list
  107. exit
  108. ;;
  109. -i)
  110. input_file=$2
  111. if [ ! -r "$input_file" ]; then
  112. echo "Input file isn't readable"
  113. exit 1
  114. fi
  115. exec 0<"$input_file"
  116. shift 2
  117. ;;
  118. -q)
  119. QUIET=yes
  120. shift
  121. ;;
  122. -r)
  123. REMOVE=yes
  124. shift
  125. ;;
  126. --)
  127. shift
  128. break
  129. ;;
  130. *)
  131. echo "Fatal error. See $PROGRAM_NAME -h for usage"
  132. exit 1
  133. ;;
  134. esac
  135. done
  136. if [ ! -t 0 ]; then
  137. # read packages from file
  138. while read LINE; do
  139. PACKAGES+=($LINE)
  140. done
  141. else
  142. # generate list of packages
  143. PACKAGES=( $(eval $COMMAND) )
  144. fi
  145. if [ ${#PACKAGES[*]} -eq 0 ]; then
  146. # if PACKAGES is empty
  147. echo "Nothing to do."
  148. exit
  149. fi
  150. # quiet mode on, *_log_file on
  151. if [ -n "$QUIET" -a -n "$output_log_file" ]; then
  152. exec 1>"$output_log_file"
  153. fi
  154. if [ -n "$QUIET" -a -n "$error_log_file" ]; then
  155. exec 2>"$error_log_file"
  156. fi
  157. # quiet mode on, *_log_file off
  158. if [ -n "$QUIET" -a "$QUIET" == 'yes' -a -z "$error_log_file" ]; then
  159. exec 2>/dev/null
  160. fi
  161. if [ -n "$QUIET" -a "$QUIET" == 'yes' -a -z "$error_log_file" ]; then
  162. exec 1>/dev/null
  163. fi
  164. # quiet mode off, *_log_file on
  165. if [ -z "$QUIET" -a -n "$output_log_file" ]; then
  166. exec 1> >(tee $output_log_file)
  167. fi
  168. if [ -z "$QUIET" -a -n "$error_log_file" ]; then
  169. exec 2> >(tee $error_log_file)
  170. fi
  171. # quiet mode off, *_log_file off
  172. # nothing to do
  173. # Rebuild packages
  174. for package in ${PACKAGES[*]}; do
  175. $YAOURT $package 2>&1
  176. if [ $? -eq 0 ]; then
  177. echo "Rebuilding the package '$package' completed successfull"
  178. if [ -n "$REMOVE" -a -n "$input_file" -a -w "$input_file" ]; then
  179. sed "/^$package$/d" -i "$input_file"
  180. fi
  181. else
  182. echo "Rebuilding the package '$package' failed" 1>&2
  183. fi
  184. done