3 # pt -- the MacPorts port tool"
4 # Copyright (c)2007-2009 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
27 declare -i WIDTH=${COLUMNS:-80}
29 LINE="-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
32 # Display version information.
34 echo "$NAME $VERSION ($DATE) -- the MacPorts port tool"
35 echo "Copyright (c)2007-2009 Alexander Barton, alex@barton.de"
37 echo "This is free software; see the source for copying conditions. There is NO"
38 echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
43 # Display some usage information and exit.
46 echo "Usage: $0 <options> <command> [<args> ...]"
48 echo "Available commands:"
49 echo " deps <port> [<port> [...]] -- examine dependencies"
50 echo " install <port> [<port> [...]] -- install port(s)"
51 echo " remove <port> [<port> [...]] -- remove port(s)"
52 echo " clean <port> [<port> [...]] -- clean port build files"
53 echo " update -- update port database"
54 echo " outdated -- list all outdated ports"
55 echo " upgrade -- upgrade outdated port(s)"
56 echo " freshup -- update port database & list outdated ports"
57 echo " search <term> -- search for port(s)"
58 echo " list <term> -- list port(s)"
59 echo " info <port> -- display details of a port"
60 echo " status -- show MacPorts status"
62 echo "The following global options are available:"
63 echo " -q: quiet mode, little output +q: no quiet mode, normal output"
64 echo " -v: enable verbose mode +v: disable verbose mode"
65 echo " -V: verbose port(1) messages +V: no verbose port(1) messages"
66 echo " -d: enable debug mode +d: disable verbose mode"
72 # Initialize "state machine", clear temporary files.
74 Verbose_Msg "Initializing port states ..."
75 port list >"$TMPDIR/available"
76 Debug_Msg "Listing installed ports ..."
77 port installed | grep "^ " >"$TMPDIR/installed"
78 Debug_Msg "Listing inactive ports ..."
79 port list inactive >"$TMPDIR/inactive"
80 echo -n '' >"$TMPDIR/required.all"
81 echo -n '' >"$TMPDIR/missing.all"
82 echo -n "" >"$TMPDIR/outdated"
83 echo -n "" >"$TMPDIR/outdated.info"
85 declare -i c_avail=`cat "$TMPDIR/available" | wc -l`
86 declare -i c_inst=`cat "$TMPDIR/installed" | wc -l`
87 declare -i c_inact=`cat "$TMPDIR/inactive" | wc -l`
89 [ $c_inst -eq $c_avail ] && c_inst=0
90 [ $c_inact -eq $c_avail ] && c_inact=0
91 [ $c_inact -eq 0 ] && s="" || s=" ($c_inact inactive)"
92 Verbose_Msg "$c_avail ports available, $c_inst installed$s."
96 # Reset "required" and "missing" package lists, move saved
97 # packages to "required.all"/"missing.all" lists for later
100 Debug_Msg "Resetting internal state ..."
103 [ -r "$TMPDIR/required" ] \
104 && cat "$TMPDIR/required" >>"$TMPDIR/required.all"
105 echo -n '' >"$TMPDIR/required"
107 [ -r "$TMPDIR/missing" ] \
108 && cat "$TMPDIR/missing" >>"$TMPDIR/missing.all"
109 echo -n '' >"$TMPDIR/missing"
113 # Check, if this script is running with root privileges.
115 [ "$UID" -eq 0 -o "$EUID" -eq 0 ] && return 0
116 Error_Msg "This function must be executed with root privileges!"
121 # List contents of temporary directory to console.
122 echo "--- $TMPDIR ---"; ls -al "$TMPDIR"; echo "---"
126 # Dump contents of a file to console.
127 echo "--- $1 ---"; cat "$1"; echo "---"
131 # Exit this scripts after cleaning up.
134 Debug_Msg "Cleaning up ..."
135 [ -n "$TMPDIR" ] && rm -rf "$TMPDIR"
136 Debug_Msg "Done. Exit code: $1"
140 Clean_Exit_Signal() {
141 # Exit this script with exit code 127, used when an interup signal
142 # (SIGINT) has been catched.
149 # Display message, if not running in "quiet" mode.
150 # $*: message to display.
152 [ -n "$QUIET" ] && return
153 if [ "$1" = "-n" ]; then
162 # Display message, even when running in "quiet" mode.
163 # $*: message to display.
165 if [ "$1" = "-n" ]; then
174 # Display message when running in "debug" or "verbose" mode.
175 # $*: message to display.
177 [ -z "$QUIET" -a -n "$VERBOSE" ] && echo $*
181 # Display message when running in "debug" mode.
182 # $*: message to display.
184 [ -n "$DEBUG" ] && echo $*
188 # Display warning message.
189 # $*: message to display.
195 # Display error message.
196 # $*: message to display.
203 [ $? -ne 0 ] && return 0
206 Msg_NoQuiet -n "$* [Y/n] "
207 while [ "$x" != "" -a "$x" != "n" -a "$x" != "N" -a "$x" != "y" -a "$x" != "Y" ]; do
210 [ -z "$x" ] && x="yes"
211 [ "$x" = "n" ] && x="no"
212 [ "$x" = "y" ] && x="yes"
214 [ "$x" = "no" ] && return 1 || return 0
218 # Call the real "port(1)" command, show and hide output as configured.
219 # $*: arguments for port(1).
222 [ -n "$VERBOSE" ] && args=""
223 [ -n "$PORT_VERBOSE" ] && args="-v "
224 [ -n "$DEBUG" ] && args="-v -d "
225 local cmd="port $args$*"
227 declare -i a=${#cmd}+4; a=$WIDTH-$a; a=$a/2
229 local top="${LINE:0:$a}[ $cmd ]${LINE}"
232 if [ "$args" = "" ]; then
233 $cmd | while read x; do
234 msg=$(echo "$x"|sed -e 's/--->//g'|sed -e 's/^ //g')
236 if [ ${#msg} -ge $WIDTH ]; then
237 declare -i w=$WIDTH-3
238 echo "${msg:0:$w}..."
243 elif [ "$args" = "-v " -o "$args" = "-v -d " ]; then
244 Msg_NoQuiet "${top:0:$WIDTH}"
246 Msg_NoQuiet "${bottom:0:$WIDTH}"
248 $cmd >/dev/null 2>&1 ; r=$?
255 # Check, if the given port exists
258 port info "$1" 2>/dev/null | grep 'Maintainers: ' >/dev/null; local r=$?
259 if [ $r -ne 0 ]; then
260 Error_Msg "Port \"$1\" does not exist!"
263 Debug_Msg "Port \"$1\" exists. Ok."
268 # Check, if the given port is installed
272 [ $? -eq 0 ] || return 1
274 grep "^ $1 " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
275 if [ $r -ne 0 ]; then
276 Error_Msg "Port \"$1\" is not installed!"
279 Debug_Msg "Port \"$1\" is installed. Ok."
283 Dump_Package_File() {
284 # Display contents of a "pt package-list file", like "required[.all]".
286 # $2: package-list file name
287 # $3: "u": show only un-installed ports
289 pkgs=$(cat "$2" | sort -u)
290 [ -z "$pkgs" ] && return 0
297 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
298 if [ "$3" = "u" ]; then
299 [ $r -eq 0 ] && continue
301 [ $r -eq 0 ] && d="$d[i]"
304 if [ $x -gt $WIDTH ]; then
305 Msg_NoQuiet; Msg_NoQuiet -n " "
315 Count_Package_File() {
316 # Return number of packages listed in a "pt package-list file".
317 # $1: package-list file name
318 # $2: "u": show only un-installed ports
320 pkgs=$(cat "$1" | sort -u)
323 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
324 if [ "$2" = "u" ]; then
325 [ $r -eq 0 ] && continue
334 grep "^$1\$" "$TMP4" >/dev/null 2>&1
335 if [ $? -eq 0 ]; then
336 Debug_Msg "Dependencies for \"$1\" already checked. Skipping."
339 Debug_Msg "Checking dependencies for \"$1\" ..."
341 port deps "$1" | while read x; do
342 if [ "$x" = "$1 has no dependencies" ]; then
345 if [ "$x" = "$1 has build dependencies on:" ]; then
346 phase="build"; continue
348 if [ "$x" = "$1 has library dependencies on:" ]; then
349 phase="lib"; continue
351 if [ "$x" = "$1 has runtime dependencies on:" ]; then
352 phase="runtime"; continue
357 echo "$1:$x" >>"$TMP1"
361 echo "$1:$x" >>"$TMP2"
365 echo "$1:$x" >>"$TMP3"
369 Warn_Msg "Unknown phase, token \"$x\"!?"
384 [ -z "$4" ] && t=$(head -n 1 "$2" | cut -d':' -f1) || t="$4"
386 while [ -n "$t" ]; do
387 deps=$(grep "^$t:" "$2" 2>/dev/null | sort -u)
388 grep -v "^$t:" "$2" >"$TMP" 2>/dev/null
392 port=$(echo "$d" | cut -d':' -f1)
393 req=$(echo "$d" | cut -d':' -f2)
396 while [ $x -lt $level ]; do
397 [ -n "$VERBOSE" -a -z "$QUIET" ] && echo -n ' '
401 Verbose_Msg "Port \"$t\" depends on $req"
402 echo "$req" >>"$TMPDIR/required"
404 Deps_a $level+1 "$2" "$3" "$req"
407 [ "$level" -le 1 ] && t=$(head -n 1 "$2" | cut -d':' -f1) || t=""
412 # Display dependancies of a port
415 Port_Exists "$1"; local r=$?
416 [ $r -ne 0 ] && return $r
418 touch "$TMP1" "$TMP2" "$TMP3" "$TMP4"
420 Msg "Analyzing port \"$1\" ..."
423 build_deps=$(cat "$TMP1")
424 lib_deps=$(cat "$TMP2")
425 runtime_deps=$(cat "$TMP3")
427 if [ -z "$build_deps" -a -z "$lib_deps" -a -z "$runtime_deps" ]; then
428 Msg "Port \"$1\" has no dependencies."
432 if [ -n "$build_deps" ]; then
433 Verbose_Msg; Verbose_Msg "Build dependencies:"
434 Deps_a 1 "$TMP1" build
436 if [ -n "$lib_deps" ]; then
437 Verbose_Msg; Verbose_Msg "Library dependencies:"
440 if [ -n "$runtime_deps" ]; then
441 Verbose_Msg; Verbose_Msg "Runtime dependencies:"
442 Deps_a 1 "$TMP3" runtime
446 deps=$(cat "$TMPDIR/required" | sort -u)
447 declare -i count=$(cat "$TMPDIR/required" | sort -u | wc -l )
451 && Msg_NoQuiet "The following port is required by \"$1\":" \
452 || Msg_NoQuiet "The following $count ports are required by \"$1\":"
457 grep "^ $d " "$TMPDIR/installed" >/dev/null 2>&1; local r=$?
458 if [ $r -eq 0 ]; then
462 not_inst="$not_inst $d"
465 if [ $x -gt $WIDTH ]; then
466 Msg_NoQuiet; Msg_NoQuiet -n " "
473 if [ -n "$not_inst" ]; then
475 && Msg_NoQuiet "The following port (probably) needs to be installed:" \
476 || Msg_NoQuiet "The following $count ports (probably!) need to be installed:"
479 for d in $not_inst; do
481 if [ $x -gt $WIDTH ]; then
482 Msg_NoQuiet; Msg_NoQuiet -n " "
486 echo "$d" >>"$TMPDIR/missing"
490 Msg_NoQuiet "All required ports are already installed."
498 # Install a given port, and its dependencies.
499 # $*: port, including version number, variant(s), ...
501 Msg_NoQuiet "Installing port \"$p\" (and dependencies) ..."
504 [ -n "$CLEAN_WORKDIR" ] && args="$args -c"
506 PortCmd $args install $*
507 if [ $? -ne 0 ]; then
508 Error_Msg "Installation of \"$1\" failed!"
515 # Clean build directory of a port
520 Msg_NoQuiet "Upgrading port \"$1\" (and dependencies): $2 -> $3 ..."
523 [ -n "$CLEAN_WORKDIR" ] && args="$args -c"
524 [ -n "$UPGRADE_REMOVE_INACTIVE" ] && args="$args -fu"
526 PortCmd $args upgrade -n $port
527 if [ $? -ne 0 ]; then
528 Error_Msg "Failed to upgrade port \"$1\"!"
535 # Clean build directory of a port
538 Msg_NoQuiet "Cleaning build directory of port \"$1\" ..."
540 PortCmd clean --all "$1"
541 if [ $? -ne 0 ]; then
542 Error_Msg "Failed to clean port \"$1\"!"
549 # Remove a given port
552 Msg_NoQuiet "Removing port \"$1\" ..."
554 PortCmd uninstall "$1"
555 if [ $? -ne 0 ]; then
556 Error_Msg "Failed to remove port \"$1\"!"
563 if [ $# -lt 1 ]; then
564 Error_Msg "No ports requested!?"
568 deps=$(cat "$TMPDIR/required.all" | sort -u)
571 cat "$TMPDIR/missing.all" >"$TMPDIR/install.all"
573 echo "$p" >>"$TMPDIR/install.all"
577 echo "$p" >>"$TMPDIR/requested.all"
582 if [ -z "$QUIET" ]; then
583 Count_Package_File "$TMPDIR/requested.all"; c=$?
584 [ $c -eq 1 ] && s=" has" || s="s have"
586 "The following $c port$s been requested:" \
587 "$TMPDIR/requested.all"
589 Count_Package_File "$TMPDIR/required.all"; c_req=$?
590 [ $c_req -eq 1 ] && s=" is" || s="s are"
592 "The following $c_req port$s required to fulfil dependencies:" \
593 "$TMPDIR/required.all"
594 Count_Package_File "$TMPDIR/missing.all"; c_add=$?
595 [ $c_add -eq 1 ] && s=" (probably!) needs" || s="s (probably!) need"
597 "The following $c_add additional port$s to be installed:" \
598 "$TMPDIR/missing.all"
599 Count_Package_File "$TMPDIR/install.all" "u"; c_all=$?
600 if [ $c_all -lt 1 ]; then
601 Msg_NoQuiet "No new ports to install."; Msg
604 [ $c_all -eq 1 ] && s="" || s="s"
606 "The following $c_all NEW port$s will be installed (if required):" \
607 "$TMPDIR/install.all" "u"
610 ports=$(tail -r "$TMPDIR/missing.all" | uniq )
612 if [ -n "$DEBUG" ]; then
613 Debug_Msg "Order of installation:"
617 [ "$#" -eq 1 ] && s1=" has" || s1="s have"
618 [ "$c_inst" -eq 1 ] && s2="" || s2="s"
619 Msg_NoQuiet "$# port$s1 been requested, $c_inst port$s2 will be installed."
620 if [ $# -ne $c_inst ]; then
621 Prompt "Do you want to continue?"
622 if [ $? -ne 0 ]; then
634 [ $? -eq 0 ] || errors=$errors+1
640 [ $count -eq 1 ] && s=" has" || s="s have"
641 Msg "$count requested port$s been installed."
642 if [ $errors -gt 0 ]; then
643 [ $errors -gt 1 ] && s="s" || s=""
644 Error_Msg "There have been failures, see above!";
656 Msg "Searching for outdated ports ..."
657 echo -n "" >"$TMPDIR/outdated"
658 echo -n "" >"$TMPDIR/outdated.info"
660 grep -v "No installed ports are outdated" | \
661 grep -v "The following installed ports are outdated" | \
662 awk "{ print \$1, \$2, \$3, \$4 }" | while read port v1 a v2; do
663 echo "$port" >>"$TMPDIR/outdated"
664 echo "$port $v1 $a $v2" >>"$TMPDIR/outdated.info"
670 Count_Package_File "$TMPDIR/outdated"
671 if [ $? -lt 1 ]; then
672 Msg "No installed ports are outdated."
676 "The following installed ports are outdated and will be UPGRADED:" \
680 if [ -n "$VERBOSE" ]; then
681 echo "Upgrade details:"
682 cat "$TMPDIR/outdated.info" | while read port v1 a v2; do
683 echo " $port $v1 -> $v2"
687 [ $c_out -gt 100 ] && c_out=100
692 List_Outdated && return 0
694 Prompt "Do you want to continue?"
695 if [ $? -ne 0 ]; then
702 cat "$TMPDIR/outdated.info" | while read port v1 a v2; do
703 Port_Upgrade "$port" "$v1" "$v2"
704 [ $? -eq 0 ] || errors=$errors+1
708 if [ $errors -gt 0 ]; then
709 [ $errors -gt 1 ] && s="s" || s=""
710 Warn_Msg "$errors error$s occured during upgrading!"
718 Count_Package_File "$TMPDIR/outdated"; c=$?
719 if [ $c -gt 0 ]; then
720 if [ $c -eq 1 ]; then
725 Warn_Msg "There $s1 still $c outdated port$s2 installed!"
733 Status "Actual ports base version" -q
734 Msg "Updating installed port software and database ..."
735 port -q selfupdate; local r=$?
736 if [ $r -eq 0 ]; then
737 Status "New ports base version"
739 Error_Msg "Failed to update port archive!"
749 if [ $? -eq 0 ]; then
751 [ $? -ne 0 ] && errors=$errors+1
756 if [ $errors -gt 0 ]; then
758 [ $errors -ne 1 ] && s="s" || s=""
759 Error_Msg "$errors failure$s while cleaning ports!"
770 if [ $? -eq 0 ]; then
772 [ $? -ne 0 ] && errors=$errors+1
777 if [ $errors -gt 0 ]; then
778 [ $errors -ne 1 ] && s="s" || s=""
779 Error_Msg "$errors failure$s while removing ports!"
788 q=$QUIET; [ "$2" != '-q' ] && QUIET=
790 && Msg -n "Ports base version: " \
792 version=$( port --version 2>/dev/null | grep "MacPorts" )
793 [ $? -eq 0 ] || version=$( port version | cut -d' ' -f2 )
799 trap Clean_Exit_Signal SIGINT
801 # read in defaults, if available
802 [ -r "/etc/$NAME.conf" ] && . "/etc/$NAME.conf"
815 PORT_VERBOSE=1; VERBOSE=1
823 QUIET=1; VERBOSE=; DEBUG=
831 DEBUG=1; VERBOSE=1; QUIET=
838 "--usage"|"--help"|"-*"|"+*")
850 TMPDIR=`mktemp -d /tmp/${NAME}.XXXXXX` || exit 2
851 Debug_Msg "Temporary directory is \"$TMPDIR\"."
864 [ $# -lt 1 ] && Usage
871 [ $# -lt 1 ] && Usage
876 [ $r -ne 0 ] && Clean_Exit $r
883 [ $# -lt 1 ] && Usage
885 Verbose_Msg "Building port dependencies ..."
888 [ $? -ne 0 ] && Clean_Exit 1
890 Deps "$p" >/dev/null; r=$?
891 [ $r -ne 0 ] && Clean_Exit $r
899 [ $# -lt 1 ] && Usage
903 "outdated"|"list-outdated")
907 "update"|"selfupdate")
910 [ $# -ne 0 ] && Usage
914 "upgrade"|"dist-upgrade")
917 [ $# -gt 0 ] && Usage
925 [ $# -ne 0 ] && Usage
933 [ $# -ne 0 ] && Usage
938 "remove"|"uninstall")
941 [ $# -lt 1 ] && Usage
948 [ $# -lt 1 ] && Usage
954 [ $# -lt 1 ] && Usage
960 [ $# -ne 0 ] && Usage