[80512] branches/gsoc11-statistics

derek at macports.org derek at macports.org
Wed Jul 13 21:55:56 PDT 2011


Revision: 80512
          http://trac.macports.org/changeset/80512
Author:   derek at macports.org
Date:     2011-07-13 21:55:56 -0700 (Wed, 13 Jul 2011)
Log Message:
-----------
Added helper scripts for use with RoR app 

- add_ports and new_ports are directly from mpwa. Slightly modified to only save ports and port categories
- generate_seed is a modified version of add_ports. For every port and category is saves to the database it also outputs
an appropriate rails statement to save that same data. This can be used to generate seeds.rb to populate the database with ports.

Added Paths:
-----------
    branches/gsoc11-statistics/server-scripts/
    branches/gsoc11-statistics/server-scripts/add_ports
    branches/gsoc11-statistics/server-scripts/generate_seed
    branches/gsoc11-statistics/server-scripts/new_ports

Added: branches/gsoc11-statistics/server-scripts/add_ports
===================================================================
--- branches/gsoc11-statistics/server-scripts/add_ports	                        (rev 0)
+++ branches/gsoc11-statistics/server-scripts/add_ports	2011-07-14 04:55:56 UTC (rev 80512)
@@ -0,0 +1,76 @@
+#!/usr/bin/env ruby
+require 'rubygems'
+require 'active_record'
+require 'fileutils'
+require 'trac4r'
+
+TIME_FILE = "/var/tmp/gsoc11-mpwa-sync"
+ROOT = File.expand_path(File.dirname(__FILE__) + "/../") 
+NEW_PORTS = "#{ROOT}/bin/new_ports"
+PORT_INDEX = "/opt/local/var/macports/sources/rsync.macports.org/release/ports"
+RAILS_ROOT = "#{ROOT}/stats-server"
+
+require File.expand_path(RAILS_ROOT + '/app/models/category.rb',  __FILE__)
+require File.expand_path(RAILS_ROOT + '/app/models/port.rb',  __FILE__)
+
+
+if File.exists?(TIME_FILE)
+  $mtime = File.stat(TIME_FILE).mtime.to_i
+else
+  $mtime = 0
+end
+
+FileUtils.touch(TIME_FILE)
+
+$ports = Array.new
+$hashed_data = Hash.new
+
+db_info = YAML.load_file(File.expand_path(RAILS_ROOT + '/config/database.yml',  __FILE__))
+#db_info['development']['database'] = RAILS_ROOT + "/" + db_info['development']['database']
+puts db_info['development'].to_yaml
+ActiveRecord::Base.establish_connection(db_info['development'])
+#trac = Trac.new(TRAC_URL, TRAC_USER, TRAC_PASS)
+
+fp = IO.popen("#{NEW_PORTS} -m #{$mtime} #{PORT_INDEX}")
+new_ports = fp.read.split("\n")
+
+if new_ports.count > 0
+  new_ports << "" #add last blank line
+end
+
+
+new_ports.each do |line|
+  unless (line == "")
+    data = line.match(/(\S+):\s+\{?(.+)\}?$/)
+    unless data.nil? #field missing, should record this if it happens
+      $hashed_data[data[1].to_sym] = data[2]
+    end
+  else
+    category_name = $hashed_data[:categories].try(:split, " ").try(:[], 0)
+    $category = Category.find_by_name(category_name)
+    if $category.nil?
+      $category = Category.new({:name => category_name})
+      $category.save
+    end
+
+    port = Port.find_by_name($hashed_data[:name])
+    if port.nil?
+      port = Port.new
+    end
+
+    port[:name] = $hashed_data[:name]
+    port[:path] = $hashed_data[:portdir]
+    port[:version] = $hashed_data[:version]
+    port[:description] = $hashed_data[:description]
+    port[:licenses] = $hashed_data[:license]
+    port[:category_id] = $category.id
+    port[:variants] = $hashed_data[:variants]
+    port[:maintainers] = $hashed_data[:maintainers]
+    port[:platforms] = $hashed_data[:platforms]
+    port[:categories] = $hashed_data[:categories]
+
+    $ports << [$hashed_data, port]
+    port.save
+    $hashed_data = {}
+  end
+end


Property changes on: branches/gsoc11-statistics/server-scripts/add_ports
___________________________________________________________________
Added: svn:executable
   + *

Added: branches/gsoc11-statistics/server-scripts/generate_seed
===================================================================
--- branches/gsoc11-statistics/server-scripts/generate_seed	                        (rev 0)
+++ branches/gsoc11-statistics/server-scripts/generate_seed	2011-07-14 04:55:56 UTC (rev 80512)
@@ -0,0 +1,106 @@
+#!/usr/bin/env ruby
+
+### Generate a valid seeds.rb for use in seeding the database with 
+### valid ports and categories
+
+require 'rubygems'
+require 'active_record'
+require 'fileutils'
+
+TIME_FILE = "/var/tmp/gsoc11-mpwa-sync"
+ROOT = File.expand_path(File.dirname(__FILE__) + "/../") 
+NEW_PORTS = "#{ROOT}/bin/new_ports"
+PORT_INDEX = "/opt/local/var/macports/sources/rsync.macports.org/release/ports"
+RAILS_ROOT = "#{ROOT}/stats-server"
+
+require File.expand_path(RAILS_ROOT + '/app/models/category.rb',  __FILE__)
+require File.expand_path(RAILS_ROOT + '/app/models/port.rb',  __FILE__)
+
+
+if File.exists?(TIME_FILE)
+  $mtime = File.stat(TIME_FILE).mtime.to_i
+else
+  $mtime = 0
+end
+
+FileUtils.touch(TIME_FILE)
+
+$ports = Array.new
+$hashed_data = Hash.new
+
+db_info = YAML.load_file(File.expand_path(RAILS_ROOT + '/config/database.yml',  __FILE__))
+ActiveRecord::Base.establish_connection(db_info['development'])
+
+fp = IO.popen("#{NEW_PORTS} -m #{$mtime} #{PORT_INDEX}")
+new_ports = fp.read.split("\n")
+
+if new_ports.count > 0
+  new_ports << "" #add last blank line
+end
+
+class String
+  # Escape single quotes
+  def escape_single_quotes
+    self.gsub(/'/, "\\\\'")
+  end
+end
+
+def esc(str)
+  if not str.nil?
+    str.escape_single_quotes
+  else
+    str
+  end
+end
+
+new_ports.each do |line|
+  unless (line == "")
+    data = line.match(/(\S+):\s+\{?(.+)\}?$/)
+    unless data.nil? #field missing, should record this if it happens
+      $hashed_data[data[1].to_sym] = data[2]
+    end
+  else
+    category_name = $hashed_data[:categories].try(:split, " ").try(:[], 0)
+    $category = Category.find_by_name(category_name)
+    if $category.nil?
+      $category = Category.new({:name => category_name})
+      puts "category = Category.new({:name => \'#{category_name}\'})"
+      puts "category.save"
+      $category.save
+    end
+
+    port = Port.find_by_name($hashed_data[:name])
+    if port.nil?
+      port = Port.new
+    end
+
+    port[:name] = $hashed_data[:name]
+    port[:path] = $hashed_data[:portdir]
+    port[:version] = $hashed_data[:version]
+    port[:description] = $hashed_data[:description]
+    port[:licenses] = $hashed_data[:license]
+    port[:category_id] = $category.id
+    port[:variants] = $hashed_data[:variants]
+    port[:maintainers] = $hashed_data[:maintainers]
+    port[:platforms] = $hashed_data[:platforms]
+    port[:categories] = $hashed_data[:categories]
+
+    $ports << [$hashed_data, port]
+    
+    puts "port = Port.new"
+    puts "port[:name] = \'#{esc($hashed_data[:name])}\'"
+    puts "port[:path] = \'#{esc($hashed_data[:portdir])}\'"
+    puts "port[:version] = \'#{esc($hashed_data[:version])}\'"
+    puts "port[:description] = \'#{esc($hashed_data[:description])}\'"
+    puts "port[:licenses] = \'#{esc($hashed_data[:license])}\'"
+    puts "port[:category_id] = #{$category.id}"
+    puts "port[:variants] = \'#{esc($hashed_data[:variants])}\'"
+    puts "port[:maintainers] = \'#{esc($hashed_data[:maintainers])}\'"
+    puts "port[:platforms] = \'#{esc($hashed_data[:platforms])}\'"
+    puts "port[:categories] = \'#{esc($hashed_data[:categories])}\'"
+    puts "port.save"
+
+    port.save
+    $hashed_data = {}
+  end
+end


Property changes on: branches/gsoc11-statistics/server-scripts/generate_seed
___________________________________________________________________
Added: svn:executable
   + *

Added: branches/gsoc11-statistics/server-scripts/new_ports
===================================================================
--- branches/gsoc11-statistics/server-scripts/new_ports	                        (rev 0)
+++ branches/gsoc11-statistics/server-scripts/new_ports	2011-07-14 04:55:56 UTC (rev 80512)
@@ -0,0 +1,140 @@
+#!/bin/sh
+# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
+# Run the Tcl interpreter \
+exec /usr/bin/tclsh "$0" "$@"
+
+catch {source \
+    [file join "/Library/Tcl" macports1.0 macports_fastload.tcl]}
+package require macports
+package require Pextlib
+
+# Globals
+array set ui_options        [list]
+array set global_options    [list]
+array set global_variations [list]
+set port_options            [list]
+
+# Pass global options into mportinit
+mportinit ui_options global_options global_variations
+
+# Standard procedures
+proc print_usage args {
+    global argv0
+    puts "Usage: $argv0 \[-d\] -m <time> \<directory\>"
+    puts "-d:\tOutput debugging information"
+    puts "-m:\tOutput ports newer than the given mtime"
+}
+
+proc pindex {portdir} {
+    global target oldfd oldmtime qindex fd directory outdir \
+           ui_options port_options
+
+    # try to reuse the existing entry if it's still valid
+    if {[info exists qindex([string tolower [file tail $portdir]])]} {
+        try {
+            set mtime [file mtime [file join $directory $portdir Portfile]]
+            if {$oldmtime < $mtime} {
+                set offset $qindex([string tolower [file tail $portdir]])
+                seek $oldfd $offset
+                gets $oldfd line
+                set name [lindex $line 0]
+                set len [lindex $line 1]
+                set line [read $oldfd $len]
+                array set portinfo $line
+
+                if {[info exists ui_options(ports_debug)]} {
+                    puts "Found entry for $portdir"
+                }
+
+                foreach field [array names portinfo] {
+                    puts $fd "${field}: $portinfo($field)"
+                }
+                puts $fd ""
+
+                return
+            }
+        } catch {*} {
+            ui_warn "failed to open entry for ${portdir}"
+        }
+    }
+}
+
+if {[expr $argc > 4]} {
+    print_usage
+    exit 1
+}
+
+for {set i 0} {$i < $argc} {incr i} {
+    set arg [lindex $argv $i]
+    switch -regex -- $arg {
+        {^-.+} {
+            if {$arg == "-d"} { # Turn on debug output
+                set ui_options(ports_debug) yes
+            } elseif {$arg == "-m"} { # output ports newer than mtime
+                incr i
+                set oldmtime [lindex $argv $i]
+            } else {
+                puts stderr "Unknown option: $arg"
+                print_usage
+                exit 1
+            }
+        }
+        default {
+            set directory [file join [pwd] $arg]
+        }
+    }
+}
+
+if {![info exists directory]} {
+    set directory .
+}
+
+if {![info exists oldmtime]} {
+    set oldmtime 0
+}
+
+# cd to input directory
+if {[catch {cd $directory} result]} {
+    puts stderr "$result"
+    exit 1
+} else {
+    set directory [pwd]
+}
+
+# Set output directory to full path
+if {[info exists outdir]} {
+    if {[catch {file mkdir $outdir} result]} {
+        puts stderr "$result"
+        exit 1
+    }
+    if {[catch {cd $outdir} result]} {
+        puts stderr "$result"
+        exit 1
+    } else {
+        set outdir [pwd]
+    }
+} else {
+    set outdir $directory
+}
+
+set outpath [file join $outdir PortIndex]
+# open old index for comparison
+if {[file isfile $outpath] && [file isfile ${outpath}.quick]} {
+    if {![catch {set oldfd [open $outpath r]}] && ![catch {set quickfd [open ${outpath}.quick r]}]} {
+        if {![catch {set quicklist [read $quickfd]}]} {
+            foreach entry [split $quicklist "\n"] {
+                set qindex([lindex $entry 0]) [lindex $entry 1]
+            }
+        }
+        close $quickfd
+    }
+} else {
+    set newest 0
+}
+
+set fd stdout
+mporttraverse pindex $directory
+if {[info exists oldfd]} {
+    close $oldfd
+}
+close $fd


Property changes on: branches/gsoc11-statistics/server-scripts/new_ports
___________________________________________________________________
Added: svn:executable
   + *
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20110713/0d07d39f/attachment.html>


More information about the macports-changes mailing list