3 # pt -- the MacPorts port tool"
4 # Copyright (c)2007,2008 Alexander Barton, alex@barton.de
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
22 UPGRADE_REMOVE_INACTIVE=1
25 declare -i WIDTH=${COLUMNS:-80}
27 LINE="-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
30 # Display version information.
32 echo "$NAME $VERSION ($DATE) -- the MacPorts port tool"
33 echo "Copyright (c)2007 Alexander Barton, alex@barton.de"
35 echo "This is free software; see the source for copying conditions. There is NO"
36 echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
41 # Display some usage information and exit.
44 echo "Usage: $0 <options> <command> [<args> ...]"
46 echo "Available commands:"
47 echo " deps <port> [<port> [...]] -- examine dependencies"
48 echo " install <port> [<port> [...]] -- install port(s)"
49 echo " remove <port> [<port> [...]] -- remove port(s)"
50 echo " clean <port> [<port> [...]] -- clean port build files"
51 echo " update -- update port database"
52 echo " upgrade -- upgrade outdated port(s)"
53 echo " freshup -- update port database & list outdated ports"
54 echo " search <term> -- search for port(s)"
55 echo " list <term> -- list port(s)"
56 echo " info <port> -- display details of a port"
57 echo " status -- show MacPorts status"
59 echo "The following global options are available:"
60 echo " -q: quiet mode, little output +q: no quiet mode, normal output"
61 echo " -v: enable verbose mode +v: disable verbose mode"
62 echo " -V: verbose port(1) messages +V: no verbose port(1) messages"
63 echo " -d: enable debug mode +d: disable verbose mode"
69 # Initialize "state machine", clear temporary files.
71 Verbose_Msg "Initializing port states ..."
72 port list >"$TMPDIR/available"
73 Debug_Msg "Listing installed ports ..."
74 port installed | grep "^ " >"$TMPDIR/installed"
75 Debug_Msg "Listing inactive ports ..."
76 port list inactive >"$TMPDIR/inactive"
77 echo -n '' >"$TMPDIR/required.all"
78 echo -n '' >"$TMPDIR/missing.all"
79 echo -n "" >"$TMPDIR/outdated"
80 echo -n "" >"$TMPDIR/outdated.info"
82 declare -i c_avail=`cat "$TMPDIR/available" | wc -l`
83 declare -i c_inst=`cat "$TMPDIR/installed" | wc -l`
84 declare -i c_inact=`cat "$TMPDIR/inactive" | wc -l`
86 [ $c_inst -eq $c_avail ] && c_inst=0
87 [ $c_inact -eq $c_avail ] && c_inact=0
88 [ $c_inact -eq 0 ] && s="" || s=" ($c_inact inactive)"
89 Verbose_Msg "$c_avail ports available, $c_inst installed$s."
93 # Reset "required" and "missing" package lists, move saved
94 # packages to "required.all"/"missing.all" lists for later
97 Debug_Msg "Resetting internal state ..."
100 [ -r "$TMPDIR/required" ] \
101 && cat "$TMPDIR/required" >>"$TMPDIR/required.all"
102 echo -n '' >"$TMPDIR/required"
104 [ -r "$TMPDIR/missing" ] \
105 && cat "$TMPDIR/missing" >>"$TMPDIR/missing.all"
106 echo -n '' >"$TMPDIR/missing"
110 # Check, if this script is running with root privileges.
112 [ "$UID" -eq 0 -o "$EUID" -eq 0 ] && return 0
113 Error_Msg "This function must be executed with root privileges!"
118 # List contents of temporary directory to console.
119 echo "--- $TMPDIR ---"; ls -al "$TMPDIR"; echo "---"
123 # Dump contents of a file to console.
124 echo "--- $1 ---"; cat "$1"; echo "---"
128 # Exit this scripts after cleaning up.
131 Debug_Msg "Cleaning up ..."
132 [ -n "$TMPDIR" ] && rm -rf "$TMPDIR"
133 Debug_Msg "Done. Exit code: $1"
137 Clean_Exit_Signal() {
138 # Exit this script with exit code 127, used when an interup signal
139 # (SIGINT) has been catched.
146 # Display message, if not running in "quiet" mode.
147 # $*: message to display.
149 [ -n "$QUIET" ] && return
150 if [ "$1" = "-n" ]; then
159 # Display message, even when running in "quiet" mode.
160 # $*: message to display.
162 if [ "$1" = "-n" ]; then
171 # Display message when running in "debug" or "verbose" mode.
172 # $*: message to display.
174 [ -z "$QUIET" -a -n "$VERBOSE" ] && echo $*
178 # Display message when running in "debug" mode.
179 # $*: message to display.
181 [ -n "$DEBUG" ] && echo $*
185 # Display warning message.
186 # $*: message to display.
192 # Display error message.
193 # $*: message to display.
200 [ $? -ne 0 ] && return 0
203 Msg_NoQuiet -n "$* [Y/n] "
204 while [ "$x" != "" -a "$x" != "n" -a "$x" != "N" -a "$x" != "y" -a "$x" != "Y" ]; do
207 [ -z "$x" ] && x="yes"
208 [ "$x" = "n" ] && x="no"
209 [ "$x" = "y" ] && x="yes"
211 [ "$x" = "no" ] && return 1 || return 0
215 # Call the real "port(1)" command, show and hide output as configured.
216 # $*: arguments for port(1).
219 [ -n "$VERBOSE" ] && args=""
220 [ -n "$PORT_VERBOSE" ] && args="-v "
221 [ -n "$DEBUG" ] && args="-v -d "
222 local cmd="port $args$*"
224 declare -i a=${#cmd}+4; a=$WIDTH-$a; a=$a/2
226 local top="${LINE:0:$a}[ $cmd ]${LINE}"
229 if [ "$args" = "" ]; then
230 $cmd | while read x; do
231 msg=$(echo "$x"|sed -e 's/--->//g'|sed -e 's/^ //g')
233 if [ ${#msg} -ge $WIDTH ]; then
234 declare -i w=$WIDTH-3
235 echo "${msg:0:$w}..."
240 elif [ "$args" = "-v " -o "$args" = "-v -d " ]; then
241 Msg_NoQuiet "${top:0:$WIDTH}"
243 Msg_NoQuiet "${bottom:0:$WIDTH}"
245 $cmd >/dev/null 2>&1 ; r=$?
252 # Check, if the given port exists
255 port info "$1" 2>/dev/null | grep 'Maintainers: ' >/dev/null; local r=$?
256 if [ $r -ne 0 ]; then
257 Error_Msg "Port \"$1\" does not exist!"
260 Debug_Msg "Port \"$1\" exists. Ok."
265 # Check, if the given port is installed
269 [ $? -eq 0 ] || return 1
271 grep "^ $1 " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
272 if [ $r -ne 0 ]; then
273 Error_Msg "Port \"$1\" is not installed!"
276 Debug_Msg "Port \"$1\" is installed. Ok."
280 Dump_Package_File() {
281 # Display contents of a "pt package-list file", like "required[.all]".
283 # $2: package-list file name
284 # $3: "u": show only un-installed ports
286 pkgs=$(cat "$2" | sort -u)
287 [ -z "$pkgs" ] && return 0
294 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
295 if [ "$3" = "u" ]; then
296 [ $r -eq 0 ] && continue
298 [ $r -eq 0 ] && d="$d[i]"
301 if [ $x -gt $WIDTH ]; then
302 Msg_NoQuiet; Msg_NoQuiet -n " "
312 Count_Package_File() {
313 # Return number of packages listed in a "pt package-list file".
314 # $1: package-list file name
315 # $2: "u": show only un-installed ports
317 pkgs=$(cat "$1" | sort -u)
320 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
321 if [ "$2" = "u" ]; then
322 [ $r -eq 0 ] && continue
331 grep "^$1\$" "$TMP4" >/dev/null 2>&1
332 if [ $? -eq 0 ]; then
333 Debug_Msg "Dependencies for \"$1\" already checked. Skipping."
336 Debug_Msg "Checking dependencies for \"$1\" ..."
338 port deps "$1" | while read x; do
339 if [ "$x" = "$1 has no dependencies" ]; then
342 if [ "$x" = "$1 has build dependencies on:" ]; then
343 phase="build"; continue
345 if [ "$x" = "$1 has library dependencies on:" ]; then
346 phase="lib"; continue
348 if [ "$x" = "$1 has runtime dependencies on:" ]; then
349 phase="runtime"; continue
354 echo "$1:$x" >>"$TMP1"
358 echo "$1:$x" >>"$TMP2"
362 echo "$1:$x" >>"$TMP3"
366 Warn_Msg "Unknown phase, token \"$x\"!?"
381 [ -z "$4" ] && t=$(head -n 1 "$2" | cut -d':' -f1) || t="$4"
383 while [ -n "$t" ]; do
384 deps=$(grep "^$t:" "$2" 2>/dev/null | sort -u)
385 grep -v "^$t:" "$2" >"$TMP" 2>/dev/null
389 port=$(echo "$d" | cut -d':' -f1)
390 req=$(echo "$d" | cut -d':' -f2)
393 while [ $x -lt $level ]; do
394 [ -n "$VERBOSE" -a -z "$QUIET" ] && echo -n ' '
398 Verbose_Msg "Port \"$t\" depends on $req"
399 echo "$req" >>"$TMPDIR/required"
401 Deps_a $level+1 "$2" "$3" "$req"
404 [ "$level" -le 1 ] && t=$(head -n 1 "$2" | cut -d':' -f1) || t=""
409 # Display dependancies of a port
412 Port_Exists "$1"; local r=$?
413 [ $r -ne 0 ] && return $r
415 touch "$TMP1" "$TMP2" "$TMP3" "$TMP4"
417 Msg "Analyzing port \"$1\" ..."
420 build_deps=$(cat "$TMP1")
421 lib_deps=$(cat "$TMP2")
422 runtime_deps=$(cat "$TMP3")
424 if [ -z "$build_deps" -a -z "$lib_deps" -a -z "$runtime_deps" ]; then
425 Msg "Port \"$1\" has no dependencies."
429 if [ -n "$build_deps" ]; then
430 Verbose_Msg; Verbose_Msg "Build dependencies:"
431 Deps_a 1 "$TMP1" build
433 if [ -n "$lib_deps" ]; then
434 Verbose_Msg; Verbose_Msg "Library dependencies:"
437 if [ -n "$runtime_deps" ]; then
438 Verbose_Msg; Verbose_Msg "Runtime dependencies:"
439 Deps_a 1 "$TMP3" runtime
443 deps=$(cat "$TMPDIR/required" | sort -u)
444 declare -i count=$(cat "$TMPDIR/required" | sort -u | wc -l )
448 && Msg_NoQuiet "The following port is required by \"$1\":" \
449 || Msg_NoQuiet "The following $count ports are required by \"$1\":"
454 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
455 if [ $r -eq 0 ]; then
459 not_inst="$not_inst $d"
462 if [ $x -gt $WIDTH ]; then
463 Msg_NoQuiet; Msg_NoQuiet -n " "
470 if [ -n "$not_inst" ]; then
472 && Msg_NoQuiet "The following port (probably) needs to be installed:" \
473 || Msg_NoQuiet "The following $count ports (probably!) need to be installed:"
476 for d in $not_inst; do
478 if [ $x -gt $WIDTH ]; then
479 Msg_NoQuiet; Msg_NoQuiet -n " "
483 echo "$d" >>"$TMPDIR/missing"
487 Msg_NoQuiet "All required ports are already installed."
495 # Install a given port, and its dependencies.
496 # $*: port, including version number, variant(s), ...
498 Msg_NoQuiet "Installing port \"$p\" (and dependencies) ..."
501 [ -n "$CLEAN_WORKDIR" ] && args="$args -c"
503 PortCmd $args install $*
504 if [ $? -ne 0 ]; then
505 Error_Msg "Installation of \"$1\" failed!"
512 # Clean build directory of a port
517 Msg_NoQuiet "Upgrading port \"$1\" (and dependencies): $2 -> $3 ..."
520 [ -n "$CLEAN_WORKDIR" ] && args="$args -c"
521 [ -n "$UPGRADE_REMOVE_INACTIVE" ] && args="$args -fu"
523 PortCmd $args upgrade -n $port
524 if [ $? -ne 0 ]; then
525 Error_Msg "Failed to upgrade port \"$1\"!"
532 # Clean build directory of a port
535 Msg_NoQuiet "Cleaning build directory of port \"$1\" ..."
537 PortCmd clean --all "$1"
538 if [ $? -ne 0 ]; then
539 Error_Msg "Failed to clean port \"$1\"!"
546 # Remove a given port
549 Msg_NoQuiet "Removing port \"$1\" ..."
551 PortCmd uninstall "$1"
552 if [ $? -ne 0 ]; then
553 Error_Msg "Failed to remove port \"$1\"!"
560 if [ $# -lt 1 ]; then
561 Error_Msg "No ports requested!?"
565 deps=$(cat "$TMPDIR/required.all" | sort -u)
568 cat "$TMPDIR/missing.all" >"$TMPDIR/install.all"
570 echo "$p" >>"$TMPDIR/install.all"
574 echo "$p" >>"$TMPDIR/requested.all"
579 if [ -z "$QUIET" ]; then
580 Count_Package_File "$TMPDIR/requested.all"; c=$?
581 [ $c -eq 1 ] && s=" has" || s="s have"
583 "The following $c port$s been requested:" \
584 "$TMPDIR/requested.all"
586 Count_Package_File "$TMPDIR/required.all"; c_req=$?
587 [ $c_req -eq 1 ] && s=" is" || s="s are"
589 "The following $c_req port$s required to fulfil dependencies:" \
590 "$TMPDIR/required.all"
591 Count_Package_File "$TMPDIR/missing.all"; c_add=$?
592 [ $c_add -eq 1 ] && s=" (probably!) needs" || s="s (probably!) need"
594 "The following $c_add additional port$s to be installed:" \
595 "$TMPDIR/missing.all"
596 Count_Package_File "$TMPDIR/install.all" "u"; c_all=$?
597 if [ $c_all -lt 1 ]; then
598 Msg_NoQuiet "No new ports to install."; Msg
601 [ $c_all -eq 1 ] && s="" || s="s"
603 "The following $c_all NEW port$s will be installed (if required):" \
604 "$TMPDIR/install.all" "u"
607 ports=$(tail -r "$TMPDIR/missing.all" | uniq )
609 if [ -n "$DEBUG" ]; then
610 Debug_Msg "Order of installation:"
614 [ "$#" -eq 1 ] && s1=" has" || s1="s have"
615 [ "$c_inst" -eq 1 ] && s2="" || s2="s"
616 Msg_NoQuiet "$# port$s1 been requested, $c_inst port$s2 will be installed."
617 if [ $# -ne $c_inst ]; then
618 Prompt "Do you want to continue?"
619 if [ $? -ne 0 ]; then
631 [ $? -eq 0 ] || errors=$errors+1
637 [ $count -eq 1 ] && s=" has" || s="s have"
638 Msg "$count requested port$s been installed."
639 if [ $errors -gt 0 ]; then
640 [ $errors -gt 1 ] && s="s" || s=""
641 Error_Msg "There have been failures, see above!";
653 Msg "Searching for outdated ports ..."
654 echo -n "" >"$TMPDIR/outdated"
655 echo -n "" >"$TMPDIR/outdated.info"
657 grep -v "No installed ports are outdated" | \
658 grep -v "The following installed ports are outdated" | \
659 awk "{ print \$1, \$2, \$3, \$4 }" | while read port v1 a v2; do
660 echo "$port" >>"$TMPDIR/outdated"
661 echo "$port $v1 $a $v2" >>"$TMPDIR/outdated.info"
667 Count_Package_File "$TMPDIR/outdated"
668 if [ $? -lt 1 ]; then
669 Msg "No installed ports are outdated."
673 "The following installed ports are outdated and will be UPGRADED:" \
677 if [ -n "$VERBOSE" ]; then
678 echo "Upgrade details:"
679 cat "$TMPDIR/outdated.info" | while read port v1 a v2; do
680 echo " $port $v1 -> $v2"
684 [ $c_out -gt 100 ] && c_out=100
689 List_Outdated && return 0
691 Prompt "Do you want to continue?"
692 if [ $? -ne 0 ]; then
699 cat "$TMPDIR/outdated.info" | while read port v1 a v2; do
700 Port_Upgrade "$port" "$v1" "$v2"
701 [ $? -eq 0 ] || errors=$errors+1
705 if [ $errors -gt 0 ]; then
706 [ $errors -gt 1 ] && s="s" || s=""
707 Warn_Msg "$errors error$s occured during upgrading!"
715 Count_Package_File "$TMPDIR/outdated"; c=$?
716 if [ $c -gt 0 ]; then
717 if [ $c -eq 1 ]; then
722 Warn_Msg "There $s1 still $c outdated port$s2 installed!"
730 Status "Actual ports base version" -q
731 Msg "Updating installed port software and database ..."
732 port -q selfupdate; local r=$?
733 if [ $r -eq 0 ]; then
734 Status "New ports base version"
736 Error_Msg "Failed to update port archive!"
746 if [ $? -eq 0 ]; then
748 [ $? -ne 0 ] && errors=$errors+1
753 if [ $errors -gt 0 ]; then
755 [ $errors -ne 1 ] && s="s" || s=""
756 Error_Msg "$errors failure$s while cleaning ports!"
767 if [ $? -eq 0 ]; then
769 [ $? -ne 0 ] && errors=$errors+1
774 if [ $errors -gt 0 ]; then
775 [ $errors -ne 1 ] && s="s" || s=""
776 Error_Msg "$errors failure$s while removing ports!"
785 q=$QUIET; [ "$2" != '-q' ] && QUIET=
787 && Msg -n "Ports base version: " \
789 version=$( port --version </dev/null | grep "MacPorts" )
795 trap Clean_Exit_Signal SIGINT
797 # read in defaults, if available
798 [ -r "/etc/$NAME.conf" ] && . "/etc/$NAME.conf"
811 PORT_VERBOSE=1; VERBOSE=1
819 QUIET=1; VERBOSE=; DEBUG=
827 DEBUG=1; VERBOSE=1; QUIET=
834 "--usage"|"--help"|"-*"|"+*")
846 TMPDIR=`mktemp -d /tmp/${NAME}.XXXXXX` || exit 2
847 Debug_Msg "Temporary directory is \"$TMPDIR\"."
860 [ $# -lt 1 ] && Usage
867 [ $# -lt 1 ] && Usage
872 [ $r -ne 0 ] && Clean_Exit $r
879 [ $# -lt 1 ] && Usage
881 Verbose_Msg "Building port dependencies ..."
884 [ $? -ne 0 ] && Clean_Exit 1
886 Deps "$p" >/dev/null; r=$?
887 [ $r -ne 0 ] && Clean_Exit $r
895 [ $# -lt 1 ] && Usage
899 "outdated"|"list-outdated")
903 "update"|"selfupdate")
906 [ $# -ne 0 ] && Usage
910 "upgrade"|"dist-upgrade")
913 [ $# -gt 0 ] && Usage
918 "freshup"|"fresh"|"up")
921 [ $# -ne 0 ] && Usage
926 "remove"|"uninstall")
929 [ $# -lt 1 ] && Usage
936 [ $# -lt 1 ] && Usage
942 [ $# -lt 1 ] && Usage
948 [ $# -ne 0 ] && Usage