Changes between Initial Version and Version 1 of Scripts/testport_script


Ignore:
Timestamp:
Apr 23, 2012, 6:37:52 PM (13 years ago)
Author:
kato23@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Scripts/testport_script

    v1 v1  
     1{{{testport}}} is a Bash script to test install a specified port in a custom MacPorts location in the {{{/opt}}} directory.
     2
     3The default location is {{{/opt/macports-test}}}.
     4
     5----
     6
     7{{{
     8
     9#!/bin/bash
     10
     11# testport  --  test install a specified port in /opt/macports-test (default)
     12
     13# 1. move the /opt/local MacPorts system to ${opt_local_off} to make sure it is not interfering with the custom ${MP_PREFIX} MacPorts build process
     14# 2. move /usr/local to ${usr_local_off} to make sure it is not interfering with the custom ${MP_PREFIX} MacPorts build process
     15# 3. install a fresh MacPorts 2.0.4 system to "${MP_PREFIX}"
     16# 4. install the specified port to the custom ${MP_PREFIX} MacPorts system
     17
     18# Small getopts tutorial,
     19# http://wiki.bash-hackers.org/howto/getopts_tutorial
     20
     21# usage:
     22# testport gawk
     23# testport -n -l /opt/macports-gnu gawk
     24
     25
     26show_usage() {
     27   echo
     28   echo "$(basename $0)  --  test install a specified port in /opt/macports-test (default)"
     29   echo
     30   echo "Usage: $(basename $0) [-d] [-e] [-h] [-n] [-p] [-r] [-s] [-u] [-v] [-l dir] portname"
     31   echo '
     32-d: enable debug mode
     33-e: fetch & extract the distribution files for portname
     34-h: help
     35-n: delete /opt/macports-test (default) & perform a new MacPorts install from scratch
     36-p: print PATH variable value
     37-r: remove / uninstall specified port before reinstalling it
     38-s: build and install from source only
     39-u: update MacPorts system and uprade outdated ports
     40-v: enable verbose mode
     41-l dir: specify dir as location of MacPorts system (otherwise defaults to /opt/macports-test)
     42'
     43   return 0
     44}
     45
     46
     47trapcount=0
     48# cf. http://fvue.nl/wiki/Bash:_Error_handling
     49on_exit() {
     50   #set -xv
     51   trapcount=$((trapcount + 1))
     52   if [[ $trapcount -eq 1 ]]; then
     53      cd    # avoid: sudo: cannot get working directory
     54      #find -x "${tmpDir}" -ls 1>&2
     55      echo
     56      [[ -d "${tmpDir}" ]] && rm -rf "${tmpDir}"
     57      [[ -d "${usr_local_off}" ]] && sudo mv -iv "${usr_local_off}" /usr/local
     58      [[ -d "${opt_local_off}" ]] && sudo mv -iv "${opt_local_off}" /opt/local
     59      printf "\n\n\n"
     60      echo dscl . -change /Users/macports NFSHomeDirectory "${MP_PREFIX}/var/macports/home" "${dsclHome}"
     61      current_dscl_home="$(dscl . -read /Users/macports NFSHomeDirectory | sed 's/^NFSHomeDirectory: *//')"
     62      if [[ "${current_dscl_home}" != "${dsclHome}" ]]; then
     63         dscl . -change /Users/macports NFSHomeDirectory "${MP_PREFIX}/var/macports/home" "${dsclHome}"
     64      fi
     65      printf '\n\n%s\n\n\n' "export PATH=\"${MP_PREFIX}/bin:${MP_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin\""
     66  fi
     67   exit
     68}
     69
     70unset CDPATH PATH IFS LC_ALL MP_PREFIX all_new
     71IFS=$' \t\n'
     72LC_ALL=C
     73PATH='/sbin:/usr/bin:/bin:/usr/sbin:/sbin'
     74export IFS LC_ALL PATH
     75
     76
     77MP_PREFIX=""
     78all_new=0
     79update=0
     80remove=0
     81verbose=0
     82debug=0
     83printpath=0
     84extract=0
     85build_source=0
     86clean_all=0
     87
     88
     89while getopts ":cdehl:nprsuv" opt; do
     90  case "$opt" in
     91    c)
     92      clean_all=1
     93      ;;
     94    d)
     95      debug=1
     96      ;;
     97    e)
     98      extract=1
     99      ;;
     100    h)
     101      show_usage
     102      exit
     103      ;;
     104    l)
     105      MP_PREFIX="$OPTARG"
     106      ;;
     107    n)
     108      all_new=1
     109      ;;
     110    p)
     111      printpath=1
     112      ;;
     113    r)
     114      remove=1
     115      ;;
     116    d)
     117      build_source=1
     118      ;;
     119    u)
     120      update=1
     121      ;;
     122    v)
     123      verbose=1
     124      ;;
     125    \?)
     126      echo "Invalid option: -$OPTARG" 1>&2
     127      show_usage 1>&2
     128      exit 1
     129      ;;
     130    :)
     131      echo "Option -$OPTARG requires an argument." 1>&2
     132      show_usage 1>&2
     133      exit 1
     134      ;;
     135  esac
     136done
     137
     138
     139shift $((OPTIND-1))
     140
     141if [[ -z "$MP_PREFIX" ]]; then
     142   MP_PREFIX='/opt/macports-test'
     143fi
     144
     145if [[ "$MP_PREFIX" == '/opt/local' ]]; then
     146   echo 'Use of /opt/local is not allowed!' 1>&2
     147   exit 1
     148fi
     149
     150if [[ "$MP_PREFIX" == '/' ]]; then
     151   echo 'Use of / is not allowed!' 1>&2
     152   exit 1
     153fi
     154
     155if [[ $(dirname "${MP_PREFIX}") != '/opt' ]]; then
     156   echo 'Use: /opt/somedir' 1>&2
     157   exit 1
     158fi
     159
     160if [[ ! -d "$MP_PREFIX" ]]; then
     161   all_new=1
     162fi
     163
     164if [[ $all_new -eq 1 ]]; then
     165   clean_all=0
     166   remove=0
     167   update=0
     168fi
     169
     170
     171declare -rx MP_PREFIX
     172
     173unset PATH
     174PATH="${MP_PREFIX}/bin:${MP_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
     175export PATH
     176
     177if [[ $printpath -eq 1 ]]; then
     178   printf '\n\n%s\n\n\n' "export PATH=\"${MP_PREFIX}/bin:${MP_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin\""
     179   exit
     180fi
     181
     182if [[ $all_new -ne 1 ]] && [[ ! -x "${MP_PREFIX}/bin/port" ]]; then
     183   echo "No port command found at: ${MP_PREFIX}/bin/port" 1>&2
     184   exit 1
     185fi
     186
     187
     188if [[ $# -eq 0 ]]; then
     189  if [[ $clean_all -eq 1 ]]; then
     190      ( port -f clean --all installed 2>/dev/null ) || true
     191      exit
     192   elif [[ $update -eq 1 ]]; then
     193      port -f clean --all installed
     194      port selfupdate
     195      port outdated
     196      ( port upgrade -R -u outdated 2>/dev/null ) || true
     197      exit
     198   else
     199      echo
     200      echo 'No port to install given!'
     201      show_usage 1>&2
     202      exit 1
     203   fi
     204fi
     205
     206
     207if [[ $(id -u) -ne 0 ]] || [[ "${HOME}" != '/var/root' ]]; then
     208   echo 'This script must be run in a root shell to prevent sudo timeout!' 1>&2
     209   echo 'Use: sudo -H -i' 1>&2
     210   exit 1
     211fi
     212
     213
     214dsclHome="$(dscl . -read /Users/macports NFSHomeDirectory | sed 's/^NFSHomeDirectory: *//')"
     215dscl . -change /Users/macports NFSHomeDirectory "${dsclHome}" "${MP_PREFIX}/var/macports/home"
     216
     217
     218# make sure the current working directory exists
     219pwd -P 1>/dev/null || exit 1
     220
     221
     222# prevent idle sleep
     223pmset -a force sleep 0 displaysleep 0 disksleep 0
     224
     225
     226sleep 1
     227unset usr_local_off opt_local_off tmpDir
     228usr_local_off="/usr/local-off-$(date '+%Y-%m-%d-%H_%M_%S')"
     229opt_local_off="/opt/local-off-$(date '+%Y-%m-%d-%H_%M_%S')"
     230
     231tmpDir="$(mktemp -d /tmp/macports.XXXXXX)" || exit 1
     232
     233declare -rx usr_local_off opt_local_off tmpDir
     234
     235trap on_exit EXIT TERM HUP INT QUIT
     236
     237echo
     238
     239# make sure /usr/local is not interfering with MacPorts build processes for /opt/local
     240[[ -d '/usr/local' ]] && sudo mv -iv /usr/local "${usr_local_off}"
     241
     242# back up the old MacPorts system
     243[[ -d '/opt/local' ]] && sudo mv -iv /opt/local "${opt_local_off}"
     244
     245echo
     246
     247# clean up previous "${MP_PREFIX}" directory
     248if [[ -x "${MP_PREFIX}/bin/port" ]]; then
     249   if [[ $clean_all -eq 1 ]]; then
     250      ( port -f clean --all installed 2>/dev/null ) || true
     251   else
     252      ( port -f clean --work "$@" 2>/dev/null ) || true
     253   fi
     254fi
     255
     256
     257if [[ $all_new -eq 1 ]]; then
     258
     259   if [[ -x "${MP_PREFIX}/bin/port" ]]; then
     260      ( port -f uninstall installed 2>/dev/null ) || true
     261   fi
     262
     263   # since "rm -rf" is a dangerous command, we restrict its use to /opt/somedir
     264   if [[ -d "${MP_PREFIX}" ]] && [[ $(dirname "${MP_PREFIX}") == '/opt' ]]; then
     265      rm -rf "${MP_PREFIX}"
     266   fi
     267
     268   # get a fresh MacPorts 2.0.4 install in ${MP_PREFIX}
     269
     270   cd "${tmpDir}" || exit 1
     271
     272   # cf. http://guide.macports.org/#installing.macports.source.multiple
     273   unset PATH
     274   export PATH='/bin:/sbin:/usr/bin:/usr/sbin'
     275   curl -L -O https://distfiles.macports.org/MacPorts/MacPorts-2.0.4.tar.bz2 || exit 1
     276   tar -xjf MacPorts-2.0.4.tar.bz2
     277   cd MacPorts-2.0.4 || exit 1
     278   #--with-install-user=$owner --with-install-group=$group --with-directory-mode=$perms"
     279   ./configure --prefix="${MP_PREFIX}" --with-tclpackage="${MP_PREFIX}/tcl" --with-applications-dir="${MP_PREFIX}/Applications"
     280
     281   make
     282   make install
     283
     284   # get the Portfiles and update the system
     285   "${MP_PREFIX}/bin/port" selfupdate
     286
     287fi  # all_new
     288
     289
     290cd "${tmpDir}" || exit 1
     291
     292unset PATH
     293PATH="${MP_PREFIX}/bin:${MP_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
     294export PATH
     295
     296if [[ $update -eq 1 ]]; then
     297   port -f clean --all installed
     298   port selfupdate
     299   port outdated
     300   ( port upgrade -R -u outdated 2>/dev/null ) || true
     301fi
     302
     303
     304if [[ $remove -eq 1 ]]; then
     305   port -f clean --all "$@"
     306   port -f -v uninstall "$@"
     307fi
     308
     309
     310if [[ $verbose -eq 1 ]]; then
     311   port -f -v install "$@"
     312elif [[ $debug -eq 1 ]]; then
     313   port -f -d install "$@"
     314elif [[ $extract -eq 1 ]]; then
     315   port -f clean --all "$@"
     316   port -f extract "$@"
     317elif [[ $build_source -eq 1 ]] && [[ $verbose -eq 1 ]]; then
     318   port -f clean --all "$@"
     319   port -f -s -v install "$@"
     320elif [[ $build_source -eq 1 ]] && [[ $debug -eq 1 ]]; then
     321   port -f -s -d install "$@"
     322elif [[ $build_source -eq 1 ]]; then
     323   port -f -s install "$@"
     324else
     325   port -f install "$@"
     326fi
     327
     328
     329exit 0
     330
     331}}}