Advertisement
cpman

Gentoo Updater Script

Sep 14th, 2014
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.33 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Copyright (C) 2014 Alexander Mohn <pogrmman+dev@gmail.com>
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy of this script to use it without restriction,
  5. # including without limitation of the rights to use, copy, modify, merge, publish, distribute, and/or sublicense this script
  6. # subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of this
  8. # script.
  9. # THIS SCRIPT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR
  11. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  12. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THIS SCRIPT.
  13.  
  14. # This script provides a way to update and maintain your Gentoo system
  15. # with a single command.
  16.  
  17.  
  18. ####### Functions #######
  19.  
  20. # This updates the portage tree.
  21. function sync
  22. {
  23.     read -p "Press [Enter] to update the portage tree..."
  24.     sudo emerge --sync
  25. }
  26.  
  27. # This logs the old python version.
  28. function python_old
  29. {
  30.     mkdir /tmp/update
  31.     eselect python list > /tmp/update/python_old.lst                                    # Makes a list of old python versions.
  32.     awk '{print $2}' < /tmp/update/python_old.lst > /tmp/update/python_old              # Extracts version names.
  33.     python_old=($(cat /tmp/update/python_old))
  34.     pyv2_old=${python_old[1]}
  35.     pyv3_old=${python_old[2]}
  36.     echo $pyv2_old > /tmp/update/pyv2_old
  37.     echo $pyv3_old > /tmp/update/pyv3_old
  38.     cut -c 9 /tmp/update/pyv2_old > /tmp/update/v2_old                                  # Converts version names into numbers.
  39.     cut -c 9 /tmp/update/pyv3_old > /tmp/update/v3_old
  40.     pyv2_old=($(cat /tmp/update/v2_old))
  41.     pyv3_old=($(cat /tmp/update/v3_old))
  42. }
  43.  
  44. # This logs the new python version.
  45. function python_new
  46. {
  47.     eselect python list > /tmp/update/python.lst                                            # Lists new versions of python.
  48.     awk '{print $2}' < /tmp/update/python.lst > /tmp/update/python                      # Extracts the version names.
  49.     py=($(cat /tmp/update/python))
  50.     pyv2=${py[1]}
  51.     pyv3=${py[2]}
  52.     echo $pyv2 > /tmp/update/pyv2
  53.     echo $pyv3 > /tmp/update/pyv3
  54.     cut -c 9 /tmp/update/pyv2 > /tmp/update/v2                                          # Converts the version names into numbers.
  55.     cut -c 9 /tmp/update/pyv3 > /tmp/update/v3
  56.     pyv2=($(cat /tmp/update/v2))
  57.     pyv3=($(cat /tmp/update/v3))
  58. }
  59.  
  60. # This updates the system.
  61. function update
  62. {
  63.     read -p "Press [Enter] key to begin updating system..."
  64.     python_old
  65.     sudo emerge --ask --update --deep --with-bdeps=y --newuse @world
  66.     python_new
  67.     if [ "$pyv2_old" != "$pyv2" ]; then
  68.         read -p "Press [Enter] key to fix files broken by Python 2.x update..."
  69.         sudo python-updater
  70.     fi
  71.     if [ "$pyv3_old" != "$pyv3" ]; then
  72.         read -p "Press [Enter] key to fix files broken by Python 3.x update..."
  73.         sudo python-updater
  74.     fi
  75. }
  76.  
  77. # This changes any configuration files that must be updated.
  78. function config
  79. {
  80.     read -p "Press [Enter] key to change configuration files..."
  81.     sudo dispatch-conf
  82. }
  83. # This checks for broken packages.
  84. function pkg_check
  85. {
  86.     read -p "Press [Enter] key to check for broken packages..."
  87.     sudo revdep-rebuild -p
  88. }
  89.  
  90. # This rebuilds broken packages.
  91. function pkg_rebuild
  92. {
  93.     read -p "Press [Enter] key to rebuild broken packages..."
  94.     sudo revdep-rebuild
  95. }
  96.  
  97. # This removes obsolete packages.
  98. function clean
  99. {
  100.     read -p "Press [Enter] key to remove obsolete packages..."
  101.     sudo emerge --ask --depclean --with-bdeps=y
  102. }
  103.  
  104. # This prints introductory text.
  105. function intro
  106. {
  107.     cat <<- _INTRO_
  108.     Welcome to the system updater.
  109.     You may be asked to enter your password several times during the process.
  110.     If you do not have sudo privilages for emerge, revdep-rebuild, dispatch-conf,
  111.     python-updater, and perl-cleaner this script may fail.
  112.     _INTRO_
  113. }
  114.  
  115. # If no options are specified.
  116. function noopts
  117. {
  118.     cat <<- _DESC_
  119.     This script helps to keep your Gentoo system up to date.
  120.     You must specify at least one option.
  121.     USAGE: update [-option1] [-option2] [-option3] [-option4] [-option5] [-option6]
  122.  
  123.             -S Sync the portage tree.
  124.             -U Update the system.
  125.             -C Change config files.
  126.             -P Check for broken packages.
  127.             -p Rebuild broken packages.
  128.             -D Remove obsolete packages and dependenicies.
  129.             -h Display this help text.
  130.    
  131.     WARNING: Options will be parsed in the order in which they are given.
  132.     It is generally wise to use -S before -U, -U before -D, and -D before -p.
  133.     _DESC_
  134. }
  135.  
  136. ####### End of Function Declarations #######
  137.  
  138. ####### Main #######
  139.  
  140. if [ "$1" = "" ]; then                                                                  # Checks to see if any options are specified.
  141.     noopts                                                                              # If not, it displays usage.
  142. else
  143.     if [ "$1" = "-h" ]; then                                                            # Checks to see if -h is specified.
  144.         noopts                                                                          # If it is, it displays usage.
  145.     else               
  146.         intro                                                                           # Display introductory text.
  147.         while [ "$1" != "" ]; do                                                        # Option parser.
  148.             case $1 in
  149.                 -h ) noopts ;;
  150.                 -S ) sync ;;
  151.                 -U ) update ;;
  152.                 -C ) config ;;
  153.                 -P ) pkg_check ;;
  154.                 -p ) pkg_rebuild ;;
  155.                 -D ) clean ;;
  156.             esac
  157.             shift
  158.         done                                                                    # End of option parser.
  159.         echo "Done updating system!"
  160.     fi
  161. fi
  162. rm -r /tmp/update
  163. exit 0
  164.  
  165. ####### End #######
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement