<!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>[121220] branches/gsoc14-cleanup/src</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="https://trac.macports.org/changeset/121220">121220</a></dd>
<dt>Author</dt> <dd>ksammons@macports.org</dd>
<dt>Date</dt> <dd>2014-06-20 16:13:59 -0700 (Fri, 20 Jun 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Moved the calls to reclaim into macports.tcl</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branchesgsoc14cleanupsrcmacports10macportstcl">branches/gsoc14-cleanup/src/macports1.0/macports.tcl</a></li>
<li><a href="#branchesgsoc14cleanupsrcportport">branches/gsoc14-cleanup/src/port/port</a></li>
<li><a href="#branchesgsoc14cleanupsrcportporttcl">branches/gsoc14-cleanup/src/port/port.tcl</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#branchesgsoc14cleanupsrcmacports10Meedmd">branches/gsoc14-cleanup/src/macports1.0/Meed.md</a></li>
<li><a href="#branchesgsoc14cleanupsrcmacports10">branches/gsoc14-cleanup/src/macports1.0/~</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branchesgsoc14cleanupsrcmacports10Meedmd"></a>
<div class="addfile"><h4>Added: branches/gsoc14-cleanup/src/macports1.0/Meed.md ( => )</h4>
<pre class="diff"><span>
<span class="info">Modified: branches/gsoc14-cleanup/src/macports1.0/macports.tcl
===================================================================
</span><del>--- branches/gsoc14-cleanup/src/macports1.0/macports.tcl        2014-06-20 23:13:56 UTC (rev 121219)
</del><ins>+++ branches/gsoc14-cleanup/src/macports1.0/macports.tcl        2014-06-20 23:13:59 UTC (rev 121220)
</ins><span class="lines">@@ -36,6 +36,7 @@
</span><span class="cx"> package provide macports 1.0
</span><span class="cx"> package require macports_dlist 1.0
</span><span class="cx"> package require macports_util 1.0
</span><ins>+package require reclaim 1.0
</ins><span class="cx"> package require Tclx
</span><span class="cx"> 
</span><span class="cx"> namespace eval macports {
</span><span class="lines">@@ -4363,6 +4364,11 @@
</span><span class="cx">     return yes
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+proc macports::reclaim_main {} {
+    reclaim::main
+    return 0
+}
+
</ins><span class="cx"> proc macports::revupgrade {opts} {
</span><span class="cx">     set run_loop 1
</span><span class="cx">     array set broken_port_counts {}
</span></span></pre></div>
<a id="branchesgsoc14cleanupsrcmacports10"></a>
<div class="addfile"><h4>Added: branches/gsoc14-cleanup/src/macports1.0/~ (0 => 121220)</h4>
<pre class="diff"><span>
<span class="info">--- branches/gsoc14-cleanup/src/macports1.0/~                                (rev 0)
+++ branches/gsoc14-cleanup/src/macports1.0/~        2014-06-20 23:13:59 UTC (rev 121220)
</span><span class="lines">@@ -0,0 +1,174 @@
</span><ins>+# -*- 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
+
+# TODO:
+# Add distfile version checking.
+# Remove the useless/structure comments and add actual docstrings.
+# Pretty sure we should be using ui_msg, instead of puts and what not. Should probably add that.
+# Add test cases
+# Add copyright notice
+
+# Finished:
+# Register the &quot;port cleanup&quot; command with port.tcl and all that involves.
+# Implement a hash-map, or multidimensional array for ease of app info keeping. Write it yourself if you have to.
+# Figure out what the hell is going on with &quot;port clean all&quot; vs &quot;port clean installed&quot; the 'clean' target is provided by this package
+
+package provide reclaim 1.0
+package require macports
+
+namespace eval reclaim {
+}
+
+proc reclaim::main {args} {
+
+    # The main function. Calls each individual function that needs to be run.
+    # Args: 
+    #           None
+    # Returns:
+    #           None
+
+    uninstall_inactive
+    check_distfiles
+}
+
+proc walk_files {dir delete installed_ports} {
+
+    # Recursively walk through each item in the given directory and if requested, delete each file that isn't a directory.
+    # Args:
+    #           dir    - A string path of the given directory to walk through
+    #           delete - Whether to delete each file found that isn't a directory or not. Set to 'yes' or 'no'. 
+    # Returns: 
+    #           None
+
+    foreach item [readdir $dir] {
+
+        set currentPath [file join $dir $item]
+
+        if {[file isdirectory $currentPath]} {
+            if {[lsearch $installed_ports $item] == -1}
+                puts &quot;MOO&quot;
+                walk_files $currentPath $delete $installed_ports
+            }
+
+        } else {
+            #puts &quot;Found distfile: $item&quot;
+
+            if {$delete eq &quot;yes&quot;} {
+                set path [file join $dir $item]
+                #puts &quot;Removing distfile: $item&quot;
+
+                # Because we're only deleting files (not directories) that we know exist, if there was an error, it's because of lack of root privledges.
+                #if {[catch {file delete $path} result]} { 
+                    #elevateToRoot &quot;reclaim&quot;
+                    #file delete $path
+                #}
+            }
+        }
+    }
+}
+
+proc check_distfiles {} {
+
+    # Check for distfiles in both the root, and home directories. If found, delete them.
+    # Args:
+    #               None
+    #
+    # Returns:
+    #               0 on successful execution
+
+    global macports::portdbpath
+    global macports::user_home
+
+    # The root and home distfile folder locations, respectively. 
+    set root_dist       [file join ${macports::portdbpath} distfiles]
+    set root_archive    [file join ${macports::portdbpath} software] 
+    set home            ${macports::user_home}/.macports$root_dist
+
+    set port_info [get_info]
+    set names [list]
+
+    foreach port $port_info {
+        lappend names [lindex $port 0]
+    }
+
+    # Walk through each directory, and delete any files found.
+    walk_files $root_archive yes $names
+    #walk_files $home yes
+
+    # Returning 0 is in style this year. 
+    return 0
+} 
+
+proc is_inactive {app} {
+
+    # Determine's whether an application is inactive or not.
+    # Args: 
+    #           app - An array where the fourth item in it is the activity of the application.
+    # Returns:
+    #           1 if inactive, 0 if active.
+
+    if {[lindex $app 4] == 0} {
+        return 1
+    }
+    return 0
+}
+
+proc get_info {} {
+
+    # Get's the information of all installed appliations (those returned by registry::instaled), and returns it in a
+    # multidimensional list.
+    #
+    # Args:
+    #           None
+    #Returns:
+    #           A multidimensional list where each app is a sublist, i.e., {{First Application Info} {Second Application Info} {...}}
+    #           Indexes of each sublist are: 0 = name, 1 = version, 2 = revision, 3 = variets, 4 = activity, and 5 = epoch.
+    
+    set installed_apps [registry::installed]
+    set app_info [list] 
+
+    foreach app $installed_apps {
+
+        set name     [lindex $app 0]
+        set version  [lindex $app 1]
+        set revision [lindex $app 2]
+        set varients [lindex $app 3]
+        set active   [lindex $app 4]
+        set epoch    [lindex $app 5]
+
+        lappend app_info [list $name $version $revision $varients $active $epoch]
+    }
+
+    return $app_info
+}
+
+proc uninstall_inactive {} {
+
+    # Attempts to uninstall all inactive applications. (Performance is now O(N)!)
+    #
+    # Args: 
+    #           None
+    # Returns: 
+    #           0 if execution was successful.
+
+    set apps [get_info]
+    set inactive_count 0
+
+    foreach app $apps {
+
+        if { [is_inactive $app] } {
+            set name [lindex $app 0]
+            puts &quot;Uninstalling: $name&quot;
+            incr inactive_count
+
+            # Note: 'uninstall' takes a name, version, and an options list. 
+            registry_uninstall [lindex $app 0] [lindex $app 1] {}
+        }
+    }
+    if { $inactive_count == 0 } {
+        puts &quot;Found no inactive ports.&quot;
+    }
+
+    return 0
+}
+
+
</ins></span></pre></div>
<a id="branchesgsoc14cleanupsrcportport"></a>
<div class="modfile"><h4>Modified: branches/gsoc14-cleanup/src/port/port (121219 => 121220)</h4>
<pre class="diff"><span>
<span class="info">--- branches/gsoc14-cleanup/src/port/port        2014-06-20 23:13:56 UTC (rev 121219)
+++ branches/gsoc14-cleanup/src/port/port        2014-06-20 23:13:59 UTC (rev 121220)
</span><span class="lines">@@ -44,7 +44,6 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> package require macports
</span><del>-package require reclaim 1.0
</del><span class="cx"> package require Pextlib 1.0
</span><span class="cx"> 
</span><span class="cx"> # Standard procedures
</span><span class="lines">@@ -2733,7 +2732,7 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> proc action_reclaim { action portlist opts } {
</span><del>-    reclaim::main 
</del><ins>+    macports::reclaim_main 
</ins><span class="cx">     return 0
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="branchesgsoc14cleanupsrcportporttcl"></a>
<div class="modfile"><h4>Modified: branches/gsoc14-cleanup/src/port/port.tcl (121219 => 121220)</h4>
<pre class="diff"><span>
<span class="info">--- branches/gsoc14-cleanup/src/port/port.tcl        2014-06-20 23:13:56 UTC (rev 121219)
+++ branches/gsoc14-cleanup/src/port/port.tcl        2014-06-20 23:13:59 UTC (rev 121220)
</span><span class="lines">@@ -2733,7 +2733,7 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> proc action_reclaim { action portlist opts } {
</span><del>-    reclaim::main 
</del><ins>+    macports::reclaim_main  
</ins><span class="cx">     return 0
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>