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 " search <term> -- search for port(s)"
54 echo " list <term> -- list port(s)"
55 echo " info <port> -- display details of a port"
56 echo " status -- show MacPorts status"
58 echo "The following global options are available:"
59 echo " -q: quiet mode, little output +q: no quiet mode, normal output"
60 echo " -v: enable verbose mode +v: disable verbose mode"
61 echo " -V: verbose port(1) messages +V: no verbose port(1) messages"
62 echo " -d: enable debug mode +d: disable verbose mode"
68 # Initialize "state machine", clear temporary files.
70 Verbose_Msg "Initializing port states ..."
71 port list >"$TMPDIR/available"
72 Debug_Msg "Listing installed ports ..."
73 port installed | grep "^ " >"$TMPDIR/installed"
74 Debug_Msg "Listing inactive ports ..."
75 port list inactive >"$TMPDIR/inactive"
76 echo -n '' >"$TMPDIR/required.all"
77 echo -n '' >"$TMPDIR/missing.all"
78 echo -n "" >"$TMPDIR/outdated"
79 echo -n "" >"$TMPDIR/outdated.info"
81 declare -i c_avail=`cat "$TMPDIR/available" | wc -l`
82 declare -i c_inst=`cat "$TMPDIR/installed" | wc -l`
83 declare -i c_inact=`cat "$TMPDIR/inactive" | wc -l`
85 [ $c_inst -eq $c_avail ] && c_inst=0
86 [ $c_inact -eq $c_avail ] && c_inact=0
87 [ $c_inact -eq 0 ] && s="" || s=" ($c_inact inactive)"
88 Verbose_Msg "$c_avail ports available, $c_inst installed$s."
92 # Reset "required" and "missing" package lists, move saved
93 # packages to "required.all"/"missing.all" lists for later
96 Debug_Msg "Resetting internal state ..."
99 [ -r "$TMPDIR/required" ] \
100 && cat "$TMPDIR/required" >>"$TMPDIR/required.all"
101 echo -n '' >"$TMPDIR/required"
103 [ -r "$TMPDIR/missing" ] \
104 && cat "$TMPDIR/missing" >>"$TMPDIR/missing.all"
105 echo -n '' >"$TMPDIR/missing"
109 # Check, if this script is running with root privileges.
111 [ "$UID" -eq 0 -o "$EUID" -eq 0 ] && return 0
112 Error_Msg "This function must be executed with root privileges!"
117 # List contents of temporary directory to console.
118 echo "--- $TMPDIR ---"; ls -al "$TMPDIR"; echo "---"
122 # Dump contents of a file to console.
123 echo "--- $1 ---"; cat "$1"; echo "---"
127 # Exit this scripts after cleaning up.
130 Debug_Msg "Cleaning up ..."
131 [ -n "$TMPDIR" ] && rm -rf "$TMPDIR"
132 Debug_Msg "Done. Exit code: $1"
136 Clean_Exit_Signal() {
137 # Exit this script with exit code 127, used when an interup signal
138 # (SIGINT) has been catched.
145 # Display message, if not running in "quiet" mode.
146 # $*: message to display.
148 [ -n "$QUIET" ] && return
149 if [ "$1" = "-n" ]; then
158 # Display message, even when running in "quiet" mode.
159 # $*: message to display.
161 if [ "$1" = "-n" ]; then
170 # Display message when running in "debug" or "verbose" mode.
171 # $*: message to display.
173 [ -z "$QUIET" -a -n "$VERBOSE" ] && echo $*
177 # Display message when running in "debug" mode.
178 # $*: message to display.
180 [ -n "$DEBUG" ] && echo $*
184 # Display warning message.
185 # $*: message to display.
191 # Display error message.
192 # $*: message to display.
199 [ $? -ne 0 ] && return 0
202 Msg_NoQuiet -n "$* [Y/n] "
203 while [ "$x" != "" -a "$x" != "n" -a "$x" != "N" -a "$x" != "y" -a "$x" != "Y" ]; do
206 [ -z "$x" ] && x="yes"
207 [ "$x" = "n" ] && x="no"
208 [ "$x" = "y" ] && x="yes"
210 [ "$x" = "no" ] && return 1 || return 0
214 # Call the real "port(1)" command, show and hide output as configured.
215 # $*: arguments for port(1).
218 [ -n "$VERBOSE" ] && args=""
219 [ -n "$PORT_VERBOSE" ] && args="-v "
220 [ -n "$DEBUG" ] && args="-v -d "
221 local cmd="port $args$*"
223 declare -i a=${#cmd}+4; a=$WIDTH-$a; a=$a/2
225 local top="${LINE:0:$a}[ $cmd ]${LINE}"
228 if [ "$args" = "" ]; then
229 $cmd | while read x; do
230 msg=$(echo "$x"|sed -e 's/--->//g'|sed -e 's/^ //g')
232 if [ ${#msg} -ge $WIDTH ]; then
233 declare -i w=$WIDTH-3
234 echo "${msg:0:$w}..."
239 elif [ "$args" = "-v " -o "$args" = "-v -d " ]; then
240 Msg_NoQuiet "${top:0:$WIDTH}"
242 Msg_NoQuiet "${bottom:0:$WIDTH}"
244 $cmd >/dev/null 2>&1 ; r=$?
251 # Check, if the given port exists
254 port info "$1" 2>/dev/null | grep 'Maintainers: ' >/dev/null; local r=$?
255 if [ $r -ne 0 ]; then
256 Error_Msg "Port \"$1\" does not exist!"
259 Debug_Msg "Port \"$1\" exists. Ok."
264 # Check, if the given port is installed
268 [ $? -eq 0 ] || return 1
270 grep "^ $1 " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
271 if [ $r -ne 0 ]; then
272 Error_Msg "Port \"$1\" is not installed!"
275 Debug_Msg "Port \"$1\" is installed. Ok."
279 Dump_Package_File() {
280 # Display contents of a "pt package-list file", like "required[.all]".
282 # $2: package-list file name
283 # $3: "u": show only un-installed ports
285 pkgs=$(cat "$2" | sort -u)
286 [ -z "$pkgs" ] && return 0
293 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
294 if [ "$3" = "u" ]; then
295 [ $r -eq 0 ] && continue
297 [ $r -eq 0 ] && d="$d[i]"
300 if [ $x -gt $WIDTH ]; then
301 Msg_NoQuiet; Msg_NoQuiet -n " "
311 Count_Package_File() {
312 # Return number of packages listed in a "pt package-list file".
313 # $1: package-list file name
314 # $2: "u": show only un-installed ports
316 pkgs=$(cat "$1" | sort -u)
319 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
320 if [ "$2" = "u" ]; then
321 [ $r -eq 0 ] && continue
330 grep "^$1\$" "$TMP4" >/dev/null 2>&1
331 if [ $? -eq 0 ]; then
332 Debug_Msg "Dependencies for \"$1\" already checked. Skipping."
335 Debug_Msg "Checking dependencies for \"$1\" ..."
337 port deps "$1" | while read x; do
338 if [ "$x" = "$1 has no dependencies" ]; then
341 if [ "$x" = "$1 has build dependencies on:" ]; then
342 phase="build"; continue
344 if [ "$x" = "$1 has library dependencies on:" ]; then
345 phase="lib"; continue
347 if [ "$x" = "$1 has runtime dependencies on:" ]; then
348 phase="runtime"; continue
353 echo "$1:$x" >>"$TMP1"
357 echo "$1:$x" >>"$TMP2"
361 echo "$1:$x" >>"$TMP3"
365 Warn_Msg "Unknown phase, token \"$x\"!?"
380 [ -z "$4" ] && t=$(head -n 1 "$2" | cut -d':' -f1) || t="$4"
382 while [ -n "$t" ]; do
383 deps=$(grep "^$t:" "$2" 2>/dev/null | sort -u)
384 grep -v "^$t:" "$2" >"$TMP" 2>/dev/null
388 port=$(echo "$d" | cut -d':' -f1)
389 req=$(echo "$d" | cut -d':' -f2)
392 while [ $x -lt $level ]; do
393 [ -n "$VERBOSE" -a -z "$QUIET" ] && echo -n ' '
397 Verbose_Msg "Port \"$t\" depends on $req"
398 echo "$req" >>"$TMPDIR/required"
400 Deps_a $level+1 "$2" "$3" "$req"
403 [ "$level" -le 1 ] && t=$(head -n 1 "$2" | cut -d':' -f1) || t=""
408 # Display dependancies of a port
411 Port_Exists "$1"; local r=$?
412 [ $r -ne 0 ] && return $r
414 touch "$TMP1" "$TMP2" "$TMP3" "$TMP4"
416 Msg "Analyzing port \"$1\" ..."
419 build_deps=$(cat "$TMP1")
420 lib_deps=$(cat "$TMP2")
421 runtime_deps=$(cat "$TMP3")
423 if [ -z "$build_deps" -a -z "$lib_deps" -a -z "$runtime_deps" ]; then
424 Msg "Port \"$1\" has no dependencies."
428 if [ -n "$build_deps" ]; then
429 Verbose_Msg; Verbose_Msg "Build dependencies:"
430 Deps_a 1 "$TMP1" build
432 if [ -n "$lib_deps" ]; then
433 Verbose_Msg; Verbose_Msg "Library dependencies:"
436 if [ -n "$runtime_deps" ]; then
437 Verbose_Msg; Verbose_Msg "Runtime dependencies:"
438 Deps_a 1 "$TMP3" runtime
442 deps=$(cat "$TMPDIR/required" | sort -u)
443 declare -i count=$(cat "$TMPDIR/required" | sort -u | wc -l )
447 && Msg_NoQuiet "The following port is required by \"$1\":" \
448 || Msg_NoQuiet "The following $count ports are required by \"$1\":"
453 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
454 if [ $r -eq 0 ]; then
458 not_inst="$not_inst $d"
461 if [ $x -gt $WIDTH ]; then
462 Msg_NoQuiet; Msg_NoQuiet -n " "
469 if [ -n "$not_inst" ]; then
471 && Msg_NoQuiet "The following port (probably) needs to be installed:" \
472 || Msg_NoQuiet "The following $count ports (probably!) need to be installed:"
475 for d in $not_inst; do
477 if [ $x -gt $WIDTH ]; then
478 Msg_NoQuiet; Msg_NoQuiet -n " "
482 echo "$d" >>"$TMPDIR/missing"
486 Msg_NoQuiet "All required ports are already installed."
494 # Install a given port, and its dependencies.
495 # $*: port, including version number, variant(s), ...
497 Msg_NoQuiet "Installing port \"$p\" (and dependencies) ..."
500 [ -n "$CLEAN_WORKDIR" ] && args="$args -c"
502 PortCmd $args install $*
503 if [ $? -ne 0 ]; then
504 Error_Msg "Installation of \"$1\" failed!"
511 # Clean build directory of a port
516 Msg_NoQuiet "Upgrading port \"$1\" (and dependencies): $2 -> $3 ..."
519 [ -n "$CLEAN_WORKDIR" ] && args="$args -c"
520 [ -n "$UPGRADE_REMOVE_INACTIVE" ] && args="$args -fu"
522 PortCmd $args upgrade -n $port
523 if [ $? -ne 0 ]; then
524 Error_Msg "Failed to upgrade port \"$1\"!"
531 # Clean build directory of a port
534 Msg_NoQuiet "Cleaning build directory of port \"$1\" ..."
536 PortCmd clean --all "$1"
537 if [ $? -ne 0 ]; then
538 Error_Msg "Failed to clean port \"$1\"!"
545 # Remove a given port
548 Msg_NoQuiet "Removing port \"$1\" ..."
550 PortCmd uninstall "$1"
551 if [ $? -ne 0 ]; then
552 Error_Msg "Failed to remove port \"$1\"!"
559 if [ $# -lt 1 ]; then
560 Error_Msg "No ports requested!?"
564 deps=$(cat "$TMPDIR/required.all" | sort -u)
567 cat "$TMPDIR/missing.all" >"$TMPDIR/install.all"
569 echo "$p" >>"$TMPDIR/install.all"
573 echo "$p" >>"$TMPDIR/requested.all"
578 if [ -z "$QUIET" ]; then
579 Count_Package_File "$TMPDIR/requested.all"; c=$?
580 [ $c -eq 1 ] && s=" has" || s="s have"
582 "The following $c port$s been requested:" \
583 "$TMPDIR/requested.all"
585 Count_Package_File "$TMPDIR/required.all"; c_req=$?
586 [ $c_req -eq 1 ] && s=" is" || s="s are"
588 "The following $c_req port$s required to fulfil dependencies:" \
589 "$TMPDIR/required.all"
590 Count_Package_File "$TMPDIR/missing.all"; c_add=$?
591 [ $c_add -eq 1 ] && s=" (probably!) needs" || s="s (probably!) need"
593 "The following $c_add additional port$s to be installed:" \
594 "$TMPDIR/missing.all"
595 Count_Package_File "$TMPDIR/install.all" "u"; c_all=$?
596 if [ $c_all -lt 1 ]; then
597 Msg_NoQuiet "No new ports to install."; Msg
600 [ $c_all -eq 1 ] && s="" || s="s"
602 "The following $c_all NEW port$s will be installed (if required):" \
603 "$TMPDIR/install.all" "u"
606 ports=$(tail -r "$TMPDIR/missing.all" | uniq )
608 if [ -n "$DEBUG" ]; then
609 Debug_Msg "Order of installation:"
613 [ "$#" -eq 1 ] && s1=" has" || s1="s have"
614 [ "$c_inst" -eq 1 ] && s2="" || s2="s"
615 Msg_NoQuiet "$# port$s1 been requested, $c_inst port$s2 will be installed."
616 if [ $# -ne $c_inst ]; then
617 Prompt "Do you want to continue?"
618 if [ $? -ne 0 ]; then
630 [ $? -eq 0 ] || errors=$errors+1
636 [ $count -eq 1 ] && s=" has" || s="s have"
637 Msg "$count requested port$s been installed."
638 if [ $errors -gt 0 ]; then
639 [ $errors -gt 1 ] && s="s" || s=""
640 Error_Msg "There have been failures, see above!";
652 Msg "Searching for outdated ports ..."
653 echo -n "" >"$TMPDIR/outdated"
654 echo -n "" >"$TMPDIR/outdated.info"
656 grep -v "No installed ports are outdated" | \
657 grep -v "The following installed ports are outdated" | \
658 awk "{ print \$1, \$2, \$3, \$4 }" | while read port v1 a v2; do
659 echo "$port" >>"$TMPDIR/outdated"
660 echo "$port $v1 $a $v2" >>"$TMPDIR/outdated.info"
666 Count_Package_File "$TMPDIR/outdated"
667 if [ $? -lt 1 ]; then
668 Msg "No installed ports are outdated."
672 "The following installed ports are outdated and will be UPGRADED:" \
676 if [ -n "$VERBOSE" ]; then
677 echo "Upgrade details:"
678 cat "$TMPDIR/outdated.info" | while read port v1 a v2; do
679 echo " $port $v1 -> $v2"
683 [ $c_out -gt 100 ] && c_out=100
688 List_Outdated && return 0
690 Prompt "Do you want to continue?"
691 if [ $? -ne 0 ]; then
698 cat "$TMPDIR/outdated.info" | while read port v1 a v2; do
699 Port_Upgrade "$port" "$v1" "$v2"
700 [ $? -eq 0 ] || errors=$errors+1
704 if [ $errors -gt 0 ]; then
705 [ $errors -gt 1 ] && s="s" || s=""
706 Warn_Msg "$errors error$s occured during upgrading!"
714 Count_Package_File "$TMPDIR/outdated"; c=$?
715 if [ $c -gt 0 ]; then
716 if [ $c -eq 1 ]; then
721 Warn_Msg "There $s1 still $c outdated port$s2 installed!"
729 Status "Actual ports base version" -q
730 Msg "Updating installed port software and database ..."
731 port -q selfupdate; local r=$?
732 if [ $r -eq 0 ]; then
733 Status "New ports base version"
735 Error_Msg "Failed to update port archive!"
745 if [ $? -eq 0 ]; then
747 [ $? -ne 0 ] && errors=$errors+1
752 if [ $errors -gt 0 ]; then
754 [ $errors -ne 1 ] && s="s" || s=""
755 Error_Msg "$errors failure$s while cleaning ports!"
766 if [ $? -eq 0 ]; then
768 [ $? -ne 0 ] && errors=$errors+1
773 if [ $errors -gt 0 ]; then
774 [ $errors -ne 1 ] && s="s" || s=""
775 Error_Msg "$errors failure$s while removing ports!"
784 q=$QUIET; [ "$2" != '-q' ] && QUIET=
786 && Msg -n "Ports base version: " \
788 version=$( port --version </dev/null | grep "MacPorts" )
794 trap Clean_Exit_Signal SIGINT
796 # read in defaults, if available
797 [ -r "/etc/$NAME.conf" ] && . "/etc/$NAME.conf"
810 PORT_VERBOSE=1; VERBOSE=1
818 QUIET=1; VERBOSE=; DEBUG=
826 DEBUG=1; VERBOSE=1; QUIET=
833 "--usage"|"--help"|"-*"|"+*")
845 TMPDIR=`mktemp -d /tmp/${NAME}.XXXXXX` || exit 2
846 Debug_Msg "Temporary directory is \"$TMPDIR\"."
859 [ $# -lt 1 ] && Usage
866 [ $# -lt 1 ] && Usage
871 [ $r -ne 0 ] && Clean_Exit $r
878 [ $# -lt 1 ] && Usage
880 Verbose_Msg "Building port dependencies ..."
883 [ $? -ne 0 ] && Clean_Exit 1
885 Deps "$p" >/dev/null; r=$?
886 [ $r -ne 0 ] && Clean_Exit $r
894 [ $# -lt 1 ] && Usage
898 "outdated"|"list-outdated")
902 "update"|"selfupdate")
905 [ $# -ne 0 ] && Usage
909 "upgrade"|"dist-upgrade")
912 [ $# -gt 0 ] && Usage
917 "remove"|"uninstall")
920 [ $# -lt 1 ] && Usage
927 [ $# -lt 1 ] && Usage
933 [ $# -lt 1 ] && Usage
939 [ $# -ne 0 ] && Usage