<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[62946] users</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.macports.org/changeset/62946">62946</a></dd>
<dt>Author</dt> <dd>jmr@macports.org</dd>
<dt>Date</dt> <dd>2010-01-22 07:40:08 -0800 (Fri, 22 Jan 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>script to correctly install a list of ports with their specified variants</pre>

<h3>Added Paths</h3>
<ul>
<li>users/jmr/</li>
<li>users/jmr/restore_ports/</li>
<li><a href="#usersjmrrestore_portsrestore_portstcl">users/jmr/restore_ports/restore_ports.tcl</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="usersjmrrestore_portsrestore_portstcl"></a>
<div class="addfile"><h4>Added: users/jmr/restore_ports/restore_ports.tcl (0 => 62946)</h4>
<pre class="diff"><span>
<span class="info">--- users/jmr/restore_ports/restore_ports.tcl                                (rev 0)
+++ users/jmr/restore_ports/restore_ports.tcl        2010-01-22 15:40:08 UTC (rev 62946)
</span><span class="lines">@@ -0,0 +1,239 @@
</span><ins>+#!/usr/bin/tclsh
+# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
+#
+# Install a list of ports given in the form produced by 'port installed', in
+# correct dependency order so as to preserve the selected variants.
+#
+# Todo:
+# Handle conflicting ports somehow
+# Once &quot;good enough&quot;, integrate into port
+
+
+set MY_VERSION 0.1
+
+proc printUsage {} {
+   puts &quot;Usage: $::argv0 \[-hV\] \[-t macports-tcl-path\] \[filename\]&quot;
+   puts &quot;   -h   This help&quot;
+   puts &quot;   -t   Give a different location for the base MacPorts Tcl&quot;
+   puts &quot;        file (defaults to /Library/Tcl)&quot;
+   puts &quot;   -V   show version and MacPorts version being used&quot;
+}
+
+
+proc dependenciesForPort {portName variantInfo} {
+   set dependencyList [list]
+   set portSearchResult [mportlookup $portName]
+   if {[llength $portSearchResult] &lt; 1} {
+      return -code error &quot;port \&quot;$portName\&quot; not found&quot;
+   }
+   array set portInfo [lindex $portSearchResult 1]
+   set mport [mportopen $portInfo(porturl) {} $variantInfo]
+   array unset portInfo
+   array set portInfo [mportinfo $mport]
+   mportclose $mport
+   foreach dependencyType {depends_fetch depends_extract depends_build depends_lib depends_run} {
+      if {[info exists portInfo($dependencyType)] &amp;&amp; [string length $portInfo($dependencyType)] &gt; 0} {
+         foreach dependency $portInfo($dependencyType) {
+            lappend dependencyList [lindex [split $dependency :] end]
+         }
+      }
+   }
+
+   return $dependencyList
+}
+
+proc sort_ports {portList} {
+    array set port_installed {}
+    array set port_deps {}
+    array set port_in_list {}
+    
+    set newList [list]
+    foreach port $portList {
+        set name [lindex $port 0]
+        #ui_msg &quot;name = $name&quot;
+        set version [lindex $port 1]
+        set variants &quot;&quot;
+        # XXX will need updating when we start recording -variants
+        set variantsStart [string first &quot;+&quot; $version]
+        if {$variantsStart != -1} {
+            set variants [string range $version $variantsStart end]
+            set variant_names [lrange [split $variants +] 1 end]
+            set variants [list]
+            foreach v $variant_names {
+                lappend variants $v &quot;+&quot;
+            }
+        }
+        #ui_msg &quot;variants = $variants&quot;
+        set active 0
+        if {[llength $port] &gt; 2 &amp;&amp; [lindex $port 2] == &quot;(active)&quot;} {
+            set active 1
+        }
+        #ui_msg &quot;active = $active&quot;
+
+        if {![info exists port_in_list($name)]} {
+            set port_in_list($name) 1
+            set port_installed($name) 0
+        } else {
+            incr port_in_list($name)
+        }
+        if {![info exists port_deps(${name},${variants})]} {
+            set port_deps(${name},${variants}) [dependenciesForPort $name $variants]
+        }
+        lappend newList [list $active $name $variants]
+    }
+    unset portList
+
+    set operationList [list]
+    while {[llength $newList] &gt; 0} {
+        set oldLen [llength $newList]
+        foreach port $newList {
+            foreach {active name variants} $port break
+            # ensure active versions are installed after inactive versions,
+            # since installing will also activate and we don't want to
+            # displace the active version
+            if {$active &amp;&amp; $port_installed($name) &lt; ($port_in_list($name) - 1)} {
+                continue
+            }
+            set installable 1
+            foreach dep $port_deps(${name},${variants}) {
+                # XXX maybe check dep is active here?
+                if {[info exists port_installed($dep)] &amp;&amp; $port_installed($dep) == 0} {
+                    set installable 0
+                    break
+                }
+            }
+            if {$installable} {
+                lappend operationList [list $name $variants $active]
+                incr port_installed($name)
+                set index [lsearch $newList [list $active $name $variants]]
+                #ui_msg &quot;deleting \&quot;[list $active $name $variants]\&quot; from list&quot;
+                #ui_msg &quot;list with element: $newList&quot;
+                set newList [lreplace $newList $index $index]
+                #ui_msg &quot;list without element: $newList&quot;
+            }
+        }
+        if {[llength $newList] == $oldLen} {
+            ui_error &quot;we appear to be stuck, exiting...&quot;
+            return -code error &quot;infinite loop&quot;
+        }
+    }
+    
+    return $operationList
+}
+
+proc install_ports {operationList} {
+    foreach op $operationList {
+        set name [string trim [lindex $op 0]]
+        set variations [lindex $op 1]
+        set active [lindex $op 2]
+        
+         if {[catch {set res [mportlookup $name]} result]} {
+            global errorInfo
+            ui_debug &quot;$errorInfo&quot;
+            return -code error &quot;lookup of portname $name failed: $result&quot;
+        }
+        if {[llength $res] &lt; 2} {
+            ui_warn &quot;Skipping $name (not in the ports tree)&quot;
+            continue
+        }
+        array unset portinfo
+        array set portinfo [lindex $res 1]
+        set porturl $portinfo(porturl)
+        
+        # XXX should explicitly turn off default variants that don't appear in the list
+        set filtered_variations [mport_filtervariants $variations no]
+        
+        if {[catch {set workername [mportopen $porturl {} $filtered_variations]} result]} {
+            global errorInfo
+            ui_debug &quot;$errorInfo&quot;
+            return -code error &quot;Unable to open port '$name': $result&quot;
+        }
+        if {[catch {set result [mportexec $workername install]} result]} {
+            global errorInfo
+            mportclose $workername
+            ui_msg &quot;$errorInfo&quot;
+            return -code error &quot;Unable to execute target 'install' for port '$name': $result&quot;
+        } else {
+            mportclose $workername
+        }
+        
+        # XXX some ports may be reactivated to fulfil dependencies - check again at the end?
+        if {!$active} {
+            if {[catch {portimage::deactivate $name &quot;&quot; &quot;&quot;} result]} {
+                global errorInfo
+                ui_debug &quot;$errorInfo&quot;
+                return -code error &quot;port deactivate failed: $result&quot;
+            }
+        }
+    }
+}
+
+proc read_portlist {filename} {
+    if {$filename == &quot;-&quot;} {
+        set infile stdin
+    } else {
+        set infile [open $filename r]
+    }
+    set data [read -nonewline $infile]
+    set portList [split $data \n]
+    close $infile
+    if {[lindex $portList 0] == &quot;The following ports are currently installed:&quot;} {
+        set portList [lrange $portList 1 end]
+    }
+    return $portList
+}
+
+# Begin
+
+set macportsTclPath &quot;/Library/Tcl&quot;
+#set macportsTclPath &quot;/opt/mptest/share/macports/Tcl&quot;
+set showVersion 0
+
+while {[string index [lindex $::argv 0] 0] == &quot;-&quot; } {
+   switch [string range [lindex $::argv 0] 1 end] {
+      h {
+         printUsage
+         exit 0
+      }
+      t {
+         if {[llength $::argv] &lt; 2} {
+            puts &quot;-t needs a path&quot;
+            printUsage
+            exit 1
+         }
+         set macportsTclPath [lindex $::argv 1]
+         set ::argv [lrange $::argv 1 end]
+      }
+      V {
+         set showVersion 1
+      }
+      default {
+         puts &quot;Unknown option [lindex $::argv 0]&quot;
+         printUsage
+         exit 1
+      }
+   }
+   set ::argv [lrange $::argv 1 end]
+}
+
+source ${macportsTclPath}/macports1.0/macports_fastload.tcl
+package require macports
+mportinit
+
+if {$showVersion} {
+   puts &quot;Version $MY_VERSION&quot;
+   puts &quot;MacPorts version [macports::version]&quot;
+   exit 0
+}
+
+if {[llength $::argv] == 0} {
+   set filename &quot;-&quot;
+} else {
+    set filename [lindex $::argv 0]
+}
+set portList [read_portlist $filename]
+#ui_msg &quot;portlist = $portList&quot;
+
+set operationList [sort_ports $portList]
+
+install_ports $operationList
</ins><span class="cx">Property changes on: users/jmr/restore_ports/restore_ports.tcl
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4>Added: svn:executable</h4></div>
</div>

</body>
</html>