--- /dev/null
+require "rake/clean"
+require "rake/testtask"
+require "rake/packagetask"
+require "rake/contrib/sshpublisher"
+
+PKG_NAME = "ruby-cruxutils"
+PKG_VERSION = "0.1.1"
+
+Rake::TestTask.new do |t|
+ t.libs = ["lib"]
+ t.test_files = FileList["test/test_*.rb"]
+ t.verbose = true
+end
+
+task :install do
+ destdir = ENV["DESTDIR"] || ""
+
+ ddir = destdir + Config::CONFIG["sitelibdir"]
+ FileUtils::Verbose.mkdir_p(ddir) unless File.directory?(ddir)
+ FileUtils::Verbose.install("lib/cruxutils.rb", ddir, :mode => 0644)
+
+ ddir = destdir + Config::CONFIG["sitelibdir"] + "/cruxutils"
+ FileUtils::Verbose.mkdir_p(ddir) unless File.directory?(ddir)
+ FileUtils::Verbose.install(Dir["lib/cruxutils/*.rb"], ddir,
+ :mode => 0644)
+end
+
+Rake::PackageTask.new(PKG_NAME, PKG_VERSION) do |t|
+ t.need_tar_gz = true
+ t.package_files.include("[A-Z]*", "lib/cruxutils.rb",
+ "lib/cruxutils/*.rb", "test/**/*")
+end
+
+task :publish => [:package] do
+ Rake::SshFilePublisher.new("code-monkey.de",
+ ".", "pkg",
+ "#{PKG_NAME}-#{PKG_VERSION}.tar.gz").
+ upload
+end
--- /dev/null
+module CruxUtils
+ class CruxUtilsError < StandardError; end
+end
+
+require "cruxutils/port.rb"
+require "cruxutils/package.rb"
+require "cruxutils/packagedb.rb"
--- /dev/null
+#--
+# PDF::Writer for Ruby.
+# http://rubyforge.org/projects/ruby-pdf/
+# Copyright 2003 - 2005 Austin Ziegler.
+#
+# Licensed under a MIT-style licence. See LICENCE in the main distribution
+# for full licensing information.
+#
+# $Id: ohash.rb,v 1.2 2005/05/16 03:59:21 austin Exp $
+#++
+ # Based on [ruby-talk:20551]. Updated to (hopefully) be 1.8 friendly.
+class OHash < Hash
+ alias_method :store, :[]=
+ alias_method :each_pair, :each
+
+ def initialize(*args)
+ @keys = []
+ super
+ end
+
+ def []=(key, val)
+ @keys << key unless has_key?(key)
+ super
+ end
+
+ def delete(key)
+ @keys.delete(key) if has_key?(key)
+ super
+ end
+
+ def each
+ @keys.each { |k| yield k, self[k] }
+ end
+
+ def each_key
+ @keys.each { |k| yield k }
+ end
+
+ def each_value
+ @keys.each { |k| yield self[k] }
+ end
+
+ def first
+ self[@keys[0]]
+ end
+
+ def last
+ self[@keys[-1]]
+ end
+
+ def first?(item)
+ self[@keys[0]] == item
+ end
+
+ def last?(item)
+ self[@keys[-1]] == item
+ end
+
+ def replace(other)
+ other.each do |key, value|
+ self[key] = value
+ end
+
+ self
+ end
+end
--- /dev/null
+module CruxUtils
+
+class Package
+ attr_reader :name, :version, :release, :files
+
+ def initialize(name, version, release, files)
+ @name = name.to_str.dup.freeze
+ @version = version.freeze
+ @release = release.freeze
+ @files = files.dup.freeze
+ end
+
+ def filename
+ "%s#%s-%s.pkg.tar.gz" % [@name, @version, @release]
+ end
+end
+
+end
--- /dev/null
+module CruxUtils
+
+class PackageDb
+ class PackageDbError < CruxUtilsError; end
+ class PackageNotFoundError < PackageDbError; end
+
+ FILE = "/var/lib/pkg/db"
+
+ def PackageDb.open
+ db = PackageDb.new
+
+ yield db
+ ensure
+ db.close
+ end
+
+ def initialize
+ @lines = File.read(FILE).split("\n").map { |l| l.strip }
+ end
+
+ def close
+ end
+
+ def [](name)
+ name = name.to_str.strip
+ found = -1
+
+ found = @lines.find { |l| l == name }
+ raise(PackageNotFoundError) if found.nil?
+
+ i = @lines.index(found)
+
+ version, release = @lines[i + 1].match(/^(.*)-(\d+)$/).captures
+ files = []
+
+ (i + 2).upto(@lines.length) do |i|
+ break if @lines[i].strip.empty?
+
+ files << @lines[i]
+ end
+
+ Package.new(name, version, release, files.sort)
+ end
+end
+
+end
--- /dev/null
+require "cruxutils/ohash.rb"
+require "tempfile"
+require "fileutils"
+
+module CruxUtils
+
+class Port
+ COMMENT_MAP = OHash.new
+ COMMENT_MAP["Description"] = :description
+ COMMENT_MAP["URL"] = :url
+ COMMENT_MAP["Packager"] = :packager
+ COMMENT_MAP["Maintainer"] = :maintainer
+ COMMENT_MAP["Depends on"] = :dependencies
+ COMMENT_MAP["Group"] = :group
+ COMMENT_MAP["Nice to have"] = :nice_to_have
+
+ def Port.open(dir)
+ yield Port.new(dir)
+ end
+
+ def initialize(dir)
+ @dir = dir
+
+ @attribs = Hash.new { |h, k| h[k] = "" }
+
+ @attribs[:dependencies] = Dependencies.new
+ @attribs[:source] = Sources.new
+ @attribs[:build] = BuildFunc.new
+
+ read
+ end
+
+ def method_missing(meth, *args)
+ if meth.to_s.match(/=$/) then
+ m2 = meth.to_s[0...-1].to_sym
+ @attribs[m2] = (args.length < 2 ? args.first : args)
+ else
+ @attribs[meth]
+ end
+ end
+
+ def dependencies
+ @attribs[:dependencies].uniq!
+ @attribs[:dependencies]
+ end
+
+ def has_footprint?
+ File.exist?(footprint)
+ end
+
+ def has_md5sum?
+ File.exist?(md5sum)
+ end
+
+ def write
+ Tempfile.open("pkgfile") do |file|
+ COMMENT_MAP.each do |key, symbol|
+ value = @attribs[symbol]
+ next if value.empty?
+
+ file << "# #{key}: " << value << "\n"
+ end
+
+ file << "\n"
+
+ vars = [:name, :version, :release, :source]
+ vars.each do |var|
+ file << "#{var}=" <<
+ @attribs[var] << "\n"
+ end
+
+ file << "\n" << build << "\n"
+ file.flush
+
+ FileUtils.cp(file.path, pkgfile)
+ end
+ end
+
+ private
+ def read
+ vars = [:name, :version, :release]
+ section = :comments
+
+ cmt_map = OHash.new.replace(COMMENT_MAP)
+
+ File.read(pkgfile).split("\n").each do |line|
+ line.chomp!
+
+ cmt_map.each do |key, symbol|
+ md = line.match(/^#\s*#{key}: (.+)$/)
+ unless md.nil?
+ @attribs[symbol].replace(md.captures.first.strip)
+ cmt_map.delete(key)
+ end
+ end
+
+ vars.each do |var|
+ md = line.match(/^#{var}=(.+)$/)
+ unless md.nil?
+ @attribs[var] = $1.strip
+ vars.delete(key)
+ end
+ end
+
+ # handle source array
+ section = :void if line.match(/^\s*$/) && section != :build
+
+ if section == :source && line.match(/(.+)\)$/)
+ @attribs[:source] << $1.strip
+ elsif section == :source && line.match(/(.+)\\$/)
+ @attribs[:source] << $1.strip
+ elsif line.match(/^source=\((.+)\)$/)
+ @attribs[:source] << $1.strip
+ elsif line.match(/^source=\((.+)\\$/)
+ @attribs[:source] << $1.strip
+ section = :source
+ end
+
+ section = :end unless line.match(/^\}/).nil?
+ if section == :build
+ # remove initial indentation
+ tmp = line.sub(/^( |\t)/, "")
+
+ @attribs[:build] << (tmp || "")
+ end
+ section = :build unless line.match(/^build/).nil?
+ end
+ end
+
+ def pkgfile
+ File.join(@dir, "Pkgfile")
+ end
+
+ def footprint
+ File.join(@dir, ".footprint")
+ end
+
+ def md5sum
+ File.join(@dir, ".md5sum")
+ end
+end
+
+class Dependencies < Array
+ def to_s
+ uniq.join(", ")
+ end
+
+ def replace(arg)
+ if arg.is_a?(Array)
+ super
+ else
+ clear
+
+ arg.split(", ").each { |d| self << d.strip }
+ end
+
+ uniq!
+
+ self
+ end
+end
+
+class Sources < Array
+ def to_s
+ indent = " " * "source=(".length
+ "(" + join(" \\\n" + indent) + ")"
+ end
+end
+
+class BuildFunc < Array
+ def to_s
+ inject("build() {\n") do |a, line|
+ if line != ""
+ line = "\t" + line
+ end
+
+ a << line << "\n"
+ a
+ end + "}"
+ end
+end
+
+end
--- /dev/null
+require "test/unit"
+require "cruxutils"
+
+class TestComments < Test::Unit::TestCase
+ def setup
+ @port = CruxUtils::Port.new(File.dirname(__FILE__) + "/valgrind")
+ end
+
+ def test_comments
+ assert_equal("An open-source memory debugger for x86-GNU/Linux",
+ @port.description)
+ assert_equal("http://valgrind.org", @port.url)
+ assert_equal("Tilman Sauerbeck, tilman at code-monkey dot de",
+ @port.maintainer)
+ assert_equal("", @port.packager)
+ assert_equal(["foo", "bar"], @port.dependencies)
+ assert_equal("valgrind", @port.name)
+ assert_equal("3.1.0", @port.version)
+ assert_equal("1", @port.release)
+ assert_equal(["http://$name.org/downloads/$name-$version.tar.bz2"],
+ @port.source)
+ end
+end
--- /dev/null
+# Description: An open-source memory debugger for x86-GNU/Linux
+# URL: http://valgrind.org
+# Maintainer: Tilman Sauerbeck, tilman at code-monkey dot de
+# Depends on: foo, bar
+
+name=valgrind
+version=3.1.0
+release=1
+source=(http://$name.org/downloads/$name-$version.tar.bz2)
+
+build() {
+ cd $name-$version
+ ./configure --prefix=/usr
+ make
+ make DESTDIR=$PKG install
+ rm -rf $PKG/usr/share
+}